diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 07f4035668ab..1f1bdbfdb3f7 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -8067,7 +8067,13 @@ function buildOptimisticMovedReportAction( * Builds an optimistic CHANGE_POLICY report action with a randomly generated reportActionID. * This action is used when we change the workspace of a report. */ -function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefined, toPolicyID: string, currentUserAccountID: number, automaticAction = false): ReportAction { +function buildOptimisticChangePolicyReportAction( + fromPolicyID: string | undefined, + toPolicyID: string, + currentUserAccountID: number, + delegateAccountID: number | undefined, + automaticAction = false, +): ReportAction { const originalMessage = { fromPolicy: fromPolicyID, toPolicy: toPolicyID, @@ -8097,6 +8103,7 @@ function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefine return { actionName: CONST.REPORT.ACTIONS.TYPE.CHANGE_POLICY, actorAccountID: currentUserAccountID, + delegateAccountID, avatar: getCurrentUserAvatar(), created: DateUtils.getDBTime(), originalMessage, @@ -8228,18 +8235,16 @@ function buildOptimisticReportPreview( chatReport: OnyxInputOrEntry, iouReport: Report, getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'], + delegateAccountIDParam: number | undefined, comment = '', transaction: OnyxInputOrEntry = null, childReportID?: string, reportActionID?: string, - delegateAccountIDParam: number | undefined = undefined, ): ReportAction { const hasReceipt = hasReceiptTransactionUtils(transaction); const message = getReportPreviewReportActionMessage({reportOrID: iouReport}, getCurrencyDecimals); const created = DateUtils.getDBTime(); const reportActorAccountID = (isInvoiceReport(iouReport) || isExpenseReport(iouReport) ? iouReport?.ownerAccountID : iouReport?.managerID) ?? -1; - // Falls back to module-level delegateEmail (from Onyx.connect) for callers not yet migrated; will be removed in https://github.com/Expensify/App/issues/66425 - const effectiveDelegateAccountID = delegateAccountIDParam ?? (delegateEmail ? getPersonalDetailByEmail(delegateEmail)?.accountID : undefined); const isTestDriveTransaction = !!transaction?.receipt?.isTestDriveReceipt; const isScanRequest = transaction ? isScanRequestTransactionUtils(transaction) : false; return { @@ -8258,7 +8263,7 @@ function buildOptimisticReportPreview( type: CONST.REPORT.MESSAGE.TYPE.COMMENT, }, ], - delegateAccountID: effectiveDelegateAccountID, + delegateAccountID: delegateAccountIDParam, created, accountID: iouReport?.managerID, // The preview is initially whispered if created with a receipt, so the actor is the current user as well diff --git a/src/libs/actions/IOU/Hold.ts b/src/libs/actions/IOU/Hold.ts index 6bc1415026e2..b5462cc78beb 100644 --- a/src/libs/actions/IOU/Hold.ts +++ b/src/libs/actions/IOU/Hold.ts @@ -768,11 +768,11 @@ function getReportFromHoldRequestsOnyxData({ chatReport, optimisticExpenseReport, getCurrencyDecimals, + delegateAccountID, '', firstHoldTransaction, optimisticExpenseReport.reportID, newParentReportActionID, - delegateAccountID, ); let optimisticCreatedReportForUnapprovedAction: OnyxTypes.ReportAction | null = null; diff --git a/src/libs/actions/IOU/MoneyRequestBuilder.ts b/src/libs/actions/IOU/MoneyRequestBuilder.ts index e8e360ee95db..3e59391573c4 100644 --- a/src/libs/actions/IOU/MoneyRequestBuilder.ts +++ b/src/libs/actions/IOU/MoneyRequestBuilder.ts @@ -1684,11 +1684,11 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma chatReport, iouReport, getCurrencyDecimals, + delegateAccountID, comment, optimisticTransaction, undefined, optimisticReportPreviewActionID, - delegateAccountID, ); chatReport.lastVisibleActionCreated = reportPreviewAction.created; diff --git a/src/libs/actions/IOU/PerDiem.ts b/src/libs/actions/IOU/PerDiem.ts index efd38ff313c8..ca1417ac0d58 100644 --- a/src/libs/actions/IOU/PerDiem.ts +++ b/src/libs/actions/IOU/PerDiem.ts @@ -611,11 +611,11 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI chatReport, iouReport, getCurrencyDecimals, + delegateAccountID, comment, optimisticTransaction, undefined, optimisticReportPreviewActionID, - delegateAccountID, ); chatReport.lastVisibleActionCreated = reportPreviewAction.created; diff --git a/src/libs/actions/IOU/RejectMoneyRequest.ts b/src/libs/actions/IOU/RejectMoneyRequest.ts index c3126757bcc6..12665389c627 100644 --- a/src/libs/actions/IOU/RejectMoneyRequest.ts +++ b/src/libs/actions/IOU/RejectMoneyRequest.ts @@ -534,7 +534,7 @@ function prepareRejectMoneyRequestData({ delegateAccountIDParam: delegateAccountID, }); - reportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, newExpenseReport, getCurrencyDecimals, undefined, transaction, undefined, undefined, delegateAccountID); + reportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, newExpenseReport, getCurrencyDecimals, delegateAccountID, undefined, transaction); // The reject action posted below already tells the user this expense left the report, // so a MOVED_TRANSACTION action in the same thread would repeat it createdIOUReportActionID = iouAction.reportActionID; diff --git a/src/libs/actions/IOU/SendInvoice.ts b/src/libs/actions/IOU/SendInvoice.ts index b559eb2ab9f9..5c284553e861 100644 --- a/src/libs/actions/IOU/SendInvoice.ts +++ b/src/libs/actions/IOU/SendInvoice.ts @@ -737,16 +737,7 @@ function getSendInvoiceInformation({ } // STEP 5: Build optimistic reportActions. - const reportPreviewAction = buildOptimisticReportPreview( - chatReport, - optimisticInvoiceReport, - getCurrencyDecimals, - trimmedComment, - optimisticTransaction, - undefined, - undefined, - delegateAccountID, - ); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, optimisticInvoiceReport, getCurrencyDecimals, delegateAccountID, trimmedComment, optimisticTransaction); optimisticInvoiceReport.parentReportActionID = reportPreviewAction.reportActionID; chatReport.lastVisibleActionCreated = reportPreviewAction.created; const [optimisticCreatedActionForChat, optimisticCreatedActionForIOUReport, iouAction, optimisticTransactionThread, optimisticCreatedActionForTransactionThread] = diff --git a/src/libs/actions/IOU/SendMoney.ts b/src/libs/actions/IOU/SendMoney.ts index c485f862ef50..59846bb499c7 100644 --- a/src/libs/actions/IOU/SendMoney.ts +++ b/src/libs/actions/IOU/SendMoney.ts @@ -160,7 +160,7 @@ function getSendMoneyParams({ delegateAccountIDParam: delegateAccountID, }); - const reportPreviewAction = buildOptimisticReportPreview(chatReport, optimisticIOUReport, getCurrencyDecimals, undefined, undefined, undefined, undefined, delegateAccountID); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, optimisticIOUReport, getCurrencyDecimals, delegateAccountID); // Change the method to set for new reports because it doesn't exist yet, is faster, // and we need the data to be available when we navigate to the chat page diff --git a/src/libs/actions/IOU/Split.ts b/src/libs/actions/IOU/Split.ts index 9ea9f3c65239..a5a7b4082dc0 100644 --- a/src/libs/actions/IOU/Split.ts +++ b/src/libs/actions/IOU/Split.ts @@ -1148,16 +1148,7 @@ function completeSplitBill({ if (oneOnOneReportPreviewAction) { oneOnOneReportPreviewAction = updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction, getCurrencyDecimals); } else { - oneOnOneReportPreviewAction = buildOptimisticReportPreview( - oneOnOneChatReport, - oneOnOneIOUReport, - getCurrencyDecimals, - '', - oneOnOneTransaction, - undefined, - undefined, - delegateAccountID, - ); + oneOnOneReportPreviewAction = buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport, getCurrencyDecimals, delegateAccountID, '', oneOnOneTransaction); } const hasViolations = hasViolationsReportUtils(oneOnOneIOUReport.reportID, transactionViolations, sessionAccountID, sessionEmail ?? ''); @@ -1933,16 +1924,7 @@ function createSplitsAndOnyxData({ if (oneOnOneReportPreviewAction) { oneOnOneReportPreviewAction = updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction, getCurrencyDecimals); } else { - oneOnOneReportPreviewAction = buildOptimisticReportPreview( - oneOnOneChatReport, - oneOnOneIOUReport, - getCurrencyDecimals, - '', - oneOnOneTransaction, - undefined, - undefined, - delegateAccountID, - ); + oneOnOneReportPreviewAction = buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport, getCurrencyDecimals, delegateAccountID, '', oneOnOneTransaction); } const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories) : []; diff --git a/src/libs/actions/IOU/TrackExpense.ts b/src/libs/actions/IOU/TrackExpense.ts index 9c8f744d1d7b..63c924566019 100644 --- a/src/libs/actions/IOU/TrackExpense.ts +++ b/src/libs/actions/IOU/TrackExpense.ts @@ -1193,7 +1193,7 @@ function getTrackExpenseInformation(params: GetTrackExpenseInformationParams): T if (reportPreviewAction) { reportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, false, comment, optimisticTransaction); } else { - reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimals, comment, optimisticTransaction, undefined, undefined, delegateAccountID); + reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimals, delegateAccountID, comment, optimisticTransaction); // Generated ReportPreview action is a parent report action of the iou report. // We are setting the iou report's parentReportActionID to display subtitle correctly in IOU page when offline. iouReport.parentReportActionID = reportPreviewAction.reportActionID; diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 58c48ff940aa..383de0ef198d 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -7945,6 +7945,7 @@ function buildOptimisticChangePolicyData({ isTrackIntentUser, getCurrencyDecimals, rules, + delegateAccountID, }: { report: Report; parentReport: OnyxEntry; @@ -7961,6 +7962,7 @@ function buildOptimisticChangePolicyData({ isTrackIntentUser: boolean | undefined; getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; rules: OnyxCollection; + delegateAccountID: number | undefined; }) { const optimisticData: Array< OnyxUpdate< @@ -8192,8 +8194,7 @@ function buildOptimisticChangePolicyData({ // 3. Optimistically create a new REPORT_PREVIEW reportAction with the newReportPreviewActionID // and set it as a parent of the moved report const policyExpenseChat = optimisticPolicyExpenseChatReport ?? getPolicyExpenseChat(report.ownerAccountID, policy.id); - // TODO: delegateAccountIDParam will be threaded in PR 15 (https://github.com/Expensify/App/issues/66425) - const optimisticReportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, report, getCurrencyDecimals, '', null, undefined, undefined, undefined); + const optimisticReportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, report, getCurrencyDecimals, delegateAccountID, '', null); const newPolicyExpenseChatReportID = policyExpenseChat?.reportID; @@ -8247,7 +8248,7 @@ function buildOptimisticChangePolicyData({ }); // 4. Optimistically create a CHANGE_POLICY reportAction on the report using the reportActionID - const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(report.policyID, policy.id, currentUserAccountID); + const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(report.policyID, policy.id, currentUserAccountID, delegateAccountID); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, @@ -8445,6 +8446,7 @@ function changeReportPolicy({ getCurrencyDecimals, reportTransactions, rules, + delegateAccountID, }: { report: Report; parentReport: OnyxEntry; @@ -8462,6 +8464,7 @@ function changeReportPolicy({ getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; reportTransactions: Transaction[]; rules: OnyxCollection; + delegateAccountID: number | undefined; }) { if (!report || !policy || report.policyID === policy.id || !isExpenseReport(report) || shouldBlockChangeReportPolicyForMapOrGPSRequirement(reportTransactions, policy)) { return; @@ -8482,6 +8485,7 @@ function changeReportPolicy({ isTrackIntentUser, getCurrencyDecimals, rules, + delegateAccountID, }); const params = { @@ -8518,6 +8522,7 @@ function changeReportPolicyAndInviteSubmitter({ getCurrencyDecimals, reportTransactions, rules, + delegateAccountID, }: { report: Report; parentReport: OnyxEntry; @@ -8536,6 +8541,7 @@ function changeReportPolicyAndInviteSubmitter({ getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; reportTransactions: Transaction[]; rules: OnyxCollection; + delegateAccountID: number | undefined; }) { if ( !report.reportID || @@ -8599,6 +8605,7 @@ function changeReportPolicyAndInviteSubmitter({ isTrackIntentUser, getCurrencyDecimals, rules, + delegateAccountID, }); const optimisticData = [...optimisticAddMembersData, ...optimisticChangePolicyData]; diff --git a/src/pages/DynamicReportChangeWorkspacePage.tsx b/src/pages/DynamicReportChangeWorkspacePage.tsx index 6786d7f56d6a..a99070d05f0f 100644 --- a/src/pages/DynamicReportChangeWorkspacePage.tsx +++ b/src/pages/DynamicReportChangeWorkspacePage.tsx @@ -12,6 +12,7 @@ import useBlockDistanceRequest from '@hooks/useBlockDistanceRequest'; import {useCurrencyListActions} from '@hooks/useCurrencyList'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useDebouncedState from '@hooks/useDebouncedState'; +import useDelegateAccountID from '@hooks/useDelegateAccountID'; import useDynamicBackPath from '@hooks/useDynamicBackPath'; import {useIsAppLoadPending} from '@hooks/useInFlightRequests'; import useIsInLandscapeMode from '@hooks/useIsInLandscapeMode'; @@ -101,6 +102,7 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); const session = useSession(); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); + const delegateAccountID = useDelegateAccountID(); const hasViolations = hasViolationsReportUtils(report?.reportID, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? ''); const [ownerBillingGracePeriodEnd] = useOnyx(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END); const [userBillingGracePeriods] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END); @@ -201,6 +203,7 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace isTrackIntentUser, reportTransactions, rules, + delegateAccountID, }); refreshSearch(); return; @@ -223,6 +226,7 @@ function DynamicReportChangeWorkspacePage({report}: DynamicReportChangeWorkspace isTrackIntentUser, reportTransactions, rules, + delegateAccountID, }); refreshSearch(); }; diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 5e1f90560ae6..ad9c16cdd31e 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -3814,6 +3814,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: targetPolicy, currentUserAccountID: 1, @@ -3851,6 +3852,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: newPolicy, currentUserAccountID: 1, @@ -3914,6 +3916,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport, policy: newPolicy, currentUserAccountID: 1, @@ -3987,6 +3990,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: newPolicy, currentUserAccountID: 1, @@ -4086,6 +4090,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: newPolicy, currentUserAccountID: 1, @@ -4173,6 +4178,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: newPolicy, currentUserAccountID: 1, @@ -4228,6 +4234,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: createRandomPolicy(Number(2)), currentUser: {accountID: 1}, @@ -4321,6 +4328,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: newPolicy, currentUser: {accountID: 1}, @@ -4368,6 +4376,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: createRandomPolicy(Number(2)), currentUser: {accountID: 1}, @@ -4401,6 +4410,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: targetPolicy, currentUser: {accountID: 1}, @@ -4433,6 +4443,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: createRandomPolicy(Number(2)), currentUser: {accountID: 1}, @@ -4465,6 +4476,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: createRandomPolicy(Number(2)), currentUser: {accountID: 1}, @@ -4499,6 +4511,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: createRandomPolicy(Number(2)), currentUser: {accountID: 1}, @@ -4553,6 +4566,7 @@ describe('actions/Report', () => { report: expenseReport, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy: targetPolicy, currentUser: {accountID: 1, email: 'current-user@expensifail.com'}, @@ -5024,6 +5038,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy, currentUserAccountID: 1, @@ -5048,6 +5063,64 @@ describe('actions/Report', () => { }); }); + it('should set delegateAccountID on the optimistic report preview action', () => { + const delegateAccountID = 99; + const report: OnyxTypes.Report = { + ...createRandomReport(1, undefined), + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + type: CONST.REPORT.TYPE.EXPENSE, + }; + const policy = createRandomPolicy(Number(1)); + const {optimisticReportPreviewAction} = Report.buildOptimisticChangePolicyData({ + report, + getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, + rules: undefined, + delegateAccountID, + parentReport: undefined, + policy, + currentUserAccountID: 1, + currentUserEmail: '', + ownerLogin: undefined, + managerLogin: '', + hasViolationsParam: false, + isASAPSubmitBetaEnabled: true, + isReportLastVisibleArchived: undefined, + reportPreviewAction: undefined, + isTrackIntentUser: false, + }); + expect(optimisticReportPreviewAction.delegateAccountID).toBe(delegateAccountID); + }); + + // Without this the system message renders as the delegator until the API response arrives, + // then swaps to the copilot. + it('should set delegateAccountID on the optimistic change policy action', () => { + const delegateAccountID = 99; + const report: OnyxTypes.Report = { + ...createRandomReport(1, undefined), + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + type: CONST.REPORT.TYPE.EXPENSE, + }; + const policy = createRandomPolicy(Number(1)); + const {optimisticMovedReportAction} = Report.buildOptimisticChangePolicyData({ + report, + getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, + rules: undefined, + delegateAccountID, + parentReport: undefined, + policy, + currentUserAccountID: 1, + currentUserEmail: '', + ownerLogin: undefined, + managerLogin: '', + hasViolationsParam: false, + isASAPSubmitBetaEnabled: true, + isReportLastVisibleArchived: undefined, + reportPreviewAction: undefined, + isTrackIntentUser: false, + }); + expect(optimisticMovedReportAction.delegateAccountID).toBe(delegateAccountID); + }); + it('should set pendingAction and clear convertedAmount when moving to workspace with different currency', async () => { const reportID = 'testReport123'; const transactionID = 'testTransaction456'; @@ -5080,6 +5153,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy, currentUserAccountID: 1, @@ -5146,6 +5220,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy, currentUserAccountID: 1, @@ -5196,6 +5271,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy, currentUserAccountID: 1, @@ -5259,6 +5335,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport: undefined, policy, currentUserAccountID: 1, @@ -5325,6 +5402,7 @@ describe('actions/Report', () => { report, getCurrencyDecimals: TestHelper.getCurrencyDecimalsLocal, rules: undefined, + delegateAccountID: undefined, parentReport, policy, currentUserAccountID: 1, @@ -10758,14 +10836,29 @@ describe('actions/Report', () => { it('sets delegateAccountID when delegateAccountIDParam is provided', () => { const chatReport = createMock({reportID: 'chat1'}); const iouReport = createMock({reportID: 'iou1', ownerAccountID: 1, managerID: 2}); - const result = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, TestHelper.getCurrencyDecimalsLocal, '', null, undefined, undefined, DELEGATE_ACCOUNT_ID); + const result = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, TestHelper.getCurrencyDecimalsLocal, DELEGATE_ACCOUNT_ID, '', null); expect(result.delegateAccountID).toBe(DELEGATE_ACCOUNT_ID); }); it('does not set delegateAccountID when delegateAccountIDParam is undefined', () => { const chatReport = createMock({reportID: 'chat2'}); const iouReport = createMock({reportID: 'iou2', ownerAccountID: 1, managerID: 2}); - const result = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, TestHelper.getCurrencyDecimalsLocal, '', null, undefined, undefined, undefined); + const result = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, TestHelper.getCurrencyDecimalsLocal, undefined, '', null); + expect(result.delegateAccountID).toBeUndefined(); + }); + + // The builder used to read the signed-in delegate from Onyx when the caller passed nothing, + // which silently attributed the action to a copilot the caller never asked for. + it('does not fall back to the signed-in delegate when delegateAccountIDParam is undefined', async () => { + const delegateLogin = 'copilot@example.com'; + await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[DELEGATE_ACCOUNT_ID]: {accountID: DELEGATE_ACCOUNT_ID, login: delegateLogin}}); + await Onyx.merge(ONYXKEYS.ACCOUNT, {delegatedAccess: {delegate: delegateLogin}}); + await waitForBatchedUpdates(); + + const chatReport = createMock({reportID: 'chat3'}); + const iouReport = createMock({reportID: 'iou3', ownerAccountID: 1, managerID: 2}); + const result = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, TestHelper.getCurrencyDecimalsLocal, undefined, '', null); + expect(result.delegateAccountID).toBeUndefined(); }); }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 4d4330031087..bbcf3a069fa2 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -5632,6 +5632,7 @@ describe('ReportUtils', () => { chatReport, expenseReport, getCurrencyDecimalsLocal, + undefined, '', expenseTransaction, expenseReport.reportID, @@ -14280,7 +14281,7 @@ describe('ReportUtils', () => { managerID: 2, }; - const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal, undefined); expect(reportPreviewAction.childOwnerAccountID).toBe(iouReport.ownerAccountID); expect(reportPreviewAction.childManagerAccountID).toBe(iouReport.managerID); @@ -14302,7 +14303,7 @@ describe('ReportUtils', () => { managerID: 2, }; - const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal, undefined); const updatedPreviewAction = updateReportPreview( iouReport, reportPreviewAction, @@ -23300,7 +23301,7 @@ describe('ReportUtils', () => { }; // REPORT_PREVIEW action that sits in the chat report and links to the expense report - const reportPreviewAction = buildOptimisticReportPreview(chatReport, expenseReport, getCurrencyDecimalsLocal, '', transaction); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, expenseReport, getCurrencyDecimalsLocal, undefined, '', transaction); beforeAll(async () => { await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID}); diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index 1fc63d900db9..d142a4479a30 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -934,7 +934,7 @@ describe('SidebarUtils', () => { participantAccountIDs: [actorAccountID], }, }; - const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal, '', transaction); + const reportPreviewAction = buildOptimisticReportPreview(chatReport, iouReport, getCurrencyDecimalsLocal, undefined, '', transaction); await act(async () => { await Onyx.set(ONYXKEYS.SESSION, {accountID: 55555});