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
20 changes: 7 additions & 13 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {format, lastDayOfMonth, setDate} from 'date-fns';
import {format, setDate} from 'date-fns';
import {Str} from 'expensify-common';
import Onyx from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
Expand All @@ -9,7 +9,6 @@ import type {Policy, Report, ReportNextStep, TransactionViolations} from '@src/t
import type {Message} from '@src/types/onyx/ReportNextStep';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import {getNextApproverAccountID} from './actions/IOU';
import DateUtils from './DateUtils';
import EmailUtils from './EmailUtils';
import {getLoginsByAccountIDs, getPersonalDetailsByIDs} from './PersonalDetailsUtils';
import {getCorrectedAutoReportingFrequency, getReimburserAccountID} from './PolicyUtils';
Expand Down Expand Up @@ -252,27 +251,22 @@ function buildNextStep(report: OnyxEntry<Report>, predictedNextStatus: ValueOf<t

if (autoReportingFrequency) {
const currentDate = new Date();
let autoSubmissionDate: Date | null = null;
let formattedDate = '';
let autoSubmissionDate = '';
let monthlyText = '';

if (autoReportingOffset === CONST.POLICY.AUTO_REPORTING_OFFSET.LAST_DAY_OF_MONTH) {
autoSubmissionDate = lastDayOfMonth(currentDate);
monthlyText = 'on the last day of the month';
} else if (autoReportingOffset === CONST.POLICY.AUTO_REPORTING_OFFSET.LAST_BUSINESS_DAY_OF_MONTH) {
const lastBusinessDayOfMonth = DateUtils.getLastBusinessDayOfMonth(currentDate);
autoSubmissionDate = setDate(currentDate, lastBusinessDayOfMonth);
monthlyText = 'on the last business day of the month';
} else if (autoReportingOffset !== undefined) {
autoSubmissionDate = setDate(currentDate, autoReportingOffset);
}

if (autoSubmissionDate) {
formattedDate = format(autoSubmissionDate, CONST.DATE.ORDINAL_DAY_OF_MONTH);
autoSubmissionDate = format(setDate(currentDate, autoReportingOffset), CONST.DATE.ORDINAL_DAY_OF_MONTH);
}

const harvestingSuffixes: Record<DeepValueOf<typeof CONST.POLICY.AUTO_REPORTING_FREQUENCIES>, string> = {
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.IMMEDIATE]: 'later today',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.WEEKLY]: 'on Sunday',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.SEMI_MONTHLY]: 'on the 1st and 16th of each month',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MONTHLY]: formattedDate ? `on the ${formattedDate} of each month` : '',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MONTHLY]: autoSubmissionDate ? `on the ${autoSubmissionDate} of each month` : monthlyText,
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP]: 'at the end of their trip',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT]: '',
[CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL]: '',
Expand Down
9 changes: 2 additions & 7 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {format, lastDayOfMonth, setDate} from 'date-fns';
import Onyx from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import {buildNextStep} from '@libs/NextStepUtils';
import {buildOptimisticExpenseReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -297,7 +295,7 @@ describe('libs/NextStepUtils', () => {
text: ' %expenses to automatically submit',
},
{
text: ` on the ${format(lastDayOfMonth(new Date()), CONST.DATE.ORDINAL_DAY_OF_MONTH)} of each month`,
text: ` on the last day of the month`,
},
];

Expand All @@ -309,14 +307,11 @@ describe('libs/NextStepUtils', () => {
},
}).then(() => {
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
});

test('monthly on the last business day', () => {
const lastBusinessDayOfMonth = DateUtils.getLastBusinessDayOfMonth(new Date());

// Waiting for userSubmitter's expense(s) to automatically submit on lastBusinessDayOfMonth of each month
optimisticNextStep.message = [
{
Expand All @@ -335,7 +330,7 @@ describe('libs/NextStepUtils', () => {
text: ' %expenses to automatically submit',
},
{
text: ` on the ${format(setDate(new Date(), lastBusinessDayOfMonth), CONST.DATE.ORDINAL_DAY_OF_MONTH)} of each month`,
text: ` on the last business day of the month`,
},
];

Expand Down