Edits history of script submission #17581 for ' Pay Component to a check. (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Pay Component to a check.
     * Add a new pay component on an individual unprocessed check.
     */
    export async function main(auth: RT.Paychex, checkId: string, body: Body) {
    	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
    	const url = new URL(`https://api.paychex.com/checks/${checkId}/checkcomponents`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		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.
     */
    
    /**
     * Informational pay components on the check.
     */
    export interface Body {
    	/**
    	 * The identifier of the pay component to add to the check. An overtime pay component can't be placed on a worker that is OT exempt.
    	 */
    	componentId?: string
    	/**
    	 * The unique identifier associated for the pay component on this check.
    	 */
    	checkComponentId?: string
    	/**
    	 * The name given to the pay component
    	 */
    	name?: string
    	/**
    	 * The category that this component falls into.
    	 */
    	classificationType?: string
    	/**
    	 * Description
    	 */
    	description?: string
    	/**
    	 * The effect that the pay component will have on the check amount.
    	 */
    	effectOnPay?: 'ADDITION' | 'ADDITION_WITH_IN_OUT' | 'EMPLOYER_INFORMATIONAL' | 'REDUCTION'
    	/**
    	 * The date that the pay component is able to be applied on a check.
    	 */
    	startDate?: string
    	/**
    	 * The date that the pay component is not available to be applied on a check moving forward.
    	 */
    	endDate?: string
    	/**
    	 * Applies To WorkerTypes.
    	 */
    	appliesToWorkerTypes?: ('EMPLOYEE' | 'CONTRACTOR' | 'INDEPENDENT_CONTRACTOR')[]
    	/**
    	 * This is used optionally for overriding a job when it needs to be different then the workers default. This option is only available when the client has job costing.
    	 */
    	jobId?: string
    	/**
    	 * This is used optionally for overriding a labor assignment when it needs to be different then the workers assignment distribution. This option is only available when the client has labor assignment.
    	 */
    	laborAssignmentId?: string
    	/**
    	 * The rate identifier for the workers compensation
    	 */
    	payRateId?: string
    	/**
    	 * The rate amount that will be applied for this component. Used in conjunction with Hours or Units.
    	 */
    	payRate?: string
    	/**
    	 * The number of hours that will be applied for this component. Used in conjunction with rate.
    	 */
    	payHours?: string
    	/**
    	 * The number of units that will be applied for this component. Used in conjunction with rate.
    	 */
    	payUnits?: string
    	/**
    	 * The flat amount to be applied for this component. Not used with Rate, Hours, or Units.
    	 */
    	payAmount?: string
    	/**
    	 * This is used optionally for memoing the payHours or payUnits so that they are informational when using a payAmount.
    	 */
    	memoed?: boolean
    	/**
    	 * This is used optionally for specifying a date that the pay component was generated on.
    	 */
    	lineDate?: string
    	/**
    	 * Whether or not this Check Pay Component is recurring or not. A recurring (true) means this is a representation of a Worker Pay Component on the check. A non-recurring (false) is the most common scenario and represents individual check pay components.
    	 */
    	recurring?: boolean
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago