import {z} from 'zod' export type ShiftSwapStatus = number; export type GetApiShiftSwapsArgs = {ShiftId?: string | undefined, FromUserId?: string | undefined, ToUserId?: string | undefined, Status?: ShiftSwapStatus | undefined, IsOpenPool?: boolean | undefined, CreatedAfter?: string | undefined, CreatedBefore?: string | undefined, Query?: string | undefined, SortBy?: string | undefined, SortOrder?: string | undefined, Page?: (number | unknown) | undefined, Limit?: (number | unknown) | undefined}; export type GetApiShiftSwapsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const shiftSummaryDto = z.object({id: z.string().optional(), locationId: z.string().nullable().optional(), locationName: z.string().nullable().optional(), shiftDate: z.string().optional(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), income: z.string().nullable().optional(), status: z.string().optional(), cancelBy: z.string().nullable().optional(), userId: z.string().optional()}); 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 shiftSwapStatus = z.number().int(); export const shiftSwapResponseDto = z.object({id: z.string().optional(), shiftId: z.string().optional(), shift: shiftSummaryDto.nullable().optional(), fromUserId: z.string().optional(), fromUser: userSummaryDto.nullable().optional(), toUserId: z.string().nullable().optional(), toUser: userSummaryDto.nullable().optional(), notes: z.string().nullable().optional(), status: shiftSwapStatus.optional(), resolvedByUserId: z.string().nullable().optional(), resolvedByUser: userSummaryDto.nullable().optional(), resolvedAt: z.string().nullable().optional(), expiresAt: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const shiftSwapListResponseDto = z.object({shiftSwaps: z.array(shiftSwapResponseDto).optional(), totalCount: z.union([z.number().int(), z.string()]).optional(), pendingCount: z.union([z.number().int(), z.string()]).optional(), acceptedCount: z.union([z.number().int(), z.string()]).optional(), declinedCount: 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 shift swap requests * * GET /ShiftSwaps */ export const getApiShiftSwaps = async (args: GetApiShiftSwapsArgs, options?: GetApiShiftSwapsOptions) => { 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.ShiftId != null) query.append("ShiftId", String(args.ShiftId)) if (args.FromUserId != null) query.append("FromUserId", String(args.FromUserId)) if (args.ToUserId != null) query.append("ToUserId", String(args.ToUserId)) if (args.Status != null) query.append("Status", String(args.Status)) if (args.IsOpenPool != null) query.append("IsOpenPool", String(args.IsOpenPool)) if (args.CreatedAfter != null) query.append("CreatedAfter", String(args.CreatedAfter)) if (args.CreatedBefore != null) query.append("CreatedBefore", String(args.CreatedBefore)) 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(/\/+$/, '')}/ShiftSwaps${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(shiftSwapListResponseDto, await res.json()) };