Skip to content

Commit c38b2e1

Browse files
authored
Merge pull request Expensify#64787 from daledah/fix/63792
fix: calculate violations when changing transaction report
2 parents 67dc880 + 4e3cb7c commit c38b2e1

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

src/libs/actions/Transaction.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as CollectionUtils from '@libs/CollectionUtils';
1111
import DateUtils from '@libs/DateUtils';
1212
import * as NumberUtils from '@libs/NumberUtils';
1313
import {rand64} from '@libs/NumberUtils';
14+
import {hasDependentTags, isPaidGroupPolicy} from '@libs/PolicyUtils';
1415
import {getAllReportActions, getIOUActionForReportID, getOriginalMessage, getTrackExpenseActionableWhisper, isModifiedExpenseAction} from '@libs/ReportActionsUtils';
1516
import {
1617
buildOptimisticCreatedReportAction,
@@ -21,13 +22,16 @@ import {
2122
findSelfDMReportID,
2223
} from '@libs/ReportUtils';
2324
import {getAmount, waypointHasValidAddress} from '@libs/TransactionUtils';
25+
import ViolationsUtils from '@libs/Violations/ViolationsUtils';
2426
import CONST from '@src/CONST';
2527
import 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';
2729
import type {OriginalMessageModifiedExpense} from '@src/types/onyx/OriginalMessage';
2830
import type {OnyxData} from '@src/types/onyx/Request';
2931
import type {WaypointCollection} from '@src/types/onyx/Transaction';
3032
import type TransactionState from '@src/types/utils/TransactionStateType';
33+
import {getPolicyCategoriesData} from './Policy/Category';
34+
import {getPolicyTagsData} from './Policy/Tag';
3135

3236
let recentWaypoints: RecentWaypoint[] = [];
3337
Onyx.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,

src/pages/AddUnreportedExpense.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {ListItem, SectionListDataType, SelectionListHandle} from '@componen
1111
import UnreportedExpensesSkeleton from '@components/Skeletons/UnreportedExpensesSkeleton';
1212
import useLocalize from '@hooks/useLocalize';
1313
import useNetwork from '@hooks/useNetwork';
14+
import usePolicy from '@hooks/usePolicy';
1415
import useThemeStyles from '@hooks/useThemeStyles';
1516
import {fetchUnreportedExpenses} from '@libs/actions/UnreportedExpenses';
1617
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
@@ -40,6 +41,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
4041

4142
const {reportID, backToReport} = route.params;
4243
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: true});
44+
const policy = usePolicy(report?.policyID);
4345
const [hasMoreUnreportedTransactionsResults] = useOnyx(ONYXKEYS.HAS_MORE_UNREPORTED_TRANSACTIONS_RESULTS, {canBeMissing: true});
4446
const [isLoadingUnreportedTransactions] = useOnyx(ONYXKEYS.IS_LOADING_UNREPORTED_TRANSACTIONS, {canBeMissing: true});
4547
const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline;
@@ -180,7 +182,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
180182
return;
181183
}
182184
Navigation.dismissModal();
183-
changeTransactionsReport([...selectedIds], report?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID);
185+
changeTransactionsReport([...selectedIds], report?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID, policy);
184186
setErrorMessage('');
185187
}}
186188
onEndReached={fetchMoreUnreportedTransactions}

0 commit comments

Comments
 (0)