Edits history of script submission #17704 for ' Update Screening Order Status (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Update Screening Order Status
     * > 🚧 Partner Restricted
    > All background check API endpoints are restricted to background check providers that have signed a Paylocity technology partnership agreement.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	body: {
    		candidateScreeningOrderId?: string
    		updatedAt?: string
    		oldStatus?:
    			| 'Ordered'
    			| 'WaitingOnCandidate'
    			| 'InProgress'
    			| 'Hold'
    			| 'Complete'
    			| 'CompleteWithCancelled'
    			| 'Cancelled'
    			| 'CompleteNeedsReview'
    			| 'CompleteNotEligible'
    			| 'Suspended'
    			| 'Resumed'
    			| 'CompleteDisputed'
    			| 'InvitationExpired'
    			| 'InvitationCancelled'
    			| 'PreAdverseActionSent'
    			| 'AdverseActionCancelled'
    			| 'Dispute'
    			| 'AdverseActionSent'
    		newStatus?:
    			| 'Ordered'
    			| 'WaitingOnCandidate'
    			| 'InProgress'
    			| 'Hold'
    			| 'Complete'
    			| 'CompleteWithCancelled'
    			| 'Cancelled'
    			| 'CompleteNeedsReview'
    			| 'CompleteNotEligible'
    			| 'Suspended'
    			| 'Resumed'
    			| 'CompleteDisputed'
    			| 'InvitationExpired'
    			| 'InvitationCancelled'
    			| 'PreAdverseActionSent'
    			| 'AdverseActionCancelled'
    			| 'Dispute'
    			| 'AdverseActionSent'
    		services?: {
    			serviceName?: string
    			oldStatus?:
    				| 'InProgress'
    				| 'Cancelled'
    				| 'Suspended'
    				| 'Pending'
    				| 'Completed'
    				| 'Disputed'
    				| 'Expired'
    				| 'NeedsReview'
    				| 'NotEligible'
    			newStatus?:
    				| 'InProgress'
    				| 'Cancelled'
    				| 'Suspended'
    				| 'Pending'
    				| 'Completed'
    				| 'Disputed'
    				| 'Expired'
    				| 'NeedsReview'
    				| 'NotEligible'
    			note?: string
    		}[]
    		screeningReportDocuments?: string[]
    	}
    ) {
    	const url = new URL(
    		`https://dc1prodgwext.paylocity.com/compliance/v1/companies/${companyId}/backgroundCheck/candidateScreeningOrders/status`
    	)
    
    	const response = await fetch(url, {
    		method: 'PATCH',
    		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.json()
    }
    
    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