Edits history of script submission #17616 for ' Add new employee (paylocity)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paylocity = {
    	clientId: string
    	clientSecret: string
    }
    /**
     * Add new employee
     * New Employee API sends new employee data directly to Paylocity Payroll/HR solution. Companies who use the New Hire Template in Paylocity Payroll/HR solution may require additional fields when hiring employees. New Employee API Requests will honor these required fields.
     */
    export async function main(
    	auth: Paylocity,
    	companyId: string,
    	body: {
    		additionalDirectDeposit?: {
    			accountNumber?: string
    			accountType?: string
    			amount?: number
    			amountType?: string
    			blockSpecial?: false | true
    			isSkipPreNote?: false | true
    			nameOnAccount?: string
    			preNoteDate?: string
    			routingNumber?: string
    		}[]
    		additionalRate?: {
    			changeReason?: string
    			costCenter1?: string
    			costCenter2?: string
    			costCenter3?: string
    			effectiveDate?: string
    			endCheckDate?: string
    			job?: string
    			rate?: number
    			rateCode?: string
    			rateNotes?: string
    			ratePer?: string
    			shift?: string
    		}[]
    		benefitSetup?: {
    			benefitClass?: string
    			benefitClassEffectiveDate?: string
    			benefitSalary?: number
    			benefitSalaryEffectiveDate?: string
    			doNotApplyAdministrativePeriod?: false | true
    			isMeasureAcaEligibility?: false | true
    		}
    		birthDate?: string
    		companyName?: string
    		currency?: 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
    			reviewerCompanyNumber?: string
    			reviewerEmployeeId?: string
    			shift?: string
    			supervisorCompanyNumber?: string
    			supervisorEmployeeId?: string
    			tipped?: string
    			unionAffiliationDate?: string
    			unionCode?: string
    			unionPosition?: string
    			workersCompensation?: string
    		}
    		disabilityDescription?: string
    		emergencyContacts?: {
    			address1?: string
    			address2?: string
    			city?: string
    			country?: string
    			county?: string
    			email?: string
    			firstName: string
    			homePhone?: string
    			lastName: string
    			mobilePhone?: string
    			notes?: string
    			pager?: string
    			primaryPhone?: string
    			priority?: string
    			relationship?: string
    			state?: string
    			syncEmployeeInfo?: false | true
    			workExtension?: string
    			workPhone?: string
    			zip?: string
    		}[]
    		employeeId?: string
    		ethnicity?: string
    		federalTax?: {
    			amount?: number
    			exemptions?: number
    			filingStatus?: string
    			percentage?: number
    			taxCalculationCode?: string
    		}
    		firstName?: string
    		gender?: string
    		homeAddress?: {
    			address1?: string
    			address2?: string
    			city?: string
    			country?: string
    			county?: string
    			emailAddress?: string
    			mobilePhone?: string
    			phone?: string
    			postalCode?: string
    			state?: string
    		}
    		isHighlyCompensated?: 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
    			blockSpecial?: false | true
    			isSkipPreNote?: false | true
    			nameOnAccount?: string
    			preNoteDate?: string
    			routingNumber?: string
    		}
    		maritalStatus?: string
    		middleName?: string
    		nonPrimaryStateTax?: {
    			amount?: number
    			exemptions?: number
    			exemptions2?: number
    			filingStatus?: string
    			percentage?: number
    			reciprocityCode?: string
    			specialCheckCalc?: string
    			taxCalculationCode?: string
    			taxCode?: string
    		}
    		ownerPercent?: number
    		preferredName?: string
    		primaryPayRate?: {
    			annualSalary?: number
    			baseRate?: number
    			beginCheckDate?: string
    			changeReason?: string
    			defaultHours?: number
    			effectiveDate?: string
    			isAutoPay?: false | true
    			payFrequency?: string
    			payGrade?: string
    			payRateNote?: string
    			payType?: string
    			ratePer?: string
    			salary?: number
    		}
    		primaryStateTax?: {
    			amount?: number
    			exemptions?: number
    			exemptions2?: number
    			filingStatus?: string
    			percentage?: number
    			specialCheckCalc?: string
    			taxCalculationCode?: string
    			taxCode?: string
    		}
    		priorLastName?: string
    		salutation?: string
    		ssn?: string
    		status?: {
    			adjustedSeniorityDate?: string
    			changeReason?: string
    			effectiveDate?: string
    			employeeStatus?: string
    			hireDate?: string
    			isEligibleForRehire?: false | true
    			reHireDate?: string
    		}
    		suffix?: string
    		taxSetup?: {
    			fitwExemptNotes?: string
    			fitwExemptReason?: string
    			futaExemptNotes?: string
    			futaExemptReason?: string
    			isEmployee943?: false | true
    			isPension?: false | true
    			isStatutory?: false | true
    			medExemptNotes?: string
    			medExemptReason?: string
    			sitwExemptNotes?: string
    			sitwExemptReason?: string
    			ssExemptNotes?: string
    			ssExemptReason?: string
    			suiExemptNotes?: 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
    			location?: string
    			mailStop?: 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/companies/${companyId}/employees`)
    
    	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