diff --git a/.changeset/smooth-garlics-remain.md b/.changeset/smooth-garlics-remain.md new file mode 100644 index 000000000..700c50e34 --- /dev/null +++ b/.changeset/smooth-garlics-remain.md @@ -0,0 +1,5 @@ +--- +'@voucherify/sdk': patch +--- + +Added `error` object in `VoucherifyError`. Added `validation_rules` in VouchersCreateParameters. diff --git a/packages/sdk/src/VoucherifyError.ts b/packages/sdk/src/VoucherifyError.ts index f1713f487..90550f802 100644 --- a/packages/sdk/src/VoucherifyError.ts +++ b/packages/sdk/src/VoucherifyError.ts @@ -13,6 +13,7 @@ export class VoucherifyError extends Error { public related_object_ids?: string[] public related_object_type?: string public related_object_total?: number + public error?: { message: string } public cause?: AxiosError constructor(statusCode: number, body?: unknown, axiosError?: AxiosError) { @@ -31,6 +32,7 @@ export class VoucherifyError extends Error { this.related_object_ids = (body).related_object_ids this.related_object_type = (body).related_object_type this.related_object_total = (body).related_object_total + this.error = (body).error this.cause = axiosError } } diff --git a/packages/sdk/src/types/Vouchers.ts b/packages/sdk/src/types/Vouchers.ts index 7f5ddfd2b..2e173d42d 100644 --- a/packages/sdk/src/types/Vouchers.ts +++ b/packages/sdk/src/types/Vouchers.ts @@ -133,6 +133,7 @@ export interface VouchersCreateParameters { redemption?: { quantity: number } + validation_rules?: string[] } export type VouchersCreate = VouchersCreateParameters & diff --git a/packages/sdk/test/redemptions.spec.ts b/packages/sdk/test/redemptions.spec.ts new file mode 100644 index 000000000..3298b9023 --- /dev/null +++ b/packages/sdk/test/redemptions.spec.ts @@ -0,0 +1,60 @@ +import { voucherifyClient as client } from './client' +import {DiscountVouchersTypesEnum} from "@voucherify/sdk"; +import {generateRandomString} from "./utils/generateRandomString"; + +jest.setTimeout(15000) + +describe('Redemptions API', () => { + it('redemption that failed due validation rule validate error should has .error.message element if defined in validation rule', async () => { + const errorMessage = 'CUSTOMER NOT IN SEGMENT' + + const validationRule = await client.validationRules.create({ + name: 'Customer must be in segment', + rules: { + 1: { + name: 'customer_segment', + rules: {}, + property: null, + conditions: { + "$is": [ + "seg_" + generateRandomString() + ] + } + }, + logic: '1' + }, + error: { + message: errorMessage, + } + }) + + const voucher = await client.vouchers.create({ + type: 'DISCOUNT_VOUCHER', + discount: { + amount_off: 2000, + type: DiscountVouchersTypesEnum.AMOUNT, + }, + redemption: { + quantity: 1, + }, + metadata: {}, + validation_rules: [ + validationRule.id + ] + }) + + try { + await client.redemptions.redeem(voucher.code, { + customer: { + source_id: 'cust_' + generateRandomString(), + name: 'John Doe', + object: 'customer', + } + }) + } catch (error) { + expect(error.message).toBeDefined() + expect(error.error.message).toBeDefined() + expect(error.error.message).toEqual(errorMessage) + } + }) +})