Skip to content

Commit d26382a

Browse files
MelvinBotdmkt9
andcommitted
Refactor: Extract shared optimistic/failure transaction data helpers in Duplicate.ts
Extract buildOptimisticTransactionData and buildFailureTransactionData helpers to eliminate duplication between mergeDuplicates and resolveDuplicates. Co-authored-by: dmkt9 <dmkt9@users.noreply.github.com>
1 parent 7d8ad1a commit d26382a

1 file changed

Lines changed: 96 additions & 57 deletions

File tree

src/libs/actions/IOU/Duplicate.ts

Lines changed: 96 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -71,53 +71,105 @@ function getIOUActionForTransactions(transactionIDList: Array<string | undefined
7171
);
7272
}
7373

74-
type MergeDuplicatesFuncParams = MergeDuplicatesParams & {currentUserLogin: string; currentUserAccountID: number; taxAmount?: number; taxValue?: string};
74+
type DuplicateTransactionParams = {
75+
transactionID: string | undefined;
76+
originalSelectedTransaction: OnyxEntry<OnyxTypes.Transaction>;
77+
billable: boolean;
78+
comment: string;
79+
category: string;
80+
created: string;
81+
currency: string;
82+
merchant: string;
83+
reimbursable: boolean;
84+
tag: string;
85+
taxCode?: string;
86+
taxAmount?: number;
87+
taxValue?: string;
88+
};
7589

