Skip to content

Commit 7b004f4

Browse files
authored
Merge pull request Expensify#69788 from Expensify/georgia-moveiou-fix
Call MoveIOUToPolicy even when policy expense chat is not loaded
2 parents 3392aec + 141d3ef commit 7b004f4

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

src/components/KYCWall/BaseKYCWall.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import getClickedTargetLocation from '@libs/getClickedTargetLocation';
1212
import Log from '@libs/Log';
1313
import Navigation from '@libs/Navigation/Navigation';
1414
import {hasExpensifyPaymentMethod} from '@libs/PaymentUtils';
15-
import {getBankAccountRoute, getPolicyExpenseChat, isExpenseReport as isExpenseReportReportUtils, isIOUReport} from '@libs/ReportUtils';
15+
import {getBankAccountRoute, isExpenseReport as isExpenseReportReportUtils, isIOUReport} from '@libs/ReportUtils';
1616
import {kycWallRef} from '@userActions/PaymentMethods';
1717
import {createWorkspaceFromIOUPayment} from '@userActions/Policy/Policy';
1818
import {setKYCWallSource} from '@userActions/Wallet';
@@ -55,7 +55,7 @@ function KYCWall({
5555
const [bankAccountList = getEmptyObject<BankAccountList>()] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
5656
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true});
5757
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, {canBeMissing: true});
58-
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true});
58+
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
5959

6060
const {formatPhoneNumber} = useLocalize();
6161

