Skip to content

Commit

Permalink
[patch] Add support for endpoint for listing customer's redeemables (#…
Browse files Browse the repository at this point in the history
…268)

* Added list redeemables endpoint.

* Added CustomerRedeemablesListQueryParams

* Fixed listRedeemables url

* Added Redeemable statuses.

* Added tests for customer redeemable. Fixed formatting.

* Changeset for customer redeemable

* fix - drop 'page' param from 'CustomerRedeemablesListQueryParams' interface as it is not available

* fix - appropiate enum values for 'voucher_type' field in 'CustomerRedeemable'

* fix - add explicit enum values for 'type' field in 'Redeemable'

* fix - add dedicated type for Voucher Redeemable in Customers's Redeemabel List since it is not the same as Voucher type

* fix - data field in 'CustomerRedeemablesListResponse' is plain array, not Record. Add more fields to 'RedeemableContainerVoucher'

* fix - add missing fields 'has_more' and 'more_starting_after' to 'CustomerRedeemablesListResponse'

* fixes for CustomerRedeemables endpoint:
- types names following CONTRIBUTING.md
- add missing voucher.gift.effect
- adding missing voucher.loyalty_card.next_expiration_date and voucher.loyalty_card.next_expiration_points
- adding missing voucher.redemption.redeemed_points
- voucher.active as optional field

* Change NOT_ACTIVE validity status to NOT_ACTIVE_YET

---------

Co-authored-by: jkaliszuk <[email protected]>
Co-authored-by: Marcin Slezak <[email protected]>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent b5c096c commit 3d23881
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
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 }
95 changes: 95 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,99 @@ 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: CustomerRedeemablesListItemResponse[]
has_more: boolean
more_starting_after?: string
}

export interface CustomerRedeemablesListItemResponse {
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'
redeemable: CustomerRedeemablesListItemContainerResponse
}

export interface CustomerRedeemablesListItemContainerVoucherResponse {
id: string
code: string
campaign?: string
camapign_id?: string
category_id?: string
type: 'GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD'
discount?: DiscountAmount | DiscountPercent | DiscountUnit | DiscountFixed
gift?: {
amount: number
balance: number
effect: string
}
loyalty_card?: {
points: number
balance: number
next_expiration_date?: string
next_expiration_points?: 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?: {
object: 'list'
quantity?: number
redeemed_quantity: number
url: string
redeemed_points?: number
}
active?: boolean
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 CustomerRedeemablesListItemContainerResponse {
type: 'voucher'
voucher?: CustomerRedeemablesListItemContainerVoucherResponse
status: 'ACTIVE' | 'USED' | 'DISABLED' | 'NOT_ACTIVE_YET' | '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()
})
})

0 comments on commit 3d23881

Please sign in to comment.