Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
p-zielinski committed Nov 9, 2023
1 parent 5d797d6 commit 63a37d1
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 18 deletions.
10 changes: 5 additions & 5 deletions packages/sdk/src/types/Orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ export type OrderItemRequest = {

export type OrderResponse = {
id?: string
source_id: string | null
created_at: string
updated_at: string | null
status: 'CREATED' | 'PAID' | 'CANCELED' | 'FULFILLED'
source_id?: string | null
created_at?: string
updated_at?: string | null
status?: 'CREATED' | 'PAID' | 'CANCELED' | 'FULFILLED'
amount?: number
initial_amount?: number
discount_amount?: number
Expand All @@ -155,7 +155,7 @@ export type OrderResponse = {
items_applied_discount_amount?: number
total_applied_discount_amount?: number
items?: OrderItemResponse[]
metadata?: Record<string, unknown>
metadata: Record<string, unknown>
customer?: CustomerResponse
customer_id: string | null
referrer?: ReferrerResponse
Expand Down
26 changes: 13 additions & 13 deletions packages/sdk/src/types/Qualifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export type QualificationsCheckEligibilityRequestBody = {
}

export type QualificationsCheckEligibilityResponseBody = {
redeemables?: QualificationsRedeemablesResponse
redeemables: QualificationsRedeemablesResponse
tracking_id?: string
order?: OrderResponse
stacking_rules?: QualificationsStackingRulesResponse
stacking_rules: QualificationsStackingRulesResponse
}

//domain type
Expand All @@ -54,7 +54,7 @@ export type QualificationsFiltersFields =

export type QualificationsFiltersCondition = '$in' | '$not_in' | '$is' | '$is_not' | '$has_value' | '$is_unknown'

export type QualificationsFieldConditions = { conditions?: Partial<Record<QualificationsFiltersCondition, unknown>> }
export type QualificationsFieldConditions = { conditions?: Partial<Record<QualificationsFiltersCondition, any>> }

export type QualificationsRedeemablesResponse = {
object: 'list'
Expand All @@ -63,31 +63,31 @@ export type QualificationsRedeemablesResponse = {
total: number
has_more: boolean
more_starting_after?: string
joint_categories: string[]
}

export type QualificationsStackingRulesResponse = {
redeemables_limit: number
applicable_redeemables_limit: number
applicable_exclusive_redeemables_limit: number
exclusive_categories: string[]
joint_categories: string[]
}

export type QualificationsRedeemableSingleResponse = QualificationsRedeemableSingleResponseBase & {
redeemables: QualificationsRedeemableSingleResponseBase[]
redeemables?: QualificationsRedeemableSingleResponseBase[]
}

export type QualificationsRedeemableSingleResponseBase = {
id?: string
object?: 'campaign' | 'promotion_tier' | 'promotion_stack' | 'voucher'
created_at?: string
result?: RedeemableSingleResultResponse
id: string
object: 'campaign' | 'promotion_tier' | 'promotion_stack' | 'voucher'
created_at: string
result: RedeemableSingleResultResponse
order?: OrderResponse
validation_rule_id?: string
applicable_to?: ApplicableToResultList
inapplicable_to?: InapplicableToResultList
metadata?: Record<string, unknown>
categories?: Category[]
applicable_to: ApplicableToResultList
inapplicable_to: InapplicableToResultList
metadata: Record<string, unknown> | null
categories: Category[]
banner?: string
name?: string
campaign_name?: string
Expand Down
117 changes: 117 additions & 0 deletions packages/sdk/test/qualifications.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { voucherifyClient as client } from './client'
import { generateDiscountVoucher } from './utils/generateDiscountVoucher'
import { isoRegex } from './utils/isoRegex'
import { QualificationsRedeemablesResponse } from '@voucherify/sdk'

describe('Qualifications API', () => {
describe('List ALL scenario qualification', () => {
it('Should return qualifications list with "ALL" scenario', async () => {
return
const qualificationsResult = await client.qualifications.checkEligibility({
customer: { name: 'john' },
order: {},
options: { limit: 100, expand: ['redeemable', 'category', 'validation_rules'] },
})

expect(qualificationsResult.redeemables instanceof Object).toEqual(true)
expect(qualificationsResult.redeemables.object).toEqual('list')
expect(qualificationsResult.redeemables.data_ref).toEqual('data')
expect(Array.isArray(qualificationsResult.redeemables.data)).toEqual(true)
expect(typeof qualificationsResult.redeemables.total).toEqual('number')
expect(typeof qualificationsResult.redeemables.has_more).toEqual('boolean')
qualificationsResult.redeemables.data.forEach(redeemable => {
if (redeemable.id) {
expect(typeof redeemable.id).toEqual('string')
}
if (redeemable.object) {
expect(typeof redeemable.object).toEqual('string')
}
if (redeemable.created_at) {
expect(typeof redeemable.created_at).toEqual('string')
}
if (redeemable.result) {
expect(Object.keys(redeemable.result).length > 0).toEqual(true)
}
if (redeemable.order) {
expect('metadata' in redeemable.order).toEqual(true)
expect('customer_id' in redeemable.order).toEqual(true)
expect('referrer_id' in redeemable.order).toEqual(true)
expect('object' in redeemable.order).toEqual(true)
}
if (redeemable.validation_rule_id) {
expect(typeof redeemable.validation_rule_id).toEqual('string')
}
expect(Object.keys(redeemable.applicable_to).length > 3).toEqual(true)
expect(Object.keys(redeemable.inapplicable_to).length > 3).toEqual(true)
expect('metadata' in redeemable).toEqual(true)
expect(Array.isArray(redeemable.categories)).toEqual(true)
if (redeemable.banner) {
expect(typeof redeemable.banner).toEqual('string')
}
if (redeemable.name) {
expect(typeof redeemable.name).toEqual('string')
}
if (redeemable.campaign_name) {
expect(typeof redeemable.campaign_name).toEqual('string')
}
if (redeemable.campaign_id) {
expect(typeof redeemable.campaign_id).toEqual('string')
}
if (redeemable.validation_rules_assignments) {
expect(Object.keys(redeemable.validation_rules_assignments).length > 3).toEqual(true)
}
})
})

it('Should find qualification using filters', async () => {
const code = (await generateDiscountVoucher()).code
const checkEligibility = async () =>
await client.qualifications.checkEligibility({
customer: { name: 'john' },
order: {},
options: {
limit: 100,
expand: ['redeemable', 'category', 'validation_rules'],
filters: {
code: { conditions: { $in: [code] } },
},
},
})
const checkResponse = async () =>
expect((await checkEligibility()).redeemables).toEqual(
expect.objectContaining({
object: 'list',
data_ref: 'data',
data: [
{
id: code,
object: 'voucher',
created_at: expect.stringMatching(isoRegex),
result: {
discount: expect.objectContaining({ type: 'AMOUNT', effect: 'APPLY_TO_ORDER', amount_off: 2000 }),
},
order: { metadata: {}, customer_id: null, referrer_id: null, object: 'order' },
applicable_to: { data: [], total: 0, data_ref: 'data', object: 'list' },
inapplicable_to: { data: [], total: 0, data_ref: 'data', object: 'list' },
metadata: {},
categories: [],
},
],
total: 1,
has_more: false,
} as QualificationsRedeemablesResponse),
)

let checkNumber = 0
while ((await checkEligibility()).redeemables.total === 0) {
checkNumber += 1
await new Promise(r => setTimeout(r, 1000))
if (checkNumber === 10) {
await checkResponse()
return
}
}
await checkResponse()
})
})
})

0 comments on commit 63a37d1

Please sign in to comment.