Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoucherifyError update #260

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion packages/sdk/src/types/Vouchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ 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)
}
})
})
Loading