import {z} from 'zod' export type SchedulingEvaluateShiftClaimsItemV1 = {ref: string, shiftClaimId?: (string | null) | undefined, employeeId?: (number | null) | undefined, shiftId?: (string | null) | undefined, actions: Array<'CLAIM_SHIFT' | 'APPROVE_SHIFT_CLAIM'>}; export type SchedulingEvaluateShiftClaimsV1 = {items: Array}; export type CreateApiApiV1SchedulingShiftClaimsEvaluateArgs = {body: SchedulingEvaluateShiftClaimsV1}; export type CreateApiApiV1SchedulingShiftClaimsEvaluateOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const schedulingSchedulingEvaluationSubjectV1 = z.object({type: z.string(), id: z.string()}); export const schedulingSchedulingEvaluationPeriodV1 = z.object({start: z.string(), end: z.string(), timezone: z.string().nullable().optional()}); export const schedulingSchedulingEvaluationContextV1 = z.object({question: z.string(), subjects: z.array(schedulingSchedulingEvaluationSubjectV1), state: z.string(), scheduleView: z.string(), window: schedulingSchedulingEvaluationPeriodV1}); export const schedulingSchedulingEvaluationFindingV1 = z.object({ref: z.string(), coverageRef: z.string(), type: z.string(), code: z.string(), subjects: z.array(schedulingSchedulingEvaluationSubjectV1), period: schedulingSchedulingEvaluationPeriodV1.nullable().optional(), treatment: z.string(), details: z.object({})}); export const schedulingSchedulingEvaluationCoverageReasonV1 = z.object({code: z.string()}); export const schedulingSchedulingEvaluationCoverageV1 = z.object({ref: z.string(), capability: z.string(), subjects: z.array(schedulingSchedulingEvaluationSubjectV1), period: schedulingSchedulingEvaluationPeriodV1, status: z.string(), reason: schedulingSchedulingEvaluationCoverageReasonV1.nullable().optional()}); export const schedulingSchedulingEvaluationDecisionReasonV1 = z.object({code: z.string(), findingRefs: z.array(z.string()).optional(), coverageRefs: z.array(z.string()).optional()}); export const schedulingSchedulingEvaluationDecisionV1 = z.object({action: z.string(), outcome: z.string(), reasons: z.array(schedulingSchedulingEvaluationDecisionReasonV1)}); export const schedulingSchedulingEvaluationV1 = z.object({context: schedulingSchedulingEvaluationContextV1, evaluatedAt: z.string(), findings: z.array(schedulingSchedulingEvaluationFindingV1), coverage: z.array(schedulingSchedulingEvaluationCoverageV1), decision: schedulingSchedulingEvaluationDecisionV1.nullable()}); export const schedulingSchedulingShiftClaimEvaluationResultV1 = z.object({ref: z.string(), evaluations: z.array(schedulingSchedulingEvaluationV1)}); export const schedulingSchedulingShiftClaimEvaluationErrorV1 = z.object({ref: z.string(), status: z.number().int(), code: z.string(), title: z.string(), detail: z.string()}); export const schedulingSchedulingShiftClaimEvaluationBatchV1 = z.object({results: z.array(schedulingSchedulingShiftClaimEvaluationResultV1), errors: z.array(schedulingSchedulingShiftClaimEvaluationErrorV1)}); 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 }; /** * Evaluate Shift Claims * * POST /api/v1/scheduling/shift-claims/evaluate * * Also accepts auth: oauth */ export const createApiApiV1SchedulingShiftClaimsEvaluate = async (args: CreateApiApiV1SchedulingShiftClaimsEvaluateArgs, options?: CreateApiApiV1SchedulingShiftClaimsEvaluateOptions) => { 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/evaluate${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(schedulingSchedulingShiftClaimEvaluationBatchV1, await res.json()) };