Edits history of script submission #17553 for ' Company Job (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Company Job
     * Update a single Job.
     */
    export async function main(auth: RT.Paychex, companyId: string, jobId: string, body: Body) {
    	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
    	const url = new URL(`https://api.paychex.com/companies/${companyId}/jobs/${jobId}`)
    
    	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.
     */
    
    /**
     * The state representation of Jobs within a company.
     */
    export interface Body {
    	/**
    	 * The unique identifier associated with this job.
    	 */
    	jobId?: string
    	/**
    	 * Id that you define which is used for error handling/responses.
    	 */
    	jobCorrelationId?: string
    	/**
    	 * The name of the job.
    	 */
    	jobName?: string
    	/**
    	 * The start date associated with this job.
    	 */
    	startDate?: string
    	/**
    	 * The end date associated with this job.
    	 */
    	endDate?: string
    	/**
    	 * Date which this pay component has started for the 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
    	}[]
    	/**
    	 * Data elements of a Job Number to show the segments and number.For segmentation this can be up to 3 different segments.
    	 */
    	jobNumber?: {
    		/**
    		 * This is segment 1 or the number associated to the job when segmentation is not used.
    		 */
    		segment1?: string
    		/**
    		 * This is segment 2.
    		 */
    		segment2?: string
    		/**
    		 * This is segment 3.
    		 */
    		segment3?: string
    		[k: string]: unknown
    	}
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago