Skip to content

Commit 95ff78d

Browse files
axshaniilyamerman
andauthored
Rewards (#152)
* full implementation of Reward * Add rewardTransaction to transactions test * Uncomment billpay test Co-authored-by: ilyamerman <[email protected]>
1 parent ef7200c commit 95ff78d

File tree

9 files changed

+338
-11
lines changed

9 files changed

+338
-11
lines changed

resources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export * from "./transactions"
1919
export * from "./webhooks"
2020
export * from "./billPays"
2121
export * from "./checkDeposit"
22+
export * from "./rewards"
2223

resources/rewards.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Account, Customer, Transaction } from "../types"
2+
import { Include, UnitConfig, UnitResponse } from "../types/common"
3+
import { CreateRewardRequest, Reward, RewardListParams } from "../types/rewards"
4+
import { BaseResource } from "./baseResource"
5+
6+
export class Rewards extends BaseResource {
7+
constructor(token: string, basePath: string, config?: UnitConfig) {
8+
super(token, basePath + "/rewards", config)
9+
}
10+
11+
public async create(request: CreateRewardRequest): Promise<UnitResponse<Reward>> {
12+
return this.httpPost<UnitResponse<Reward>>("", { data: request })
13+
}
14+
15+
/**
16+
* Optional. A comma-separated list of related resources to include in the response.
17+
* Related resources include: customer, account, transaction. See [Getting Related Resources](https://developers.unit.co/about-jsonapi/#intro-getting-related-resources)
18+
*/
19+
public async get(id: string, include?: string): Promise<UnitResponse<Reward & Include<Account[] | Customer[] | Transaction[]>>> {
20+
const params = { include: include ? `include=${include}` : "" }
21+
return this.httpGet<UnitResponse<Reward & Include<Account[] | Customer[] | Transaction[]>>>(`/${id}`, { params })
22+
}
23+
24+
public async list(params?: RewardListParams): Promise<UnitResponse<Reward[] & Include<Account[] | Customer[] | Transaction[]>>> {
25+
const parameters: any = {
26+
"page[limit]": (params?.limit ? params.limit : 100),
27+
"page[offset]": (params?.offset ? params.offset : 0),
28+
...(params?.transactionId && { "filter[transactionId]": params.transactionId }),
29+
...(params?.rewardedTransactionId && { "filter[rewardedTransactionId]": params.rewardedTransactionId }),
30+
...(params?.receivingAccountId && { "filter[receivingAccountId]": params.receivingAccountId }),
31+
...(params?.customerId && { "filter[customerId]": params.customerId }),
32+
...(params?.cardId && { "filter[cardId]": params.cardId }),
33+
...(params?.tags && { "filter[tags]": params.tags }),
34+
...(params?.since && { "filter[since]": params.since }),
35+
...(params?.until && { "filter[until]": params.until }),
36+
"sort": params?.sort ? params.sort : "-createdAt",
37+
"include": params?.include ? params.include : ""
38+
}
39+
40+
if (params?.status)
41+
params.status.forEach((s, idx) => {
42+
parameters[`filter[status][${idx}]`] = s
43+
})
44+
45+
return this.httpGet<UnitResponse<Reward[] & Include<Account[] | Customer[] | Transaction[]>>>("", { params: parameters })
46+
}
47+
48+
}

tests/billPays.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { Unit } from "../unit"
22

33
import dotenv from "dotenv"
44
dotenv.config()
5-
// const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
5+
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
66

77
describe("pass", () => {
88
test.todo("pass")
99
})
1010

11-
// describe("Bill Pays List", () => {
12-
// test("Get Billers List", async () => {
13-
// const res = await unit.billPays.get({name: "Electric"})
14-
// res.data.forEach(element => {
15-
// expect(element.type === "biller").toBeTruthy()
16-
// })
17-
// })
18-
// })
11+
describe("Bill Pays List", () => {
12+
test("Get Billers List", async () => {
13+
const res = await unit.billPays.get({name: "Electric"})
14+
res.data.forEach(element => {
15+
expect(element.type === "biller").toBeTruthy()
16+
})
17+
})
18+
})

