Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoQA] Feat: Add submit action #28947

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line introduced bug here: #38579
where the optimistic data from the client differs from what is returned by the backend.

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) {
waterim marked this conversation as resolved.
Show resolved Hide resolved
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]: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @waterim @Beamanator @burczu @mountiny, should the reportId for failureData should have been the optimistic id optimisticSubmittedReportAction.reportActionID?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes correct this should have been optimisticSubmittedReportAction.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,
Beamanator marked this conversation as resolved.
Show resolved Hide resolved
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,
};
Loading