import {z} from 'zod' export type GetApiApiBusinessBusinessIdBranchBranchIdWorktimetableArgs = {businessId: string, branchId: string, from_date: string, to_date: string, activity_type?: string | undefined}; export type GetApiApiBusinessBusinessIdBranchBranchIdWorktimetableOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const dateTimeZone = z.object({id: z.string().optional(), fixed: z.boolean().optional()}); export const chronology = z.object({zone: dateTimeZone.optional()}); export const durationFieldType = z.object({name: z.string().optional()}); export const dateTimeFieldType = z.object({durationType: durationFieldType.optional(), rangeDurationType: durationFieldType.optional(), name: z.string().optional()}); export const durationField = z.object({unitMillis: z.number().int().optional(), precise: z.boolean().optional(), name: z.string().optional(), supported: z.boolean().optional(), type: durationFieldType.optional()}); export const dateTimeField = z.object({rangeDurationField: durationField.optional(), durationField: durationField.optional(), leapDurationField: durationField.optional(), minimumValue: z.number().int().optional(), maximumValue: z.number().int().optional(), name: z.string().optional(), supported: z.boolean().optional(), type: dateTimeFieldType.optional(), lenient: z.boolean().optional()}); export const localTime = z.object({millisOfSecond: z.number().int().optional(), millisOfDay: z.number().int().optional(), secondOfMinute: z.number().int().optional(), minuteOfHour: z.number().int().optional(), hourOfDay: z.number().int().optional(), chronology: chronology.optional(), fieldTypes: z.array(dateTimeFieldType).optional(), values: z.array(z.number().int()).optional(), fields: z.array(dateTimeField).optional()}); export const workTimeSlot = z.object({date: z.string().optional(), startTime: localTime.optional(), endTime: localTime.optional(), timeOffStartTime: localTime.optional(), timeOffEndTime: localTime.optional(), type: z.enum(["WORKING", "NON_WORKING", "NOT_SPECIFIED"]).optional(), custom: z.string().optional(), branchId: z.string().optional(), workActivityId: z.string().optional(), timeOffPolicyId: z.string().optional()}); export const workTimeTable = z.object({staffId: z.string().optional(), branchId: z.string().optional(), timeSlots: z.array(workTimeSlot).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 }; /** * Retrieve a branch timetable * * GET /api/business/{businessId}/branch/{branchId}/worktimetable */ export const getApiApiBusinessBusinessIdBranchBranchIdWorktimetable = async (args: GetApiApiBusinessBusinessIdBranchBranchIdWorktimetableArgs, options?: GetApiApiBusinessBusinessIdBranchBranchIdWorktimetableOptions) => { const {baseUrl = "http://api-gateway-eu.phorest.com/third-party-api-server", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.activity_type != null) query.append("activity_type", String(args.activity_type)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/business/${encodeURIComponent(String(args.businessId))}/branch/${encodeURIComponent(String(args.branchId))}/worktimetable${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workTimeTable, await res.json()) };