tests/rewards.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Unit, CreateRewardRequest } from "../unit"
2+
import { createIndividualAccount } from "./accounts.spec"
3+
4+
import dotenv from "dotenv"
5+
dotenv.config()
6+
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
7+
8+
describe("Test Reward Resource", () => {
9+
test("create new reward", async () => {
10+
const createAccountRes = await createIndividualAccount()
11+
const req: CreateRewardRequest = {
12+
type: "reward",
13+
attributes: {
14+
amount: 3000,
15+
description: "Reward for transaction #5678"
16+
},
17+
relationships: {
18+
receivingAccount: {
19+
data: {
20+
"type": "depositAccount",
21+
"id": createAccountRes.data.id
22+
}
23+
}
24+
}
25+
}
26+
27+
const res = await unit.rewards.create(req)
28+
expect(res.data.type).toBe("reward")
29+
})
30+
31+
test("list and get rewards",async () => {
32+
const res = await unit.rewards.list()
33+
34+
res.data.forEach(element => {
35+
expect(element.type).toBe("reward")
36+
})
37+
38+
res.data.forEach(async element => {
39+
const rewardRes = await unit.rewards.get(element.id)
40+
expect(rewardRes.data.type).toBe("reward")
41+
})
42+
})
43+
})

tests/transactions.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const transactionsId: { accountId: string; transactionId: string; }[] = []
77

88
const transactionTypes = ["originatedAchTransaction","adjustmentTransaction","atmTransaction","bookTransaction","cardReversalTransaction","cardTransaction",
99
"dishonoredReceivedAchTransaction","disputeTransaction","feeTransaction","interestTransaction","purchaseTransaction","receivedAchTransaction","releaseTransaction",
10-
"returnedAchTransaction","returnedReceivedAchTransaction","wireTransaction"]
10+
"returnedAchTransaction","returnedReceivedAchTransaction","wireTransaction","rewardTransaction"]
1111

