Skip to content

Commit

Permalink
Merge pull request #55635 from Expensify/dangrous-updatenextstepcopy
Browse files Browse the repository at this point in the history
Update optimistic next steps for manual and empty reports
  • Loading branch information
deetergp authored Jan 22, 2025
2 parents 76d187f + 56c5096 commit c8b112e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
45 changes: 34 additions & 11 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type DeepValueOf from '@src/types/utils/DeepValueOf';
import {getNextApproverAccountID} from './actions/IOU';
import DateUtils from './DateUtils';
import EmailUtils from './EmailUtils';
import * as PolicyUtils from './PolicyUtils';
import * as ReportUtils from './ReportUtils';
import {getCorrectedAutoReportingFrequency, getReimburserAccountID} from './PolicyUtils';
import {getDisplayNameForParticipant, getPersonalDetailsForAccountID, isExpenseReport, isInvoiceReport, isPayer} from './ReportUtils';

let currentUserAccountID = -1;
let currentUserEmail = '';
Expand All @@ -23,7 +23,7 @@ Onyx.connect({
return;
}

currentUserAccountID = value?.accountID ?? -1;
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
currentUserEmail = value?.email ?? '';
},
});
Expand Down Expand Up @@ -69,7 +69,7 @@ function parseMessage(messages: Message[] | undefined) {
function getNextApproverDisplayName(report: OnyxEntry<Report>) {
const approverAccountID = getNextApproverAccountID(report);

return ReportUtils.getDisplayNameForParticipant(approverAccountID) ?? ReportUtils.getPersonalDetailsForAccountID(approverAccountID).login;
return getDisplayNameForParticipant(approverAccountID) ?? getPersonalDetailsForAccountID(approverAccountID).login;
}

/**
Expand All @@ -81,18 +81,18 @@ function getNextApproverDisplayName(report: OnyxEntry<Report>) {
* @returns nextStep
*/
function buildNextStep(report: OnyxEntry<Report>, predictedNextStatus: ValueOf<typeof CONST.REPORT.STATUS_NUM>): ReportNextStep | null {
if (!ReportUtils.isExpenseReport(report)) {
if (!isExpenseReport(report)) {
return null;
}

const {policyID = '', ownerAccountID = -1} = report ?? {};
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as Policy);
const {harvesting, autoReportingOffset} = policy;
const autoReportingFrequency = PolicyUtils.getCorrectedAutoReportingFrequency(policy);
const ownerDisplayName = ReportUtils.getDisplayNameForParticipant(ownerAccountID);
const autoReportingFrequency = getCorrectedAutoReportingFrequency(policy);
const ownerDisplayName = getDisplayNameForParticipant(ownerAccountID);
const nextApproverDisplayName = getNextApproverDisplayName(report);

const reimburserAccountID = PolicyUtils.getReimburserAccountID(policy);
const reimburserAccountID = getReimburserAccountID(policy);
const hasValidAccount = !!policy?.achAccount?.accountNumber;
const type: ReportNextStep['type'] = 'neutral';
let optimisticNextStep: ReportNextStep | null;
Expand Down Expand Up @@ -194,6 +194,29 @@ function buildNextStep(report: OnyxEntry<Report>, predictedNextStatus: ValueOf<t
});
}

// Manual submission
if (report?.total !== 0 && !harvesting?.enabled && autoReportingFrequency === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL) {
optimisticNextStep.message = [
{
text: 'Waiting for ',
},
{
text: `${ownerDisplayName}`,
type: 'strong',
clickToCopyText: ownerAccountID === currentUserAccountID ? currentUserEmail : '',
},
{
text: ' to ',
},
{
text: 'submit',
},
{
text: ' %expenses.',
},
];
}

break;

// Generates an optimistic nextStep once a report has been submitted
Expand Down Expand Up @@ -240,8 +263,8 @@ function buildNextStep(report: OnyxEntry<Report>, predictedNextStatus: ValueOf<t
// Generates an optimistic nextStep once a report has been approved
case CONST.REPORT.STATUS_NUM.APPROVED:
if (
ReportUtils.isInvoiceReport(report) ||
!ReportUtils.isPayer(
isInvoiceReport(report) ||
!isPayer(
{
accountID: currentUserAccountID,
email: currentUserEmail,
Expand All @@ -266,7 +289,7 @@ function buildNextStep(report: OnyxEntry<Report>, predictedNextStatus: ValueOf<t
text: 'an admin',
}
: {
text: ReportUtils.getDisplayNameForParticipant(reimburserAccountID),
text: getDisplayNameForParticipant(reimburserAccountID),
type: 'strong',
},
{
Expand Down
46 changes: 23 additions & 23 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {format, lastDayOfMonth, setDate} from 'date-fns';
import Onyx from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import * as NextStepUtils from '@libs/NextStepUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {buildNextStep} from '@libs/NextStepUtils';
import {buildOptimisticExpenseReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report, ReportNextStep} from '@src/types/onyx';
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('libs/NextStepUtils', () => {
icon: CONST.NEXT_STEP.ICONS.HOURGLASS,
message: [],
};
const report = ReportUtils.buildOptimisticExpenseReport('fake-chat-report-id-1', policyID, 1, -500, CONST.CURRENCY.USD) as Report;
const report = buildOptimisticExpenseReport('fake-chat-report-id-1', policyID, 1, -500, CONST.CURRENCY.USD) as Report;

beforeAll(() => {
const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [policy], (item) => item.id);
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('libs/NextStepUtils', () => {
},
];

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -357,14 +357,14 @@ describe('libs/NextStepUtils', () => {
enabled: true,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

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

test('manual', () => {
// Waiting for userSubmitter to add expense(s).
// Waiting for userSubmitter to submit expense(s).
optimisticNextStep.message = [
{
text: 'Waiting for ',
Expand All @@ -377,7 +377,7 @@ describe('libs/NextStepUtils', () => {
text: ' to ',
},
{
text: 'add',
text: 'submit',
},
{
text: ' %expenses.',
Expand All @@ -390,7 +390,7 @@ describe('libs/NextStepUtils', () => {
enabled: false,
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -421,7 +421,7 @@ describe('libs/NextStepUtils', () => {
},
];

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -453,7 +453,7 @@ describe('libs/NextStepUtils', () => {
accountNumber: '123456789',
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

expect(result).toMatchObject(optimisticNextStep);

Expand Down Expand Up @@ -495,7 +495,7 @@ describe('libs/NextStepUtils', () => {
},
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -532,7 +532,7 @@ describe('libs/NextStepUtils', () => {
},
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand All @@ -549,7 +549,7 @@ describe('libs/NextStepUtils', () => {
return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.CLOSED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.CLOSED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand All @@ -566,7 +566,7 @@ describe('libs/NextStepUtils', () => {
},
];

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand Down Expand Up @@ -597,7 +597,7 @@ describe('libs/NextStepUtils', () => {
report.stateNum = CONST.REPORT.STATE_NUM.APPROVED;
report.statusNum = CONST.REPORT.STATUS_NUM.APPROVED;

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

expect(result).toMatchObject(optimisticNextStep);

Expand Down Expand Up @@ -633,7 +633,7 @@ describe('libs/NextStepUtils', () => {
accountNumber: '123456789',
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

expect(result).toMatchObject(optimisticNextStep);
});
Expand All @@ -648,7 +648,7 @@ describe('libs/NextStepUtils', () => {
},
];

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.REIMBURSED);
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.REIMBURSED);

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

0 comments on commit c8b112e

Please sign in to comment.