diff --git a/src/CONST.ts b/src/CONST.ts index 5d75f43cedd9..652a38200ab1 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -482,6 +482,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 a8e80101c07f..746ecb7e20a4 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. * @@ -3837,6 +3878,7 @@ export { buildOptimisticEditedTaskReportAction, buildOptimisticIOUReport, buildOptimisticApprovedReportAction, + buildOptimisticSubmittedReportAction, buildOptimisticExpenseReport, buildOptimisticIOUReportAction, buildOptimisticReportPreview, diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 960ce989cc3c..2c046bfc2a24 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2106,6 +2106,80 @@ 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); + + 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.PROCESSING, + 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'), + }, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, + value: { + state: CONST.REPORT.STATE.OPEN, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + }, + }, + ]; + + API.write( + 'SubmitReport', + { + reportID: expenseReport.reportID, + managerEmail: expenseReport.managerEmail, + managerAccountID: expenseReport.managerID, + reportActionID: optimisticSubmittedReportAction.reportActionID, + }, + {optimisticData, successData, failureData}, + ); +} + /** * @param {String} paymentType * @param {Object} chatReport @@ -2327,10 +2401,6 @@ function getIOUReportID(iou, route) { return lodashGet(route, 'params.reportID') || lodashGet(iou, 'participants.0.reportID', ''); } -function submitReport() { - // Will be implemented in https://github.com/Expensify/App/issues/28763 -} - export { createDistanceRequest, editMoneyRequest, @@ -2340,6 +2410,7 @@ export { requestMoney, sendMoneyElsewhere, approveMoneyRequest, + submitReport, payMoneyRequest, sendMoneyWithWallet, startMoneyRequest, @@ -2362,5 +2433,4 @@ export { updateDistanceRequest, replaceReceipt, getIOUReportID, - submitReport, };