diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 3456d99b66ed..afeacae1ce53 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -252,7 +252,7 @@ import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails'; import type {QuickActionName} from '@src/types/onyx/QuickAction'; import type RecentlyUsedTags from '@src/types/onyx/RecentlyUsedTags'; -import type {InvoiceReceiver, InvoiceReceiverType} from '@src/types/onyx/Report'; +import type {InvoiceReceiver, InvoiceReceiverType, ReportNextStep} from '@src/types/onyx/Report'; import type ReportAction from '@src/types/onyx/ReportAction'; import type {OnyxData} from '@src/types/onyx/Request'; import type {SearchTransaction} from '@src/types/onyx/SearchResults'; @@ -574,7 +574,8 @@ type MoneyRequestOptimisticParams = { destinations?: string[]; }; personalDetailListAction?: OnyxTypes.PersonalDetailsList; - nextStep?: OnyxTypes.ReportNextStepDeprecated | null; + nextStepDeprecated?: OnyxTypes.ReportNextStepDeprecated | null; + nextStep?: ReportNextStep | null; testDriveCommentReportActionID?: string; }; @@ -1624,6 +1625,7 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR policyRecentlyUsed, personalDetailListAction, nextStep, + nextStepDeprecated, testDriveCommentReportActionID, } = optimisticParams; @@ -1894,11 +1896,42 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR }); } - if (!isEmptyObject(nextStep)) { + if (!isEmptyObject(nextStepDeprecated)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iou.report.reportID}`, - value: nextStep, + value: nextStepDeprecated, + }); + } + if (!isEmptyObject(nextStep)) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + value: { + nextStep, + pendingFields: { + nextStep: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + }, + }, + }); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + value: { + pendingFields: { + nextStep: null, + }, + }, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + value: { + nextStep: iou.report.nextStep ?? null, + pendingFields: { + nextStep: null, + }, + }, }); } @@ -2157,7 +2190,6 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR if (!policy || !isPaidGroupPolicy(policy) || transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID) { return [optimisticData, successData, failureData]; } - const violationsOnyxData = ViolationsUtils.getViolationsOnyxData( transaction, [], @@ -2170,10 +2202,20 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR if (violationsOnyxData) { const shouldFixViolations = Array.isArray(violationsOnyxData.value) && violationsOnyxData.value.length > 0; - + const optimisticNextStep = buildOptimisticNextStep({ + report: iou.report, + predictedNextStatus: iou.report.statusNum ?? CONST.REPORT.STATE_NUM.OPEN, + shouldFixViolations, + policy, + currentUserAccountIDParam, + currentUserEmailParam, + hasViolations, + isASAPSubmitBetaEnabled, + }); optimisticData.push(violationsOnyxData, { key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iou.report.reportID}`, onyxMethod: Onyx.METHOD.SET, + // buildOptimisticNextStep is used in parallel // eslint-disable-next-line @typescript-eslint/no-deprecated value: buildNextStepNew({ report: iou.report, @@ -2186,11 +2228,40 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR isASAPSubmitBetaEnabled, }), }); + optimisticData.push({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + onyxMethod: Onyx.METHOD.MERGE, + value: { + nextStep: optimisticNextStep, + pendingFields: { + nextStep: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + }, + }, + }); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + value: { + pendingFields: { + nextStep: null, + }, + }, + }); failureData.push({ onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`, value: [], }); + failureData.push({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iou.report.reportID}`, + onyxMethod: Onyx.METHOD.MERGE, + value: { + nextStep: iou.report.nextStep ?? null, + pendingFields: { + nextStep: null, + }, + }, + }); } return [optimisticData, successData, failureData]; @@ -3674,8 +3745,19 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma const predictedNextStatus = policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.OPEN; const hasViolations = hasViolationsReportUtils(iouReport.reportID, transactionViolations, currentUserAccountIDParam, currentUserEmailParam); + // buildOptimisticNextStep is used in parallel // eslint-disable-next-line @typescript-eslint/no-deprecated - const optimisticNextStep = buildNextStepNew({ + const optimisticNextStepDeprecated = buildNextStepNew({ + report: iouReport, + predictedNextStatus, + policy, + currentUserAccountIDParam, + currentUserEmailParam, + hasViolations, + isASAPSubmitBetaEnabled, + }); + + const optimisticNextStep = buildOptimisticNextStep({ report: iouReport, predictedNextStatus, policy, @@ -3718,6 +3800,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma currencies: optimisticPolicyRecentlyUsedCurrencies, }, personalDetailListAction: optimisticPersonalDetailListAction, + nextStepDeprecated: optimisticNextStepDeprecated, nextStep: optimisticNextStep, testDriveCommentReportActionID, }, @@ -3979,8 +4062,26 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI : {}; const predictedNextStatus = policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.OPEN; + // buildOptimisticNextStep is used in parallel // eslint-disable-next-line @typescript-eslint/no-deprecated - const optimisticNextStep = buildNextStepNew({report: iouReport, predictedNextStatus, currentUserAccountIDParam, currentUserEmailParam, hasViolations, isASAPSubmitBetaEnabled, policy}); + const optimisticNextStepDeprecated = buildNextStepNew({ + report: iouReport, + predictedNextStatus, + currentUserAccountIDParam, + currentUserEmailParam, + hasViolations, + isASAPSubmitBetaEnabled, + policy, + }); + const optimisticNextStep = buildOptimisticNextStep({ + report: iouReport, + predictedNextStatus, + currentUserAccountIDParam, + currentUserEmailParam, + hasViolations, + isASAPSubmitBetaEnabled, + policy, + }); // STEP 5: Build Onyx Data const [optimisticData, successData, failureData] = buildOnyxDataForMoneyRequest({ @@ -4016,6 +4117,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI }, personalDetailListAction: optimisticPersonalDetailListAction, nextStep: optimisticNextStep, + nextStepDeprecated: optimisticNextStepDeprecated, }, currentUserAccountIDParam, currentUserEmailParam, @@ -4820,9 +4922,20 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U const shouldFixViolations = Array.isArray(violationsOnyxData.value) && violationsOnyxData.value.length > 0; const moneyRequestReport = updatedMoneyRequestReport ?? iouReport ?? undefined; const hasViolations = hasViolationsReportUtils(moneyRequestReport?.reportID, allTransactionViolations, currentUserAccountIDParam, currentUserEmailParam); + const optimisticNextStep = buildOptimisticNextStep({ + report: moneyRequestReport, + predictedNextStatus: iouReport?.statusNum ?? CONST.REPORT.STATUS_NUM.OPEN, + shouldFixViolations, + currentUserAccountIDParam, + currentUserEmailParam, + hasViolations, + isASAPSubmitBetaEnabled, + policy, + }); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`, + // buildOptimisticNextStep is used in parallel // eslint-disable-next-line @typescript-eslint/no-deprecated value: buildNextStepNew({ report: moneyRequestReport, @@ -4835,11 +4948,40 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U policy, }), }); + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: { + nextStep: optimisticNextStep, + pendingFields: { + nextStep: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + }, + }, + }); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`, value: currentNextStep, }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: { + nextStep: iouReport?.nextStep ?? null, + pendingFields: { + nextStep: null, + }, + }, + }); + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: { + pendingFields: { + nextStep: null, + }, + }, + }); } } @@ -11781,7 +11923,11 @@ function cancelPayment( statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, isWaitingOnBankAccount: expenseReport.isWaitingOnBankAccount, isCancelledIOU: false, - nextStep: expenseReport.nextStep ?? null, + nextStep: + buildOptimisticNextStep({ + report: expenseReport, + predictedNextStatus: CONST.REPORT.STATUS_NUM.REIMBURSED, + }) ?? null, pendingFields: { nextStep: null, }, @@ -11833,6 +11979,7 @@ function cancelPayment( failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, + // buildOptimisticNextStep is used in parallel // eslint-disable-next-line @typescript-eslint/no-deprecated value: buildNextStepNew({ report: expenseReport,