Edits history of script submission #17617 for ' Add new employee to Web Link (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Add new employee to Web Link
     * Add new employee to Web Link will send partially completed or potentially erroneous new hire record to Web Link, where it can be corrected and competed by company administrator or authorized Paylocity Service Bureau employee.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	body: {
    		additionalDirectDeposit?: {
    			accountNumber?: string
    			accountType?: string
    			amount?: number
    			amountType?: string
    			isSkipPreNote?: false | true
    			preNoteDate?: string
    			routingNumber?: string
    		}[]
    		benefitSetup?: {
    			benefitClass?: string
    			benefitClassEffectiveDate?: string
    			benefitSalary?: number
    			benefitSalaryEffectiveDate?: string
    			doNotApplyAdministrativePeriod?: false | true
    			isMeasureAcaEligibility?: false | true
    		}[]
    		birthDate?: string
    		customBooleanFields?: {
    			category: 'PayrollAndHR'
    			label: string
    			value: false | true
    		}[]
    		customDateFields?: {
    			category: 'PayrollAndHR'
    			label: string
    			value: string
    		}[]
    		customDropDownFields?: {
    			category: 'PayrollAndHR'
    			label: string
    			value: string
    		}[]
    		customNumberFields?: {
    			category: 'PayrollAndHR'
    			label: string
    			value: number
    		}[]
    		customTextFields?: {
    			category: 'PayrollAndHR'
    			label: string
    			value: string
    		}[]
    		departmentPosition?: {
    			changeReason?: string
    			clockBadgeNumber?: string
    			costCenter1?: string
    			costCenter2?: string
    			costCenter3?: string
    			effectiveDate?: string
    			employeeType?: string
    			equalEmploymentOpportunityClass?: string
    			isMinimumWageExempt?: false | true
    			isOvertimeExempt?: false | true
    			isSupervisorReviewer?: false | true
    			isUnionDuesCollected?: false | true
    			isUnionInitiationCollected?: false | true
    			jobTitle?: string
    			payGroup?: string
    			positionCode?: string
    			shift?: string
    			supervisorCompanyNumber?: string
    			supervisorEmployeeId?: string
    			tipped?: string
    			unionAffiliationDate?: string
    			unionCode?: string
    			unionPosition?: string
    			workersCompensation?: string
    		}[]
    		disabilityDescription?: string
    		employeeId?: string
    		ethnicity?: string
    		federalTax?: {
    			amount?: number
    			deductionsAmount?: number
    			dependentsAmount?: number
    			exemptions?: number
    			filingStatus?: string
    			higherRate?: false | true
    			otherIncomeAmount?: number
    			percentage?: number
    			taxCalculationCode?: string
    			w4FormYear?: number
    		}[]
    		firstName?: string
    		fitwExemptReason?: string
    		futaExemptReason?: string
    		gender?: string
    		homeAddress?: {
    			address1?: string
    			address2?: string
    			city?: string
    			country?: string
    			county?: string
    			emailAddress?: string
    			mobilePhone?: string
    			phone?: string
    			postalCode?: string
    			state?: string
    		}[]
    		isEmployee943?: false | true
    		isSmoker?: false | true
    		lastName?: string
    		localTax?: {
    			exemptions?: number
    			exemptions2?: number
    			filingStatus?: string
    			residentPSD?: string
    			taxCode?: string
    			workPSD?: string
    		}[]
    		mainDirectDeposit?: {
    			accountNumber?: string
    			accountType?: string
    			isSkipPreNote?: false | true
    			preNoteDate?: string
    			routingNumber?: string
    		}[]
    		maritalStatus?: string
    		medExemptReason?: string
    		middleName?: string
    		nonPrimaryStateTax?: {
    			amount?: number
    			deductionsAmount?: number
    			dependentsAmount?: number
    			exemptions?: number
    			exemptions2?: number
    			filingStatus?: string
    			higherRate?: false | true
    			otherIncomeAmount?: number
    			percentage?: number
    			reciprocityCode?: string
    			specialCheckCalc?: number
    			taxCalculationCode?: number
    			taxCode?: number
    			w4FormYear?: number
    		}[]
    		preferredName?: string
    		primaryPayRate?: {
    			baseRate?: number
    			changeReason?: string
    			defaultHours?: number
    			effectiveDate?: string
    			isAutoPay?: false | true
    			payFrequency?: string
    			payGrade?: string
    			payType?: string
    			ratePer?: string
    			salary?: number
    		}[]
    		primaryStateTax?: {
    			amount?: number
    			deductionsAmount?: number
    			dependentsAmount?: number
    			exemptions?: number
    			exemptions2?: number
    			filingStatus?: string
    			higherRate?: false | true
    			otherIncomeAmount?: number
    			percentage?: number
    			specialCheckCalc?: string
    			taxCalculationCode?: string
    			taxCode?: string
    			w4FormYear?: number
    		}[]
    		priorLastName?: string
    		salutation?: string
    		sitwExemptReason?: string
    		ssExemptReason?: string
    		ssn?: string
    		status?: {
    			adjustedSeniorityDate?: string
    			changeReason?: string
    			effectiveDate?: string
    			employeeStatus?: string
    			hireDate?: string
    			isEligibleForRehire?: string
    		}[]
    		suffix?: string
    		suiExemptReason?: string
    		suiState?: string
    		taxDistributionCode1099R?: string
    		taxForm?: string
    		veteranDescription?: string
    		webTime?: {
    			badgeNumber?: string
    			chargeRate?: number
    			isTimeLaborEnabled?: false | true
    		}
    		workAddress?: {
    			address1?: string
    			address2?: string
    			city?: string
    			country?: string
    			county?: string
    			emailAddress?: string
    			mobilePhone?: string
    			pager?: string
    			phone?: string
    			phoneExtension?: string
    			postalCode?: string
    			state?: string
    		}[]
    		workEligibility?: {
    			alienOrAdmissionDocumentNumber?: string
    			attestedDate?: string
    			countryOfIssuance?: string
    			foreignPassportNumber?: string
    			i94AdmissionNumber?: string
    			i9DateVerified?: string
    			i9Notes?: string
    			isI9Verified?: false | true
    			isSsnVerified?: false | true
    			ssnDateVerified?: string
    			ssnNotes?: string
    			visaType?: string
    			workAuthorization?: string
    			workUntil?: string
    		}[]
    	}
    ) {
    	const url = new URL(
    		`https://dc1prodgwext.paylocity.com/api/v2/weblinkstaging/companies/${companyId}/employees/newemployees`
    	)
    
    	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.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