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

Adds Transaction Thread Optimistic ID's #32461

Closed
119 changes: 114 additions & 5 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ function buildOnyxDataForMoneyRequest(
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
transactionThreadReport,
transactionThreadCreatedReportAction,
) {
const optimisticData = [
{
Expand Down Expand Up @@ -338,7 +340,18 @@ function buildOnyxDataForMoneyRequest(
[iouAction.reportActionID]: iouAction,
},
},

{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport.reportID}`,
value: transactionThreadReport,
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport.reportID}`,
value: {
[transactionThreadCreatedReportAction.reportActionID]: transactionThreadCreatedReportAction,
},
},
// Remove the temporary transaction used during the creation flow
{
onyxMethod: Onyx.METHOD.SET,
Expand Down Expand Up @@ -440,6 +453,16 @@ function buildOnyxDataForMoneyRequest(
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport.reportID}`,
value: {
[transactionThreadCreatedReportAction.reportActionID]: {
pendingAction: null,
errors: null,
},
},
},
];

const failureData = [
Expand Down Expand Up @@ -531,6 +554,15 @@ function buildOnyxDataForMoneyRequest(
}),
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
[transactionThreadCreatedReportAction.reportActionID]: {
errors: ErrorUtils.getMicroSecondOnyxError('iou.error.genericCreateFailureMessage'),
},
},
},
];

return [optimisticData, successData, failureData];
Expand All @@ -554,6 +586,8 @@ function buildOnyxDataForMoneyRequest(
* @param {String} [category]
* @param {String} [tag]
* @param {Boolean} [billable]
* @param {Number} [transactionThreadReportID]
* @param {Number} [createdReportActionForThread]
* @returns {Object} data
* @returns {String} data.payerEmail
* @returns {Object} data.iouReport
Expand Down Expand Up @@ -704,6 +738,9 @@ function getMoneyRequestInformation(
currentTime,
);

const transactionThread = ReportUtils.buildTransactionThread(iouAction, iouReport.reportID);
const optimisticCreatedActionForThread = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail);

let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID);
if (reportPreviewAction) {
reportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, false, comment, optimisticTransaction);
Expand Down Expand Up @@ -743,6 +780,8 @@ function getMoneyRequestInformation(
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
transactionThread,
optimisticCreatedActionForThread,
);

return {
Expand All @@ -760,6 +799,8 @@ function getMoneyRequestInformation(
successData,
failureData,
},
transactionThreadReportID: Number(transactionThread.reportID),
createdReportActionForThread: Number(optimisticCreatedActionForThread.reportActionID),
};
}

Expand Down Expand Up @@ -787,7 +828,18 @@ function createDistanceRequest(report, participant, comment, created, category,
source: ReceiptGeneric,
state: CONST.IOU.RECEIPT_STATE.OPEN,
};
const {iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = getMoneyRequestInformation(
const {
iouReport,
chatReport,
transaction,
iouAction,
createdChatReportActionID,
createdIOUReportActionID,
reportPreviewAction,
onyxData,
transactionThreadReportID,
createdReportActionForThread,
} = getMoneyRequestInformation(
currentChatReport,
participant,
comment,
Expand Down Expand Up @@ -819,6 +871,8 @@ function createDistanceRequest(report, participant, comment, created, category,
category,
tag,
billable,
transactionThreadReportID,
createdReportActionForThread,
},
onyxData,
);
Expand Down Expand Up @@ -1073,8 +1127,20 @@ function requestMoney(
// If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report;
const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} =
getMoneyRequestInformation(currentChatReport, participant, comment, amount, currency, created, merchant, payeeAccountID, payeeEmail, receipt, undefined, category, tag, billable);
const {
payerAccountID,
payerEmail,
iouReport,
chatReport,
transaction,
iouAction,
createdChatReportActionID,
createdIOUReportActionID,
reportPreviewAction,
onyxData,
transactionThreadReportID,
createdReportActionForThread,
} = getMoneyRequestInformation(currentChatReport, participant, comment, amount, currency, created, merchant, payeeAccountID, payeeEmail, receipt, undefined, category, tag, billable);
const activeReportID = isMoneyRequestReport ? report.reportID : chatReport.reportID;

API.write(
Expand All @@ -1099,6 +1165,8 @@ function requestMoney(
category,
tag,
billable,
transactionThreadReportID,
createdReportActionForThread,
},
onyxData,
);
Expand Down Expand Up @@ -1409,6 +1477,10 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
// Add tag to optimistic policy recently used tags when a participant is a workspace
const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag) : {};

// Add transaction thread to optimistic data
const transactionThread = ReportUtils.buildTransactionThread(oneOnOneIOUReport, oneOnOneIOUReport.reportID);
const optimisticCreatedActionForThread = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmailForIOUSplit);

// STEP 5: Build Onyx Data
const [oneOnOneOptimisticData, oneOnOneSuccessData, oneOnOneFailureData] = buildOnyxDataForMoneyRequest(
oneOnOneChatReport,
Expand All @@ -1423,6 +1495,8 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
optimisticPolicyRecentlyUsedTags,
isNewOneOnOneChatReport,
shouldCreateNewOneOnOneIOUReport,
transactionThread,
optimisticCreatedActionForThread,
);

const individualSplit = {
Expand Down Expand Up @@ -1935,6 +2009,9 @@ function completeSplitBill(chatReportID, reportAction, updatedTransaction, sessi
oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport, '', oneOnOneTransaction);
}

const transactionThread = ReportUtils.buildTransactionThread(oneOnOneIOUReport, oneOnOneIOUReport.reportID);
const optimisticCreatedActionForThread = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmailForIOUSplit);

const [oneOnOneOptimisticData, oneOnOneSuccessData, oneOnOneFailureData] = buildOnyxDataForMoneyRequest(
oneOnOneChatReport,
oneOnOneIOUReport,
Expand All @@ -1948,6 +2025,9 @@ function completeSplitBill(chatReportID, reportAction, updatedTransaction, sessi
{},
isNewOneOnOneChatReport,
shouldCreateNewOneOnOneIOUReport,
transactionThread.reportID,
transactionThread,
optimisticCreatedActionForThread,
);

splits.push({
Expand All @@ -1961,6 +2041,8 @@ function completeSplitBill(chatReportID, reportAction, updatedTransaction, sessi
createdChatReportActionID: oneOnOneCreatedActionForChat.reportActionID,
createdIOUReportActionID: oneOnOneCreatedActionForIOU.reportActionID,
reportPreviewReportActionID: oneOnOneReportPreviewAction.reportActionID,
transactionThreadReportID: transactionThread.reportID,
createdReportActionIDForThread: optimisticCreatedActionForThread.reportActionID,
});

optimisticData.push(...oneOnOneOptimisticData);
Expand Down Expand Up @@ -2563,6 +2645,9 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType

const reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticIOUReport);

const transactionThread = ReportUtils.buildTransactionThread(optimisticIOUReportAction, optimisticIOUReport.reportID);
const optimisticCreatedActionForThread = ReportUtils.buildOptimisticCreatedReportAction(recipientEmail);

// First, add data that will be used in all cases
const optimisticChatReportData = {
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -2600,6 +2685,14 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
},
};

const optimisticTransactionThreadReportActionsData = {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionThread.reportID}`,
value: {
[optimisticCreatedActionForThread.reportActionID]: optimisticCreatedActionForThread,
},
};

const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -2624,6 +2717,15 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThread.reportID}`,
value: {
[optimisticCreatedActionForThread.reportActionID]: {
pendingAction: null,
},
},
},
];

const failureData = [
Expand Down Expand Up @@ -2701,7 +2803,14 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
});
}

const optimisticData = [optimisticChatReportData, optimisticIOUReportData, optimisticChatReportActionsData, optimisticIOUReportActionsData, optimisticTransactionData];
const optimisticData = [
optimisticChatReportData,
optimisticIOUReportData,
optimisticChatReportActionsData,
optimisticIOUReportActionsData,
optimisticTransactionData,
optimisticTransactionThreadReportActionsData,
];
if (!_.isEmpty(optimisticPersonalDetailListData)) {
optimisticData.push(optimisticPersonalDetailListData);
}
Expand Down
Loading
Loading