@@ -11,6 +11,7 @@ import * as CollectionUtils from '@libs/CollectionUtils';
1111import DateUtils from '@libs/DateUtils' ;
1212import * as NumberUtils from '@libs/NumberUtils' ;
1313import { rand64 } from '@libs/NumberUtils' ;
14+ import { hasDependentTags , isPaidGroupPolicy } from '@libs/PolicyUtils' ;
1415import { getAllReportActions , getIOUActionForReportID , getOriginalMessage , getTrackExpenseActionableWhisper , isModifiedExpenseAction } from '@libs/ReportActionsUtils' ;
1516import {
1617 buildOptimisticCreatedReportAction ,
@@ -21,13 +22,16 @@ import {
2122 findSelfDMReportID ,
2223} from '@libs/ReportUtils' ;
2324import { getAmount , waypointHasValidAddress } from '@libs/TransactionUtils' ;
25+ import ViolationsUtils from '@libs/Violations/ViolationsUtils' ;
2426import CONST from '@src/CONST' ;
2527import ONYXKEYS from '@src/ONYXKEYS' ;
26- import type { PersonalDetails , RecentWaypoint , Report , ReportAction , ReviewDuplicates , Transaction , TransactionViolation , TransactionViolations } from '@src/types/onyx' ;
28+ import type { PersonalDetails , Policy , RecentWaypoint , Report , ReportAction , ReviewDuplicates , Transaction , TransactionViolation , TransactionViolations } from '@src/types/onyx' ;
2729import type { OriginalMessageModifiedExpense } from '@src/types/onyx/OriginalMessage' ;
2830import type { OnyxData } from '@src/types/onyx/Request' ;
2931import type { WaypointCollection } from '@src/types/onyx/Transaction' ;
3032import type TransactionState from '@src/types/utils/TransactionStateType' ;
33+ import { getPolicyCategoriesData } from './Policy/Category' ;
34+ import { getPolicyTagsData } from './Policy/Tag' ;
3135
3236let recentWaypoints : RecentWaypoint [ ] = [ ] ;
3337Onyx . connect ( {
@@ -598,7 +602,7 @@ function setTransactionReport(transactionID: string, reportID: string, isDraft:
598602 Onyx . merge ( `${ isDraft ? ONYXKEYS . COLLECTION . TRANSACTION_DRAFT : ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` , { reportID} ) ;
599603}
600604
601- function changeTransactionsReport ( transactionIDs : string [ ] , reportID : string ) {
605+ function changeTransactionsReport ( transactionIDs : string [ ] , reportID : string , policy ?: OnyxEntry < Policy > ) {
602606 const newReport = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ] ;
603607 if ( ! newReport ) {
604608 return ;
@@ -655,7 +659,27 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
655659 } ,
656660 } ) ;
657661
658- // 2. Keep track of the new report totals
662+ // 2. Calculate transaction violations if moving transaction to a workspace
663+ if ( isPaidGroupPolicy ( policy ) && policy ?. id ) {
664+ const policyTagList = getPolicyTagsData ( policy . id ) ;
665+ const violationData = ViolationsUtils . getViolationsOnyxData (
666+ transaction ,
667+ allTransactionViolations ,
668+ policy ,
669+ policyTagList ,
670+ getPolicyCategoriesData ( policy . id ) ,
671+ hasDependentTags ( policy , policyTagList ) ,
672+ false ,
673+ ) ;
674+ optimisticData . push ( violationData ) ;
675+ failureData . push ( {
676+ onyxMethod : Onyx . METHOD . MERGE ,
677+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction . transactionID } ` ,
678+ value : allTransactionViolation ?. [ transaction . transactionID ] ,
679+ } ) ;
680+ }
681+
682+ // 3. Keep track of the new report totals
659683 const transactionAmount = getAmount ( transaction ) ;
660684 if ( oldReport ) {
661685 updatedReportTotals [ oldReportID ] = ( updatedReportTotals [ oldReportID ] ? updatedReportTotals [ oldReportID ] : ( oldReport ?. total ?? 0 ) ) + transactionAmount ;
@@ -664,7 +688,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
664688 updatedReportTotals [ reportID ] = ( updatedReportTotals [ reportID ] ? updatedReportTotals [ reportID ] : ( newReport . total ?? 0 ) ) - transactionAmount ;
665689 }
666690
667- // 3 . Optimistically update the IOU action reportID
691+ // 4 . Optimistically update the IOU action reportID
668692 const optimisticMoneyRequestReportActionID = rand64 ( ) ;
669693
670694 const newIOUAction = {
@@ -738,7 +762,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
738762 ) ;
739763 }
740764
741- // 4 . Optimistically update the transaction thread and all threads in the transaction thread
765+ // 5 . Optimistically update the transaction thread and all threads in the transaction thread
742766 optimisticData . push ( {
743767 onyxMethod : Onyx . METHOD . MERGE ,
744768 key : `${ ONYXKEYS . COLLECTION . REPORT } ${ newIOUAction . childReportID } ` ,
@@ -761,7 +785,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
761785 } ) ;
762786 }
763787
764- // 5 . (Optional) Create transactionThread if it doesn't exist
788+ // 6 . (Optional) Create transactionThread if it doesn't exist
765789 let transactionThreadReportID = newIOUAction . childReportID ;
766790 let transactionThreadCreatedReportActionID ;
767791 if ( ! transactionThreadReportID ) {
@@ -821,7 +845,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
821845 ) ;
822846 }
823847
824- // 6 . Add MOVED_TRANSACTION or UNREPORTED_TRANSACTION report actions
848+ // 7 . Add MOVED_TRANSACTION or UNREPORTED_TRANSACTION report actions
825849 const movedAction =
826850 reportID === CONST . REPORT . UNREPORTED_REPORT_ID
827851 ? buildOptimisticUnreportedTransactionAction ( transactionThreadReportID , transaction . reportID )
@@ -861,7 +885,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
861885 return ;
862886 }
863887
864- // 7 . Update the report totals
888+ // 8 . Update the report totals
865889 Object . entries ( updatedReportTotals ) . forEach ( ( [ reportIDToUpdate , total ] ) => {
866890 optimisticData . push ( {
867891 onyxMethod : Onyx . METHOD . MERGE ,
0 commit comments