Skip to content

Commit

Permalink
Added support for v1/customers/{customerId}/activity (method `listA…
Browse files Browse the repository at this point in the history
…ctivity`) and fix type `ValidationValidateStackableResponse` (#277)
  • Loading branch information
p-zielinski authored Sep 30, 2024
1 parent da414f6 commit 2ddd6ec
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-chairs-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@voucherify/sdk': minor
---

Added support for `v1/customers/{customerId}/activity` (method `listActivity`) and fix type `ValidationValidateStackableResponse`
18 changes: 10 additions & 8 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,19 @@ You can optionally define scrolling cursor based on customer creation date using

Keep in mind this operation may drain your API call limits fairly quickly - each api call fetches 100 customers. So if you have 100.000 customers, you will use 1000 API calls.

#### [Update Customer's Consents](https://docs.voucherify.io/reference/update-customers-consents)
#### Update Customer's Consents - Deprecated

```javascript
client.customers.updateConsents(customer, consents)
```

#### [List Customers Activities](https://docs.voucherify.io/reference/get-customer-activities)
#### [List Customer's Activity](https://docs.voucherify.io/reference/list-customer-activity)

```javascript
client.customers.listActivity(customerIdOrSourceId, params)
```

#### List Customers Activities - Deprecated - use listActivity

```javascript
client.customers.listActivities(customerId)
Expand Down Expand Up @@ -768,15 +774,11 @@ client.customers.listRedeemables(id, params)

---

### Consents
### Consents - Deprecated

Methods are provided within `client.consents.*` namespace.

- [Get Consents](#get-consents)

You can [update Customer's consents](#update-customers-consents) in `client.customer.*` namespace.

#### [Get Consents](https://docs.voucherify.io/reference/get-consents)
#### Get Consents - Deprecated

```javascript
client.consents.list()
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/Consents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Consents {
constructor(private client: RequestController) {}

/**
* @see https://docs.voucherify.io/reference/get-consents
* @deprecated This method is deprecated. We’re removing this method in next major version.
*/
public list() {
return this.client.get<T.ConsentsListResponse>('/consents')
Expand Down
14 changes: 11 additions & 3 deletions packages/sdk/src/Customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,22 @@ class Customers {
)
}
/**
* @see https://docs.voucherify.io/reference/update-customers-consents
* @see https://docs.voucherify.io/reference/list-customer-activity
*/
public listActivity(customerIdOrSourceId: string, params?: T.CustomerActivityListQueryParams) {
return this.client.get<T.CustomerActivityListResponse>(
`/customers/${encode(customerIdOrSourceId)}/activity`,
params,
)
}
/**
* @deprecated This method is deprecated. We’re removing this method in next major version.
*/
public updateConsents(idOrSourceId: string, consents: T.CustomersUpdateConsentsBody) {
return this.client.put<undefined>(`/customers/${encode(idOrSourceId)}/consents`, consents)
}

/**
* @see https://docs.voucherify.io/reference/get-customer-activities
* @deprecated This method is deprecated in favor of the `listActivity` method. We’re removing this method in next major version.
*/
public listActivities(customerId: string, params?: T.CustomerActivitiesListQueryParams) {
return this.client.get<T.CustomerActivitiesListResponse>(`/customers/${encode(customerId)}/activities`, params)
Expand Down
20 changes: 20 additions & 0 deletions packages/sdk/src/types/Customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ export interface CustomersCommonListResponse {
has_more?: boolean
}

export interface CustomerActivityListQueryParams {
limit?: number //min 1, max 100
order?: 'created_at' | '-created_at'
starting_after_id?: string
campaign_type?: 'LOYALTY_PROGRAM' | 'PROMOTION' | 'DISCOUNT_COUPONS' | 'GIFT_VOUCHERS' | 'REFERRAL_PROGRAM'
campaign_id?: string
category?: 'ACTION' | 'EFFECT'
type?: string
start_date?: string //ISO format date
end_date?: string //ISO format date
}

export interface CustomerActivityListResponse {
object: 'list'
data_ref: 'data'
data: Record<string, unknown>[]
has_more: boolean
more_starting_after?: string
}

export interface CustomerActivitiesListQueryParams {
limit?: number
order?: 'created_at' | '-created_at'
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/types/Validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export interface ValidationValidateStackableResponse {
session?: ValidationSessionResponse
order?: OrdersCreateResponse
redeemables?: StackableRedeemableResponse[]
skipped_redeemables?: StackableRedeemableSkippedResponse
inapplicable_redeemables?: StackableRedeemableInapplicableResponse
skipped_redeemables?: StackableRedeemableSkippedResponse[]
inapplicable_redeemables?: StackableRedeemableInapplicableResponse[]
stacking_rules: ValidationsStackingRules
}

Expand Down
20 changes: 20 additions & 0 deletions packages/sdk/test/customers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import { CustomerRequest, DiscountVouchersTypesEnum, DistributionsPublicationsCr
jest.setTimeout(15000)

describe('Customers API', () => {
it('should list customer activity of newly created and updated user', async () => {
const customerSourceId = generateRandomString()
await client.customers.create({ source_id: customerSourceId }) //'customer.created' event
await client.customers.update({ source_id: customerSourceId, name: generateRandomString(), metadata: { xxx: 1 } }) //'customer.updated' event
let moreStartingAfterId: string | undefined
do {
const result = await client.customers.listActivity(customerSourceId, {
limit: 1,
starting_after_id: moreStartingAfterId,
})
result.data.forEach(data => expect(data.data).toBeDefined())
moreStartingAfterId = result.more_starting_after
if (result.has_more) {
expect(moreStartingAfterId).not.toEqual(undefined)
} else {
expect(moreStartingAfterId).toEqual(undefined)
}
} while (moreStartingAfterId)
})

it('should import customer via csv file', async () => {
const customerSourceId = generateRandomString()
await generateCustomerCSV(customerSourceId)
Expand Down

0 comments on commit 2ddd6ec

Please sign in to comment.