import {z} from 'zod' export type CreateShiftDto = {userId?: (string | null) | undefined, locationId?: (string | null) | undefined, contactInfo?: (string | null) | undefined, shiftDate?: string | undefined, startTime?: (string | null) | undefined, endTime?: (string | null) | undefined, income?: (string | null) | undefined, cancelBy?: (string | null) | undefined, notes?: (string | null) | undefined, status?: (string | null) | undefined}; export type CreateApiShiftsArgs = {body: CreateShiftDto}; export type CreateApiShiftsOptions = {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 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 }; /** * Create a new shift * * POST /Shifts */ export const createApiShifts = async (args: CreateApiShiftsArgs, options?: CreateApiShiftsOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Shifts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(shiftResponseDto, await res.json()) };