-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TransactionUtils.ts
487 lines (422 loc) · 17.2 KB
/
TransactionUtils.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import Onyx, {OnyxCollection} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {RecentWaypoint, ReportAction, Transaction} from '@src/types/onyx';
import {Comment, Receipt, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction';
import {isExpensifyCard} from './CardUtils';
import DateUtils from './DateUtils';
import * as NumberUtils from './NumberUtils';
type AdditionalTransactionChanges = {comment?: string; waypoints?: WaypointCollection};
type TransactionChanges = Partial<Transaction> & AdditionalTransactionChanges;
let allTransactions: OnyxCollection<Transaction> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
return;
}
allTransactions = Object.fromEntries(Object.entries(value).filter(([, transaction]) => !!transaction));
},
});
/**
* Optimistically generate a transaction.
*
* @param amount – in cents
* @param [existingTransactionID] When creating a distance request, an empty transaction has already been created with a transactionID. In that case, the transaction here needs to have it's transactionID match what was already generated.
*/
function buildOptimisticTransaction(
amount: number,
currency: string,
reportID: string,
comment = '',
created = '',
source = '',
originalTransactionID = '',
merchant = '',
receipt: Receipt = {},
filename = '',
existingTransactionID: string | null = null,
category = '',
tag = '',
billable = false,
): Transaction {
// transactionIDs are random, positive, 64-bit numeric strings.
// Because JS can only handle 53-bit numbers, transactionIDs are strings in the front-end (just like reportActionID)
const transactionID = existingTransactionID ?? NumberUtils.rand64();
const commentJSON: Comment = {comment};
if (source) {
commentJSON.source = source;
}
if (originalTransactionID) {
commentJSON.originalTransactionID = originalTransactionID;
}
return {
transactionID,
amount,
currency,
reportID,
comment: commentJSON,
merchant: merchant || CONST.TRANSACTION.DEFAULT_MERCHANT,
created: created || DateUtils.getDBTime(),
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
receipt,
filename,
category,
tag,
billable,
};
}
/**
* Check if the transaction has an Ereceipt
*/
function hasEReceipt(transaction: Transaction | undefined | null): boolean {
return !!transaction?.hasEReceipt;
}
function hasReceipt(transaction: Transaction | undefined | null): boolean {
return !!transaction?.receipt?.state || hasEReceipt(transaction);
}
function isMerchantMissing(transaction: Transaction) {
const isMerchantEmpty =
transaction.merchant === CONST.TRANSACTION.UNKNOWN_MERCHANT || transaction.merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT || transaction.merchant === '';
const isModifiedMerchantEmpty =
!transaction.modifiedMerchant ||
transaction.modifiedMerchant === CONST.TRANSACTION.UNKNOWN_MERCHANT ||
transaction.modifiedMerchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT ||
transaction.modifiedMerchant === '';
return isMerchantEmpty && isModifiedMerchantEmpty;
}
function isAmountMissing(transaction: Transaction) {
return transaction.amount === 0 && (!transaction.modifiedAmount || transaction.modifiedAmount === 0);
}
function isCreatedMissing(transaction: Transaction) {
return transaction.created === '' && (!transaction.created || transaction.modifiedCreated === '');
}
function areRequiredFieldsEmpty(transaction: Transaction): boolean {
return isMerchantMissing(transaction) || isAmountMissing(transaction) || isCreatedMissing(transaction);
}
/**
* Given the edit made to the money request, return an updated transaction object.
*/
function getUpdatedTransaction(transaction: Transaction, transactionChanges: TransactionChanges, isFromExpenseReport: boolean, shouldUpdateReceiptState = true): Transaction {
// Only changing the first level fields so no need for deep clone now
const updatedTransaction = {...transaction};
let shouldStopSmartscan = false;
// The comment property does not have its modifiedComment counterpart
if (Object.hasOwn(transactionChanges, 'comment')) {
updatedTransaction.comment = {
...updatedTransaction.comment,
comment: transactionChanges.comment,
};
}
if (Object.hasOwn(transactionChanges, 'created')) {
updatedTransaction.modifiedCreated = transactionChanges.created;
shouldStopSmartscan = true;
}
if (Object.hasOwn(transactionChanges, 'amount') && typeof transactionChanges.amount === 'number') {
updatedTransaction.modifiedAmount = isFromExpenseReport ? -transactionChanges.amount : transactionChanges.amount;
shouldStopSmartscan = true;
}
if (Object.hasOwn(transactionChanges, 'currency')) {
updatedTransaction.modifiedCurrency = transactionChanges.currency;
shouldStopSmartscan = true;
}
if (Object.hasOwn(transactionChanges, 'merchant')) {
updatedTransaction.modifiedMerchant = transactionChanges.merchant;
shouldStopSmartscan = true;
}
if (Object.hasOwn(transactionChanges, 'waypoints')) {
updatedTransaction.modifiedWaypoints = transactionChanges.waypoints;
shouldStopSmartscan = true;
}
if (Object.hasOwn(transactionChanges, 'billable') && typeof transactionChanges.billable === 'boolean') {
updatedTransaction.billable = transactionChanges.billable;
}
if (Object.hasOwn(transactionChanges, 'category') && typeof transactionChanges.category === 'string') {
updatedTransaction.category = transactionChanges.category;
}
if (Object.hasOwn(transactionChanges, 'tag') && typeof transactionChanges.tag === 'string') {
updatedTransaction.tag = transactionChanges.tag;
}
if (
shouldUpdateReceiptState &&
shouldStopSmartscan &&
transaction?.receipt &&
Object.keys(transaction.receipt).length > 0 &&
transaction?.receipt?.state !== CONST.IOU.RECEIPT_STATE.OPEN
) {
updatedTransaction.receipt.state = CONST.IOU.RECEIPT_STATE.OPEN;
}
updatedTransaction.pendingFields = {
...(Object.hasOwn(transactionChanges, 'comment') && {comment: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'created') && {created: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'amount') && {amount: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'currency') && {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'merchant') && {merchant: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'waypoints') && {waypoints: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'billable') && {billable: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'category') && {category: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(Object.hasOwn(transactionChanges, 'tag') && {tag: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
};
return updatedTransaction;
}
/**
* Retrieve the particular transaction object given its ID.
*
* @deprecated Use withOnyx() or Onyx.connect() instead
*/
function getTransaction(transactionID: string): Transaction | Record<string, never> {
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}
/**
* Return the comment field (referred to as description in the App) from the transaction.
* The comment does not have its modifiedComment counterpart.
*/
function getDescription(transaction: Transaction): string {
// Casting the description to string to avoid wrong data types (e.g. number) being returned from the API
return transaction?.comment?.comment?.toString() ?? '';
}
/**
* Return the amount field from the transaction, return the modifiedAmount if present.
*/
function getAmount(transaction: Transaction, isFromExpenseReport: boolean): number {
// IOU requests cannot have negative values but they can be stored as negative values, let's return absolute value
if (!isFromExpenseReport) {
const amount = transaction?.modifiedAmount ?? 0;
if (amount) {
return Math.abs(amount);
}
return Math.abs(transaction?.amount ?? 0);
}
// Expense report case:
// The amounts are stored using an opposite sign and negative values can be set,
// we need to return an opposite sign than is saved in the transaction object
let amount = transaction?.modifiedAmount ?? 0;
if (amount) {
return -amount;
}
// To avoid -0 being shown, lets only change the sign if the value is other than 0.
amount = transaction?.amount ?? 0;
return amount ? -amount : 0;
}
/**
* Return the currency field from the transaction, return the modifiedCurrency if present.
*/
function getCurrency(transaction: Transaction): string {
const currency = transaction?.modifiedCurrency ?? '';
if (currency) {
return currency;
}
return transaction?.currency ?? CONST.CURRENCY.USD;
}
/**
* Return the original currency field from the transaction.
*/
function getOriginalCurrency(transaction: Transaction): string {
return transaction?.originalCurrency ?? '';
}
/**
* Return the absolute value of the original amount field from the transaction.
*/
function getOriginalAmount(transaction: Transaction): number {
const amount = transaction?.originalAmount ?? 0;
return Math.abs(amount);
}
/**
* Return the merchant field from the transaction, return the modifiedMerchant if present.
*/
function getMerchant(transaction: Transaction): string {
return transaction?.modifiedMerchant ? transaction.modifiedMerchant : transaction?.merchant || '';
}
/**
* Return the mccGroup field from the transaction, return the modifiedMCCGroup if present.
*/
function getMCCGroup(transaction: Transaction): string {
return transaction?.modifiedMCCGroup ? transaction.modifiedMCCGroup : transaction?.mccGroup ?? '';
}
/**
* Return the waypoints field from the transaction, return the modifiedWaypoints if present.
*/
function getWaypoints(transaction: Transaction): WaypointCollection | undefined {
return transaction?.modifiedWaypoints ?? transaction?.comment?.waypoints;
}
/**
* Return the category from the transaction. This "category" field has no "modified" complement.
*/
function getCategory(transaction: Transaction): string {
return transaction?.category ?? '';
}
/**
* Return the cardID from the transaction.
*/
function getCardID(transaction: Transaction): number {
return transaction?.cardID ?? 0;
}
/**
* Return the billable field from the transaction. This "billable" field has no "modified" complement.
*/
function getBillable(transaction: Transaction): boolean {
return transaction?.billable ?? false;
}
/**
* Return the tag from the transaction. This "tag" field has no "modified" complement.
*/
function getTag(transaction: Transaction): string {
return transaction?.tag ?? '';
}
/**
* Return the created field from the transaction, return the modifiedCreated if present.
*/
function getCreated(transaction: Transaction, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING): string {
const created = transaction?.modifiedCreated ? transaction.modifiedCreated : transaction?.created || '';
return DateUtils.formatWithUTCTimeZone(created, dateFormat);
}
function isDistanceRequest(transaction: Transaction): boolean {
const type = transaction?.comment?.type;
const customUnitName = transaction?.comment?.customUnit?.name;
return type === CONST.TRANSACTION.TYPE.CUSTOM_UNIT && customUnitName === CONST.CUSTOM_UNITS.NAME_DISTANCE;
}
/**
* Determine whether a transaction is made with an Expensify card.
*/
function isExpensifyCardTransaction(transaction: Transaction): boolean {
if (!transaction.cardID) {
return false;
}
return isExpensifyCard(transaction.cardID);
}
/**
* Check if the transaction status is set to Pending.
*/
function isPending(transaction: Transaction): boolean {
if (!transaction.status) {
return false;
}
return transaction.status === CONST.TRANSACTION.STATUS.PENDING;
}
/**
* Check if the transaction status is set to Posted.
*/
function isPosted(transaction: Transaction): boolean {
if (!transaction.status) {
return false;
}
return transaction.status === CONST.TRANSACTION.STATUS.POSTED;
}
function isReceiptBeingScanned(transaction: Transaction): boolean {
return [CONST.IOU.RECEIPT_STATE.SCANREADY, CONST.IOU.RECEIPT_STATE.SCANNING].some((value) => value === transaction.receipt.state);
}
/**
* Check if the transaction has a non-smartscanning receipt and is missing required fields
*/
function hasMissingSmartscanFields(transaction: Transaction): boolean {
return hasReceipt(transaction) && !isDistanceRequest(transaction) && !isReceiptBeingScanned(transaction) && areRequiredFieldsEmpty(transaction);
}
/**
* Check if the transaction has a defined route
*/
function hasRoute(transaction: Transaction): boolean {
return !!transaction?.routes?.route0?.geometry?.coordinates;
}
/**
* Get the transactions related to a report preview with receipts
* Get the details linked to the IOU reportAction
*
* @deprecated Use Onyx.connect() or withOnyx() instead
*/
function getLinkedTransaction(reportAction: ReportAction): Transaction | Record<string, never> {
let transactionID = '';
if (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
transactionID = reportAction.originalMessage?.IOUTransactionID ?? '';
}
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}
function getAllReportTransactions(reportID?: string): Transaction[] {
// `reportID` from the `/CreateDistanceRequest` endpoint return's number instead of string for created `transaction`.
// For reference, https://github.com/Expensify/App/pull/26536#issuecomment-1703573277.
// We will update this in a follow-up Issue. According to this comment: https://github.com/Expensify/App/pull/26536#issuecomment-1703591019.
const transactions: Transaction[] = Object.values(allTransactions ?? {}).filter((transaction): transaction is Transaction => transaction !== null);
return transactions.filter((transaction) => `${transaction.reportID}` === `${reportID}`);
}
function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): boolean {
return !!waypoint?.address?.trim();
}
/**
* Converts the key of a waypoint to its index
*/
function getWaypointIndex(key: string): number {
return Number(key.replace('waypoint', ''));
}
/**
* Filters the waypoints which are valid and returns those
*/
function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = false): WaypointCollection {
const sortedIndexes = Object.keys(waypoints)
.map(getWaypointIndex)
.sort((a, b) => a - b);
const waypointValues = sortedIndexes.map((index) => waypoints[`waypoint${index}`]);
// Ensure the number of waypoints is between 2 and 25
if (waypointValues.length < 2 || waypointValues.length > 25) {
return {};
}
let lastWaypointIndex = -1;
return waypointValues.reduce<WaypointCollection>((acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[lastWaypointIndex];
// Check if the waypoint has a valid address
if (!waypointHasValidAddress(currentWaypoint)) {
return acc;
}
// Check for adjacent waypoints with the same address
if (previousWaypoint && currentWaypoint?.address === previousWaypoint.address) {
return acc;
}
const validatedWaypoints: WaypointCollection = {...acc, [`waypoint${reArrangeIndexes ? lastWaypointIndex + 1 : index}`]: currentWaypoint};
lastWaypointIndex += 1;
return validatedWaypoints;
}, {});
}
/**
* Returns the most recent transactions in an object
*/
function getRecentTransactions(transactions: Record<string, string>, size = 2): string[] {
return Object.keys(transactions)
.sort((transactionID1, transactionID2) => (new Date(transactions[transactionID1]) < new Date(transactions[transactionID2]) ? 1 : -1))
.slice(0, size);
}
export {
buildOptimisticTransaction,
getUpdatedTransaction,
getTransaction,
getDescription,
getAmount,
getCurrency,
getCardID,
getOriginalCurrency,
getOriginalAmount,
getMerchant,
getMCCGroup,
getCreated,
getCategory,
getBillable,
getTag,
getLinkedTransaction,
getAllReportTransactions,
hasReceipt,
hasEReceipt,
hasRoute,
isReceiptBeingScanned,
getValidWaypoints,
isDistanceRequest,
isExpensifyCardTransaction,
isPending,
isPosted,
getWaypoints,
isAmountMissing,
isMerchantMissing,
isCreatedMissing,
areRequiredFieldsEmpty,
hasMissingSmartscanFields,
getWaypointIndex,
waypointHasValidAddress,
getRecentTransactions,
};