76-
/** Merge several transactions into one by updating the fields of the one we want to keep and deleting the rest */
77-
function mergeDuplicates({
78-
transactionThreadReportID: optimisticTransactionThreadReportID,
79-
currentUserLogin,
80-
currentUserAccountID,
90+
function buildOptimisticTransactionData({
91+
transactionID,
92+
originalSelectedTransaction,
93+
billable,
94+
comment,
95+
category,
96+
created,
97+
currency,
98+
merchant,
99+
reimbursable,
100+
tag,
101+
taxCode,
81102
taxAmount,
82103
taxValue,
83-
...params
84-
}: MergeDuplicatesFuncParams) {
85-
const allParams: MergeDuplicatesParams = {...params};
86-
const allTransactions = getAllTransactions();
87-
const allTransactionViolations = getAllTransactionViolations();
88-
const allReports = getAllReports();
89-
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`];
90-
91-
const optimisticTransactionData: OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> = {
104+
}: DuplicateTransactionParams): OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> {
105+
return {
92106
onyxMethod: Onyx.METHOD.MERGE,
93-
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`,
107+
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
94108
value: {
95109
...originalSelectedTransaction,
96-
billable: params.billable,
110+
billable,
97111
comment: {
98-
comment: params.comment,
112+
comment,
99113
},
100-
category: params.category,
101-
created: params.created,
102-
currency: params.currency,
103-
modifiedMerchant: params.merchant,
104-
reimbursable: params.reimbursable,
105-
tag: params.tag,
106-
taxCode: params.taxCode ?? originalSelectedTransaction?.taxCode,
114+
category,
115+
created,
116+
currency,
117+
modifiedMerchant: merchant,
118+
reimbursable,
119+
tag,
120+
taxCode: taxCode ?? originalSelectedTransaction?.taxCode,
107121
taxAmount: taxAmount ?? originalSelectedTransaction?.taxAmount,
108122
taxValue: taxValue ?? originalSelectedTransaction?.taxValue,
109123
// Clear `taxName` to stay consistent with the server response,
110124
// and avoid retaining an outdated value that doesn't match the new `taxCode`.
111-
taxName: params.taxCode ? null : originalSelectedTransaction?.taxName,
125+
taxName: taxCode ? null : originalSelectedTransaction?.taxName,
112126
},
113127
};
128+
}
114129

115-
const failureTransactionData: OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> = {
130+
function buildFailureTransactionData(transactionID: string | undefined, originalSelectedTransaction: OnyxEntry<OnyxTypes.Transaction>): OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> {
131+
return {
116132
onyxMethod: Onyx.METHOD.MERGE,
117-
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`,
133+
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
118134
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
119135
value: originalSelectedTransaction as OnyxTypes.Transaction,
120136
};
137+
}
138+
139+
type MergeDuplicatesFuncParams = MergeDuplicatesParams & {currentUserLogin: string; currentUserAccountID: number; taxAmount?: number; taxValue?: string};
140+
141+
/** Merge several transactions into one by updating the fields of the one we want to keep and deleting the rest */
142+
function mergeDuplicates({
143+
transactionThreadReportID: optimisticTransactionThreadReportID,
144+
currentUserLogin,
145+
currentUserAccountID,
146+
taxAmount,
147+
taxValue,
148+
...params
149+
}: MergeDuplicatesFuncParams) {
150+
const allParams: MergeDuplicatesParams = {...params};
151+
const allTransactions = getAllTransactions();
152+
const allTransactionViolations = getAllTransactionViolations();
153+
const allReports = getAllReports();
154+
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`];
155+
156+
const optimisticTransactionData = buildOptimisticTransactionData({
157+
transactionID: params.transactionID,
158+
originalSelectedTransaction,
159+
billable: params.billable,
160+
comment: params.comment,
161+
category: params.category,
162+
created: params.created,
163+
currency: params.currency,
164+
merchant: params.merchant,
165+
reimbursable: params.reimbursable,
166+
tag: params.tag,
167+
taxCode: params.taxCode,
168+
taxAmount,
169+
taxValue,
170+
});
171+
172+
const failureTransactionData = buildFailureTransactionData(params.transactionID, originalSelectedTransaction);
121173

122174
const optimisticTransactionDuplicatesData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION>> = params.transactionIDList.map((id) => ({
123175
onyxMethod: Onyx.METHOD.SET,
@@ -367,36 +419,23 @@ function resolveDuplicates({taxAmount, taxValue, ...params}: MergeDuplicatesPara
367419

368420
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`];
369421

370-
const optimisticTransactionData: OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> = {
371-
onyxMethod: Onyx.METHOD.MERGE,
372-
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`,
373-
value: {
374-
...originalSelectedTransaction,
375-
billable: params.billable,
376-
comment: {
377-
comment: params.comment,
378-
},
379-
category: params.category,
380-
created: params.created,
381-
currency: params.currency,
382-
modifiedMerchant: params.merchant,
383-
reimbursable: params.reimbursable,
384-
tag: params.tag,
385-
taxCode: params.taxCode ?? originalSelectedTransaction?.taxCode,
386-
taxAmount: taxAmount ?? originalSelectedTransaction?.taxAmount,
387-
taxValue: taxValue ?? originalSelectedTransaction?.taxValue,
388-
// Clear `taxName` to stay consistent with the server response,
389-
// and avoid retaining an outdated value that doesn't match the new `taxCode`.
390-
taxName: params.taxCode ? null : originalSelectedTransaction?.taxName,
391-
},
392-
};
422+
const optimisticTransactionData = buildOptimisticTransactionData({
423+
transactionID: params.transactionID,
424+
originalSelectedTransaction,
425+
billable: params.billable,
426+
comment: params.comment,
427+
category: params.category,
428+
created: params.created,
429+
currency: params.currency,
430+
merchant: params.merchant,
431+
reimbursable: params.reimbursable,
432+
tag: params.tag,
433+
taxCode: params.taxCode,
434+
taxAmount,
435+
taxValue,
436+
});
393437

394-
const failureTransactionData: OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION> = {
395-
onyxMethod: Onyx.METHOD.MERGE,
396-
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`,
397-
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
398-
value: originalSelectedTransaction as OnyxTypes.Transaction,
399-
};
438+
const failureTransactionData = buildFailureTransactionData(params.transactionID, originalSelectedTransaction);
400439

401440
const optimisticTransactionViolations: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS>> = [...params.transactionIDList, params.transactionID].map((id) => {
402441
const violations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${id}`] ?? [];

0 commit comments

Comments
 (0)