import {z} from 'zod' export type TimeOffCreateDenialV1 = {managerNote?: (string | null) | undefined, bypass?: boolean | undefined}; export type CreateApiApiV1TimeOffRequestsIdDenialsArgs = {id: number, returnActions?: boolean | undefined, body: TimeOffCreateDenialV1}; export type CreateApiApiV1TimeOffRequestsIdDenialsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeOffRequestDailyAmountV1 = z.object({date: z.string().optional(), amount: z.number().optional()}); export const availableAction = z.object({href: z.string().optional(), fields: z.array(z.object({})).nullable().optional(), method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]).nullable().optional()}); export const timeOffRequestV1 = z.object({id: z.number().int().optional(), employeeId: z.number().int().optional(), categoryId: z.number().int().optional(), startDate: z.string().optional(), endDate: z.string().optional(), amount: z.number().optional(), unit: z.enum(["HOURS", "DAYS"]).optional(), status: z.enum(["REQUESTED", "APPROVED", "DENIED", "CANCELED"]).optional(), requestedAt: z.string().optional(), dailyAmounts: z.array(timeOffRequestDailyAmountV1).optional(), employeeNote: z.string().nullable().optional(), managerNote: z.string().nullable().optional(), statusUpdatedAt: z.string().nullable().optional(), statusUpdatedBy: z.number().int().nullable().optional(), _links: z.record(z.string(), availableAction).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 }; /** * Deny Time Off Request * * POST /api/v1/time-off/requests/{id}/denials * * Also accepts auth: oauth */ export const createApiApiV1TimeOffRequestsIdDenials = async (args: CreateApiApiV1TimeOffRequestsIdDenialsArgs, options?: CreateApiApiV1TimeOffRequestsIdDenialsOptions) => { 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 (args.returnActions != null) query.append("returnActions", String(args.returnActions)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time-off/requests/${encodeURIComponent(String(args.id))}/denials${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(timeOffRequestV1, await res.json()) };