import {z} from 'zod' export type TimeOffCreatePolicyMilestoneV1 = {accrualBeginsInterval: number, accrualBeginsType: 'IMMEDIATELY' | 'DAYS' | 'WEEKS' | 'MONTHS' | 'YEARS', accrualHours?: (number | null) | undefined, accrualInterval: 'HOURLY' | 'DAILY' | 'WEEKLY' | 'BIWEEKLY' | 'SEMIMONTHLY' | 'MONTHLY' | 'QUARTERLY' | 'SEMIANNUALLY' | 'ANNUALLY' | 'ANNIVERSARY', maximumHours?: (number | null) | undefined, maximumCarryOver?: (number | null) | undefined, minimumCarryOver?: (number | null) | undefined, accruesPerHourWorked?: boolean | undefined, preaccruing?: boolean | undefined}; export type TimeOffCreatePolicyVersionV1 = {effectiveDate: string, rateTransitions?: ('IMMEDIATELY' | 'NEXT_PERIOD') | undefined, firstAccrual?: ('PRORATE' | 'FULL') | undefined, carryOverYmd?: string | undefined, negativeBalanceResetOnCarryOver?: boolean | undefined, milestones: Array}; export type TimeOffUpdatePolicyV1 = {name?: string | undefined, version?: TimeOffCreatePolicyVersionV1 | undefined}; export type PatchApiApiV1TimeOffPoliciesIdArgs = {id: number, body: TimeOffUpdatePolicyV1}; export type PatchApiApiV1TimeOffPoliciesIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeOffPolicyMilestoneV1 = z.object({id: z.number().int().optional(), accrualBeginsInterval: z.number().int().optional(), accrualBeginsType: z.enum(["IMMEDIATELY", "DAYS", "WEEKS", "MONTHS", "YEARS"]).optional(), accrualHours: z.number().nullable().optional(), accrualInterval: z.enum(["HOURLY", "DAILY", "WEEKLY", "BIWEEKLY", "SEMIMONTHLY", "MONTHLY", "QUARTERLY", "SEMIANNUALLY", "ANNUALLY", "ANNIVERSARY"]).optional(), maximumHours: z.number().nullable().optional(), maximumCarryOver: z.number().nullable().optional(), minimumCarryOver: z.number().nullable().optional(), accruesPerHourWorked: z.boolean().optional(), preaccruing: z.boolean().optional()}); export const timeOffPolicyVersionV1 = z.object({id: z.number().int().optional(), policyId: z.number().int().optional(), effectiveDate: z.string().nullable().optional(), status: z.enum(["ACTIVE", "ARCHIVED"]).optional(), rateTransitions: z.enum(["IMMEDIATELY", "NEXT_PERIOD"]).optional(), firstAccrual: z.enum(["PRORATE", "FULL"]).optional(), carryOverYmd: z.string().nullable().optional(), negativeBalanceResetOnCarryOver: z.boolean().optional(), milestones: z.array(timeOffPolicyMilestoneV1).optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional()}); export const timeOffPolicyV1 = z.object({id: z.number().int().optional(), name: z.string().optional(), categoryId: z.number().int().optional(), type: z.enum(["ACCRUING", "UNLIMITED", "MANUAL"]).optional(), currentVersion: timeOffPolicyVersionV1.nullable().optional(), assignedEmployeeCount: z.number().int().optional(), createdAt: z.string().optional(), updatedAt: z.string().nullable().optional(), deletedAt: z.string().nullable().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 }; /** * Update Time Off Policy * * PATCH /api/v1/time-off/policies/{id} * * Also accepts auth: oauth */ export const patchApiApiV1TimeOffPoliciesId = async (args: PatchApiApiV1TimeOffPoliciesIdArgs, options?: PatchApiApiV1TimeOffPoliciesIdOptions) => { const {baseUrl = "https://companySubDomain.bamboohr.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time-off/policies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(timeOffPolicyV1, await res.json()) };