From d0f440af63c1cd08d20d8269e6a5654673d3f38d Mon Sep 17 00:00:00 2001 From: Weslley Alves Date: Wed, 27 Dec 2023 22:35:08 -0300 Subject: [PATCH 1/3] Fix oldCreated date format when the created date was changed --- src/libs/ModifiedExpenseMessage.ts | 38 ++++++++++++++---------- tests/unit/ModifiedExpenseMessageTest.ts | 37 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index c3d9b0a85339..73271ba1c508 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 {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'; @@ -87,6 +87,26 @@ function getForDistanceRequest(newDistance: string, oldDistance: string, newAmou }); } +/** + * Build message fragment for the created property when this value was changed + * @param setFragments + * @param removalFragments + * @param changeFragments + * @param [reportActionOriginalMessage] + */ +function buildMessageFragmentForCreated(setFragments: string[], removalFragments: string[], changeFragments: string[], reportActionOriginalMessage?: ExpenseOriginalMessage) { + const {created, oldCreated} = reportActionOriginalMessage ?? {}; + + if (!oldCreated || !created) { + return; + } + + // Take only the YYYY-MM-DD value as the original date includes timestamp + const formattedOldCreated = DateUtils.formatWithUTCTimeZone(oldCreated, CONST.DATE.FNS_FORMAT_STRING); + + buildMessageFragmentForValue(created, formattedOldCreated, Localize.translateLocal('common.date'), false, setFragments, removalFragments, changeFragments); +} + /** * Get the report action message when expense has been modified. * @@ -143,21 +163,7 @@ 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); - buildMessageFragmentForValue( - reportActionOriginalMessage?.created ?? '', - formattedOldCreated, - Localize.translateLocal('common.date'), - false, - setFragments, - removalFragments, - changeFragments, - ); - } + buildMessageFragmentForCreated(setFragments, removalFragments, changeFragments, reportActionOriginalMessage); if (hasModifiedMerchant) { buildMessageFragmentForValue( 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); + }); + }); }); }); From 3ac64a71be13f7392bad5cd76f87250a82562b49 Mon Sep 17 00:00:00 2001 From: Weslley Alves Date: Wed, 27 Dec 2023 23:59:02 -0300 Subject: [PATCH 2/3] Remove JSDoc comments that are redundant with TypeScript --- src/libs/ModifiedExpenseMessage.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index 73271ba1c508..3951a208925e 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -89,10 +89,6 @@ function getForDistanceRequest(newDistance: string, oldDistance: string, newAmou /** * Build message fragment for the created property when this value was changed - * @param setFragments - * @param removalFragments - * @param changeFragments - * @param [reportActionOriginalMessage] */ function buildMessageFragmentForCreated(setFragments: string[], removalFragments: string[], changeFragments: string[], reportActionOriginalMessage?: ExpenseOriginalMessage) { const {created, oldCreated} = reportActionOriginalMessage ?? {}; From 326f61db384de6570d3a0fe0708acfc878ce1760 Mon Sep 17 00:00:00 2001 From: Weslley Alves Date: Thu, 11 Jan 2024 20:38:07 -0300 Subject: [PATCH 3/3] Remove inner method and put its content in the place where is using the removed method --- src/libs/ModifiedExpenseMessage.ts | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index c38a0deaea40..90d37b2328ce 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -87,22 +87,6 @@ function getForDistanceRequest(newDistance: string, oldDistance: string, newAmou }); } -/** - * Build message fragment for the created property when this value was changed - */ -function buildMessageFragmentForCreated(setFragments: string[], removalFragments: string[], changeFragments: string[], reportActionOriginalMessage?: ExpenseOriginalMessage) { - const {created, oldCreated} = reportActionOriginalMessage ?? {}; - - if (!oldCreated || !created) { - return; - } - - // Take only the YYYY-MM-DD value as the original date includes timestamp - const formattedOldCreated = DateUtils.formatWithUTCTimeZone(oldCreated, CONST.DATE.FNS_FORMAT_STRING); - - buildMessageFragmentForValue(created, formattedOldCreated, Localize.translateLocal('common.date'), false, setFragments, removalFragments, changeFragments); -} - /** * Get the report action message when expense has been modified. * @@ -159,7 +143,19 @@ function getForReportAction(reportAction: ReportAction): string { ); } - buildMessageFragmentForCreated(setFragments, removalFragments, changeFragments, reportActionOriginalMessage); + if (reportActionOriginalMessage?.oldCreated && reportActionOriginalMessage?.created) { + const formattedOldCreated = DateUtils.formatWithUTCTimeZone(reportActionOriginalMessage.oldCreated, CONST.DATE.FNS_FORMAT_STRING); + + buildMessageFragmentForValue( + reportActionOriginalMessage.created, + formattedOldCreated, + Localize.translateLocal('common.date'), + false, + setFragments, + removalFragments, + changeFragments, + ); + } if (hasModifiedMerchant) { buildMessageFragmentForValue(