Skip to content

Commit 6d97fae

Browse files
Merge pull request #56202 from linhvovan29546/fix/55864-move-expense-to-selfDM-message
fix: moving expense to selfDM message not correct
2 parents 4bfb82c + cd76242 commit 6d97fae

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

src/languages/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ const translations = {
996996
threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Tracking ${formattedAmount} ${comment ? `for ${comment}` : ''}`,
997997
threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} sent${comment ? ` for ${comment}` : ''}`,
998998
movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `moved expense from self DM to ${workspaceName ?? `chat with ${reportName}`}`,
999+
movedToSelfDM: 'moved expense to self DM',
9991000
tagSelection: 'Select a tag to better organize your spend.',
10001001
categorySelection: 'Select a category to better organize your spend.',
10011002
error: {

src/languages/es.ts

+1
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ const translations = {
994994
threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Seguimiento ${formattedAmount} ${comment ? `para ${comment}` : ''}`,
995995
threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} enviado${comment ? ` para ${comment}` : ''}`,
996996
movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `movió el gasto desde su propio mensaje directo a ${workspaceName ?? `un chat con ${reportName}`}`,
997+
movedToSelfDM: 'movió el gasto a su propio mensaje directo',
997998
tagSelection: 'Selecciona una etiqueta para organizar mejor tus gastos.',
998999
categorySelection: 'Selecciona una categoría para organizar mejor tus gastos.',
9991000
error: {

src/libs/ModifiedExpenseMessage.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Log from './Log';
1212
import {getCleanedTagName, getSortedTagKeys} from './PolicyUtils';
1313
import {getOriginalMessage, isModifiedExpenseAction} from './ReportActionsUtils';
1414
// eslint-disable-next-line import/no-cycle
15-
import {buildReportNameFromParticipantNames, getPolicyExpenseChatName, getPolicyName, getRootParentReport, isPolicyExpenseChat} from './ReportUtils';
15+
import {buildReportNameFromParticipantNames, getPolicyExpenseChatName, getPolicyName, getRootParentReport, isPolicyExpenseChat, isSelfDM} from './ReportUtils';
1616
import {getTagArrayFromName} from './TransactionUtils';
1717

1818
let allPolicyTags: OnyxCollection<PolicyTagLists> = {};
@@ -139,11 +139,19 @@ function getForDistanceRequest(newMerchant: string, oldMerchant: string, newAmou
139139
function getForExpenseMovedFromSelfDM(destinationReportID: string) {
140140
const destinationReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`];
141141
const rootParentReport = getRootParentReport(destinationReport);
142-
143-
// The "Move report" flow only supports moving expenses to a policy expense chat or a 1:1 DM.
142+
// In OldDot, expenses could be moved to a self-DM. Return the corresponding message for this case.
143+
if (isSelfDM(rootParentReport)) {
144+
return translateLocal('iou.movedToSelfDM');
145+
}
146+
// In NewDot, the "Move report" flow only supports moving expenses from self-DM to:
147+
// - A policy expense chat
148+
// - A 1:1 DM
144149
const reportName = isPolicyExpenseChat(rootParentReport) ? getPolicyExpenseChatName(rootParentReport) : buildReportNameFromParticipantNames({report: rootParentReport});
145150
const policyName = getPolicyName(rootParentReport, true);
146-
151+
// If we can't determine either the report name or policy name, return the default message
152+
if (isEmpty(policyName) && !reportName) {
153+
return translateLocal('iou.changedTheExpense');
154+
}
147155
return translateLocal('iou.movedFromSelfDM', {
148156
reportName,
149157
workspaceName: !isEmpty(policyName) ? policyName : undefined,

tests/unit/ModifiedExpenseMessageTest.ts

+108
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import Onyx from 'react-native-onyx';
12
import ModifiedExpenseMessage from '@libs/ModifiedExpenseMessage';
23
import CONST from '@src/CONST';
4+
import {translate} from '@src/libs/Localize';
5+
import ONYXKEYS from '@src/ONYXKEYS';
36
import createRandomReportAction from '../utils/collections/reportActions';
47
import createRandomReport from '../utils/collections/reports';
8+
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
59

610
describe('ModifiedExpenseMessage', () => {
711
describe('getForAction', () => {
@@ -396,5 +400,109 @@ describe('ModifiedExpenseMessage', () => {
396400
expect(result).toEqual(expectedResult);
397401
});
398402
});
403+
404+
describe('when moving an expense', () => {
405+
beforeEach(() => Onyx.clear());
406+
it('return the message "moved expense to self DM" when moving an expense from an expense chat or 1:1 DM to selfDM', async () => {
407+
// Given the selfDM report and report action
408+
const selfDMReport = {
409+
...report,
410+
chatType: CONST.REPORT.CHAT_TYPE.SELF_DM,
411+
};
412+
const reportAction = {
413+
...createRandomReportAction(1),
414+
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
415+
originalMessage: {
416+
movedToReportID: selfDMReport.reportID,
417+
},
418+
};
419+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`]: selfDMReport});
420+
await waitForBatchedUpdates();
421+
422+
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedToSelfDM');
423+
424+
// When the expense is moved from an expense chat or 1:1 DM to selfDM
425+
const result = ModifiedExpenseMessage.getForReportAction(selfDMReport.reportID, reportAction);
426+
// Then it should return the 'moved expense to self DM' message
427+
expect(result).toEqual(expectedResult);
428+
});
429+
430+
it('return the message "changed the expense" when reportName and workspace name are empty', async () => {
431+
// Given the policyExpenseChat with reportName is empty and report action
432+
const policyExpenseChat = {
433+
...report,
434+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
435+
reportName: '',
436+
isOwnPolicyExpenseChat: false,
437+
};
438+
const reportAction = {
439+
...createRandomReportAction(1),
440+
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
441+
originalMessage: {
442+
movedToReportID: policyExpenseChat.reportID,
443+
},
444+
};
445+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat});
446+
await waitForBatchedUpdates();
447+
448+
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.changedTheExpense');
449+
450+
// When the expense is moved to an expense chat with reportName empty
451+
const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction);
452+
// Then it should return the 'changed the expense' message
453+
expect(result).toEqual(expectedResult);
454+
});
455+
456+
it('return the message "moved expense from self DM to policyName" when both reportName and policyName are present', async () => {
457+
// Given the policyExpenseChat with both reportName and policyName are present and report action
458+
const policyExpenseChat = {
459+
...report,
460+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
461+
isOwnPolicyExpenseChat: false,
462+
policyName: 'fake policyName',
463+
};
464+
const reportAction = {
465+
...createRandomReportAction(1),
466+
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
467+
originalMessage: {
468+
movedToReportID: policyExpenseChat.reportID,
469+
},
470+
};
471+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat});
472+
await waitForBatchedUpdates();
473+
474+
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromSelfDM', {reportName: policyExpenseChat.reportName, workspaceName: policyExpenseChat.policyName});
475+
476+
// When the expense is moved to an expense chat with both reportName and policyName are present
477+
const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction);
478+
// Then it should return the correct text message
479+
expect(result).toEqual(expectedResult);
480+
});
481+
482+
it('return the message "moved expense from self DM to chat with reportName" when only reportName is present', async () => {
483+
// Given the policyExpenseChat with only reportName is present and report action
484+
const policyExpenseChat = {
485+
...report,
486+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
487+
isOwnPolicyExpenseChat: false,
488+
};
489+
const reportAction = {
490+
...createRandomReportAction(1),
491+
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
492+
originalMessage: {
493+
movedToReportID: policyExpenseChat.reportID,
494+
},
495+
};
496+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat});
497+
await waitForBatchedUpdates();
498+
499+
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromSelfDM', {reportName: policyExpenseChat.reportName});
500+
501+
// When the expense is moved to an expense chat with only reportName is present
502+
const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction);
503+
// Then it should return the correct text message
504+
expect(result).toEqual(expectedResult);
505+
});
506+
});
399507
});
400508
});

0 commit comments

Comments
 (0)