import {z} from 'zod' export type GetApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsArgs = {loyaltyProgramId: number, loyaltyTransactionType?: ('manual' | 'session' | 'import') | undefined, subledgerId?: string | undefined, customerSessionIDs?: Array | undefined, transactionUUIDs?: Array | undefined, startDate?: string | undefined, endDate?: string | undefined, pageSize?: number | undefined, skip?: number | undefined, awaitsActivation?: boolean | undefined}; export type GetApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyCardIdentifier = z.string().min(4).max(108); export const loyaltyLedgerEntryFlags = z.object({createsNegativeBalance: z.boolean().optional()}); export const loyaltyProgramTransaction = z.object({id: z.number().int(), transactionUUID: z.string(), programId: z.number().int(), campaignId: z.number().int().optional(), created: z.string(), type: z.enum(["addition", "subtraction"]).max(255), amount: z.number(), name: z.string().max(255), startDate: z.string().max(64), expiryDate: z.string(), customerProfileId: z.string().max(255).optional(), cardIdentifier: loyaltyCardIdentifier.optional(), subledgerId: z.string().max(64), customerSessionId: z.string().max(255).optional(), importId: z.number().int().optional(), userId: z.number().int().optional(), userEmail: z.string().optional(), rulesetId: z.number().int().optional(), ruleName: z.string().optional(), flags: loyaltyLedgerEntryFlags.optional(), validityDuration: z.string().optional()}); export const getApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsResponse = z.object({hasMore: z.boolean(), data: z.array(loyaltyProgramTransaction)}); 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 loyalty program transactions * * GET /v1/loyalty_programs/{loyaltyProgramId}/transactions * * Also accepts auth: management_key */ export const getApiV1LoyaltyProgramsLoyaltyProgramIdTransactions = async (args: GetApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsArgs, options?: GetApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsOptions) => { 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.loyaltyTransactionType != null) query.append("loyaltyTransactionType", String(args.loyaltyTransactionType)) 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 (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.awaitsActivation != null) query.append("awaitsActivation", String(args.awaitsActivation)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loyalty_programs/${encodeURIComponent(String(args.loyaltyProgramId))}/transactions${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(getApiV1LoyaltyProgramsLoyaltyProgramIdTransactionsResponse, await res.json()) };