@@ -71,40 +71,105 @@ function getIOUActionForTransactions(transactionIDList: Array<string | undefined
7171 ) ;
7272}
7373
74- type MergeDuplicatesFuncParams = MergeDuplicatesParams & { currentUserLogin : string ; currentUserAccountID : number } ;
75-
76- /** Merge several transactions into one by updating the fields of the one we want to keep and deleting the rest */
77- function mergeDuplicates ( { transactionThreadReportID : optimisticTransactionThreadReportID , currentUserLogin, currentUserAccountID, ...params } : MergeDuplicatesFuncParams ) {
78- const allParams : MergeDuplicatesParams = { ...params } ;
79- const allTransactions = getAllTransactions ( ) ;
80- const allTransactionViolations = getAllTransactionViolations ( ) ;
81- const allReports = getAllReports ( ) ;
82- const originalSelectedTransaction = allTransactions [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ] ;
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+ } ;
8389
84- const optimisticTransactionData : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > = {
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,
102+ taxAmount,
103+ taxValue,
104+ } : DuplicateTransactionParams ) : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > {
105+ return {
85106 onyxMethod : Onyx . METHOD . MERGE ,
86- key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ,
107+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` ,
87108 value : {
88109 ...originalSelectedTransaction ,
89- billable : params . billable ,
110+ billable,
90111 comment : {
91- comment : params . comment ,
112+ comment,
92113 } ,
93- category : params . category ,
94- created : params . created ,
95- currency : params . currency ,
96- modifiedMerchant : params . merchant ,
97- reimbursable : params . reimbursable ,
98- tag : params . tag ,
114+ category,
115+ created,
116+ currency,
117+ modifiedMerchant : merchant ,
118+ reimbursable,
119+ tag,
120+ taxCode : taxCode ?? originalSelectedTransaction ?. taxCode ,
121+ taxAmount : taxAmount ?? originalSelectedTransaction ?. taxAmount ,
122+ taxValue : taxValue ?? originalSelectedTransaction ?. taxValue ,
123+ // Clear `taxName` to stay consistent with the server response,
124+ // and avoid retaining an outdated value that doesn't match the new `taxCode`.
125+ taxName : taxCode ? null : originalSelectedTransaction ?. taxName ,
99126 } ,
100127 } ;
128+ }
101129
102- const failureTransactionData : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > = {
130+ function buildFailureTransactionData ( transactionID : string | undefined , originalSelectedTransaction : OnyxEntry < OnyxTypes . Transaction > ) : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > {
131+ return {
103132 onyxMethod : Onyx . METHOD . MERGE ,
104- key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ,
133+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` ,
105134 // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
106135 value : originalSelectedTransaction as OnyxTypes . Transaction ,
107136 } ;
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 ) ;
108173
109174 const optimisticTransactionDuplicatesData : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > > = params . transactionIDList . map ( ( id ) => ( {
110175 onyxMethod : Onyx . METHOD . SET ,
@@ -344,7 +409,7 @@ function mergeDuplicates({transactionThreadReportID: optimisticTransactionThread
344409}
345410
346411/** Instead of merging the duplicates, it updates the transaction we want to keep and puts the others on hold without deleting them */
347- function resolveDuplicates ( params : MergeDuplicatesParams ) {
412+ function resolveDuplicates ( { taxAmount , taxValue , ... params } : MergeDuplicatesParams & { taxAmount ?: number ; taxValue ?: string } ) {
348413 if ( ! params . transactionID ) {
349414 return ;
350415 }
@@ -354,30 +419,23 @@ function resolveDuplicates(params: MergeDuplicatesParams) {
354419
355420 const originalSelectedTransaction = allTransactions [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ] ;
356421
357- const optimisticTransactionData : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > = {
358- onyxMethod : Onyx . METHOD . MERGE ,
359- key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ,
360- value : {
361- ...originalSelectedTransaction ,
362- billable : params . billable ,
363- comment : {
364- comment : params . comment ,
365- } ,
366- category : params . category ,
367- created : params . created ,
368- currency : params . currency ,
369- modifiedMerchant : params . merchant ,
370- reimbursable : params . reimbursable ,
371- tag : params . tag ,
372- } ,
373- } ;
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+ } ) ;
374437
375- const failureTransactionData : OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION > = {
376- onyxMethod : Onyx . METHOD . MERGE ,
377- key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ params . transactionID } ` ,
378- // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
379- value : originalSelectedTransaction as OnyxTypes . Transaction ,
380- } ;
438+ const failureTransactionData = buildFailureTransactionData ( params . transactionID , originalSelectedTransaction ) ;
381439
382440 const optimisticTransactionViolations : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS > > = [ ...params . transactionIDList , params . transactionID ] . map ( ( id ) => {
383441 const violations = allTransactionViolations [ `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ id } ` ] ?? [ ] ;
0 commit comments