import {z} from 'zod' export type GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsArgs = {loyaltyProgramId: number, loyaltyCardId: string, startDate?: string | undefined, endDate?: string | undefined, pageSize?: number | undefined, skip?: number | undefined, subledgerId?: string | undefined, customerSessionIDs?: Array | undefined, transactionUUIDs?: Array | undefined}; export type GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyCardIdentifier = z.string().min(4).max(108); export const cardLedgerTransactionLogEntry = z.object({transactionUUID: z.string(), created: z.string(), programId: z.number().int(), cardIdentifier: loyaltyCardIdentifier, applicationId: z.number().int().optional(), sessionId: z.number().int().optional(), customerSessionId: z.string().max(255).optional(), type: z.enum(["addition", "subtraction"]).max(255), name: z.string().max(255), startDate: z.string().max(64), expiryDate: z.string(), subledgerId: z.string().max(64), amount: z.number(), id: z.number().int()}); export const getApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsResponse = z.object({hasMore: z.boolean(), data: z.array(cardLedgerTransactionLogEntry)}); 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 }; /** * List card's transactions (Management API) * * GET /v1/loyalty_programs/{loyaltyProgramId}/cards/{loyaltyCardId}/logs * * Also accepts auth: management_key */ export const getApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogs = async (args: GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsArgs, options?: GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.subledgerId != null) query.append("subledgerId", String(args.subledgerId)) for (const item of args.customerSessionIDs ?? []) query.append("customerSessionIDs", String(item)) for (const item of args.transactionUUIDs ?? []) query.append("transactionUUIDs", String(item)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loyalty_programs/${encodeURIComponent(String(args.loyaltyProgramId))}/cards/${encodeURIComponent(String(args.loyaltyCardId))}/logs${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(getApiV1LoyaltyProgramsLoyaltyProgramIdCardsLoyaltyCardIdLogsResponse, await res.json()) };