From da7d34066d70a07b1111b3eb1ea9d0bc838bd681 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Thu, 5 Oct 2023 20:41:58 +0200 Subject: [PATCH 1/5] add submit action --- src/CONST.ts | 1 + src/libs/ReportUtils.js | 42 +++++++++++++++++++++++++++++++ src/libs/actions/IOU.js | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/src/CONST.ts b/src/CONST.ts index 9a4e1faf170e..74cc9de8c444 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -480,6 +480,7 @@ const CONST = { ADDCOMMENT: 'ADDCOMMENT', CLOSED: 'CLOSED', CREATED: 'CREATED', + SUBMITTED: 'SUBMITTED', TASKEDITED: 'TASKEDITED', TASKCANCELLED: 'TASKCANCELLED', IOU: 'IOU', diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 7efdd7d96ca9..dc73ae0243f9 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2202,6 +2202,9 @@ function getIOUReportActionMessage(iouReportID, type, total, comment, currency, case CONST.REPORT.ACTIONS.TYPE.APPROVED: iouMessage = `approved ${amount}`; break; + case CONST.REPORT.ACTIONS.TYPE.SUBMITTED: + iouMessage = `submitted ${amount}`; + break; case CONST.IOU.REPORT_ACTION_TYPE.CREATE: iouMessage = `requested ${amount}${comment && ` for ${comment}`}`; break; @@ -2359,6 +2362,44 @@ function buildOptimisticApprovedReportAction(amount, currency, expenseReportID) }; } +/** + * Builds an optimistic SUBMITTED report action with a randomly generated reportActionID. + * + * @param {Number} amount + * @param {String} currency + * @param {Number} expenseReportID + * + * @returns {Object} + */ +function buildOptimisticSubmittedReportAction(amount, currency, expenseReportID) { + const originalMessage = { + amount, + currency, + expenseReportID, + }; + + return { + actionName: CONST.REPORT.ACTIONS.TYPE.SUBMITTED, + actorAccountID: currentUserAccountID, + automatic: false, + avatar: lodashGet(currentUserPersonalDetails, 'avatar', UserUtils.getDefaultAvatar(currentUserAccountID)), + isAttachment: false, + originalMessage, + message: getIOUReportActionMessage(expenseReportID, CONST.REPORT.ACTIONS.TYPE.SUBMITTED, Math.abs(amount), '', currency), + 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 report preview action with a randomly generated reportActionID. * @@ -3831,6 +3872,7 @@ export { buildOptimisticEditedTaskReportAction, buildOptimisticIOUReport, buildOptimisticApprovedReportAction, + buildOptimisticSubmittedReportAction, buildOptimisticExpenseReport, buildOptimisticIOUReportAction, buildOptimisticReportPreview, diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index b6f193ceeef4..6e19b1824db7 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2096,6 +2096,60 @@ function approveMoneyRequest(expenseReport) { API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); } +function submitReport(expenseReport) { + const optimisticSubmittedReportAction = ReportUtils.buildOptimisticSubmittedReportAction(expenseReport.total, expenseReport.currency, expenseReport.reportID); + + const optimisticReportActionsData = { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, + value: { + [optimisticSubmittedReportAction.reportActionID]: { + ...optimisticSubmittedReportAction, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }, + }; + const optimisticIOUReportData = { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, + value: { + ...expenseReport, + lastMessageText: optimisticSubmittedReportAction.message[0].text, + lastMessageHtml: optimisticSubmittedReportAction.message[0].html, + state: CONST.REPORT.STATE.SUBMITTED, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS.SUBMITTED, + }, + }; + const optimisticData = [optimisticIOUReportData, optimisticReportActionsData]; + + const successData = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, + value: { + [optimisticSubmittedReportAction.reportActionID]: { + pendingAction: null, + }, + }, + }, + ]; + + const failureData = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, + value: { + [expenseReport.reportActionID]: { + errors: ErrorUtils.getMicroSecondOnyxError('iou.error.other'), + }, + }, + }, + ]; + + API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); +} + /** * @param {String} paymentType * @param {Object} chatReport @@ -2311,6 +2365,7 @@ export { requestMoney, sendMoneyElsewhere, approveMoneyRequest, + submitReport, payMoneyRequest, sendMoneyWithWallet, startMoneyRequest, From 9ca8e1e47aee821a056e6dbb203a4c407c008401 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Thu, 5 Oct 2023 20:54:29 +0200 Subject: [PATCH 2/5] lint fix --- src/libs/actions/IOU.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 6e19b1824db7..9046b94bb8e7 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2147,7 +2147,7 @@ function submitReport(expenseReport) { }, ]; - API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); + API.write('SubmitReport', {reportID: expenseReport.reportID, submittedReportActionID: optimisticSubmittedReportAction.reportActionID}, {optimisticData, successData, failureData}); } /** From 96fd57be7082a629dd00b2aebba5be1225cee52d Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Sun, 8 Oct 2023 22:01:39 +0200 Subject: [PATCH 3/5] adjustments --- src/libs/actions/IOU.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 9046b94bb8e7..b1e6360fcb13 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2096,6 +2096,9 @@ function approveMoneyRequest(expenseReport) { API.write('ApproveMoneyRequest', {reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID}, {optimisticData, successData, failureData}); } +/** + * @param {Object} expenseReport + */ function submitReport(expenseReport) { const optimisticSubmittedReportAction = ReportUtils.buildOptimisticSubmittedReportAction(expenseReport.total, expenseReport.currency, expenseReport.reportID); @@ -2147,7 +2150,16 @@ function submitReport(expenseReport) { }, ]; - API.write('SubmitReport', {reportID: expenseReport.reportID, submittedReportActionID: optimisticSubmittedReportAction.reportActionID}, {optimisticData, successData, failureData}); + API.write( + 'SubmitReport', + { + reportID: expenseReport.reportID, + managerEmail: expenseReport.managerEmail, + managerAccountID: expenseReport.managerID, + reportActionID: optimisticSubmittedReportAction.reportActionID, + }, + {optimisticData, successData, failureData}, + ); } /** From f0b9557fd3efc7a6b4072761941a6f3074dbba90 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Mon, 9 Oct 2023 17:16:34 +0200 Subject: [PATCH 4/5] adjustments --- src/libs/actions/IOU.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index b99ffeb603c5..44eba0cef5a5 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2120,7 +2120,7 @@ function submitReport(expenseReport) { lastMessageText: optimisticSubmittedReportAction.message[0].text, lastMessageHtml: optimisticSubmittedReportAction.message[0].html, state: CONST.REPORT.STATE.SUBMITTED, - stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + stateNum: CONST.REPORT.STATE_NUM.PROCESSING, statusNum: CONST.REPORT.STATUS.SUBMITTED, }, }; @@ -2148,6 +2148,14 @@ function submitReport(expenseReport) { }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, + value: { + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS.OPEN, + }, + }, ]; API.write( From 60013c0b90df62f69be9195bb9da9955ce0c7cc2 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Mon, 9 Oct 2023 17:25:02 +0200 Subject: [PATCH 5/5] fixes --- src/libs/actions/IOU.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index c99c8d17175a..2c046bfc2a24 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2162,8 +2162,8 @@ function submitReport(expenseReport) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, value: { + state: CONST.REPORT.STATE.OPEN, stateNum: CONST.REPORT.STATE_NUM.OPEN, - statusNum: CONST.REPORT.STATUS.OPEN, }, }, ];