Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as CollectionUtils from '@libs/CollectionUtils';
import DateUtils from '@libs/DateUtils';
import * as NumberUtils from '@libs/NumberUtils';
import {rand64} from '@libs/NumberUtils';
import {hasDependentTags, isPaidGroupPolicy} from '@libs/PolicyUtils';
import {getAllReportActions, getIOUActionForReportID, getOriginalMessage, getTrackExpenseActionableWhisper, isModifiedExpenseAction} from '@libs/ReportActionsUtils';
import {
buildOptimisticCreatedReportAction,
Expand All @@ -21,13 +22,16 @@ import {
findSelfDMReportID,
} from '@libs/ReportUtils';
import {getAmount, waypointHasValidAddress} from '@libs/TransactionUtils';
import ViolationsUtils from '@libs/Violations/ViolationsUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, RecentWaypoint, Report, ReportAction, ReviewDuplicates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {PersonalDetails, Policy, RecentWaypoint, Report, ReportAction, ReviewDuplicates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {OriginalMessageModifiedExpense} from '@src/types/onyx/OriginalMessage';
import type {OnyxData} from '@src/types/onyx/Request';
import type {WaypointCollection} from '@src/types/onyx/Transaction';
import type TransactionState from '@src/types/utils/TransactionStateType';
import {getPolicyCategoriesData} from './Policy/Category';
import {getPolicyTagsData} from './Policy/Tag';

let recentWaypoints: RecentWaypoint[] = [];
Onyx.connect({
Expand Down Expand Up @@ -598,7 +602,7 @@ function setTransactionReport(transactionID: string, reportID: string, isDraft:
Onyx.merge(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {reportID});
}

function changeTransactionsReport(transactionIDs: string[], reportID: string) {
function changeTransactionsReport(transactionIDs: string[], reportID: string, policy?: OnyxEntry<Policy>) {
const newReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (!newReport) {
return;
Expand Down Expand Up @@ -655,7 +659,27 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
},
});

// 2. Keep track of the new report totals
// 2. Calculate transaction violations if moving transaction to a workspace
if (isPaidGroupPolicy(policy) && policy?.id) {
const policyTagList = getPolicyTagsData(policy.id);
const violationData = ViolationsUtils.getViolationsOnyxData(
transaction,
allTransactionViolations,
policy,
policyTagList,
getPolicyCategoriesData(policy.id),
hasDependentTags(policy, policyTagList),
false,
);
optimisticData.push(violationData);
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`,
value: allTransactionViolation?.[transaction.transactionID],
});
}

// 3. Keep track of the new report totals
const transactionAmount = getAmount(transaction);
if (oldReport) {
updatedReportTotals[oldReportID] = (updatedReportTotals[oldReportID] ? updatedReportTotals[oldReportID] : (oldReport?.total ?? 0)) + transactionAmount;
Expand All @@ -664,7 +688,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
updatedReportTotals[reportID] = (updatedReportTotals[reportID] ? updatedReportTotals[reportID] : (newReport.total ?? 0)) - transactionAmount;
}

// 3. Optimistically update the IOU action reportID
// 4. Optimistically update the IOU action reportID
const optimisticMoneyRequestReportActionID = rand64();

const newIOUAction = {
Expand Down Expand Up @@ -738,7 +762,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
);
}

// 4. Optimistically update the transaction thread and all threads in the transaction thread
// 5. Optimistically update the transaction thread and all threads in the transaction thread
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${newIOUAction.childReportID}`,
Expand All @@ -761,7 +785,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
});
}

// 5. (Optional) Create transactionThread if it doesn't exist
// 6. (Optional) Create transactionThread if it doesn't exist
let transactionThreadReportID = newIOUAction.childReportID;
let transactionThreadCreatedReportActionID;
if (!transactionThreadReportID) {
Expand Down Expand Up @@ -821,7 +845,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
);
}

// 6. Add MOVED_TRANSACTION or UNREPORTED_TRANSACTION report actions
// 7. Add MOVED_TRANSACTION or UNREPORTED_TRANSACTION report actions
const movedAction =
reportID === CONST.REPORT.UNREPORTED_REPORT_ID
? buildOptimisticUnreportedTransactionAction(transactionThreadReportID, transaction.reportID)
Expand Down Expand Up @@ -861,7 +885,7 @@ function changeTransactionsReport(transactionIDs: string[], reportID: string) {
return;
}

// 7. Update the report totals
// 8. Update the report totals
Object.entries(updatedReportTotals).forEach(([reportIDToUpdate, total]) => {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
4 changes: 3 additions & 1 deletion src/pages/AddUnreportedExpense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {ListItem, SectionListDataType, SelectionListHandle} from '@componen
import UnreportedExpensesSkeleton from '@components/Skeletons/UnreportedExpensesSkeleton';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePolicy from '@hooks/usePolicy';
import useThemeStyles from '@hooks/useThemeStyles';
import {fetchUnreportedExpenses} from '@libs/actions/UnreportedExpenses';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
Expand Down Expand Up @@ -40,6 +41,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {

const {reportID, backToReport} = route.params;
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: true});
const policy = usePolicy(report?.policyID);
const [hasMoreUnreportedTransactionsResults] = useOnyx(ONYXKEYS.HAS_MORE_UNREPORTED_TRANSACTIONS_RESULTS, {canBeMissing: true});
const [isLoadingUnreportedTransactions] = useOnyx(ONYXKEYS.IS_LOADING_UNREPORTED_TRANSACTIONS, {canBeMissing: true});
const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline;
Expand Down Expand Up @@ -180,7 +182,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
return;
}
Navigation.dismissModal();
changeTransactionsReport([...selectedIds], report?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID);
changeTransactionsReport([...selectedIds], report?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID, policy);
setErrorMessage('');
}}
onEndReached={fetchMoreUnreportedTransactions}
Expand Down