diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index 61e7ce04ab71..ff5ad9327191 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -1,9 +1,9 @@ -import {format} from 'date-fns'; import Onyx from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {PolicyTags, ReportAction} from '@src/types/onyx'; import * as CurrencyUtils from './CurrencyUtils'; +import DateUtils from './DateUtils'; import * as Localize from './Localize'; import * as PolicyUtils from './PolicyUtils'; import * as ReportUtils from './ReportUtils'; @@ -145,13 +145,11 @@ function getForReportAction(reportAction: ReportAction): string { ); } - const hasModifiedCreated = reportActionOriginalMessage && 'oldCreated' in reportActionOriginalMessage && 'created' in reportActionOriginalMessage; - if (hasModifiedCreated) { - // Take only the YYYY-MM-DD value as the original date includes timestamp - let formattedOldCreated: Date | string = new Date(reportActionOriginalMessage?.oldCreated ? reportActionOriginalMessage.oldCreated : 0); - formattedOldCreated = format(formattedOldCreated, CONST.DATE.FNS_FORMAT_STRING); + if (reportActionOriginalMessage?.oldCreated && reportActionOriginalMessage?.created) { + const formattedOldCreated = DateUtils.formatWithUTCTimeZone(reportActionOriginalMessage.oldCreated, CONST.DATE.FNS_FORMAT_STRING); + buildMessageFragmentForValue( - reportActionOriginalMessage?.created ?? '', + reportActionOriginalMessage.created, formattedOldCreated, Localize.translateLocal('common.date'), false, diff --git a/tests/unit/ModifiedExpenseMessageTest.ts b/tests/unit/ModifiedExpenseMessageTest.ts index 02990aa5c751..aedc02cc628b 100644 --- a/tests/unit/ModifiedExpenseMessageTest.ts +++ b/tests/unit/ModifiedExpenseMessageTest.ts @@ -275,5 +275,42 @@ describe('ModifiedExpenseMessage', () => { expect(result).toEqual(expectedResult); }); }); + + describe('when the created date is changed', () => { + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE, + originalMessage: { + created: '2023-12-27', + oldCreated: '2023-12-26', + }, + }; + + it('returns the correct text message', () => { + const expectedResult = 'changed the date to 2023-12-27 (previously 2023-12-26).'; + + const result = ModifiedExpenseMessage.getForReportAction(reportAction); + + expect(result).toEqual(expectedResult); + }); + }); + + describe('when the created date was not changed', () => { + const reportAction = { + ...createRandomReportAction(1), + actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE, + originalMessage: { + created: '2023-12-27', + }, + }; + + it('returns the correct text message', () => { + const expectedResult = 'changed the request'; + + const result = ModifiedExpenseMessage.getForReportAction(reportAction); + + expect(result).toEqual(expectedResult); + }); + }); }); });