import {z} from 'zod' export type GetApiBulkPeopleEmploymentArgs = {limit?: number | undefined, cursor?: string | undefined, employeeIds?: string | undefined, includeArchived?: boolean | undefined}; export type GetApiBulkPeopleEmploymentOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const workingPatternHourly = z.object({workingPatternType: z.string().optional(), days: z.object({sunday: z.number().optional(), monday: z.number().optional(), tuesday: z.number().optional(), wednesday: z.number().optional(), thursday: z.number().optional(), friday: z.number().optional(), saturday: z.number().optional()}).optional(), hoursPerDay: z.number().optional(), workingPatternId: z.number().optional()}); export const workingPatternBiWeekly = z.object({workingPatternType: z.string().optional(), firstWeek: z.object({sunday: z.number().optional(), monday: z.number().optional(), tuesday: z.number().optional(), wednesday: z.number().optional(), thursday: z.number().optional(), friday: z.number().optional(), saturday: z.number().optional()}).optional(), secondWeek: z.object({sunday: z.number().optional(), monday: z.number().optional(), tuesday: z.number().optional(), wednesday: z.number().optional(), thursday: z.number().optional(), friday: z.number().optional(), saturday: z.number().optional()}).optional(), hoursPerDay: z.number().optional(), workingPatternId: z.number().optional()}); export const workingPatternFlexible = z.object({workingPatternType: z.string().optional(), weeklyWorkPercentage: z.number().optional()}); export const workingPatternEntry = z.union([workingPatternHourly, workingPatternBiWeekly, workingPatternFlexible]); export const employmentEntry = z.object({id: z.number().int().optional(), reason: z.string().optional(), effectiveDate: z.string(), personalWorkingPatternType: z.string().optional(), actualWorkingPattern: workingPatternEntry.optional(), standardWorkingPattern: z.object({}).optional(), standardWorkingPatternId: z.number().optional(), workingPattern: z.object({}).optional(), siteWorkingPattern: workingPatternHourly.optional(), hoursInDayNotWorked: z.number().optional(), weeklyHours: z.number().optional(), fte: z.number().optional(), calendarName: z.string().optional(), calendarId: z.number().int().optional(), contract: z.string().optional(), type: z.string().optional(), salaryPayType: z.string().optional(), endEffectiveDate: z.string().optional(), activeEffectiveDate: z.string().optional(), isCurrent: z.boolean().optional(), canBeDeleted: z.boolean().optional(), modificationDate: z.string().optional(), CreationDate: z.string().optional(), flsaCode: z.string().optional(), change: z.object({reason: z.string().optional(), changedBy: z.string().optional(), changedById: z.string().optional()}).optional()}); export const restrictedColumnsMetadata = z.object({no_view_permission: z.array(z.string()).optional(), no_view_history_permission: z.array(z.string()).optional()}); export const employmentEntries = z.object({values: z.array(employmentEntry).optional(), restricted_columns: restrictedColumnsMetadata.optional()}); export const bulkEmploymentTableResponse = z.object({results: z.array(z.object({employeeId: z.string().optional(), values: z.array(employmentEntries).optional()})).optional(), response_metadata: z.object({next_cursor: z.string().optional()}).optional(), errors: z.array(z.object({employeeId: z.object({error: z.string().optional(), message: z.string().optional()}).optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List employment history for a list of employees. * * GET /bulk/people/employment * * Also accepts auth: Bearer */ export const getApiBulkPeopleEmployment = async (args: GetApiBulkPeopleEmploymentArgs, options?: GetApiBulkPeopleEmploymentOptions) => { const {baseUrl = "https://api.hibob.com/v1", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.employeeIds != null) query.append("employeeIds", String(args.employeeIds)) if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bulk/people/employment${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bulkEmploymentTableResponse, await res.json()) };