Edits history of script submission #17615 for ' Add / Update Deduction (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Add / Update Deduction
     * Add/Update Deduction API sends new or updated employee deduction information directly to Paylocity Payroll/HR solution.
     */
    export async function main(
    	auth: Paylocity,
    	body: {
    		deduction?: {
    			agency?: string
    			annualMaximum?: number
    			calcCode?: string
    			caseNo?: string
    			companyNumber?: string
    			costCenter1?: string
    			costCenter2?: string
    			costCenter3?: string
    			dcode?: string
    			effectiveDate?: string
    			employeeId?: string
    			endDate?: string
    			fipsCode?: string
    			frequency?: string
    			goal?: number
    			isSelfInsuredPlan?: false | true
    			loanFirstPaymentDate401K?: string
    			loanIssueDate401K?: string
    			loanNumber?: string
    			maximum?: number
    			medicalSupport?: false | true
    			minimum?: number
    			miscInfo?: string
    			paidTowardsGoal?: number
    			priority?: number
    			rate?: number
    			reportTerminated?: false | true
    			ssn?: string
    			startDate?: string
    			stateAbbrev?: string
    		}
    	}
    ) {
    	const url = new URL(`https://dc1prodgwext.paylocity.com/api/v1/deduction`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		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.text()
    }
    
    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