Skip to content

Commit

Permalink
Merge pull request #34866 from tienifr/fix/33774
Browse files Browse the repository at this point in the history
Fix: Delay in updating green dot and total amount
  • Loading branch information
mountiny authored Feb 28, 2024
2 parents 5d737d0 + fa16620 commit 7c68b1d
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,25 @@ function getReceiptError(receipt?: Receipt, filename?: string, isScanRequest = t
: ErrorUtils.getMicroSecondOnyxErrorObject({error: CONST.IOU.RECEIPT_ERROR, source: receipt.source?.toString() ?? '', filename: filename ?? ''});
}

/** Return the object to update hasOutstandingChildRequest */
function getOutstandingChildRequest(needsToBeManuallySubmitted: boolean, policy: OnyxEntry<OnyxTypes.Policy> | EmptyObject = null): OutstandingChildRequest {
if (!needsToBeManuallySubmitted) {
function needsToBeManuallySubmitted(iouReport: OnyxTypes.Report) {
const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport);

if (isPolicyExpenseChat) {
const policy = ReportUtils.getPolicy(iouReport.policyID);
const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy);

// If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN
return isFromPaidPolicy && !policy.harvesting?.enabled;
}

return true;
}

/**
* Return the object to update hasOutstandingChildRequest
*/
function getOutstandingChildRequest(policy: OnyxEntry<OnyxTypes.Policy> | EmptyObject, iouReport: OnyxTypes.Report): OutstandingChildRequest {
if (!needsToBeManuallySubmitted(iouReport)) {
return {
hasOutstandingChildRequest: false,
};
Expand All @@ -399,8 +415,9 @@ function getOutstandingChildRequest(needsToBeManuallySubmitted: boolean, policy:
};
}

// We don't need to update hasOutstandingChildRequest in this case
return {};
return {
hasOutstandingChildRequest: iouReport.managerID === userAccountID && iouReport.total !== 0,
};
}

/** Builds the Onyx data for a money request */
Expand All @@ -423,10 +440,9 @@ function buildOnyxDataForMoneyRequest(
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagList>,
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>,
optimisticNextStep?: OnyxTypes.ReportNextStep | null,
needsToBeManuallySubmitted = true,
): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] {
const isScanRequest = TransactionUtils.isScanRequest(transaction);
const outstandingChildRequest = getOutstandingChildRequest(needsToBeManuallySubmitted, policy);
const outstandingChildRequest = getOutstandingChildRequest(policy ?? {}, iouReport);
const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null]));
const optimisticData: OnyxUpdate[] = [];

Expand Down Expand Up @@ -819,15 +835,10 @@ function getMoneyRequestInformation(
iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`] ?? null;
}

// Check if the Scheduled Submit is enabled in case of expense report
let needsToBeManuallySubmitted = true;
let isFromPaidPolicy = false;
if (isPolicyExpenseChat) {
isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy ?? null);

// If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN
needsToBeManuallySubmitted = isFromPaidPolicy && !policy?.harvesting?.enabled;

// If the linked expense report on paid policy is not draft and not instantly submitted, we need to create a new draft expense report
if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport) && !ReportUtils.isExpenseReportWithInstantSubmittedState(iouReport)) {
iouReport = null;
Expand Down Expand Up @@ -967,7 +978,6 @@ function getMoneyRequestInformation(
policyTagList,
policyCategories,
optimisticNextStep,
needsToBeManuallySubmitted,
);

return {
Expand Down Expand Up @@ -1234,11 +1244,21 @@ function getUpdateMoneyRequestParams(
}
updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, transactionDetails?.currency);

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
value: updatedMoneyRequestReport,
});
optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
value: updatedMoneyRequestReport,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.parentReportID}`,
value: {
hasOutstandingChildRequest:
iouReport && needsToBeManuallySubmitted(iouReport) && updatedMoneyRequestReport.managerID === userAccountID && updatedMoneyRequestReport.total !== 0,
},
},
);
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
Expand Down Expand Up @@ -3019,6 +3039,13 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor
[reportPreviewAction?.reportActionID ?? '']: updatedReportPreviewAction,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
value: {
hasOutstandingChildRequest: iouReport && needsToBeManuallySubmitted(iouReport) && updatedIOUReport?.managerID === userAccountID && updatedIOUReport.total !== 0,
},
},
);

if (!shouldDeleteIOUReport && updatedReportPreviewAction.childMoneyRequestCount === 0) {
Expand Down

0 comments on commit 7c68b1d

Please sign in to comment.