Skip to content

Commit

Permalink
Simplify getOptimisticOutstandingChildRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
paultsimura committed Apr 3, 2024
1 parent cfda769 commit 365429d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
22 changes: 7 additions & 15 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,6 @@ type AncestorIDs = {

type MissingPaymentMethod = 'bankAccount' | 'wallet';

type OutstandingChildRequest = {
hasOutstandingChildRequest?: boolean;
};

let currentUserEmail: string | undefined;
let currentUserPrivateDomain: string | undefined;
let currentUserAccountID: number | undefined;
Expand Down Expand Up @@ -5689,29 +5685,25 @@ function hasActionsWithErrors(reportID: string): boolean {
}

/**
* @returns the object to update `report.hasOutstandingChildRequest`
* @returns the optimistically calculated `hasOutstandingChildRequest` for the report (`null` if the value cannot be calculated).
*/
function getOutstandingChildRequest(iouReport: OnyxEntry<Report> | EmptyObject): OutstandingChildRequest {
function getOptimisticOutstandingChildRequest(iouReport: OnyxEntry<Report> | EmptyObject): boolean | null {
if (!iouReport || isEmptyObject(iouReport)) {
return {};
return null;
}

if (!isExpenseReport(iouReport)) {
return {
hasOutstandingChildRequest: iouReport.managerID === currentUserAccountID && iouReport.total !== 0,
};
return iouReport.managerID === currentUserAccountID && iouReport.total !== 0;
}

const policy = getPolicy(iouReport.policyID);
const shouldBeManuallySubmitted = PolicyUtils.isPaidGroupPolicy(policy) && !policy?.harvesting?.enabled;
if (shouldBeManuallySubmitted || PolicyUtils.isPolicyAdmin(policy)) {
return {
hasOutstandingChildRequest: true,
};
return true;
}

// We don't need to update hasOutstandingChildRequest in this case
return {};
return null;
}

export {
Expand Down Expand Up @@ -5939,7 +5931,7 @@ export {
isTrackExpenseReport,
hasActionsWithErrors,
getGroupChatName,
getOutstandingChildRequest,
getOptimisticOutstandingChildRequest,
};

export type {
Expand Down
10 changes: 6 additions & 4 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ function buildOnyxDataForMoneyRequest(
isOneOnOneSplit = false,
): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] {
const isScanRequest = TransactionUtils.isScanRequest(transaction);
const outstandingChildRequest = ReportUtils.getOutstandingChildRequest(iouReport);
const outstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(iouReport);
const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null]));
const optimisticData: OnyxUpdate[] = [];
let newQuickAction: ValueOf<typeof CONST.QUICK_ACTIONS> = isScanRequest ? CONST.QUICK_ACTIONS.REQUEST_SCAN : CONST.QUICK_ACTIONS.REQUEST_MANUAL;
Expand All @@ -470,7 +470,7 @@ function buildOnyxDataForMoneyRequest(
lastReadTime: DateUtils.getDBTime(),
lastMessageTranslationKey: '',
iouReportID: iouReport.reportID,
...outstandingChildRequest,
...(outstandingChildRequest !== null ? {hasOutstandingChildRequest: outstandingChildRequest} : {}),
...(isNewChatReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}),
},
});
Expand Down Expand Up @@ -1542,6 +1542,7 @@ function getUpdateMoneyRequestParams(

updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, transactionDetails?.currency);

const optimisticOutstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(updatedMoneyRequestReport);
optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -1551,7 +1552,7 @@ function getUpdateMoneyRequestParams(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.parentReportID}`,
value: ReportUtils.getOutstandingChildRequest(updatedMoneyRequestReport),
value: optimisticOutstandingChildRequest !== null ? {hasOutstandingChildRequest: optimisticOutstandingChildRequest} : {},
},
);
successData.push({
Expand Down Expand Up @@ -3714,6 +3715,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor
);
}

const optimisticOutstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(updatedIOUReport);
optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -3735,7 +3737,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
value: ReportUtils.getOutstandingChildRequest(updatedIOUReport),
value: optimisticOutstandingChildRequest !== null ? {hasOutstandingChildRequest: optimisticOutstandingChildRequest} : {},
},
);

Expand Down

0 comments on commit 365429d

Please sign in to comment.