Edits history of script submission #17702 for ' Update Recurring Deduction Details for a specific past/present/future deduction (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Update Recurring Deduction Details for a specific past/present/future deduction
     * **Summary Description**
    
    This function will allow the API user to update details for a particular deduction code that occurs in the past/present/future. Garnishments cannot be updated using the Deduction endpoints.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	employeeId: string,
    	deductionCode: string,
    	resourceId: string,
    	body: {
    		effectiveFrom?: string
    		effectiveTo?: string
    		calculationCode?: string
    		rate?: number
    		frequency?: string
    		agency?: string
    		arrear?: number
    		miscellaneousInfo?: string
    		note?: string
    		selfInsured?: false | true
    		priority?: number
    		loan401K?: {
    			loanNumber?: string
    			issueDate?: string
    			firstPaymentDate?: string
    		}
    		costCenters?: { level?: number; code?: string }[]
    		limits?: {
    			goal?: number
    			paidToDate?: number
    			payPeriodMinimum?: number
    			payPeriodMaximum?: number
    			annualMaximum?: number
    		}
    	}
    ) {
    	const url = new URL(
    		`https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/employees/${employeeId}/deductions/${deductionCode}/${resourceId}`
    	)
    
    	const response = await fetch(url, {
    		method: 'PUT',
    		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