Edits history of script submission #17612 for ' Add Employee to Onboarding (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Add Employee to Onboarding
     * Onboarding API sends employee data into Paylocity Onboarding to help ensure an easy and accurate hiring process for subsequent completion into Paylocity Payroll/HR solution.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	body: {
    		employeeId?: string
    		address1?: string
    		address2?: string
    		autoPayType?: string
    		baseRate?: number
    		city?: string
    		costCenter1?: string
    		costCenter2?: string
    		costCenter3?: string
    		defaultHours?: number
    		employeeStatus?: string
    		employmentType?: string
    		federalFilingStatus?: string
    		firstName: string
    		sex?: string
    		hireDate?: string
    		homePhone?: string
    		lastName: string
    		maritalStatus?: string
    		personalMobilePhone?: string
    		payFrequency?: string
    		personalEmailAddress?: string
    		payType?: string
    		ratePer?: string
    		salary?: number
    		state?: string
    		ssn?: string
    		stateFilingStatus?: string
    		suiState?: string
    		taxForm?: string
    		taxState?: string
    		userName?: string
    		workEmailAddress?: string
    		zip?: string
    	}
    ) {
    	const url = new URL(
    		`https://dc1prodgwext.paylocity.com/api/v1/companies/${companyId}/onboarding/employees`
    	)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization:
    				'Bearer ' +
    				(await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token'))
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.text()
    }
    
    async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> {
    	const params = new URLSearchParams({
    		grant_type: 'client_credentials',
    		client_id: auth.clientId,
    		client_secret: auth.clientSecret
    	})
    
    	const response = await fetch(tokenUrl, {
    		method: 'POST',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
    			'Content-Type': 'application/x-www-form-urlencoded'
    		},
    		body: params.toString()
    	})
    
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
    	}
    
    	const data = await response.json()
    	return data.access_token
    }
    

    Submitted by hugo697 235 days ago