Skip to content

Commit

Permalink
VoucherifyError update (#260)
Browse files Browse the repository at this point in the history
* Added `error` object in VoucherifyError.

* Added validation_rules to VouchersCreateParameters (mostly for test case)

* Added test Redemptions API with `redemption that failed due validation rule validate error should has .error.message element if defined in validation rule` test.

* Changeset.

* Removed comma from VouchersCreateParameters
  • Loading branch information
jkaliszuk committed Jan 23, 2024
1 parent d4da31c commit 25c3f13
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-garlics-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@voucherify/sdk': patch
---

Added `error` object in `VoucherifyError`. Added `validation_rules` in VouchersCreateParameters.
2 changes: 2 additions & 0 deletions packages/sdk/src/VoucherifyError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -31,6 +32,7 @@ export class VoucherifyError extends Error {
this.related_object_ids = (<any>body).related_object_ids
this.related_object_type = (<any>body).related_object_type
this.related_object_total = (<any>body).related_object_total
this.error = (<any>body).error
this.cause = axiosError
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/types/Vouchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export interface VouchersCreateParameters {
redemption?: {
quantity: number
}
validation_rules?: string[]
}

export type VouchersCreate = VouchersCreateParameters &
Expand Down
60 changes: 60 additions & 0 deletions packages/sdk/test/redemptions.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
})

0 comments on commit 25c3f13

Please sign in to comment.