import {z} from 'zod' export type GetApiV1LoyaltyProgramsLoyaltyProgramIdProfileIntegrationIdArgs = {loyaltyProgramId: string, integrationId: string}; export type GetApiV1LoyaltyProgramsLoyaltyProgramIdProfileIntegrationIdOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyLedgerEntryFlags = z.object({createsNegativeBalance: z.boolean().optional()}); export const loyaltyLedgerEntry = z.object({created: z.string(), programID: z.number().int(), customerProfileID: z.string().optional(), cardID: z.number().int().optional(), customerSessionID: z.string().optional(), eventID: z.number().int().optional(), type: z.string(), amount: z.number(), startDate: z.string().optional(), expiryDate: z.string().optional(), name: z.string(), subLedgerID: z.string(), userID: z.number().int().optional(), archived: z.boolean().optional(), flags: loyaltyLedgerEntryFlags.optional(), validityDuration: z.string().optional(), awaitsActivation: z.boolean().optional()}); export const tier = z.object({id: z.number().int(), name: z.string(), startDate: z.string().optional(), expiryDate: z.string().optional(), downgradePolicy: z.enum(["one_down", "balance_based"]).optional(), source: z.enum(["boost", "points"]).optional(), reason: z.string().optional()}); export const loyaltySubLedger = z.object({total: z.number(), totalActivePoints: z.number(), totalPendingPoints: z.number(), totalSpentPoints: z.number(), totalExpiredPoints: z.number(), totalNegativePoints: z.number(), transactions: z.array(loyaltyLedgerEntry).optional(), expiringPoints: z.array(loyaltyLedgerEntry).optional(), activePoints: z.array(loyaltyLedgerEntry).optional(), pendingPoints: z.array(loyaltyLedgerEntry).optional(), expiredPoints: z.array(loyaltyLedgerEntry).optional(), currentTier: tier.optional()}); export const loyaltyLedger = z.object({ledger: loyaltySubLedger, subLedgers: z.record(z.string(), loyaltySubLedger).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 }; /** * Get customer's full loyalty ledger * * GET /v1/loyalty_programs/{loyaltyProgramId}/profile/{integrationId} * * Also accepts auth: management_key */ export const getApiV1LoyaltyProgramsLoyaltyProgramIdProfileIntegrationId = async (args: GetApiV1LoyaltyProgramsLoyaltyProgramIdProfileIntegrationIdArgs, options?: GetApiV1LoyaltyProgramsLoyaltyProgramIdProfileIntegrationIdOptions) => { 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 (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loyalty_programs/${encodeURIComponent(String(args.loyaltyProgramId))}/profile/${encodeURIComponent(String(args.integrationId))}${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(loyaltyLedger, await res.json()) };