Skip to content
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/libs/API/parameters/DetachReceiptParams.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type DetachReceiptParams = {
transactionID: string;
reportActionID: string;
};

export default DetachReceiptParams;
38 changes: 38 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,43 @@ function buildOptimisticModifiedExpenseReportAction(
};
}

/**
* Builds an optimistic DETACH_RECEIPT report action with a randomly generated reportActionID.
*/
function buildOptimisticDetachReceipt(reportID: string | undefined, transactionID: string, merchant: string = CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT) {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.MANAGER_DETACH_RECEIPT,
actorAccountID: currentUserAccountID,
automatic: false,
avatar: getCurrentUserAvatar(),
created: DateUtils.getDBTime(),
isAttachmentOnly: false,
originalMessage: {
transactionID,
merchant: `${merchant}`,
},
message: [
{
type: 'COMMENT',
html: `detached a receipt from expense '${merchant}'`,
text: `detached a receipt from expense '${merchant}'`,
whisperedTo: [],
},
],
person: [
{
style: 'strong',
text: currentUserPersonalDetails?.displayName ?? String(currentUserAccountID),
type: 'TEXT',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
reportActionID: rand64(),
reportID,
shouldShow: true,
};
}

/**
* Builds an optimistic modified expense action for a tracked expense move with a randomly generated reportActionID.
* @param transactionThreadID - The reportID of the transaction thread
Expand Down Expand Up @@ -8877,6 +8914,7 @@ export {
buildOptimisticAnnounceChat,
buildOptimisticWorkspaceChats,
buildOptimisticCardAssignedReportAction,
buildOptimisticDetachReceipt,
buildParticipantsFromAccountIDs,
buildTransactionThread,
canAccessReport,
Expand Down
46 changes: 45 additions & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
buildOptimisticCancelPaymentReportAction,
buildOptimisticChatReport,
buildOptimisticCreatedReportAction,
buildOptimisticDetachReceipt,
buildOptimisticDismissedViolationReportAction,
buildOptimisticExpenseReport,
buildOptimisticHoldReportAction,
Expand Down Expand Up @@ -8625,8 +8626,51 @@ function detachReceipt(transactionID: string | undefined) {
},
},
];
const expenseReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`] ?? null;
const updatedReportAction = buildOptimisticDetachReceipt(expenseReport?.reportID, transactionID, transaction?.merchant);

const parameters: DetachReceiptParams = {transactionID};
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${updatedReportAction?.reportID}`,
value: {
[updatedReportAction.reportActionID]: updatedReportAction as OnyxTypes.ReportAction,
},
});
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${updatedReportAction?.reportID}`,
value: {
lastVisibleActionCreated: updatedReportAction.created,
lastReadTime: updatedReportAction.created,
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${updatedReportAction?.reportID}`,
value: {
lastVisibleActionCreated: expenseReport?.lastVisibleActionCreated,
lastReadTime: expenseReport?.lastReadTime,
},
});
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.reportID}`,
value: {
[updatedReportAction.reportActionID]: {pendingAction: null},
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.reportID}`,
value: {
[updatedReportAction.reportActionID]: {
...(updatedReportAction as OnyxTypes.ReportAction),
errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'),
},
},
});

const parameters: DetachReceiptParams = {transactionID, reportActionID: updatedReportAction.reportActionID};

API.write(WRITE_COMMANDS.DETACH_RECEIPT, parameters, {optimisticData, successData, failureData});
}
Expand Down