@@ -18,7 +18,7 @@ import {getDistanceRateCustomUnit, goBackWhenEnableFeature, removePendingFieldsF
1818import * as ReportUtils from '@libs/ReportUtils' ;
1919import CONST from '@src/CONST' ;
2020import ONYXKEYS from '@src/ONYXKEYS' ;
21- import type { Policy , Report , Transaction , TransactionViolation } from '@src/types/onyx' ;
21+ import type { Policy , Report , TransactionViolation } from '@src/types/onyx' ;
2222import type { ErrorFields } from '@src/types/onyx/OnyxCommon' ;
2323import type { CustomUnit , Rate } from '@src/types/onyx/Policy' ;
2424import type { OnyxData } from '@src/types/onyx/Request' ;
@@ -56,29 +56,6 @@ Onyx.connect({
5656 } ,
5757} ) ;
5858
59- let allTransactions : NonNullable < OnyxCollection < Transaction > > = { } ;
60- Onyx . connect ( {
61- key : ONYXKEYS . COLLECTION . TRANSACTION ,
62- waitForCollectionCallback : true ,
63- callback : ( value ) => {
64- if ( ! value ) {
65- allTransactions = { } ;
66- return ;
67- }
68-
69- allTransactions = value ;
70- } ,
71- } ) ;
72-
73- let transactionViolations : OnyxCollection < TransactionViolation [ ] > ;
74- Onyx . connect ( {
75- key : ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS ,
76- waitForCollectionCallback : true ,
77- callback : ( value ) => {
78- transactionViolations = value ;
79- } ,
80- } ) ;
81-
8259/**
8360 * Takes array of customUnitRates and removes pendingFields and errorFields from each rate - we don't want to send those via API
8461 */
@@ -451,7 +428,13 @@ function setPolicyDistanceRatesEnabled(policyID: string, customUnit: CustomUnit,
451428 API . write ( WRITE_COMMANDS . SET_POLICY_DISTANCE_RATES_ENABLED , params , { optimisticData, successData, failureData} ) ;
452429}
453430
454- function deletePolicyDistanceRates ( policyID : string , customUnit : CustomUnit , rateIDsToDelete : string [ ] ) {
431+ function deletePolicyDistanceRates (
432+ policyID : string ,
433+ customUnit : CustomUnit ,
434+ rateIDsToDelete : string [ ] ,
435+ transactionIDsAffected : string [ ] ,
436+ transactionViolations : OnyxCollection < TransactionViolation [ ] > ,
437+ ) {
455438 const currentRates = customUnit . rates ;
456439 const optimisticRates : Record < string , Rate > = { } ;
457440 const successRates : Record < string , Rate > = { } ;
@@ -520,22 +503,18 @@ function deletePolicyDistanceRates(policyID: string, customUnit: CustomUnit, rat
520503 } ,
521504 ] ;
522505
523- const transactions = Object . values ( allTransactions ?? { } ) . filter (
524- ( transaction ) =>
525- transaction ?. comment ?. customUnit ?. customUnitID === customUnit . customUnitID &&
526- transaction ?. comment ?. customUnit ?. customUnitRateID &&
527- rateIDsToDelete . includes ( transaction ?. comment ?. customUnit ?. customUnitRateID ) ,
528- ) ;
529506 const optimisticTransactionsViolations : OnyxUpdate [ ] = [ ] ;
507+ const failureTransactionsViolations : OnyxUpdate [ ] = [ ] ;
530508
531- transactions . forEach ( ( transaction ) => {
532- const currentTransactionViolations = transactionViolations ?. [ `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction ?. transactionID } ` ] ?? [ ] ;
509+ for ( const transactionID of transactionIDsAffected ) {
510+ const currentTransactionViolations = transactionViolations ?. [ `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transactionID } ` ] ?? [ ] ;
533511 if ( currentTransactionViolations . some ( ( violation ) => violation . name === CONST . VIOLATIONS . CUSTOM_UNIT_OUT_OF_POLICY ) ) {
534512 return ;
535513 }
514+
536515 optimisticTransactionsViolations . push ( {
537516 onyxMethod : Onyx . METHOD . MERGE ,
538- key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction ?. transactionID } ` ,
517+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transactionID } ` ,
539518 value : [
540519 ...currentTransactionViolations ,
541520 {
@@ -545,12 +524,13 @@ function deletePolicyDistanceRates(policyID: string, customUnit: CustomUnit, rat
545524 } ,
546525 ] ,
547526 } ) ;
548- } ) ;
549527
550- const failureTransactionsViolations : OnyxUpdate [ ] = transactions . map ( ( transaction ) => {
551- const currentTransactionViolations = transactionViolations ?. [ `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction ?. transactionID } ` ] ;
552- return { onyxMethod : Onyx . METHOD . MERGE , key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction ?. transactionID } ` , value : currentTransactionViolations } ;
553- } ) ;
528+ failureTransactionsViolations . push ( {
529+ onyxMethod : Onyx . METHOD . MERGE ,
530+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transactionID } ` ,
531+ value : currentTransactionViolations ,
532+ } ) ;
533+ }
554534
555535 optimisticData . push ( ...optimisticTransactionsViolations ) ;
556536 failureData . push ( ...failureTransactionsViolations ) ;
0 commit comments