import {z} from 'zod' export type GetApiBillingCashBalanceTimelineArgs = {TENANT: string, 'API-KEY': string, location_id: string, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, include_calculated?: boolean | undefined}; export type GetApiBillingCashBalanceTimelineOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cashBalanceDetail = z.object({id: z.string(), creatorId: z.string(), locationId: z.string(), closingBalance: z.number().int(), balanceDiscrepancy: z.number().int(), discrepancyAcknowledged: z.boolean().optional(), acknowledgedById: z.string().nullable().optional(), createdDate: z.string(), updatedDate: z.string().nullable().optional(), isArchived: z.boolean().optional(), locationName: z.string().nullable().optional(), creatorName: z.string().nullable().optional(), acknowledgedByName: z.string().nullable().optional()}); export const cashBalanceAdjustmentDetail = z.object({id: z.string(), locationId: z.string(), amount: z.number().int(), reason: z.string(), creatorId: z.string(), createdDate: z.string(), isArchived: z.boolean().optional(), creatorName: z.string().nullable().optional()}); export const calculatedDailyBalance = z.object({date: z.string(), endingBalance: z.number().int(), startingBalance: z.number().int(), cashPayments: z.number().int(), cashRefunds: z.number().int(), adjustments: z.number().int(), locationId: z.string(), locationName: z.string().nullable().optional()}); export const paymentSummaryForTimeline = z.object({id: z.string(), amount: z.number().int(), payerName: z.string(), creatorName: z.string(), createdDate: z.string(), paymentMedium: z.string()}); export const refundSummaryForTimeline = z.object({id: z.string(), amount: z.number().int(), payerName: z.string(), creatorName: z.string(), createdDate: z.string(), reason: z.string().nullable().optional()}); export const timelineEvent = z.object({id: z.string(), type: z.enum(["balance_closure", "adjustment", "calculated_balance", "discrepancy_note", "cash_payment", "cash_refund"]), date: z.string(), cashBalance: cashBalanceDetail.nullable().optional(), adjustment: cashBalanceAdjustmentDetail.nullable().optional(), calculatedBalance: calculatedDailyBalance.nullable().optional(), paymentSummary: paymentSummaryForTimeline.nullable().optional(), refundSummary: refundSummaryForTimeline.nullable().optional(), locationName: z.string().nullable().optional()}); export const timelineStats = z.object({totalEvents: z.number().int(), balanceClosures: z.number().int(), adjustments: z.number().int(), discrepancies: z.number().int()}); export const cashBalanceTimelineResponse = z.object({events: z.array(timelineEvent), stats: timelineStats}); 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 Cash Balance Timeline * * GET /billing/cash-balance/timeline */ export const getApiBillingCashBalanceTimeline = async (args: GetApiBillingCashBalanceTimelineArgs, options: GetApiBillingCashBalanceTimelineOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.include_calculated != null) query.append("include_calculated", String(args.include_calculated)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/cash-balance/timeline${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(cashBalanceTimelineResponse, await res.json()) };