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
3 changes: 2 additions & 1 deletion src/libs/API/parameters/ChangeTransactionsReportParams.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* A map linking the optimistic MOVED_TRANSACTION or UNREPORTED_TRANSACTION reportActionID to the transactionID.
* movedReportActionID is omitted when the move doesn't create a system message (moves out of a draft report).
* If we're creating the transactionThread as part of moving the transaction, we should also send the optimistic
* transactionThreadReportID and transactionThreadCreatedReportActionID. If the transaction is held before moving to selfDM,
* we should also send the unholdReportActionID.
*/
type TransactionThreadInfo = {
movedReportActionID: string;
movedReportActionID?: string;
moneyRequestPreviewReportActionID: string;
transactionThreadReportID?: string;
transactionThreadCreatedReportActionID?: string;
Expand Down
45 changes: 25 additions & 20 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getUnheldReimbursableTotal,
hasViolations as hasViolationsReportUtils,
isExpenseReport,
isOpenReport,
isReportTotalPending,
shouldEnableNegative,
} from '@libs/ReportUtils';
Expand Down Expand Up @@ -1579,32 +1580,36 @@ function changeTransactionsReport({
}

// 7. Add MOVED_TRANSACTION or UNREPORTED_TRANSACTION report actions
const movedAction =
reportID === CONST.REPORT.UNREPORTED_REPORT_ID
? buildOptimisticUnreportedTransactionAction(transactionThreadReportID, oldReportID)
: buildOptimisticMovedTransactionAction(transactionThreadReportID, oldReportID);
let movedAction;
if (reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
movedAction = buildOptimisticUnreportedTransactionAction(transactionThreadReportID, oldReportID);
} else if (!isOpenReport(oldReport)) {
movedAction = buildOptimisticMovedTransactionAction(transactionThreadReportID, oldReportID);
}

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction?.reportActionID]: movedAction},
});
if (movedAction) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction.reportActionID]: movedAction},
});

successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction?.reportActionID]: {pendingAction: null}},
});
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction.reportActionID]: {pendingAction: null}},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction?.reportActionID]: null},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {[movedAction.reportActionID]: null},
});
}

// Create base transaction data object
const baseTransactionData = {
movedReportActionID: movedAction.reportActionID,
movedReportActionID: movedAction?.reportActionID,
moneyRequestPreviewReportActionID: newIOUAction.reportActionID,
...(transactionThreadCreatedReportActionID
? {
Expand Down
117 changes: 117 additions & 0 deletions tests/actions/TransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';

import {format} from 'date-fns';
import Onyx from 'react-native-onyx';
import createRandomReportAction from 'tests/utils/collections/reportActions';

import {changeTransactionsReport as changeTransactionsReportAction} from '../../src/libs/actions/Transaction';
import currencyList from '../unit/currencyList.json';
Expand Down Expand Up @@ -436,6 +437,122 @@ describe('actions/Transaction', () => {
expect(updated?.modifiedMerchant).toContain('mi');
});

describe('moved system messages', () => {
const TRANSACTION_ID = 'txn-moved-message';
const TRANSACTION_THREAD_REPORT_ID = 'txn-thread-moved-message';
const SOURCE_REPORT_ID = 'source-expense-report';
const DESTINATION_REPORT_ID = 'destination-expense-report';

/**
* Seeds an expense sitting in a source report (whose state/status decide whether it is a draft),
* along with its IOU action and transaction thread, and moves it to `newReport`.
*/
async function moveExpenseOutOf(sourceReportStatus: Pick<Report, 'stateNum' | 'statusNum'>, newReport: Report | undefined) {
const policyID = generatePolicyID();
const policy: Policy = {...createRandomPolicy(3, CONST.POLICY.TYPE.TEAM, 'Moved Message Workspace'), id: policyID};

const sourceReport = {
...createRandomReport(1),
reportID: SOURCE_REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
policyID,
ownerAccountID: RORY_ACCOUNT_ID,
...sourceReportStatus,
} as Report;

const transaction: Transaction = {
transactionID: TRANSACTION_ID,
reportID: SOURCE_REPORT_ID,
amount: -1000,
currency: CONST.CURRENCY.USD,
merchant: 'Test Merchant',
created: format(new Date(), CONST.DATE.FNS_FORMAT_STRING),
};

// The IOU action links the expense to its transaction thread, which is where moved messages land.
const iouAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = {
...createRandomReportAction(1),
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
reportID: SOURCE_REPORT_ID,
actorAccountID: RORY_ACCOUNT_ID,
childReportID: TRANSACTION_THREAD_REPORT_ID,
originalMessage: {
IOUTransactionID: TRANSACTION_ID,
IOUReportID: SOURCE_REPORT_ID,
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
amount: -1000,
currency: CONST.CURRENCY.USD,
},
message: undefined,
previousMessage: undefined,
};

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${SOURCE_REPORT_ID}`, sourceReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${TRANSACTION_THREAD_REPORT_ID}`, {
...createRandomReport(2),
reportID: TRANSACTION_THREAD_REPORT_ID,
parentReportID: SOURCE_REPORT_ID,
parentReportActionID: iouAction.reportActionID,
});
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SOURCE_REPORT_ID}`, {[iouAction.reportActionID]: iouAction});
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, transaction);
await waitForBatchedUpdates();

changeTransactionsReport({
transactionIDs: [TRANSACTION_ID],
isASAPSubmitBetaEnabled: false,
accountID: RORY_ACCOUNT_ID,
email: RORY_EMAIL,
newReport,
policy,
allTransactions: {[`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`]: transaction},
policyTagList: undefined,
transactionViolations: {},
allReports: undefined,
});
await waitForBatchedUpdates();

const threadActions = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${TRANSACTION_THREAD_REPORT_ID}`);
return Object.values(threadActions ?? {});
}

const draftReportStatus = {stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN};
const submittedReportStatus = {stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED};

const destinationReport = {
reportID: DESTINATION_REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: RORY_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
} as Report;

it('should not create a MOVED_TRANSACTION action when the expense is moved out of a draft report', async () => {
// Given an expense in a draft (open) report, when it is moved to another report
const actions = await moveExpenseOutOf(draftReportStatus, destinationReport);

// Then no moved system message is created, because moves between drafts aren't part of the audit trail
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION)).toHaveLength(0);
});

it('should create a MOVED_TRANSACTION action when the expense is moved out of a submitted report', async () => {
// Given an expense in a submitted report, when it is moved to another report
const actions = await moveExpenseOutOf(submittedReportStatus, destinationReport);

// Then the moved system message is still created, since the audit trail starts once a report is submitted
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION)).toHaveLength(1);
});

it('should create an UNREPORTED_TRANSACTION action when the expense is moved to personal space from a draft report', async () => {
// Given an expense in a draft report, when it is moved to personal space (no destination report)
const actions = await moveExpenseOutOf(draftReportStatus, undefined);

// Then the moved message is still created, because the expense leaves the report entirely
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION)).toHaveLength(1);
});
});

describe('updateSplitTransactionsFromSplitExpensesFlow', () => {
it("should update split transaction's description correctly ", async () => {
const amount = 10000;
Expand Down
Loading