@@ -102,7 +102,6 @@ function KYCWall({
102102
if (!transferBalanceButtonRef.current) {
103103
return;
104104
}
105-
106105
const buttonPosition = getClickedTargetLocation(transferBalanceButtonRef.current as HTMLDivElement);
107106
const position = getAnchorPosition(buttonPosition);
108107

@@ -121,23 +120,23 @@ function KYCWall({
121120
Navigation.navigate(addDebitCardRoute ?? ROUTES.HOME);
122121
} else if (paymentMethod === CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT || policy) {
123122
if (iouReport && isIOUReport(iouReport)) {
124-
if (policy) {
125-
const policyExpenseChatReportID = getPolicyExpenseChat(iouReport.ownerAccountID, policy.id, allReports)?.reportID;
126-
if (!policyExpenseChatReportID) {
127-
const {policyExpenseChatReportID: newPolicyExpenseChatReportID} = moveIOUReportToPolicyAndInviteSubmitter(iouReport.reportID, policy.id, formatPhoneNumber) ?? {};
128-
savePreferredPaymentMethod(iouReport.policyID, policy.id, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[policy.id]);
129-
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(newPolicyExpenseChatReportID));
123+
const adminPolicy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`];
124+
if (adminPolicy) {
125+
const inviteResult = moveIOUReportToPolicyAndInviteSubmitter(iouReport?.reportID, adminPolicy.id, formatPhoneNumber);
126+
if (inviteResult?.policyExpenseChatReportID) {
127+
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(inviteResult.policyExpenseChatReportID));
130128
} else {
131-
moveIOUReportToPolicy(iouReport.reportID, policy.id, true);
132-
savePreferredPaymentMethod(iouReport.policyID, policy.id, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[policy.id]);
133-
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(policyExpenseChatReportID));
129+
const moveResult = moveIOUReportToPolicy(iouReport?.reportID, adminPolicy.id, true);
130+
savePreferredPaymentMethod(iouReport.policyID, adminPolicy.id, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[adminPolicy.id]);
131+
if (moveResult?.policyExpenseChatReportID && !moveResult.useTemporaryOptimisticExpenseChatReportID) {
132+
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(moveResult.policyExpenseChatReportID));
133+
}
134134
}
135135

136-
if (policy?.achAccount) {
136+
if (adminPolicy?.achAccount) {
137137
return;
138138
}
139-
// Navigate to the bank account set up flow for this specific policy
140-
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute(policy.id));
139+
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute(adminPolicy.id));
141140
return;
142141
}
143142

@@ -158,7 +157,7 @@ function KYCWall({
158157
Navigation.navigate(bankAccountRoute);
159158
}
160159
},
161-
[addBankAccountRoute, addDebitCardRoute, chatReport, iouReport, onSelectPaymentMethod, formatPhoneNumber, lastPaymentMethod, allReports],
160+
[addBankAccountRoute, addDebitCardRoute, chatReport, iouReport, onSelectPaymentMethod, formatPhoneNumber, lastPaymentMethod, policies],
162161
);
163162

164163
/**

src/libs/actions/Report.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5207,7 +5207,11 @@ function deleteAppReport(reportID: string | undefined) {
52075207
* @param policyID - The ID of the policy to move the report to
52085208
* @param isFromSettlementButton - Whether the action is from report preview
52095209
*/
5210-
function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlementButton?: boolean) {
5210+
function moveIOUReportToPolicy(
5211+
reportID: string,
5212+
policyID: string,
5213+
isFromSettlementButton?: boolean,
5214+
): {policyExpenseChatReportID?: string; useTemporaryOptimisticExpenseChatReportID: boolean} | undefined {
52115215
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
52125216
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
52135217
// eslint-disable-next-line deprecation/deprecation
@@ -5229,10 +5233,8 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
52295233
const iouReportID = iouReport.reportID;
52305234
const employeeAccountID = iouReport.ownerAccountID;
52315235
const expenseChatReportId = getPolicyExpenseChat(employeeAccountID, policyID)?.reportID;
5232-
5233-
if (!expenseChatReportId) {
5234-
return;
5235-
}
5236+
const useTemporaryOptimisticExpenseChatReportID = !expenseChatReportId;
5237+
const optimisticExpenseChatReportID = expenseChatReportId ?? generateReportID();
52365238

52375239
const optimisticData: OnyxUpdate[] = [];
52385240

@@ -5300,7 +5302,7 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53005302
});
53015303

53025304
// We need to move the report preview action from the DM to the expense chat.
5303-
const parentReportActions = allReportActions?.[`${iouReport.parentReportID}`];
5305+
const parentReportActions = iouReport?.parentReportID ? allReportActions?.[iouReport.parentReportID] : undefined;
53045306
const parentReportActionID = iouReport.parentReportActionID;
53055307
const reportPreview = iouReport?.parentReportID && parentReportActionID ? parentReportActions?.[parentReportActionID] : undefined;
53065308
const oldChatReportID = iouReport.chatReportID;
@@ -5320,18 +5322,18 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53205322
// Add the reportPreview action to expense chat
53215323
optimisticData.push({
53225324
onyxMethod: Onyx.METHOD.MERGE,
5323-
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
5325+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseChatReportID}`,
53245326
value: {[reportPreview.reportActionID]: {...reportPreview, childReportName: expenseReport.reportName, created: DateUtils.getDBTime()}},
53255327
});
53265328
failureData.push({
53275329
onyxMethod: Onyx.METHOD.MERGE,
5328-
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
5330+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseChatReportID}`,
53295331
value: {[reportPreview.reportActionID]: null},
53305332
});
53315333
}
53325334

53335335
// Create MOVED report action and add it to the expense report which indicates to the user where the report has been moved
5334-
const movedExpenseReportAction = buildOptimisticMovedReportAction(iouReport.policyID, policyID, expenseChatReportId, iouReportID, policyName, true);
5336+
const movedExpenseReportAction = buildOptimisticMovedReportAction(iouReport.policyID, policyID, expenseChatReportId ?? '', iouReportID, policyName, true);
53355337
optimisticData.push({
53365338
onyxMethod: Onyx.METHOD.MERGE,
53375339
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
@@ -5372,7 +5374,7 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53725374
});
53735375

53745376
// Create the MOVED report action and add it to the DM chat which indicates to the user where the report has been moved
5375-
const movedReportAction = buildOptimisticMovedReportAction(iouReport.policyID, policyID, expenseChatReportId, iouReportID, policyName);
5377+
const movedReportAction = buildOptimisticMovedReportAction(iouReport.policyID, policyID, optimisticExpenseChatReportID, iouReportID, policyName);
53765378
optimisticData.push({
53775379
onyxMethod: Onyx.METHOD.MERGE,
53785380
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
@@ -5389,10 +5391,11 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53895391
policyID,
53905392
changePolicyReportActionID: movedExpenseReportAction.reportActionID,
53915393
dmMovedReportActionID: movedReportAction.reportActionID,
5392-
optimisticReportID: expenseChatReportId,
5394+
optimisticReportID: optimisticExpenseChatReportID,
53935395
};
53945396

53955397
API.write(WRITE_COMMANDS.MOVE_IOU_REPORT_TO_EXISTING_POLICY, parameters, {optimisticData, successData, failureData});
5398+
return {policyExpenseChatReportID: optimisticExpenseChatReportID, useTemporaryOptimisticExpenseChatReportID};
53965399
}
53975400

53985401
/**

src/pages/ReportChangeWorkspacePage.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
3434
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
3535
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
3636

37-
const [policies, fetchStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
37+
const [policies, fetchStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
3838
const [reportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`, {canBeMissing: true});
3939
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
4040
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: false});
@@ -49,12 +49,11 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
4949
}
5050
const {backTo} = route.params;
5151
Navigation.goBack(backTo);
52-
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
53-
// eslint-disable-next-line deprecation/deprecation
54-
if (isIOUReport(reportID) && isPolicyAdmin(policy) && report.ownerAccountID && !isPolicyMember(policy, getLoginByAccountID(report.ownerAccountID))) {
55-
moveIOUReportToPolicyAndInviteSubmitter(reportID, policyID, formatPhoneNumber);
56-
} else if (isIOUReport(reportID) && isPolicyMember(policy, session?.email)) {
57-
moveIOUReportToPolicy(reportID, policyID);
52+
if (isIOUReport(reportID)) {
53+
const invite = moveIOUReportToPolicyAndInviteSubmitter(reportID, policy?.id, formatPhoneNumber);
54+
if (!invite?.policyExpenseChatReportID) {
55+
moveIOUReportToPolicy(reportID, policy?.id);
56+
}
5857
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
5958
// eslint-disable-next-line deprecation/deprecation
6059
} else if (isExpenseReport(report) && isPolicyAdmin(policy) && report.ownerAccountID && !isPolicyMember(policy, getLoginByAccountID(report.ownerAccountID))) {
@@ -64,7 +63,7 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
6463
changeReportPolicy(report, policy, reportNextStep, isReportLastVisibleArchived);
6564
}
6665
},
67-
[session?.email, route.params, report, reportID, reportNextStep, policies, formatPhoneNumber, isReportLastVisibleArchived],
66+
[route.params, report, reportID, reportNextStep, policies, formatPhoneNumber, isReportLastVisibleArchived],
6867
);
6968

7069
const {sections, shouldShowNoResultsFoundMessage, shouldShowSearchInput} = useWorkspaceList({

0 commit comments

Comments
 (0)