Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix submit and close next steps for optimistically created policy #39563

Merged
merged 14 commits into from
Apr 8, 2024
Merged
18 changes: 16 additions & 2 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function buildNextStep(

const {policyID = '', ownerAccountID = -1, managerID = -1} = report;
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as Policy);
const {submitsTo, harvesting, isPreventSelfApprovalEnabled, preventSelfApproval, autoReportingFrequency, autoReportingOffset} = policy;
const {submitsTo, harvesting, preventSelfApproval, autoReportingFrequency, autoReportingOffset} = policy;
const isOwner = currentUserAccountID === ownerAccountID;
const isManager = currentUserAccountID === managerID;
const isSelfApproval = currentUserAccountID === submitsTo;
Expand Down Expand Up @@ -172,7 +172,7 @@ function buildNextStep(
}

// Prevented self submitting
if ((isPreventSelfApprovalEnabled ?? preventSelfApproval) && isSelfApproval) {
if (preventSelfApproval && isSelfApproval) {
optimisticNextStep.message = [
{
text: "Oops! Looks like you're submitting to ",
Expand Down Expand Up @@ -255,6 +255,20 @@ function buildNextStep(
break;
}

// Generates an optimistic nextStep once a report has been closed for example in the case of Submit and Close approval flow
case CONST.REPORT.STATUS_NUM.CLOSED:
optimisticNextStep = {
type,
title: 'Finished!',
message: [
{
text: 'No further action required!',
},
],
};

break;

// Generates an optimistic nextStep once a report has been approved
case CONST.REPORT.STATUS_NUM.APPROVED:
// Self review
Expand Down
39 changes: 19 additions & 20 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4782,8 +4782,8 @@ function submitReport(expenseReport: OnyxTypes.Report) {
const parentReport = ReportUtils.getReport(expenseReport.parentReportID);
const policy = getPolicy(expenseReport.policyID);
const isCurrentUserManager = currentUserPersonalDetails.accountID === expenseReport.managerID;
const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, CONST.REPORT.STATUS_NUM.SUBMITTED);
const isSubmitAndClosePolicy = PolicyUtils.isSubmitAndClose(policy);
const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, isSubmitAndClosePolicy ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.SUBMITTED);

const optimisticData: OnyxUpdate[] = !isSubmitAndClosePolicy
? [
Expand All @@ -4808,11 +4808,6 @@ function submitReport(expenseReport: OnyxTypes.Report) {
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,
value: optimisticNextStep,
},
]
: [
{
Expand All @@ -4826,6 +4821,12 @@ function submitReport(expenseReport: OnyxTypes.Report) {
},
];

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,
value: optimisticNextStep,
});

if (parentReport?.reportID) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -4861,24 +4862,22 @@ function submitReport(expenseReport: OnyxTypes.Report) {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,
value: currentNextStep,
},
];
if (!isSubmitAndClosePolicy) {
failureData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`,
value: {
[optimisticSubmittedReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('iou.error.other'),
},
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`,
value: {
[optimisticSubmittedReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('iou.error.other'),
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,
value: currentNextStep,
},
);
});
}

if (parentReport?.reportID) {
Expand Down
15 changes: 15 additions & 0 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,11 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
customUnits,
makeMeAdmin,
autoReporting: true,
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
harvesting: {
enabled: true,
},
},
},
{
Expand Down Expand Up @@ -2008,6 +2013,11 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName
isPolicyExpenseChatEnabled: true,
outputCurrency,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
autoReporting: true,
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
harvesting: {
enabled: true,
},
customUnits,
areCategoriesEnabled: true,
areTagsEnabled: false,
Expand Down Expand Up @@ -2496,6 +2506,11 @@ function createWorkspaceFromIOUPayment(iouReport: Report | EmptyObject): string
// Setting the currency to USD as we can only add the VBBA for this policy currency right now
outputCurrency: CONST.CURRENCY.USD,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
autoReporting: true,
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
harvesting: {
enabled: true,
},
customUnits,
areCategoriesEnabled: true,
areTagsEnabled: false,
Expand Down
3 changes: 0 additions & 3 deletions src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback<
enabled: boolean;
};

/** @deprecated Whether the scheduled submit is enabled */
isPreventSelfApprovalEnabled?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer being sent from backend so we can remove it


/** Whether the self approval or submitting is enabled */
preventSelfApproval?: boolean;

Expand Down
26 changes: 18 additions & 8 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,24 @@ describe('libs/NextStepUtils', () => {

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

test('submit and close approval mode', () => {
report.ownerAccountID = strangeAccountID;
optimisticNextStep.title = 'Finished!';
optimisticNextStep.message = [
{
text: 'No further action required!',
},
];

return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.CLOSED);

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

describe('it generates an optimistic nextStep once a report has been approved', () => {
Expand Down Expand Up @@ -553,13 +571,5 @@ describe('libs/NextStepUtils', () => {
expect(result).toMatchObject(optimisticNextStep);
});
});

describe('it generates a nullable optimistic nextStep', () => {
test('closed status', () => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.CLOSED);

expect(result).toBeNull();
});
});
Comment on lines -556 to -563
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this usecase is only in the Cancel payment flow and even in that one I think its fair so show the Finished no further action required message. So its safe to remove

});
});
Loading