Edits history of script submission #17700 for ' Update Job Code (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Update Job Code
     * **Summary Description**
    
    The PUT Job Code endpoint enables users to update precise values regarding Job Codes from the Paylocity instance of a client.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	jobCode: string,
    	body: {
    		description?: string
    		isActive?: false | true
    		isCertified?: false | true
    		payEntry?: {
    			shift?: string
    			rateCode?: string
    			rate?: number
    			addingRateConstant?: number
    			multiplyingRateConstant?: number
    			workerComputedCode?: string
    			tax?: {
    				state?: string
    				local1?: string
    				local2?: string
    				local3?: string
    			}
    		}
    		address?: {
    			line1?: string
    			line2?: string
    			city?: string
    			state?: string
    			zip?: string
    			county?: string
    			country?: string
    		}
    		payrollBasedJournal?: {
    			jobTitleCode?: string
    			facilityId?: string
    			stateId?: string
    		}
    	}
    ) {
    	const url = new URL(
    		`https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/jobs/${jobCode}`
    	)
    
    	const response = await fetch(url, {
    		method: 'PUT',
    		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