import {z} from 'zod' export type DayPosition = 'firstHalf' | 'secondHalf' | 'fullDay'; export type ShiftOverrideDuration = {unit: 'days' | 'hours', value: number, iso: string}; export type ShiftOverrideNature = 'worked' | 'off' | 'partTime' | 'weeklyRest'; export type ShiftOverrideCandidate = {employeeId: number, position: DayPosition, startsAt: string, duration: ShiftOverrideDuration, nature: ShiftOverrideNature, comment?: (string | null) | undefined}; export type UpdateApiScheduleApiShiftOverridesArgs = {'employee.id': number, 'startsAt.between': string, body: Array}; export type UpdateApiScheduleApiShiftOverridesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dayPosition = z.enum(["firstHalf", "secondHalf", "fullDay"]); export const shiftOverrideDuration = z.object({unit: z.enum(["days", "hours"]), value: z.number().gte(0), iso: z.string()}); export const shiftOverrideEventUnit = z.enum(["day", "hour", "time"]); export const shiftOverrideNature = z.enum(["worked", "off", "partTime", "weeklyRest"]); export const shiftOverride = z.object({id: z.number().int().gte(0), employee: z.object({id: z.number().int().gte(0).optional()}), position: dayPosition, startsAt: z.string(), endsAt: z.string(), duration: shiftOverrideDuration, workCycleUnit: shiftOverrideEventUnit, nature: shiftOverrideNature, comment: z.string().nullable().optional(), isLocked: z.boolean(), createdAt: z.string(), createdBy: z.object({id: z.number().int().gte(0).optional()}).nullable().optional(), lastUpdatedAt: z.string(), lastUpdatedBy: z.object({id: z.number().int().gte(0).optional()}).nullable().optional()}); export const updateApiScheduleApiShiftOverridesResponse = z.object({items: z.array(shiftOverride).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 }; /** * Replace ShiftOverrides for an employee. * * PUT /schedule/api/shift-overrides */ export const updateApiScheduleApiShiftOverrides = async (args: UpdateApiScheduleApiShiftOverridesArgs, options?: UpdateApiScheduleApiShiftOverridesOptions) => { const {baseUrl = "https://example.ilucca.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['employee.id'] != null) query.append("employee.id", String(args['employee.id'])) if (args['startsAt.between'] != null) query.append("startsAt.between", String(args['startsAt.between'])) const url = `${baseUrl.replace(/\/+$/, '')}/schedule/api/shift-overrides${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiScheduleApiShiftOverridesResponse, await res.json()) };