diff --git a/src/libs/API/parameters/ChangeReportPolicyAndInviteSubmitterParams.ts b/src/libs/API/parameters/ChangeReportPolicyAndInviteSubmitterParams.ts new file mode 100644 index 000000000000..55a546124116 --- /dev/null +++ b/src/libs/API/parameters/ChangeReportPolicyAndInviteSubmitterParams.ts @@ -0,0 +1,9 @@ +type ChangeReportPolicyParams = { + reportID: string; + policyID: string; + reportPreviewReportActionID: string; + changePolicyReportActionID: string; + policyExpenseChatReportID: string; + policyExpenseCreatedReportActionID: string; +}; +export default ChangeReportPolicyParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index 41039b44e225..2f355e1ecf90 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -393,6 +393,7 @@ export type {default as SaveCorpayOnboardingDirectorInformationParams} from './S export type {default as MoveIOUReportToPolicyAndInviteSubmitterParams} from './MoveIOUReportToPolicyAndInviteSubmitterParams'; export type {default as MoveIOUReportToExistingPolicyParams} from './MoveIOUReportToExistingPolicyParams'; export type {default as ChangeReportPolicyParams} from './ChangeReportPolicyParams'; +export type {default as ChangeReportPolicyAndInviteSubmitterParams} from './ChangeReportPolicyAndInviteSubmitterParams'; export type {ChangeTransactionsReportParams, TransactionThreadInfo} from './ChangeTransactionsReportParams'; export type {default as ResetBankAccountSetupParams} from './ResetBankAccountSetupParams'; export type {default as SetPolicyProhibitedExpensesParams} from './SetPolicyProhibitedExpensesParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index b54f5e3cdf85..8f152d8d5699 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -479,6 +479,7 @@ const WRITE_COMMANDS = { ADD_WORK_EMAIL: 'AddWorkEmail', SAVE_CORPAY_ONBOARDING_DIRECTOR_INFORMATION: 'SaveCorpayOnboardingDirectorInformation', CHANGE_REPORT_POLICY: 'ChangeReportPolicy', + CHANGE_REPORT_POLICY_AND_INVITE_SUBMITTER: 'ChangeReportPolicyAndInviteSubmitter', CHANGE_TRANSACTIONS_REPORT: 'ChangeTransactionsReport', RETRACT_REPORT: 'RetractReport', PAY_AND_DOWNGRADE: 'PayAndDowngrade', @@ -985,6 +986,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.MERGE_WITH_VALIDATE_CODE]: Parameters.MergeWithValidateCodeParams; // Change report policy [WRITE_COMMANDS.CHANGE_REPORT_POLICY]: Parameters.ChangeReportPolicyParams; + [WRITE_COMMANDS.CHANGE_REPORT_POLICY_AND_INVITE_SUBMITTER]: Parameters.ChangeReportPolicyAndInviteSubmitterParams; [WRITE_COMMANDS.PAY_AND_DOWNGRADE]: null; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 0f9ea2531ddc..142b6d8fb9bc 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -89,7 +89,7 @@ import {shouldOnboardingRedirectToOldDot} from '@libs/OnboardingUtils'; import Parser from '@libs/Parser'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; -import {getDefaultApprover, getPolicy, isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyMember} from '@libs/PolicyUtils'; +import {getDefaultApprover, getMemberAccountIDsForWorkspace, getPolicy, isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyMember} from '@libs/PolicyUtils'; import processReportIDDeeplink from '@libs/processReportIDDeeplink'; import Pusher from '@libs/Pusher'; import type {UserIsLeavingRoomEvent, UserIsTypingEvent} from '@libs/Pusher/types'; @@ -176,6 +176,7 @@ import type { PersonalDetailsList, Policy, PolicyEmployee, + PolicyEmployeeList, PolicyReportField, QuickAction, RecentlyUsedReportFields, @@ -196,7 +197,7 @@ import {clearByKey} from './CachedPDFPaths'; import {setDownload} from './Download'; import {close} from './Modal'; import navigateFromNotification from './navigateFromNotification'; -import {buildRoomMembersOnyxData} from './Policy/Member'; +import {buildAddMembersToWorkspaceOnyxData, buildRoomMembersOnyxData} from './Policy/Member'; import {createPolicyExpenseChats} from './Policy/Policy'; import { createUpdateCommentMatcher, @@ -5434,33 +5435,28 @@ function updatePolicyIdForReportAndThreads( }); } -/** - * Changes the policy of a report and all its child reports, and moves the report to the new policy's expense chat. - */ -function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: ReportNextStep) { - if (!reportID || !policyID) { - return; - } - const reportToMove = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - if (!reportToMove || reportToMove?.policyID === policyID || !isExpenseReport(reportToMove)) { +function navigateToTrainingModal(dismissedProductTrainingNVP: OnyxEntry, reportID: string) { + if (dismissedProductTrainingNVP?.[CONST.CHANGE_POLICY_TRAINING_MODAL]) { return; } + Navigation.navigate(ROUTES.CHANGE_POLICY_EDUCATIONAL.getRoute(ROUTES.REPORT_WITH_ID.getRoute(reportID))); +} +function buildOptimisticChangePolicyData(report: Report, policyID: string, reportNextStep?: ReportNextStep) { const optimisticData: OnyxUpdate[] = []; const successData: OnyxUpdate[] = []; const failureData: OnyxUpdate[] = []; - // 1. Optimistically set the policyID on the report (and all its threads) - - // Preprocess reports to create a map of parentReportID to child reports list of reportIDs + // 1. Optimistically set the policyID on the report (and all its threads) by: + // 1.1 Preprocess reports to create a map of parentReportID to child reports list of reportIDs + // 1.2 Recursively update the policyID of the report and all its child reports + const reportID = report.reportID; const reportIDToThreadsReportIDsMap = buildReportIDToThreadsReportIDsMap(); - - // Recursively update the policyID of the report and all its child reports updatePolicyIdForReportAndThreads(reportID, policyID, reportIDToThreadsReportIDsMap, optimisticData, failureData); // 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. - const isOpenOrSubmitted = isOpenExpenseReport(reportToMove) || isProcessingReport(reportToMove); - const managerLogin = PersonalDetailsUtils.getLoginByAccountID(reportToMove.managerID ?? CONST.DEFAULT_NUMBER_ID); + const isOpenOrSubmitted = isOpenExpenseReport(report) || isProcessingReport(report); + const managerLogin = PersonalDetailsUtils.getLoginByAccountID(report.managerID ?? CONST.DEFAULT_NUMBER_ID); if (isOpenOrSubmitted && managerLogin && !isPolicyMember(managerLogin, policyID)) { optimisticData.push( { @@ -5469,13 +5465,13 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: value: { stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN, - managerID: getNextApproverAccountID(reportToMove, true), + managerID: getNextApproverAccountID(report, true), }, }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`, - value: buildNextStep(reportToMove, CONST.REPORT.STATUS_NUM.OPEN), + value: buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN), }, ); @@ -5484,9 +5480,9 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - stateNum: reportToMove.stateNum, - statusNum: reportToMove.statusNum, - managerID: reportToMove.managerID, + stateNum: report.stateNum, + statusNum: report.statusNum, + managerID: report.managerID, }, }, { @@ -5497,11 +5493,11 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: ); } - // 2. If the old workspace had a expense chat, mark the report preview action as deleted - if (reportToMove?.parentReportID && reportToMove?.parentReportActionID) { - const workspaceChatReportID = reportToMove.parentReportID; - const reportPreviewActionID = reportToMove.parentReportActionID; - const oldReportPreviewAction = allReportActions?.[workspaceChatReportID]?.[reportPreviewActionID]; + // 2. If this is a thread, we have to mark the parent report preview action as deleted to properly update the UI + if (report.parentReportID && report.parentReportActionID) { + const oldWorkspaceChatReportID = report.parentReportID; + const oldReportPreviewActionID = report.parentReportActionID; + const oldReportPreviewAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldWorkspaceChatReportID}`]?.[oldReportPreviewActionID]; const deletedTime = DateUtils.getDBTime(); const firstMessage = Array.isArray(oldReportPreviewAction?.message) ? oldReportPreviewAction.message.at(0) : null; const updatedReportPreviewAction = { @@ -5527,30 +5523,23 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChatReportID}`, - value: {[reportPreviewActionID]: updatedReportPreviewAction}, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldWorkspaceChatReportID}`, + value: {[oldReportPreviewActionID]: updatedReportPreviewAction}, }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChatReportID}`, - value: { - [reportPreviewActionID]: { - ...oldReportPreviewAction, - originalMessage: { - deleted: null, - }, - }, - }, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldWorkspaceChatReportID}`, + value: {[oldReportPreviewActionID]: oldReportPreviewAction}, }); // Update the expense chat report - const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${workspaceChatReportID}`]; - const lastMessageText = getLastVisibleMessage(workspaceChatReportID, {[reportPreviewActionID]: updatedReportPreviewAction as ReportAction})?.lastMessageText; - const lastVisibleActionCreated = getReportLastMessage(workspaceChatReportID, {[reportPreviewActionID]: updatedReportPreviewAction as ReportAction})?.lastVisibleActionCreated; + const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldWorkspaceChatReportID}`]; + const lastMessageText = getLastVisibleMessage(oldWorkspaceChatReportID, {[oldReportPreviewActionID]: updatedReportPreviewAction as ReportAction})?.lastMessageText; + const lastVisibleActionCreated = getReportLastMessage(oldWorkspaceChatReportID, {[oldReportPreviewActionID]: updatedReportPreviewAction as ReportAction})?.lastVisibleActionCreated; optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${workspaceChatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${oldWorkspaceChatReportID}`, value: { hasOutstandingChildRequest: false, iouReportID: null, @@ -5560,7 +5549,7 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${workspaceChatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${oldWorkspaceChatReportID}`, value: chatReport, }); } @@ -5568,65 +5557,65 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: // 3. Optimistically create a new REPORT_PREVIEW reportAction with the newReportPreviewActionID // and set it as a parent of the moved report const policyExpenseChat = getPolicyExpenseChat(currentUserAccountID, policyID); - const optimisticReportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, reportToMove); + const optimisticReportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, report); - if (policyExpenseChat) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat.reportID}`, - value: {[optimisticReportPreviewAction.reportActionID]: optimisticReportPreviewAction}, - }); - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat.reportID}`, - value: { - [optimisticReportPreviewAction.reportActionID]: { - pendingAction: null, - }, + const newPolicyExpenseChatReportID = policyExpenseChat?.reportID; + + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${newPolicyExpenseChatReportID}`, + value: {[optimisticReportPreviewAction.reportActionID]: optimisticReportPreviewAction}, + }); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${newPolicyExpenseChatReportID}`, + value: { + [optimisticReportPreviewAction.reportActionID]: { + pendingAction: null, }, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat.reportID}`, - value: {[optimisticReportPreviewAction.reportActionID]: null}, - }); + }, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${newPolicyExpenseChatReportID}`, + value: {[optimisticReportPreviewAction.reportActionID]: null}, + }); - // Set the new report preview action as a parent of the moved report, - // and set the parentReportID on the moved report as the expense chat reportID - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: {parentReportActionID: optimisticReportPreviewAction.reportActionID, parentReportID: policyExpenseChat.reportID}, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: {parentReportActionID: reportToMove.parentReportActionID, parentReportID: reportToMove.parentReportID}, - }); + // Set the new report preview action as a parent of the moved report, + // and set the parentReportID on the moved report as the expense chat reportID + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: {parentReportActionID: optimisticReportPreviewAction.reportActionID, parentReportID: newPolicyExpenseChatReportID}, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: {parentReportActionID: report.parentReportActionID, parentReportID: report.parentReportID}, + }); - // Set lastVisibleActionCreated - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`, - value: {lastVisibleActionCreated: optimisticReportPreviewAction?.created}, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat.reportID}`, - value: {lastVisibleActionCreated: policyExpenseChat.lastVisibleActionCreated}, - }); - } + // Set lastVisibleActionCreated + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${newPolicyExpenseChatReportID}`, + value: {lastVisibleActionCreated: optimisticReportPreviewAction?.created}, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${newPolicyExpenseChatReportID}`, + value: {lastVisibleActionCreated: policyExpenseChat?.lastVisibleActionCreated}, + }); // 4. Optimistically create a CHANGE_POLICY reportAction on the report using the reportActionID - const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(reportToMove.policyID, policyID); + const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(report.policyID, policyID); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportToMove.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: {[optimisticMovedReportAction.reportActionID]: optimisticMovedReportAction}, }); successData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportToMove.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: { [optimisticMovedReportAction.reportActionID]: { pendingAction: null, @@ -5636,7 +5625,7 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportToMove.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: { [optimisticMovedReportAction.reportActionID]: { errors: getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'), @@ -5644,20 +5633,83 @@ function changeReportPolicy(reportID: string, policyID: string, reportNextStep?: }, }); - // Call the ChangeReportPolicy API endpoint + return {optimisticData, successData, failureData, optimisticReportPreviewAction, optimisticMovedReportAction}; +} + +/** + * Changes the policy of a report and all its child reports, and moves the report to the new policy's expense chat. + */ +function changeReportPolicy(report: Report, policyID: string, reportNextStep?: ReportNextStep) { + if (!report || !policyID || report.policyID === policyID || !isExpenseReport(report)) { + return; + } + + const {optimisticData, successData, failureData, optimisticReportPreviewAction, optimisticMovedReportAction} = buildOptimisticChangePolicyData(report, policyID, reportNextStep); + const params = { - reportID: reportToMove.reportID, + reportID: report.reportID, policyID, reportPreviewReportActionID: optimisticReportPreviewAction.reportActionID, changePolicyReportActionID: optimisticMovedReportAction.reportActionID, }; API.write(WRITE_COMMANDS.CHANGE_REPORT_POLICY, params, {optimisticData, successData, failureData}); - // 5. If the dismissedProductTraining.changeReportModal is not set, + // If the dismissedProductTraining.changeReportModal is not set, // navigate to CHANGE_POLICY_EDUCATIONAL and a backTo param for the report page. - if (!nvpDismissedProductTraining?.[CONST.CHANGE_POLICY_TRAINING_MODAL]) { - Navigation.navigate(ROUTES.CHANGE_POLICY_EDUCATIONAL.getRoute(ROUTES.REPORT_WITH_ID.getRoute(reportToMove.reportID))); + navigateToTrainingModal(nvpDismissedProductTraining, report.reportID); +} + +/** + * Invites the submitter to the new report policy, changes the policy of a report and all its child reports, and moves the report to the new policy's expense chat + */ +function changeReportPolicyAndInviteSubmitter(report: Report, policyID: string, employeeList: PolicyEmployeeList | undefined) { + if (!report.reportID || !policyID || report.policyID === policyID || !isExpenseReport(report) || !report.ownerAccountID) { + return; } + + const submitterEmail = PersonalDetailsUtils.getLoginByAccountID(report.ownerAccountID); + + if (!submitterEmail) { + return; + } + const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(employeeList, false, false)); + const {optimisticData, successData, failureData, membersChats} = buildAddMembersToWorkspaceOnyxData( + {[submitterEmail]: report.ownerAccountID}, + policyID, + policyMemberAccountIDs, + CONST.POLICY.ROLE.USER, + ); + const optimisticPolicyExpenseChatReportID = membersChats.reportCreationData[submitterEmail].reportID; + const optimisticPolicyExpenseChatCreatedReportActionID = membersChats.reportCreationData[submitterEmail].reportActionID; + + if (!optimisticPolicyExpenseChatReportID || !optimisticPolicyExpenseChatCreatedReportActionID) { + return; + } + + const { + optimisticData: optimisticChangePolicyData, + successData: successChangePolicyData, + failureData: failureChangePolicyData, + optimisticReportPreviewAction, + optimisticMovedReportAction, + } = buildOptimisticChangePolicyData(report, policyID); + optimisticData.push(...optimisticChangePolicyData); + successData.push(...successChangePolicyData); + failureData.push(...failureChangePolicyData); + + const params = { + reportID: report.reportID, + policyID, + reportPreviewReportActionID: optimisticReportPreviewAction.reportActionID, + changePolicyReportActionID: optimisticMovedReportAction.reportActionID, + policyExpenseChatReportID: optimisticPolicyExpenseChatReportID, + policyExpenseCreatedReportActionID: optimisticPolicyExpenseChatCreatedReportActionID, + }; + API.write(WRITE_COMMANDS.CHANGE_REPORT_POLICY_AND_INVITE_SUBMITTER, params, {optimisticData, successData, failureData}); + + // If the dismissedProductTraining.changeReportModal is not set, + // navigate to CHANGE_POLICY_EDUCATIONAL and a backTo param for the report page. + navigateToTrainingModal(nvpDismissedProductTraining, report.reportID); } export type {Video, GuidedSetupData, TaskForParameters, IntroSelected}; @@ -5766,6 +5818,7 @@ export { moveIOUReportToPolicyAndInviteSubmitter, dismissChangePolicyModal, changeReportPolicy, + changeReportPolicyAndInviteSubmitter, removeFailedReport, openUnreportedExpense, }; diff --git a/src/pages/ReportChangeWorkspacePage.tsx b/src/pages/ReportChangeWorkspacePage.tsx index 2541633e9fb5..a0f0071ba313 100644 --- a/src/pages/ReportChangeWorkspacePage.tsx +++ b/src/pages/ReportChangeWorkspacePage.tsx @@ -11,13 +11,13 @@ import useNetwork from '@hooks/useNetwork'; import useThemeStyles from '@hooks/useThemeStyles'; import type {WorkspaceListItem} from '@hooks/useWorkspaceList'; import useWorkspaceList from '@hooks/useWorkspaceList'; -import {changeReportPolicy, moveIOUReportToPolicy, moveIOUReportToPolicyAndInviteSubmitter} from '@libs/actions/Report'; +import {changeReportPolicy, changeReportPolicyAndInviteSubmitter, moveIOUReportToPolicy, moveIOUReportToPolicyAndInviteSubmitter} from '@libs/actions/Report'; import Navigation from '@libs/Navigation/Navigation'; import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; import type {ReportChangeWorkspaceNavigatorParamList} from '@libs/Navigation/types'; import {getLoginByAccountID} from '@libs/PersonalDetailsUtils'; import {getPolicy, isPolicyAdmin, isPolicyMember} from '@libs/PolicyUtils'; -import {isIOUReport, isMoneyRequestReport, isMoneyRequestReportPendingDeletion, isWorkspaceEligibleForReportChange} from '@libs/ReportUtils'; +import {isExpenseReport, isIOUReport, isMoneyRequestReport, isMoneyRequestReportPendingDeletion, isWorkspaceEligibleForReportChange} from '@libs/ReportUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; @@ -52,11 +52,16 @@ function ReportChangeWorkspacePage({report}: ReportChangeWorkspacePageProps) { moveIOUReportToPolicyAndInviteSubmitter(reportID, policyID); } else if (isIOUReport(reportID) && isPolicyMember(session?.email, policyID)) { moveIOUReportToPolicy(reportID, policyID); + // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 + // eslint-disable-next-line deprecation/deprecation + } else if (isExpenseReport(report) && isPolicyAdmin(getPolicy(policyID)) && report.ownerAccountID && !isPolicyMember(getLoginByAccountID(report.ownerAccountID), policyID)) { + const employeeList = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.employeeList; + changeReportPolicyAndInviteSubmitter(report, policyID, employeeList); } else { - changeReportPolicy(reportID, policyID, reportNextStep); + changeReportPolicy(report, policyID, reportNextStep); } }, - [session?.email, report, reportID, reportNextStep], + [session?.email, report, reportID, reportNextStep, policies], ); const {sections, shouldShowNoResultsFoundMessage, shouldShowSearchInput} = useWorkspaceList({