diff --git a/src/languages/en.ts b/src/languages/en.ts index 57fd628b43b7c..e0a77403f361d 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -996,6 +996,7 @@ const translations = { threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Tracking ${formattedAmount} ${comment ? `for ${comment}` : ''}`, threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} sent${comment ? ` for ${comment}` : ''}`, movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `moved expense from self DM to ${workspaceName ?? `chat with ${reportName}`}`, + movedToSelfDM: 'moved expense to self DM', tagSelection: 'Select a tag to better organize your spend.', categorySelection: 'Select a category to better organize your spend.', error: { diff --git a/src/languages/es.ts b/src/languages/es.ts index ec9fa4f6a6337..60174deb1a50a 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -994,6 +994,7 @@ const translations = { threadTrackReportName: ({formattedAmount, comment}: ThreadRequestReportNameParams) => `Seguimiento ${formattedAmount} ${comment ? `para ${comment}` : ''}`, threadPaySomeoneReportName: ({formattedAmount, comment}: ThreadSentMoneyReportNameParams) => `${formattedAmount} enviado${comment ? ` para ${comment}` : ''}`, movedFromSelfDM: ({workspaceName, reportName}: MovedFromSelfDMParams) => `movió el gasto desde su propio mensaje directo a ${workspaceName ?? `un chat con ${reportName}`}`, + movedToSelfDM: 'movió el gasto a su propio mensaje directo', tagSelection: 'Selecciona una etiqueta para organizar mejor tus gastos.', categorySelection: 'Selecciona una categoría para organizar mejor tus gastos.', error: { diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index 1d5946dc25b87..9acf9f964403f 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -12,7 +12,7 @@ import Log from './Log'; import {getCleanedTagName, getSortedTagKeys} from './PolicyUtils'; import {getOriginalMessage, isModifiedExpenseAction} from './ReportActionsUtils'; // eslint-disable-next-line import/no-cycle -import {buildReportNameFromParticipantNames, getPolicyExpenseChatName, getPolicyName, getRootParentReport, isPolicyExpenseChat} from './ReportUtils'; +import {buildReportNameFromParticipantNames, getPolicyExpenseChatName, getPolicyName, getRootParentReport, isPolicyExpenseChat, isSelfDM} from './ReportUtils'; import {getTagArrayFromName} from './TransactionUtils'; let allPolicyTags: OnyxCollection = {}; @@ -139,11 +139,19 @@ function getForDistanceRequest(newMerchant: string, oldMerchant: string, newAmou function getForExpenseMovedFromSelfDM(destinationReportID: string) { const destinationReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`]; const rootParentReport = getRootParentReport(destinationReport); - - // The "Move report" flow only supports moving expenses to a policy expense chat or a 1:1 DM. + // In OldDot, expenses could be moved to a self-DM. Return the corresponding message for this case. + if (isSelfDM(rootParentReport)) { + return translateLocal('iou.movedToSelfDM'); + } + // In NewDot, the "Move report" flow only supports moving expenses from self-DM to: + // - A policy expense chat + // - A 1:1 DM const reportName = isPolicyExpenseChat(rootParentReport) ? getPolicyExpenseChatName(rootParentReport) : buildReportNameFromParticipantNames({report: rootParentReport}); const policyName = getPolicyName(rootParentReport, true); - + // If we can't determine either the report name or policy name, return the default message + if (isEmpty(policyName) && !reportName) { + return translateLocal('iou.changedTheExpense'); + } return translateLocal('iou.movedFromSelfDM', { reportName, workspaceName: !isEmpty(policyName) ? policyName : undefined, diff --git a/tests/unit/ModifiedExpenseMessageTest.ts b/tests/unit/ModifiedExpenseMessageTest.ts index fbf9b121069b1..b9e1869c72c17 100644 --- a/tests/unit/ModifiedExpenseMessageTest.ts +++ b/tests/unit/ModifiedExpenseMessageTest.ts @@ -1,7 +1,11 @@ +import Onyx from 'react-native-onyx'; import ModifiedExpenseMessage from '@libs/ModifiedExpenseMessage'; import CONST from '@src/CONST'; +import {translate} from '@src/libs/Localize'; +import ONYXKEYS from '@src/ONYXKEYS'; import createRandomReportAction from '../utils/collections/reportActions'; import createRandomReport from '../utils/collections/reports'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; describe('ModifiedExpenseMessage', () => { describe('getForAction', () => { @@ -396,5 +400,109 @@ describe('ModifiedExpenseMessage', () => { expect(result).toEqual(expectedResult); }); }); + + describe('when moving an expense', () => { + beforeEach(() => Onyx.clear()); + it('return the message "moved expense to self DM" when moving an expense from an expense chat or 1:1 DM to selfDM', async () => { + // Given the selfDM report and report action + const selfDMReport = { + ...report, + chatType: CONST.REPORT.CHAT_TYPE.SELF_DM, + }; + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE, + originalMessage: { + movedToReportID: selfDMReport.reportID, + }, + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`]: selfDMReport}); + await waitForBatchedUpdates(); + + const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedToSelfDM'); + + // When the expense is moved from an expense chat or 1:1 DM to selfDM + const result = ModifiedExpenseMessage.getForReportAction(selfDMReport.reportID, reportAction); + // Then it should return the 'moved expense to self DM' message + expect(result).toEqual(expectedResult); + }); + + it('return the message "changed the expense" when reportName and workspace name are empty', async () => { + // Given the policyExpenseChat with reportName is empty and report action + const policyExpenseChat = { + ...report, + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + reportName: '', + isOwnPolicyExpenseChat: false, + }; + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE, + originalMessage: { + movedToReportID: policyExpenseChat.reportID, + }, + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat}); + await waitForBatchedUpdates(); + + const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.changedTheExpense'); + + // When the expense is moved to an expense chat with reportName empty + const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction); + // Then it should return the 'changed the expense' message + expect(result).toEqual(expectedResult); + }); + + it('return the message "moved expense from self DM to policyName" when both reportName and policyName are present', async () => { + // Given the policyExpenseChat with both reportName and policyName are present and report action + const policyExpenseChat = { + ...report, + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + isOwnPolicyExpenseChat: false, + policyName: 'fake policyName', + }; + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE, + originalMessage: { + movedToReportID: policyExpenseChat.reportID, + }, + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat}); + await waitForBatchedUpdates(); + + const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromSelfDM', {reportName: policyExpenseChat.reportName, workspaceName: policyExpenseChat.policyName}); + + // When the expense is moved to an expense chat with both reportName and policyName are present + const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction); + // Then it should return the correct text message + expect(result).toEqual(expectedResult); + }); + + it('return the message "moved expense from self DM to chat with reportName" when only reportName is present', async () => { + // Given the policyExpenseChat with only reportName is present and report action + const policyExpenseChat = { + ...report, + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + isOwnPolicyExpenseChat: false, + }; + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE, + originalMessage: { + movedToReportID: policyExpenseChat.reportID, + }, + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}`, {[`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`]: policyExpenseChat}); + await waitForBatchedUpdates(); + + const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromSelfDM', {reportName: policyExpenseChat.reportName}); + + // When the expense is moved to an expense chat with only reportName is present + const result = ModifiedExpenseMessage.getForReportAction(policyExpenseChat.reportID, reportAction); + // Then it should return the correct text message + expect(result).toEqual(expectedResult); + }); + }); }); });