import {z} from 'zod' export type GetApiReportsEndOfDayArgs = {from?: string | undefined, to?: string | undefined, location_ids?: string | undefined}; export type GetApiReportsEndOfDayOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const endOfDayReportBooking = z.object({booked: z.string().optional(), display_source: z.string().optional(), display_status: z.string().optional(), first_name: z.string().optional(), from: z.string().optional(), id: z.number().int().optional(), last_name: z.string().optional(), owner_id: z.number().int().optional(), rental_id: z.number().int().optional(), rental_name: z.string().optional(), renter_id: z.number().int().optional(), source: z.string().optional(), status: z.string().optional(), subtotal: z.number().optional(), to: z.string().optional()}); export const endOfDayReportResponse = z.object({Meta: z.object({total_card_payments: z.number().optional(), total_cash_payments: z.number().optional(), total_payments: z.number().optional(), total_refund_amount: z.number().optional(), total_reservation_amount: z.number().optional(), total_reservation_count: z.number().int().optional()}).optional(), data: z.object({ended_bookings: z.array(endOfDayReportBooking).optional(), new_bookings: z.array(endOfDayReportBooking).optional(), started_bookings: z.array(endOfDayReportBooking).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 }; /** * Generate end of day report request * * GET /reports/end-of-day * * Also accepts auth: API-Key */ export const getApiReportsEndOfDay = async (args: GetApiReportsEndOfDayArgs, options: GetApiReportsEndOfDayOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = 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.location_ids != null) query.append("location_ids", String(args.location_ids)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/reports/end-of-day${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(endOfDayReportResponse, await res.json()) };