From ae33908060085d29e0ae3572b6bce00fbfa54b61 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Thu, 2 Nov 2023 23:31:23 +0000 Subject: [PATCH] Flow clean up --- src/CONST.ts | 1 + src/components/KYCWall/BaseKYCWall.js | 2 +- src/libs/ReportUtils.js | 45 +++++++++++++++++++++++ src/libs/actions/Policy.js | 41 ++++++++++++++------- src/pages/home/report/ReportActionItem.js | 2 +- 5 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index cb12a2a5f746..08a4252238d6 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -503,6 +503,7 @@ const CONST = { CREATED: 'CREATED', IOU: 'IOU', MODIFIEDEXPENSE: 'MODIFIEDEXPENSE', + MOVED: 'MOVED', REIMBURSEMENTQUEUED: 'REIMBURSEMENTQUEUED', RENAMED: 'RENAMED', REPORTPREVIEW: 'REPORTPREVIEW', diff --git a/src/components/KYCWall/BaseKYCWall.js b/src/components/KYCWall/BaseKYCWall.js index 92df53353cf6..46479d554bdf 100644 --- a/src/components/KYCWall/BaseKYCWall.js +++ b/src/components/KYCWall/BaseKYCWall.js @@ -99,7 +99,7 @@ class KYCWall extends React.Component { } else if (paymentMethod === CONST.PAYMENT_METHODS.DEBIT_CARD) { Navigation.navigate(this.props.addDebitCardRoute); } else if (paymentMethod === CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT) { - Policy.startCollectFlow(this.props.iouReport); + Policy.createWorkspaceFromIOUPayment(this.props.iouReport); } } diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 10630d7fb122..d3b16a73f16f 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2612,6 +2612,7 @@ function buildOptimisticIOUReportAction( whisperedToAccountIDs: _.contains([CONST.IOU.RECEIPT_STATE.SCANREADY, CONST.IOU.RECEIPT_STATE.SCANNING], receipt.state) ? [currentUserAccountID] : [], }; } + /** * Builds an optimistic APPROVED report action with a randomly generated reportActionID. * @@ -2650,6 +2651,49 @@ function buildOptimisticApprovedReportAction(amount, currency, expenseReportID) }; } +/** + * Builds an optimistic MOVED report action with a randomly generated reportActionID. + * This action is used when we move reports across workspaces. + * + * @param {String} fromPolicyID + * @param {String} toPolicyID + * @param {Number} newParentReportID + * @param {Number} movedReportID An ID of the report we are moveing across workspaces + * + * @returns {Object} + */ +function buildOptimisticMovedReportAction(fromPolicyID, toPolicyID, newParentReportID, movedReportID) { + const originalMessage = { + fromPolicyID, + toPolicyID, + newParentReportID, + movedReportID, + }; + + const policyName = getPolicyName(allReports[`${ONYXKEYS.COLLECTION.REPORT}${newParentReportID}`]); + + return { + actionName: CONST.REPORT.ACTIONS.TYPE.MOVED, + actorAccountID: currentUserAccountID, + automatic: false, + avatar: lodashGet(currentUserPersonalDetails, 'avatar', UserUtils.getDefaultAvatarURL(currentUserAccountID)), + isAttachment: false, + originalMessage, + message: "moved report", + person: [ + { + style: 'strong', + text: lodashGet(currentUserPersonalDetails, 'displayName', currentUserEmail), + type: 'TEXT', + }, + ], + reportActionID: NumberUtils.rand64(), + shouldShow: true, + created: DateUtils.getDBTime(), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }; +} + /** * Builds an optimistic SUBMITTED report action with a randomly generated reportActionID. * @@ -4216,6 +4260,7 @@ export { buildOptimisticEditedTaskReportAction, buildOptimisticIOUReport, buildOptimisticApprovedReportAction, + buildOptimisticMovedReportAction, buildOptimisticSubmittedReportAction, buildOptimisticExpenseReport, buildOptimisticIOUReportAction, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 941fa814a9b4..a022d8f61f18 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1360,7 +1360,7 @@ function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) { return lodashUnion([category], policyRecentlyUsedCategories); } -function startCollectBottomUpFlow(iouReport, firstName, lastName) { +function createWorkspaceFromIOUPayment(iouReport) { // This flow only works for IOU reports if (!ReportUtils.isIOUReport(iouReport)) { return; @@ -1544,6 +1544,7 @@ function startCollectBottomUpFlow(iouReport, firstName, lastName) { }, ...employeeWorkspaceChat.onyxSuccessData, ]; + const failureData = [ { onyxMethod: Onyx.METHOD.SET, @@ -1585,17 +1586,29 @@ function startCollectBottomUpFlow(iouReport, firstName, lastName) { // Next we need to convert the IOU report to Expense report and clean up the DM chat // Get the 1on1 chat where the request was originally made const chatReportID = iouReport.chatReportID; - ReportUtils.getReport(iouReport.chatReportID); + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: { + ...iouReport, + type: CONST.REPORT.TYPE.EXPENSE + }, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: iouReport, + }); + + const chatReport = ReportUtils.getReport(iouReport.chatReportID); const reportPreviewID = iouReport.parentReportActionID; - const membersData = [ - { - accountID: employeeAccountID, - email: employeeEmail, - workspaceChatReportID: employeeWorkspaceChat.reportCreationData[employeeEmail].reportID, - workspaceChatCreatedReportActionID: employeeWorkspaceChat.reportCreationData[employeeEmail].reportActionID, - }, - ]; + const memberData = { + accountID: Number(employeeAccountID), + email: employeeEmail, + workspaceChatReportID: Number(employeeWorkspaceChat.reportCreationData[employeeEmail].reportID), + workspaceChatCreatedReportActionID: Number(employeeWorkspaceChat.reportCreationData[employeeEmail].reportActionID), + }; API.write( 'CreateWorkspaceFromIOUPayment', @@ -1604,7 +1617,7 @@ function startCollectBottomUpFlow(iouReport, firstName, lastName) { announceChatReportID, adminsChatReportID, expenseChatReportID: workspaceChatReportID, - ownerEmail: sessionEmail, + ownerEmail: '', makeMeAdmin: false, policyName: workspaceName, type: CONST.POLICY.TYPE.TEAM, @@ -1614,11 +1627,11 @@ function startCollectBottomUpFlow(iouReport, firstName, lastName) { customUnitID, customUnitRateID, iouReportID: iouReport.reportID, - membersData: JSON.stringify(membersData), + memberData: JSON.stringify(memberData), + reportActionID: 0, }, {optimisticData, successData, failureData}, ); - // Navigation.dismissModal(CONST.TEACHERS_UNITE.PUBLIC_ROOM_ID); } export { @@ -1647,11 +1660,11 @@ export { openWorkspaceMembersPage, openWorkspaceInvitePage, removeWorkspace, + createWorkspaceFromIOUPayment, setWorkspaceInviteMembersDraft, clearErrors, dismissAddedWithPrimaryLoginMessages, openDraftWorkspaceRequest, buildOptimisticPolicyRecentlyUsedCategories, createDraftInitialWorkspace, - startCollectBottomUpFlow, }; diff --git a/src/pages/home/report/ReportActionItem.js b/src/pages/home/report/ReportActionItem.js index a7a3bc0739f3..eb0ae4f8978a 100644 --- a/src/pages/home/report/ReportActionItem.js +++ b/src/pages/home/report/ReportActionItem.js @@ -422,7 +422,7 @@ function ReportActionItem(props) { isHidden={isHidden} style={[ _.contains( - [..._.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG), CONST.REPORT.ACTIONS.TYPE.IOU, CONST.REPORT.ACTIONS.TYPE.APPROVED], + [..._.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG), CONST.REPORT.ACTIONS.TYPE.IOU, CONST.REPORT.ACTIONS.TYPE.APPROVED, CONST.REPORT.ACTIONS.TYPE.MOVED], props.action.actionName, ) ? styles.colorMuted