Edits history of script submission #17588 for ' Worker Communication (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Worker Communication
     * Update a "Active" or "In-progress" workers specific communication item.
     */
    export async function main(
    	auth: RT.Paychex,
    	workerId: string,
    	communicationId: 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}/communications/${communicationId}`
    	)
    
    	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.
     */
    
    export interface Body {
    	/**
    	 * The ID for the workers specific communication item.
    	 */
    	communicationId?: string
    	/**
    	 * A set of communication types classifying an instruction that the customer, requester, or subject must comply with in order for the screening to go forward. NOTE: PHONE and EMAIL type supports BUSINESS and PERSONAL usage type only.MOBILE_PHONE, FAX and PAGER supports BUSINESS usage type only.This data field cannot be PATCHED.
    	 */
    	type?: 'STREET_ADDRESS' | 'PO_BOX_ADDRESS' | 'PHONE' | 'MOBILE_PHONE' | 'FAX' | 'EMAIL' | 'PAGER'
    	/**
    	 * A code classifying a designated use associated with a contact method. For example, whether a telephone or email address is one for business communications or one primarily for personal use.This data field cannot be PATCHED.
    	 */
    	usageType?:
    		| 'PERSONAL'
    		| 'BUSINESS'
    		| 'HOME'
    		| 'WORK'
    		| 'LOCATION_STREET_ADDRESS'
    		| 'MAILING_ADDRESS'
    	/**
    	 * The country dialing code for a communication number
    	 */
    	dialCountry?: string
    	/**
    	 * The area dialing code for a communication number
    	 */
    	dialArea?: string
    	/**
    	 * The communication number, not including country dialing or area dialing codes
    	 */
    	dialNumber?: string
    	/**
    	 * The extension of the associated communication number
    	 */
    	dialExtension?: string
    	/**
    	 * The mailto address as specified in RFC2368
    	 */
    	uri?: string
    	/**
    	 * The street address line one
    	 */
    	streetLineOne?: string
    	/**
    	 * The street address line two
    	 */
    	streetLineTwo?: string
    	/**
    	 * The postal office box. This data field cannot be PATCHED
    	 */
    	postOfficeBox?: string
    	/**
    	 * The city name
    	 */
    	city?: string
    	/**
    	 * The zip-code
    	 */
    	postalCode?: string
    	/**
    	 * The zip-code extension
    	 */
    	postalCodeExtension?: string
    	/**
    	 * The state code (ISO 3166 subdivision code)
    	 */
    	countrySubdivisionCode?: string
    	/**
    	 * The country code (ISO 3166 alpha-2)
    	 */
    	countryCode?: 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