1212
describe("Transactions List", () => {
1313
test("Get Transactions List", async () => {

types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export * from "./transactions"
1717
export * from "./webhooks"
1818
export * from "./billPay"
1919
export * from "./checkDeposit"
20+
export * from "./rewards"

types/rewards.ts

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { Relationship } from "./common"
2+
3+
type RewardStatus = "Sent" | "Rejected"
4+
5+
export interface Reward {
6+
/**
7+
* Identifier of the reward resource.
8+
*/
9+
id: string
10+
11+
/**
12+
* Type of the reward resource. The value is always reward.
13+
*/
14+
type: "reward"
15+
16+
/**
17+
* JSON object representing the reward data.
18+
*/
19+
attributes: {
20+
/**
21+
* The date the reward was created.
22+
* RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs
23+
*/
24+
createdAt: string
25+
26+
/**
27+
* The amount (in cents) of the reward.
28+
*/
29+
amount: number
30+
31+
/**
32+
* Description of the reward.
33+
*/
34+
description: string
35+
36+
/**
37+
* Either Sent or Rejected (see rejectReason for details).
38+
*/
39+
status: RewardStatus
40+
41+
/**
42+
* Optional. More information about the status.
43+
*/
44+
rejectReason: string
45+
46+
/**
47+
* See [Tags](https://developers.unit.co/#tags).
48+
*/
49+
tags?: object
50+
}
51+
52+
/**
53+
* Describes relationships between the reward resource and other resources (accounts, transaction, customer).
54+
*/
55+
relationships: {
56+
/**
57+
* The account that received the funds.
58+
*/
59+
receivingAccount: Relationship
60+
61+
/**
62+
* The account that sent the funds.
63+
*/
64+
fundingAccount: Relationship
65+
66+
/**
67+
* Optional. The transaction that caused the reward.
68+
*/
69+
rewardedTransaction?: Relationship
70+
71+
/**
72+
* The [Customer](https://developers.unit.co/customers/) the deposit account belongs to.
73+
*/
74+
customer: Relationship
75+
76+
/**
77+
* The [Reward Transaction](https://developers.unit.co/resources/#transaction-reward) generated by the reward.
78+
*/
79+
transaction: Relationship
80+
81+
/**
82+
* Optional. The card the belongs to the rewardedTransaction (if exists)
83+
*/
84+
card?: Relationship
85+
}
86+
}
87+
88+
export interface CreateRewardRequest {
89+
type: "reward"
90+
91+
attributes: {
92+
/**
93+
* The amount (in cents) to reward the account.
94+
*/
95+
amount: number
96+
97+
/**
98+
* Description of the reward (maximum of 50 characters).
99+
*/
100+
description: string
101+
102+
/**
103+
* See [Tags](https://developers.unit.co/#tags).
104+
*/
105+
tags?: object
106+
107+
/**
108+
* See [Idempotency](https://developers.unit.co/#intro-idempotency).
109+
*/
110+
idempotencyKey?: string
111+
}
112+
113+
relationships: {
114+
/**
115+
* The account that will receive the reward.
116+
*/
117+
receivingAccount: Relationship
118+
119+
/**
120+
* Optional. The account that will fund the reward, default is the revenue account.
121+
*/
122+
fundingAccount?: Relationship
123+
124+
/**
125+
* Optional. The transaction that triggered the reward (mostly relevant for cashback rewards).
126+
*/
127+
rewardedTransaction?: Relationship
128+
}
129+
}
130+
131+
export interface RewardListParams {
132+
/**
133+
* Maximum number of resources that will be returned. Maximum is 1000 resources. See Pagination.
134+
* default: 100
135+
*/
136+
limit?: number
137+
138+
/**
139+
* Number of resources to skip. See Pagination.
140+
* default: 0
141+
*/
142+
offset?: number
143+
144+
/**
145+
* Optional. Filters the results by the specified transaction id.
146+
*/
147+
transactionId?: string
148+
149+
/**
150+
* Optional. Filters the results by the specified rewarded transaction id.
151+
*/
152+
rewardedTransactionId?: string
153+
154+
/**
155+
* Optional. Filters the results by the specified account id.
156+
*/
157+
receivingAccountId?: string
158+
159+
/**
160+
* Optional. Filters the results by the specified customer id.
161+
*/
162+
customerId?: string
163+
164+
/**
165+
* Optional. Filters the results by the specified card id.
166+
*/
167+
cardId?: string
168+
169+
/**
170+
* Optional. Filter by reward Status. Usage example: filter[status][0]=Rejected.
171+
*/
172+
status?: string[]
173+
174+
/**
175+
* Optional. Filters the rewards that occurred after the specified date. e.g. 2020-01-13T16:01:19.346Z
176+
*/
177+
since?: string
178+
179+
/**
180+
* Optional. Filters the rewards that occurred before the specified date. e.g. 2020-01-02T20:06:23.486Z
181+
*/
182+
until?: string
183+
184+
/**
185+
* Optional. Filter rewards by [Tags](https://developers.unit.co/#tags).
186+
*/
187+
tags?: object
188+
189+
/**
190+
* Optional. Leave empty or provide sort = createdAt for ascending order.Provide sort = -createdAt(leading minus sign) for descending order.
191+
* default: sort=-createdAt
192+
*/
193+
sort?: string
194+
195+
/**
196+
* Optional. A comma-separated list of related resources to include in the response.
197+
* Related resources include: customer, account. [See Getting Related Resources](https://developers.unit.co/#intro-getting-related-resources)
198+
*/
199+
include?: string
200+
}

types/transactions.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,35 @@ export type RepaidPaymentAdvanceTransaction = BaseTransaction & {
802802
paymentAdvanceTransaction: Relationship
803803
}
804804
}
805+
806+
export type RewardTransaction = BaseTransaction & {
807+
/**
808+
* Type of the transaction resource. The value is always rewardTransaction.
809+
*/
810+
type: "rewardTransaction"
811+
812+
/**
813+
* JSON object representing the transaction data.
814+
*/
815+
attributes: {
816+
/**
817+
* The receiving party of the transaction.
818+
*/
819+
receiverCounterparty: Counterparty
820+
}
821+
822+
/**
823+
* Describes relationships between the transaction resource and other resources (account, customer, receivedPayment).
824+
*/
825+
relationships: {
826+
/**
827+
* The reward belonging to this transaction.
828+
*/
829+
reward: Relationship
830+
831+
/**
832+
* The Deposit Account receiver.
833+
*/
834+
receiverAccount: Relationship
835+
}
836+
}

0 commit comments

Comments
 (0)