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

[DO_NOT_MERGE] Referral holders #267

Draft
wants to merge 5 commits into
base: jk/customer-redeemables
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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/six-cycles-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@voucherify/sdk': patch
---

New endpoint support - `referrals-redeemable-holders`
16 changes: 16 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,22 @@ client.metadataSchemas.list()
client.metadataSchemas.get(schemaName)
```

---

### Referrals

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

#### [Get Referrals Redeemable Holders](https://docs.voucherify.io/reference/get-member)

Depending on the parameters, this method sends requests to [v1/referrals/{id}/members/{memberId}/holders](https://docs.voucherify.io/reference/referrals-member-holders) or [v1/referrals/members/{memberId}/holders](https://docs.voucherify.io/reference/referrals-member-holders-1) API endpoint

```javascript
client.referrals.listHolders(campaignId, memberId)
```

---

### Api Limits Handler

Methods are provided within `client.apiLimitsHandler.*` namespace.
Expand Down
22 changes: 22 additions & 0 deletions packages/sdk/src/Referrals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { RequestController } from './RequestController'
import * as T from './types/Referrals'
import { encode } from './helpers'

class Referrals {
constructor(private client: RequestController) {}

/**
* @see https://docs.voucherify.io/reference/referrals-member-holders
* @see https://docs.voucherify.io/reference/referrals-member-holders-1
*/
public listHolders(campaignId: string | null, memberId: string, params?: T.RedeemableHoldersListQueryParams) {
return this.client.get<T.RedeemableHoldersListResponse>(
campaignId
? `/referrals/${encode(campaignId)}/members/${memberId}/holders`
: `/referrals/members/${memberId}/holders`,
params,
)
}
}

export { Referrals }
3 changes: 3 additions & 0 deletions packages/sdk/src/VoucherifyServerSide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Categories } from './Categories'
import { PromotionsStacks } from './PromotionsStacks'
import { ProductCollections } from './ProductCollections'
import { Qualifications } from './Qualifications'
import { Referrals } from './Referrals'

export interface VoucherifyServerSideOptions {
/**
Expand Down Expand Up @@ -204,6 +205,7 @@ export function VoucherifyServerSide(options: VoucherifyServerSideOptions) {
const validationRules = new ValidationRules(client)
const apiLimitsHandler = new ApiLimitsHandler(client)
const metadataSchemas = new MetadataSchemas(client)
const referrals = new Referrals(client)

return {
vouchers,
Expand All @@ -227,5 +229,6 @@ export function VoucherifyServerSide(options: VoucherifyServerSideOptions) {
asyncActions,
apiLimitsHandler,
metadataSchemas,
referrals,
}
}
27 changes: 27 additions & 0 deletions packages/sdk/src/types/Referrals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface RedeemableHoldersListQueryParams {
limit?: number
page?: number
order?: 'created_at' | '-created_at' | 'id' | '-id'
starting_after_id?: string
filters?: Record<string, any>
}

export interface RedeemableHoldersListResponse {
object: 'list'
total: number
data_ref: 'data'
data: Record<string, RedeemableHolder>[]
}

export interface RedeemableHolder {
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' | 'DISCOUNT' | 'LOYALTY_CARD' | 'LUCKY_DRAW'
object: 'redeemable_holder'
}
41 changes: 41 additions & 0 deletions packages/sdk/test/referrals.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { voucherifyClient as client } from './client'
import { generateRandomString } from './utils/generateRandomString'
import { CustomerRequest, DiscountVouchersTypesEnum, DistributionsPublicationsCreateParams } from '@voucherify/sdk'

jest.setTimeout(15000)

describe('Referrals API', () => {
it('Should return redeemable holders for campaign and member', async () => {
const createdCustomer: CustomerRequest = await client.customers.create({ source_id: generateRandomString() })

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

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

const publication = await client.distributions.publications.create(distributionsPublicationsCreateParams)

const redeemables = await client.referrals.listHolders(referralCampaign.id, publication.voucher.id)
expect(redeemables.data).toBeDefined()

const redeemables2 = await client.referrals.listHolders(null, publication.voucher.id)
expect(redeemables2.data).toBeDefined()
})
})