import {z} from 'zod' export type GetApiAttendancesExpectedTimeArgs = {from: string, to: string, Authorization: string, companyId?: string | undefined, officeId?: string | undefined, departmentId?: string | undefined, userId?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiAttendancesExpectedTimeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAttendancesExpectedTimeResponse = z.object({data: z.array(z.object({userId: z.string().optional(), days: z.array(z.object({date: z.string().optional(), expectedHours: z.number().optional(), expectedMinutes: z.number().optional()})).optional(), totalExpectedHours: z.number().optional(), expectedMinutes: z.number().optional()})).optional(), metadata: z.object({count: z.number().optional(), offset: z.number().optional(), limit: z.number().optional(), total: z.number().optional()}).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 }; /** * Returns expected time per user for a given date range (paginated). * * GET /attendances/expected-time */ export const getApiAttendancesExpectedTime = async (args: GetApiAttendancesExpectedTimeArgs, options?: GetApiAttendancesExpectedTimeOptions) => { const {baseUrl = "https://sandbox-api.kenjo.io/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.companyId != null) query.append("companyId", String(args.companyId)) if (args.officeId != null) query.append("officeId", String(args.officeId)) if (args.departmentId != null) query.append("departmentId", String(args.departmentId)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/attendances/expected-time${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(getApiAttendancesExpectedTimeResponse, await res.json()) };