Skip to content

Commit

Permalink
Merge pull request #28947 from waterim/feat/28763-submitReport-IOU-ac…
Browse files Browse the repository at this point in the history
…tion

[NoQA] Feat: Add submit action
  • Loading branch information
mountiny authored Oct 9, 2023
2 parents b3dd8c0 + 60013c0 commit 93338e3
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ const CONST = {
ADDCOMMENT: 'ADDCOMMENT',
CLOSED: 'CLOSED',
CREATED: 'CREATED',
SUBMITTED: 'SUBMITTED',
TASKEDITED: 'TASKEDITED',
TASKCANCELLED: 'TASKCANCELLED',
IOU: 'IOU',
Expand Down
42 changes: 42 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -3837,6 +3878,7 @@ export {
buildOptimisticEditedTaskReportAction,
buildOptimisticIOUReport,
buildOptimisticApprovedReportAction,
buildOptimisticSubmittedReportAction,
buildOptimisticExpenseReport,
buildOptimisticIOUReportAction,
buildOptimisticReportPreview,
Expand Down
80 changes: 75 additions & 5 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -2340,6 +2410,7 @@ export {
requestMoney,
sendMoneyElsewhere,
approveMoneyRequest,
submitReport,
payMoneyRequest,
sendMoneyWithWallet,
startMoneyRequest,
Expand All @@ -2362,5 +2433,4 @@ export {
updateDistanceRequest,
replaceReceipt,
getIOUReportID,
submitReport,
};

0 comments on commit 93338e3

Please sign in to comment.