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

[patch] Add support for endpoint for listing customer's redeemables #268

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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/hungry-roses-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@voucherify/sdk': patch
---

New endpoint support - `customer-redeemable`
7 changes: 7 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ Methods are provided within `client.customers.*` namespace.
- [Update Customers in bulk](#update-customers-in-bulk)
- [Update Customers' Metadata in bulk](#update-customers-metadata-in-bulk)
- [Import and Update Customers using CSV](#import-and-update-customers-using-csv)
- [List Redeemables](#list-redeemables)

#### [Create Customer](https://docs.voucherify.io/reference/create-customer)

Expand Down Expand Up @@ -759,6 +760,12 @@ client.customers.updateInBulk(customers)
client.customers.updateMetadataInBulk(sourceIdsAndMetadata)
```

#### [List Redeemables](https://docs.voucherify.io/reference/list-customer-redeemables)

```javascript
client.customers.listRedeemables(id, params)
```

---

### Consents
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/Customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class Customers {

return this.client.post<AAT.AsyncActionCreateResponse>(`/customers/importCSV`, form, undefined, headers)
}

/**
* @see https://docs.voucherify.io/reference/list-customer-redeemables
*/
public listRedeemables(id: string, params?: T.CustomerRedeemablesListQueryParams) {
return this.client.get<T.CustomerRedeemablesListResponse>(`/customers/${encode(id)}/redeemables`, params)
}
}

export { Customers }
91 changes: 91 additions & 0 deletions packages/sdk/src/types/Customers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Legacy code
import { DiscountUnit, DiscountAmount, DiscountPercent, DiscountFixed } from './DiscountVoucher'

export interface SimpleCustomer {
id: string
name?: string
Expand Down Expand Up @@ -121,6 +123,95 @@ export interface CustomerActivitiesListResponse {
data: Record<string, any>[]
}

export interface CustomerRedeemablesListQueryParams {
limit?: number
order?: 'created_at' | '-created_at' | 'id' | '-id'
starting_after_id?: string
filters?: Record<string, any>
}

export interface CustomerRedeemablesListResponse {
object: 'list'
total: number
data_ref: 'data'
data: CustomerRedeemable[]
has_more: boolean
more_starting_after?: string
}

export interface CustomerRedeemable {
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
id: string
created_at: string
redeemable_id: string
redeemable_object: string
customer_id: string
holder_role: 'OWNER' | 'REFERRER' | 'REFEREE'
campaign_id: string
campaign_type: 'LOYALTY_PROGRAM' | 'PROMOTION' | 'DISCOUNT_COUPONS' | 'GIFT_VOUCHERS' | 'REFERRAL_PROGRAM'
voucher_type: 'GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD'
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
redeemable: RedeemableContainer
}

export interface RedeemableContainerVoucher {
id: string
code: string
campaign?: string
camapign_id?: string
category_id?: string
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
type: 'GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD'
discount?: DiscountAmount | DiscountPercent | DiscountUnit | DiscountFixed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we do not cover "Unit Multiple" discount type in SDK. To make it a little more complex, in docs, we have two different types, "Unit" and "Unit Multiple" which have exclusively "units" attribute:

image

image

In the Voucherify domain, there is one DTO for both cases that has the "units" key. In SDK, I don't see the "units" key in the "DiscountUnit" type nor "DiscountUnitMultiple" defined.

gift?: {
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
amount: number
balance: number
}
loyalty_card?: {
points: number
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
balance: number
}
start_date?: string
expiration_date?: string
validity_timeframe?: {
interval: string
duration: string
}
validity_day_of_week?: number[]
publish?: {
object: 'list'
count: number
url: string
}
redemption?: {
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
object: 'list'
quantity?: number
redeemed_quantity: number
url: string
}
active: boolean
marcin-slezak marked this conversation as resolved.
Show resolved Hide resolved
additional_info?: string
metadata?: Record<string, any>
assets: {
qr: {
id: string
url: string
}
barcode: {
id: string
url: string
}
}
is_referral_code: boolean
holder_id?: string
updated_at?: string
created_at: string
object: 'voucher'
}

export interface RedeemableContainer {
type: 'voucher'
voucher?: RedeemableContainerVoucher
status: 'ACTIVE' | 'USED' | 'DISABLED' | 'NOT_ACTIVE' | 'EXPIRED' | 'NO_BALANCE'
}

export type CustomersCreateBody = CustomerRequest
export type CustomersCreateResponse = CustomerObject | CustomerUnconfirmed

Expand Down
31 changes: 31 additions & 0 deletions packages/sdk/test/customers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { voucherifyClient as client } from './client'
import { generateRandomString } from './utils/generateRandomString'
import { generateCustomerCSV } from './utils/generateCustomerCSV'
import { CustomerRequest, DiscountVouchersTypesEnum, DistributionsPublicationsCreateParams } from '@voucherify/sdk'

jest.setTimeout(15000)

Expand Down Expand Up @@ -173,4 +174,34 @@ describe('Customers API', () => {
}
expect(updatedCustomerWithNoAddress.address).toEqual(removedAddress)
})

it('Should return redeemable for customer', async () => {
const createdCustomer: CustomerRequest = await client.customers.create({ source_id: generateRandomString() })

const discountCampaign = await client.campaigns.create({
campaign_type: 'DISCOUNT_COUPONS',
name: generateRandomString(),
type: 'AUTO_UPDATE',
voucher: {
code_config: {
length: 3,
},
type: 'DISCOUNT_VOUCHER',
discount: {
amount_off: 0,
type: DiscountVouchersTypesEnum.AMOUNT,
},
},
})

const distributionsPublicationsCreateParams: DistributionsPublicationsCreateParams = {
customer: createdCustomer,
campaign: discountCampaign,
}

await client.distributions.publications.create(distributionsPublicationsCreateParams)

const redeemables = await client.customers.listRedeemables(createdCustomer.id as string)
expect(redeemables.data).toBeDefined()
})
})
Loading