Edits history of script submission #17604 for ' Worker Pay Rate (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Worker Pay Rate
     * Update a workers specific compensation rate.
     */
    export async function main(auth: RT.Paychex, workerId: string, rateId: string, body: Body) {
    	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
    	const url = new URL(`https://api.paychex.com/workers/${workerId}/compensation/payrates/${rateId}`)
    
    	const response = await fetch(url, {
    		method: 'PATCH',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + accessToken
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    
    async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
    	const params = new URLSearchParams({
    		grant_type: 'client_credentials'
    	})
    
    	const response = await fetch(tokenUrl, {
    		method: 'POST',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
    			'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
    }
    
    /* eslint-disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    
    /**
     * Worker Pay Rate
     */
    export interface Body {
    	/**
    	 * Unique identifier for this workers pay rate. **This ID will change if this is created for an IN_PROGRESS worker that is later completed within Flex**
    	 */
    	rateId?: string
    	/**
    	 * The date when the pay rate is going to begin.
    	 */
    	startDate?: string
    	/**
    	 * The number of the rate. A worker can have up to 25 different rates.
    	 */
    	rateNumber?: string
    	/**
    	 * Type of rate.
    	 */
    	rateType?:
    		| 'ANNUAL_SALARY'
    		| 'PER_PAY_PERIOD_SALARY'
    		| 'PIECEWORK_RATE'
    		| 'DAILY_RATE'
    		| 'HOURLY_RATE'
    	/**
    	 * Describes the rate for the worker. A maximum of 30 characters is allowed.
    	 */
    	description?: string
    	/**
    	 * The currency amount which this rate is applied.
    	 */
    	amount?: string
    	/**
    	 * Default standard hours that this rate is used with a pay frequency. This data field is not available for an IN_PROGRESS worker.
    	 */
    	standardHours?: string
    	/**
    	 * Default over time hours that this rate is used with a pay frequency. This data field is not available for an IN_PROGRESS worker.
    	 */
    	standardOvertime?: string
    	/**
    	 * If this rate is the default one to apply on the worker. This data field is not available for an IN_PROGRESS worker and is considered RATE_1.
    	 */
    	default?: boolean
    	/**
    	 * The date when the pay rate becomes effective for the worker (can be used only in POST/PATCH for an active worker).
    	 */
    	effectiveDate?: string
    	links?: {
    		rel?: string
    		href?: string
    		hreflang?: string
    		media?: string
    		title?: string
    		type?: string
    		deprecation?: string
    		profile?: string
    		name?: string
    		[k: string]: unknown
    	}[]
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago