import {z} from 'zod' export type SchedulingCreateSchedulingShiftClaimV1 = {shiftId: string}; export type CreateApiApiV1SchedulingShiftClaimsArgs = {body: SchedulingCreateSchedulingShiftClaimV1}; export type CreateApiApiV1SchedulingShiftClaimsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const schedulingSchedulingShiftClaimV1 = z.object({id: z.string(), shiftId: z.string(), employeeId: z.number().int(), status: z.enum(["pending", "approved", "declined", "withdrawn"]), resolutionReason: z.enum(["manager_denied", "shift_filled", "shift_deleted", "claiming_disabled", "assignment_blocked"]).nullable(), createdAt: z.string(), updatedAt: z.string().nullable()}); 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 }; /** * Claim a Shift * * POST /api/v1/scheduling/shift-claims * * Also accepts auth: oauth */ export const createApiApiV1SchedulingShiftClaims = async (args: CreateApiApiV1SchedulingShiftClaimsArgs, options?: CreateApiApiV1SchedulingShiftClaimsOptions) => { 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/scheduling/shift-claims${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(schedulingSchedulingShiftClaimV1, await res.json()) };