-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
transactions.test.ts
324 lines (263 loc) · 12.1 KB
/
transactions.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { describe, expect, it, beforeEach } from "vitest";
import { map } from "lodash/fp";
import {
seedDatabase,
getTransactionsForUserByObj,
getTransactionsForUserContacts,
getAllUsers,
getAllTransactions,
getAllPublicTransactions,
getBankAccountsByUserId,
createTransaction,
getTransactionsByUserId,
updateTransactionById,
getTransactionById,
getPublicTransactionsDefaultSort,
getUserById,
getBankTransferByTransactionId,
} from "../../backend/database";
import {
User,
Transaction,
TransactionRequestStatus,
DefaultPrivacyLevel,
BankTransferType,
TransactionPayload,
TransactionStatus,
} from "../../src/models";
import { getFakeAmount } from "../../src/utils/transactionUtils";
import { totalTransactions, transactionsPerUser } from "../../scripts/seedDataUtils";
describe("Transactions", () => {
beforeEach(() => {
seedDatabase();
});
it("should retrieve a list of all transactions", () => {
expect(getAllTransactions().length).toBe(totalTransactions);
});
it("should retrieve a list of all public transactions", () => {
expect(getAllPublicTransactions().length).toBeGreaterThan(transactionsPerUser);
expect(getAllPublicTransactions().length).toBeLessThan(totalTransactions);
});
it("should retrieve a list of transactions for a user (user is receiver)", () => {
const userToLookup: User = getAllUsers()[0];
const result: Transaction[] = getTransactionsForUserByObj(userToLookup.id, {
status: "complete",
});
expect(result[0].receiverId).toBe(userToLookup.id);
});
it("should retrieve a list of transactions for a user (user is sender)", () => {
const userToLookup: User = getAllUsers()[0];
const result: Transaction[] = getTransactionsForUserByObj(userToLookup.id, {});
expect(result.pop()!.senderId).toBe(userToLookup.id);
});
it("should retrieve a list of transactions for a users contacts", () => {
const userToLookup: User = getAllUsers()[0];
const result: Transaction[] = getTransactionsForUserContacts(userToLookup.id);
expect(result.length).toBeGreaterThan(transactionsPerUser);
expect(result.length).toBeLessThan(totalTransactions);
});
it("should retrieve a list of transactions for a users contacts - between date range", () => {
const userToLookup: User = getAllUsers()[0];
const result: Transaction[] = getTransactionsForUserContacts(userToLookup.id, {
dateRangeStart: new Date("Mar 09 2023"),
dateRangeEnd: new Date("Mar 09 2024"),
});
expect(result.length).toBeGreaterThan(1);
});
it("should retrieve a list of public transactions, default sort", () => {
const user: User = getAllUsers()[0];
const contactsTransactions: Transaction[] = getTransactionsForUserContacts(user.id);
expect(contactsTransactions.length).toBeGreaterThan(1);
expect(contactsTransactions.length).toBeLessThan(totalTransactions);
const response = getPublicTransactionsDefaultSort(user.id);
expect(response.contactsTransactions.length).toBeGreaterThan(1);
expect(response.contactsTransactions.length).toBeLessThan(totalTransactions);
expect(response.publicTransactions.length).toBeGreaterThan(1);
expect(response.publicTransactions.length).toBeLessThan(totalTransactions);
const ids = map("id", contactsTransactions);
expect(ids).toContain(response.contactsTransactions[9].id);
});
it("should create a payment", () => {
const sender: User = getAllUsers()[0];
const receiver: User = getAllUsers()[1];
const senderBankAccount = getBankAccountsByUserId(sender.id)[0];
const paymentDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Payment: ${sender.id} to ${receiver.id}`,
amount: getFakeAmount(),
privacyLevel: DefaultPrivacyLevel.public,
status: TransactionStatus.pending,
};
const result = createTransaction(sender.id, "payment", paymentDetails);
expect(result.id).toBeDefined();
expect(result.status).toEqual("complete");
expect(result.requestStatus).not.toBeDefined();
});
it("should create a request", () => {
const sender: User = getAllUsers()[0];
const receiver: User = getAllUsers()[1];
const senderBankAccount = getBankAccountsByUserId(sender.id)[0];
const requestDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Request: ${sender.id} to ${receiver.id}`,
amount: getFakeAmount(),
privacyLevel: DefaultPrivacyLevel.public,
status: TransactionStatus.pending,
};
const result = createTransaction(sender.id, "request", requestDetails);
expect(result.id).toBeDefined();
expect(result.status).toEqual("pending");
expect(result.requestStatus).toEqual("pending");
});
it("should create a payment and find it in the personal transactions", () => {
const sender: User = getAllUsers()[0];
const receiver: User = getAllUsers()[1];
const senderBankAccount = getBankAccountsByUserId(sender.id)[0];
const paymentDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Payment: ${sender.id} to ${receiver.id}`,
amount: getFakeAmount(),
privacyLevel: DefaultPrivacyLevel.private,
status: TransactionStatus.pending,
};
const payment = createTransaction(sender.id, "payment", paymentDetails);
expect(payment.id).toBeDefined();
const personalTransactions: Transaction[] = getTransactionsForUserByObj(sender.id, {});
const ids = map("id", personalTransactions);
expect(ids).toContain(payment.id);
});
it("should reject (update) a transaction", () => {
const user: User = getAllUsers()[0];
const transactions = getTransactionsByUserId(user.id);
expect(transactions.length).toBeGreaterThanOrEqual(transactionsPerUser);
const transaction = transactions[0];
expect(transaction.requestStatus).not.toEqual("rejected");
const edits: Partial<Transaction> = {
requestStatus: TransactionRequestStatus.rejected,
};
updateTransactionById(transaction.id, edits);
const updatedTransaction = getTransactionById(transaction.id);
expect(updatedTransaction.requestStatus).toEqual("rejected");
});
it("should accept (update) a transaction", () => {
const user: User = getAllUsers()[0];
const transactions = getTransactionsByUserId(user.id);
expect(transactions.length).toBeGreaterThanOrEqual(transactionsPerUser);
const transaction = transactions[0];
expect(transaction.requestStatus).not.toEqual("accepted");
const edits: Partial<Transaction> = {
requestStatus: TransactionRequestStatus.accepted,
};
updateTransactionById(transaction.id, edits);
const updatedTransaction = getTransactionById(transaction.id);
expect(updatedTransaction.requestStatus).toEqual("accepted");
});
it("should add additional fields (e.g. retreiverName, senderName, etc) to a list of transactions for a user for API response", () => {
const userToLookup: User = getAllUsers()[0];
const result = getPublicTransactionsDefaultSort(userToLookup.id);
const transaction = result.publicTransactions[0];
const { receiverId, senderId, receiverName, senderName } = transaction;
const receiver = getUserById(receiverId);
const sender = getUserById(senderId);
expect(receiverName).toBe(`${receiver.firstName} ${receiver.lastName}`);
expect(senderName).toBe(`${sender.firstName} ${sender.lastName}`);
expect(transaction.likes).toBeDefined();
expect(transaction.comments).toBeDefined();
});
it.skip("should create a payment and withdrawal (bank transfer) for remaining balance", () => {
const sender: User = getAllUsers()[0];
const receiver: User = getAllUsers()[1];
const senderBankAccount = getBankAccountsByUserId(sender.id)[0];
const firstPaymentAmount = 1000;
const secondPaymentAmount = 500;
const receiverTransactions = getTransactionsByUserId(receiver.id);
expect(receiverTransactions.length).toBeGreaterThan(1);
console.log("sender balance:", sender.balance + 1000);
const paymentDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Payment: ${sender.id} to ${receiver.id}`,
amount: sender.balance + firstPaymentAmount,
privacyLevel: DefaultPrivacyLevel.public,
status: TransactionStatus.pending,
};
const transaction = createTransaction(sender.id, "payment", paymentDetails);
expect(transaction.id).toBeDefined();
expect(transaction.status).toEqual("complete");
expect(transaction.requestStatus).not.toBeDefined();
const updatedSender: User = getAllUsers()[0];
expect(updatedSender.balance).toBe(0);
const withdrawal = getBankTransferByTransactionId(transaction.id);
expect(withdrawal.type).toBe(BankTransferType.withdrawal);
expect(withdrawal.amount).toBe(firstPaymentAmount);
// second transaction - $500
const secondPaymentDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Payment: ${sender.id} to ${receiver.id}`,
amount: secondPaymentAmount,
privacyLevel: DefaultPrivacyLevel.public,
status: TransactionStatus.pending,
};
const secondTransaction = createTransaction(sender.id, "payment", secondPaymentDetails);
expect(secondTransaction.id).toBeDefined();
expect(secondTransaction.status).toEqual("complete");
expect(secondTransaction.requestStatus).not.toBeDefined();
const secondUpdatedSender: User = getAllUsers()[0];
expect(secondUpdatedSender.balance).toBe(0);
const secondWithdrawal = getBankTransferByTransactionId(secondTransaction.id);
expect(secondWithdrawal.type).toBe(BankTransferType.withdrawal);
expect(secondWithdrawal.amount).toBe(secondPaymentAmount);
// Verify Deposit Transactions for Receiver
const updatedReceiverTransactions = getTransactionsByUserId(receiver.id);
expect(updatedReceiverTransactions.length).toBe(receiverTransactions.length + 2);
// Verify Receiver's Updated App Balance
const updatedReceiver: User = getAllUsers()[1];
expect(updatedReceiver.balance).toBe(
receiver.balance + firstPaymentAmount + secondPaymentAmount
);
});
it.skip("should create a request and withdrawal (bank transfer) for remaining balance", () => {
const sender: User = getAllUsers()[0];
const receiver: User = getAllUsers()[1];
const senderBankAccount = getBankAccountsByUserId(sender.id)[0];
const requestAmount = 100;
const receiverTransactions = getTransactionsByUserId(receiver.id);
expect(receiverTransactions.length).toBeGreaterThan(1);
const requestDetails: TransactionPayload = {
source: senderBankAccount.id!,
senderId: sender.id,
receiverId: receiver.id,
description: `Request: ${sender.id} to ${receiver.id}`,
amount: requestAmount,
privacyLevel: DefaultPrivacyLevel.public,
status: TransactionStatus.pending,
};
const transaction = createTransaction(sender.id, "request", requestDetails);
expect(transaction.id).toBeDefined();
expect(transaction.status).toEqual("pending");
expect(transaction.requestStatus).toBe(TransactionRequestStatus.pending);
const edits: Partial<Transaction> = {
requestStatus: TransactionRequestStatus.accepted,
};
updateTransactionById(transaction.id, edits);
const updatedTransaction = getTransactionById(transaction.id);
expect(updatedTransaction.requestStatus).toEqual("accepted");
const updatedReceiver: User = getAllUsers()[1];
expect(updatedReceiver.balance).toBe(receiver.balance + requestAmount);
// Verify Deposit Transactions for Sender
const updatedSenderTransactions = getTransactionsByUserId(sender.id);
expect(updatedSenderTransactions.length).toBe(receiverTransactions.length + 2);
// Verify Sender's Updated App Balance
const updatedSender: User = getAllUsers()[0];
expect(updatedSender.balance).toBe(sender.balance - requestAmount);
});
});