import {z} from 'zod' export type AccountStatusEntity = 'ACTIVE' | 'BLOCKED' | 'CANCELLED' | 'DELETED' | 'EXPIRED' | 'INACTIVE' | 'INVALIDATED' | 'LOCKED' | 'STOLEN' | 'USED' | 'SUSPENDED'; export type DatesEntity = {start?: string | undefined, end?: string | undefined}; export type MetaEntity = {key1?: string | undefined, key2?: string | undefined}; export type DetailsEntity = {key1?: string | undefined, reasonDescription?: string | undefined}; export type LocationEntity = {storeId?: unknown | undefined, storeParentId?: unknown | undefined}; export type AccountBalancesCreateEntity = {available?: number | undefined, totalSpend?: number | undefined, transactionCount?: number | undefined, current?: number | undefined, usable?: number | undefined, locked?: number | undefined, lifetime?: number | undefined, objectivesMet?: number | undefined}; export type AccountOverridesEntity = {continuity?: ({qualification?: ({totalTransactionUnits?: number | undefined, totalTransactionSpend?: number | undefined, totalTransactionCount?: number | undefined}) | undefined}) | undefined, offer?: ({qualification?: ({minimumProductSpend?: (number | null) | undefined, minimumUnits?: (number | null) | undefined, minimumBasketSpend?: (number | null) | undefined}) | undefined, reward?: ({creditAmount?: number | undefined, step?: number | undefined, floor?: number | undefined, ceiling?: (number | null) | undefined, multiplierValue?: number | undefined}) | undefined}) | undefined, campaign?: ({rules?: ({couponLimit?: number | undefined}) | undefined}) | undefined, reward?: ({discountAmount?: number | undefined, finalAmount?: number | undefined, percentageAmount?: number | undefined, discountValue?: number | undefined}) | undefined}; export type WalletCouponAccountCreationPayload = {status?: AccountStatusEntity | undefined, state?: string | undefined, currency?: string | undefined, dates?: DatesEntity | undefined, meta?: MetaEntity | undefined, details?: DetailsEntity | undefined, location?: LocationEntity | undefined, balances?: AccountBalancesCreateEntity | undefined, overrides?: AccountOverridesEntity | undefined}; export type WalletRelatedAccountCreationPayloadOptionalCampaignReference = WalletCouponAccountCreationPayload; export type CreateApiServicesWalletCampaignAccountsRelatedArgs = {'identity-value'?: unknown | undefined, walletId?: unknown | undefined, body: Array}; export type CreateApiServicesWalletCampaignAccountsRelatedOptions = {baseUrl?: string | undefined, eesAuthenticationClientId?: string | undefined, eesAuthenticationHash?: string | undefined, fetch?: typeof fetch | undefined}; export const accountStatusEntity = z.enum(["ACTIVE", "BLOCKED", "CANCELLED", "DELETED", "EXPIRED", "INACTIVE", "INVALIDATED", "LOCKED", "STOLEN", "USED", "SUSPENDED"]); export const datesEntity = z.object({start: z.string().optional(), end: z.string().optional()}); export const metaEntity = z.object({key1: z.string().optional(), key2: z.string().optional()}); export const accountBalancesEntity = z.object({available: z.number().int().optional(), refundable: z.number().int().optional(), totalSpend: z.number().int().optional(), totalUnits: z.number().int().optional(), transactionCount: z.number().int().optional(), current: z.number().int().optional(), usable: z.number().int().optional(), locked: z.number().int().optional(), lifetime: z.number().int().optional(), objectivesMet: z.number().int().optional()}); export const walletAccountRelationEntity = z.array(z.object({accountId: z.string(), dateCreated: z.string()})); export const accountOverridesEntity = z.object({continuity: z.object({qualification: z.object({totalTransactionUnits: z.number().int().optional(), totalTransactionSpend: z.number().int().optional(), totalTransactionCount: z.number().int().optional()}).optional()}).optional(), offer: z.object({qualification: z.object({minimumProductSpend: z.number().int().nullable().optional(), minimumUnits: z.number().int().nullable().optional(), minimumBasketSpend: z.number().int().nullable().optional()}).optional(), reward: z.object({creditAmount: z.number().int().optional(), step: z.number().int().optional(), floor: z.number().int().optional(), ceiling: z.number().int().nullable().optional(), multiplierValue: z.number().int().optional()}).optional()}).optional(), campaign: z.object({rules: z.object({couponLimit: z.number().int().optional()}).optional()}).optional(), reward: z.object({discountAmount: z.number().int().gte(1).optional(), finalAmount: z.number().int().gte(1).optional(), percentageAmount: z.number().gte(1).optional(), discountValue: z.number().int().gte(1).optional()}).optional()}); export const walletAccountEntity = z.object({accountId: z.string(), walletId: z.string(), campaignId: z.string(), type: z.enum(["ECOUPON", "VOUCHER", "STAMP_CARD", "PAPER", "DEBIT", "PLASTIC", "POINTS", "CONTINUITY", "STANDARD_SUBSCRIPTION", "QUEST"]), clientType: z.string(), status: accountStatusEntity, state: z.string(), dates: datesEntity, meta: metaEntity.optional(), balances: accountBalancesEntity, relationships: z.object({ENTITLEMENT: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), ENTITLEMENT_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), OBJECTIVE: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), OBJECTIVE_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), PROMOTION: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), PROMOTION_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), BOUNCE_BACK_REWARD: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), BOUNCE_BACK_REWARD_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), COUPON_STAMP_REWARD: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), COUPON_STAMP_REWARD_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional(), LOYALTY_POINTS_REWARD_OF: z.object({accountType: walletAccountRelationEntity.optional()}).optional()}).optional(), overrides: accountOverridesEntity.optional(), dateCreated: z.string(), lastUpdated: z.string(), mobileWallet: z.string()}); export const defaultErrorEntity = z.object({errorCode: z.enum(["AA", "AF", "ALR", "AALR", "AT", "CE", "CI", "CLPPR", "CILR", "CORQ", "CP", "CR", "CTNR", "CU", "CX", "DSN", "ELE", "FES", "IFU", "IH", "ILA", "IOC", "IP", "IPR", "IR", "ITID", "IW", "LIR", "LNA", "LPR", "MAC", "MAR", "MLV", "MNA", "MPA", "MTLE", "NF", "OC", "P", "RF", "RFL", "RW", "RWI", "TBR", "TBS", "TEXP", "TULR", "U", "UAS", "UV", "VA", "VC", "VI", "VL", "VNL", "VPUF", "VSM", "VU", "VUA", "VURC", "VURI", "VXP", "WLNF", "WTF", "WTNF"]), errorMessage: z.string()}); export const multiStatus207AccountEntity = z.object({successful: z.object({total: z.number().int(), results: z.array(walletAccountEntity)}).optional(), failed: z.object({total: z.number().int().optional(), results: z.array(defaultErrorEntity).optional()}).optional(), results: z.object({total: z.number().int().optional(), results: z.array(z.object({})).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 }; /** * Create Entitlement Coupons by Identity or WalletId * * POST /services/wallet/campaign/accounts/related */ export const createApiServicesWalletCampaignAccountsRelated = async (args: CreateApiServicesWalletCampaignAccountsRelatedArgs, options?: CreateApiServicesWalletCampaignAccountsRelatedOptions) => { const {baseUrl = "https://wallet.uk.eagleeye.com/2.0", fetch: fetchFn = fetch, eesAuthenticationClientId, eesAuthenticationHash} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['identity-value'] != null) query.append("identity-value", String(args['identity-value'])) if (args.walletId != null) query.append("walletId", String(args.walletId)) if (eesAuthenticationClientId !== undefined) headers.set("X-EES-AUTH-CLIENT-ID", eesAuthenticationClientId) if (eesAuthenticationHash !== undefined) headers.set("X-EES-AUTH-HASH", eesAuthenticationHash) const url = `${baseUrl.replace(/\/+$/, '')}/services/wallet/campaign/accounts/related${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(multiStatus207AccountEntity, await res.json()) };