Repository navigation
Fix duplicate indicator on paid reports and error when bulk-duplicating non-reimbursable expenses #88442
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
Fix duplicate indicator on paid reports and error when bulk-duplicating non-reimbursable expenses #88442
Changes from all commits
d43b7b8
d89644d
7740d6c
bf8e38b
bdb72d3
040f71e
0ae3e2b
f58c144
8ac3346
8f7b877
b865982
1c9704a
589afd4
815f23f
3e96dc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,19 +12,21 @@ import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; | |
| import {getExistingTransactionID} from '@libs/IOUUtils'; | ||
| import * as NumberUtils from '@libs/NumberUtils'; | ||
| import Parser from '@libs/Parser'; | ||
| import {isPolicyAccessible} from '@libs/PolicyUtils'; | ||
| import {isInstantSubmitEnabled, isPolicyAccessible, isSubmitAndClose} from '@libs/PolicyUtils'; | ||
| import {getIOUActionForReportID, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; | ||
| import { | ||
| buildOptimisticCreatedReportAction, | ||
| buildOptimisticDismissedViolationReportAction, | ||
| buildOptimisticHoldReportAction, | ||
| buildOptimisticResolvedDuplicatesReportAction, | ||
| buildTransactionThread, | ||
| canAddTransaction, | ||
| generateReportID, | ||
| getTransactionDetails, | ||
| } from '@libs/ReportUtils'; | ||
| import playSound, {SOUNDS} from '@libs/Sound'; | ||
| import { | ||
| getReimbursable, | ||
| getRequestType, | ||
| getTransactionType, | ||
| hasCustomUnitOutOfPolicyViolation, | ||
|
|
@@ -600,6 +602,7 @@ function createExpenseByType({ | |
| ...(params.transactionParams ?? {}), | ||
| comment: { | ||
| ...transaction.comment, | ||
| hold: undefined, | ||
| originalTransactionID: undefined, | ||
| source: undefined, | ||
| waypoints, | ||
|
|
@@ -748,6 +751,7 @@ function duplicateExpenseTransaction({ | |
| ...(params.transactionParams ?? {}), | ||
| comment: { | ||
| ...transaction.comment, | ||
| hold: undefined, | ||
| originalTransactionID: undefined, | ||
| source: undefined, | ||
| waypoints, | ||
|
|
@@ -998,8 +1002,12 @@ function bulkDuplicateExpenses({ | |
| } | ||
|
|
||
| const optimisticChatReportID = generateReportID(); | ||
| const optimisticIOUReportID = generateReportID(); | ||
| const sharedReportPreviewActionID = NumberUtils.rand64(); | ||
|
|
||
| // These are mutable: when the current IOU report can't accept more | ||
| // transactions (e.g. instant-submit + submit-and-close with non-reimbursable | ||
| // expenses), we generate fresh IDs so each expense gets its own report. | ||
| let currentOptimisticIOUReportID = generateReportID(); | ||
| let currentReportPreviewActionID = NumberUtils.rand64(); | ||
|
|
||
| // After the first iteration creates a new optimistic IOU report, subsequent | ||
| // iterations must know its ID so getMoneyRequestInformation can find and | ||
|
|
@@ -1011,6 +1019,19 @@ function bulkDuplicateExpenses({ | |
| let currentTargetReport = targetReport; | ||
| let optimisticIOUReport: OnyxEntry<OnyxTypes.Report>; | ||
|
|
||
| // When instant-submit + submit-and-close is active AND the duplicated | ||
| // expenses are all non-reimbursable (or the policy disables reimbursement | ||
| // entirely), canAddTransaction will reject the optimistic report after | ||
| // the first expense auto-submits and closes it. In that scenario every | ||
| // iteration will create a new report, so we must never defer auto-submit | ||
| // — otherwise the first expense's report stays in Draft because the | ||
| // server was told to wait for more expenses that will never arrive. | ||
| const allNonReimbursable = transactionsToDuplicate.every((t) => !getReimbursable(t)); | ||
| const policyWillSplitReport = | ||
| isInstantSubmitEnabled(targetPolicy) && | ||
| isSubmitAndClose(targetPolicy) && | ||
| (allNonReimbursable || targetPolicy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO); | ||
|
|
||
| for (let i = 0; i < transactionsToDuplicate.length; i++) { | ||
| const item = transactionsToDuplicate.at(i); | ||
| if (!item) { | ||
|
|
@@ -1020,10 +1041,34 @@ function bulkDuplicateExpenses({ | |
| const existingTransactionID = getExistingTransactionID(item.linkedTrackedExpenseReportAction); | ||
| const existingTransactionDraft = existingTransactionID ? transactionDrafts?.[existingTransactionID] : undefined; | ||
|
|
||
| // If the policy pre-check determined every expense must live on its | ||
| // own report, or the previous iteration's report can't accept more | ||
| // transactions, reset so this iteration creates its own independent | ||
| // report. policyWillSplitReport is needed because canAddTransaction | ||
| // reads transactions from Onyx, which hasn't been updated yet for | ||
| // optimistic reports (callbacks are deferred). | ||
| let reportWasSplit = false; | ||
| if (optimisticIOUReport && (policyWillSplitReport || !canAddTransaction(optimisticIOUReport))) { | ||
| optimisticIOUReport = undefined; | ||
| currentOptimisticIOUReportID = generateReportID(); | ||
| currentReportPreviewActionID = NumberUtils.rand64(); | ||
| reportWasSplit = true; | ||
| if (currentTargetReport) { | ||
| currentTargetReport = {...currentTargetReport, iouReportID: currentOptimisticIOUReportID}; | ||
| } | ||
| } | ||
|
|
||
| // Defer auto-submit only when this isn't the last expense AND the | ||
| // report wasn't just split AND the policy won't force each expense | ||
| // onto its own report. Once a split happens every subsequent expense | ||
| // will also split (the policy closes reports immediately), so none of | ||
| // them should defer. | ||
| const shouldDeferAutoSubmit = !isLastExpense && !reportWasSplit && !policyWillSplitReport; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified manually — in an instant-submit + submit-and-close workspace with reimbursement enabled and a mixed batch (reimbursable + non-reimbursable), @MelvinBot Could you also take a fresh look at the concern raised by Codex? |
||
|
|
||
| const result = duplicateExpenseTransaction({ | ||
| transaction: item, | ||
| optimisticChatReportID, | ||
| optimisticIOUReportID, | ||
| optimisticIOUReportID: currentOptimisticIOUReportID, | ||
| isASAPSubmitBetaEnabled, | ||
| introSelected, | ||
| activePolicyID, | ||
|
|
@@ -1041,9 +1086,9 @@ function bulkDuplicateExpenses({ | |
| recentWaypoints, | ||
| targetPolicyTags, | ||
| shouldPlaySound: false, | ||
| shouldDeferAutoSubmit: !isLastExpense, | ||
| shouldDeferAutoSubmit, | ||
| existingIOUReport: optimisticIOUReport, | ||
| optimisticReportPreviewActionID: sharedReportPreviewActionID, | ||
| optimisticReportPreviewActionID: currentReportPreviewActionID, | ||
| currentUserAccountID: getUserAccountID(), | ||
| currentUserLogin: getCurrentUserEmail(), | ||
| }); | ||
|
|
@@ -1053,7 +1098,7 @@ function bulkDuplicateExpenses({ | |
| } | ||
|
|
||
| if (currentTargetReport && !currentTargetReport.iouReportID) { | ||
| currentTargetReport = {...currentTargetReport, iouReportID: optimisticIOUReportID}; | ||
| currentTargetReport = {...currentTargetReport, iouReportID: currentOptimisticIOUReportID}; | ||
|
Krishna2323 marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.