Skip to content
Merged
34 changes: 32 additions & 2 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@
import {getParsedMessageWithShortMentions} from '@libs/ParsingUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PhoneNumber from '@libs/PhoneNumber';
import {getDefaultApprover, getMemberAccountIDsForWorkspace, isInstantSubmitEnabled, isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyMember, isSubmitAndClose} from '@libs/PolicyUtils';
import {
getDefaultApprover,
getMemberAccountIDsForWorkspace,
getSubmitToAccountID,
isInstantSubmitEnabled,
isPolicyAdmin as isPolicyAdminPolicyUtils,
isPolicyMember,
isSubmitAndClose,
} from '@libs/PolicyUtils';
import processReportIDDeeplink from '@libs/processReportIDDeeplink';
import Pusher from '@libs/Pusher';
import type {UserIsLeavingRoomEvent, UserIsTypingEvent} from '@libs/Pusher/types';
Expand Down Expand Up @@ -373,7 +381,7 @@
// map of reportID to all reportActions for that report
const allReportActions: OnyxCollection<ReportActions> = {};

Onyx.connect({

Check warning on line 384 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
Expand All @@ -385,7 +393,7 @@
});

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 396 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -394,7 +402,7 @@
});

let allPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
Onyx.connect({

Check warning on line 405 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand All @@ -409,7 +417,7 @@
});

let onboarding: OnyxEntry<Onboarding>;
Onyx.connect({

Check warning on line 420 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ONBOARDING,
callback: (val) => {
if (Array.isArray(val)) {
Expand Down Expand Up @@ -6554,10 +6562,30 @@
const reportIDToThreadsReportIDsMap = buildReportIDToThreadsReportIDsMap();
updatePolicyIdForReportAndThreads(reportID, policy.id, reportIDToThreadsReportIDsMap, optimisticData, failureData);

const managerLogin = PersonalDetailsUtils.getLoginByAccountID(report.managerID ?? CONST.DEFAULT_NUMBER_ID);
const newManagerAccountID = getSubmitToAccountID(policy, report);
const shouldResetApprovalChain = isProcessingReport(report) && newManagerAccountID !== report.managerID && isPolicyMember(policy, managerLogin);
if (shouldResetApprovalChain) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
managerID: newManagerAccountID,
},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
managerID: report.managerID,
},
});
}

// We reopen and reassign the report if the report is open/submitted and the manager is not a member of the new policy. This is to prevent the old manager from seeing a report that they can't action on.
let newStatusNum = report?.statusNum;
const isOpenOrSubmitted = isOpenExpenseReport(report) || isProcessingReport(report);
const managerLogin = PersonalDetailsUtils.getLoginByAccountID(report.managerID ?? CONST.DEFAULT_NUMBER_ID);
if (isOpenOrSubmitted && managerLogin && !isPolicyMember(policy, managerLogin)) {
newStatusNum = CONST.REPORT.STATUS_NUM.OPEN;
optimisticData.push({
Expand Down Expand Up @@ -6616,6 +6644,7 @@
currentUserEmailParam: email,
hasViolations: hasViolationsParam,
isASAPSubmitBetaEnabled,
bypassNextApproverID: shouldResetApprovalChain ? newManagerAccountID : undefined,
});
const optimisticNextStep = buildOptimisticNextStep({
report: {...report, policyID: policy.id},
Expand All @@ -6625,6 +6654,7 @@
currentUserEmailParam: email,
hasViolations: hasViolationsParam,
isASAPSubmitBetaEnabled,
bypassNextApproverID: shouldResetApprovalChain ? newManagerAccountID : undefined,
});

optimisticData.push({
Expand Down
Loading