import {z} from 'zod' export type GetApiV1LinksLinkIdShiftsArgs = {start_date_from?: string | undefined, start_date_to?: string | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiV1LinksLinkIdShiftsOptions = {baseUrl?: string | undefined, ClientID?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const timeEntry = z.object({id: z.string().max(32).optional(), external_id: z.string(), created_at: z.string().optional(), updated_at: z.string().optional(), entry_date: z.string(), start: z.string(), end: z.string()}); export const shift = z.object({id: z.string().max(32).optional(), external_id: z.string(), created_at: z.string().optional(), updated_at: z.string().optional(), start_date: z.string(), end_date: z.string(), time_entries: z.array(timeEntry), earnings: z.array(z.object({name: z.string(), amount: z.string(), category: z.enum(["regular", "overtime", "bonus", "commission", "other_pay"]), rate: z.string().optional(), units: z.string().optional()})).optional(), type: z.enum(["other", "delivery", "rideshare", "shift"])}); export const shiftsListResponse = z.object({count: z.number().int(), next: z.string().nullable(), previous: z.string().nullable(), results: z.array(shift)}); 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 }; /** * List all shifts * * GET /v1/links/{link_id}/shifts/ */ export const getApiV1LinksLinkIdShifts = async (args: GetApiV1LinksLinkIdShiftsArgs, options?: GetApiV1LinksLinkIdShiftsOptions) => { const {baseUrl = "https://prod.truv.com", fetch: fetchFn = fetch, ClientID, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date_from != null) query.append("start_date_from", String(args.start_date_from)) if (args.start_date_to != null) query.append("start_date_to", String(args.start_date_to)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ClientID !== undefined) headers.set("X-Access-Client-Id", ClientID) if (AccessKey !== undefined) headers.set("X-Access-Secret", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/links/${encodeURIComponent(String(args.link_id))}/shifts/${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(shiftsListResponse, await res.json()) };