import {z} from 'zod' export type GetApiShiftsArgs = {UserId?: string | undefined, LocationId?: string | undefined, Status?: string | undefined, ShiftDateFrom?: string | undefined, ShiftDateTo?: string | undefined, CancelByBefore?: string | undefined, CancelByAfter?: string | undefined, HasIncome?: boolean | undefined, Query?: string | undefined, SortBy?: string | undefined, SortOrder?: string | undefined, Page?: (number | unknown) | undefined, Limit?: (number | unknown) | undefined}; export type GetApiShiftsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userSummaryDto = z.object({id: z.string().optional(), email: z.string().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), displayName: z.string().nullable().optional(), type: z.string().optional(), roleId: z.string().nullable().optional(), roleName: z.string().nullable().optional()}); export const shiftStatus = z.number().int(); export const shiftResponseDto = z.object({id: z.string().optional(), userId: z.string().optional(), user: userSummaryDto.nullable().optional(), locationId: z.string().nullable().optional(), locationName: z.string().nullable().optional(), contactInfo: z.string().nullable().optional(), shiftDate: z.string().optional(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), income: z.string().nullable().optional(), cancelBy: z.string().nullable().optional(), notes: z.string().nullable().optional(), status: shiftStatus.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const shiftListResponseDto = z.object({shifts: z.array(shiftResponseDto).optional(), totalCount: z.union([z.number().int(), z.string()]).optional(), upcomingCount: z.union([z.number().int(), z.string()]).optional(), completedCount: z.union([z.number().int(), z.string()]).optional(), cancelledCount: z.union([z.number().int(), z.string()]).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 }; /** * Get a paginated and filtered list of shifts * * GET /Shifts */ export const getApiShifts = async (args: GetApiShiftsArgs, options?: GetApiShiftsOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.UserId != null) query.append("UserId", String(args.UserId)) if (args.LocationId != null) query.append("LocationId", String(args.LocationId)) if (args.Status != null) query.append("Status", String(args.Status)) if (args.ShiftDateFrom != null) query.append("ShiftDateFrom", String(args.ShiftDateFrom)) if (args.ShiftDateTo != null) query.append("ShiftDateTo", String(args.ShiftDateTo)) if (args.CancelByBefore != null) query.append("CancelByBefore", String(args.CancelByBefore)) if (args.CancelByAfter != null) query.append("CancelByAfter", String(args.CancelByAfter)) if (args.HasIncome != null) query.append("HasIncome", String(args.HasIncome)) if (args.Query != null) query.append("Query", String(args.Query)) if (args.SortBy != null) query.append("SortBy", String(args.SortBy)) if (args.SortOrder != null) query.append("SortOrder", String(args.SortOrder)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.Limit != null) query.append("Limit", String(args.Limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(shiftListResponseDto, await res.json()) };