diff --git a/src/libs/actions/IOU/SendInvoice.ts b/src/libs/actions/IOU/SendInvoice.ts index a7d479a61134..98af0f5aaaa1 100644 --- a/src/libs/actions/IOU/SendInvoice.ts +++ b/src/libs/actions/IOU/SendInvoice.ts @@ -32,6 +32,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; +import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails'; import type {InvoiceReceiver, InvoiceReceiverType} from '@src/types/onyx/Report'; import type {OnyxData} from '@src/types/onyx/Request'; import type {Receipt} from '@src/types/onyx/Transaction'; @@ -79,7 +80,7 @@ type SendInvoiceInformation = { type SendInvoiceOptions = { transaction: OnyxEntry; - currentUserAccountID: number; + currentUserPersonalDetails: CurrentUserPersonalDetails; policyRecentlyUsedCurrencies: string[]; invoiceChatReport?: OnyxEntry; invoiceChatReportID?: string; @@ -127,6 +128,7 @@ type BuildOnyxDataForInvoiceParams = { companyName?: string; companyWebsite?: string; participant?: Participant; + currentUserPersonalDetails: CurrentUserPersonalDetails; }; /** Builds the Onyx data for an invoice */ @@ -146,7 +148,7 @@ function buildOnyxDataForInvoice( | typeof ONYXKEYS.COLLECTION.SNAPSHOT | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE > { - const {chat, iou, transactionParams, policyParams, optimisticData: optimisticDataParams, companyName, companyWebsite, participant} = invoiceParams; + const {chat, iou, transactionParams, policyParams, optimisticData: optimisticDataParams, companyName, companyWebsite, participant, currentUserPersonalDetails} = invoiceParams; const transaction = transactionParams.transaction; const clearedPendingFields = Object.fromEntries(Object.keys(transactionParams.transaction.pendingFields ?? {}).map((key) => [key, null])); @@ -580,6 +582,7 @@ function buildOnyxDataForInvoice( const searchUpdate = getSearchOnyxUpdate({ transaction, participant, + currentUserPersonalDetails, iouReport: iou.report, iouAction: iou.action, policy: policyParams.policy, @@ -621,7 +624,7 @@ function getReceiverType(receiverParticipant: Participant | InvoiceReceiver | un /** Gathers all the data needed to create an invoice. */ function getSendInvoiceInformation({ transaction, - currentUserAccountID, + currentUserPersonalDetails, policyRecentlyUsedCurrencies, invoiceChatReport, invoiceChatReportID, @@ -638,6 +641,7 @@ function getSendInvoiceInformation({ delegateAccountID, getCurrencyDecimals, }: SendInvoiceOptions): SendInvoiceInformation { + const currentUserAccountID = currentUserPersonalDetails.accountID; const { amount = 0, currency = '', @@ -767,6 +771,7 @@ function getSendInvoiceInformation({ policyRecentlyUsedTags: optimisticPolicyRecentlyUsedTags, }, participant: receiver, + currentUserPersonalDetails, companyName, companyWebsite, }); @@ -788,7 +793,7 @@ function getSendInvoiceInformation({ } function sendInvoice({ - currentUserAccountID, + currentUserPersonalDetails, transaction, policyRecentlyUsedCurrencies, invoiceChatReport, @@ -828,7 +833,7 @@ function sendInvoice({ onyxData, } = getSendInvoiceInformation({ transaction, - currentUserAccountID, + currentUserPersonalDetails, policyRecentlyUsedCurrencies, invoiceChatReportID, invoiceChatReport, @@ -851,7 +856,7 @@ function sendInvoice({ createdReportActionIDForThread, reportActionID, senderWorkspaceID, - accountID: currentUserAccountID, + accountID: currentUserPersonalDetails.accountID, amount: transaction?.amount ?? 0, currency: transaction?.currency ?? '', comment: parsedComment, diff --git a/src/pages/iou/request/step/DynamicIOURequestStepCompanyInfo.tsx b/src/pages/iou/request/step/DynamicIOURequestStepCompanyInfo.tsx index faab6fb1f513..a32e9aef65e2 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepCompanyInfo.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepCompanyInfo.tsx @@ -113,7 +113,7 @@ function DynamicIOURequestStepCompanyInfo({route, report, transaction}: DynamicI const draftTransactionIDs = validTransactionDraftIDsSelector(transactionDrafts); sendInvoice({ getCurrencyDecimals, - currentUserAccountID: currentUserPersonalDetails.accountID, + currentUserPersonalDetails, transaction, policyRecentlyUsedCurrencies: policyRecentlyUsedCurrencies ?? [], invoiceChatReport: report, diff --git a/src/pages/iou/request/step/confirmation/submission/useInvoiceSubmission.ts b/src/pages/iou/request/step/confirmation/submission/useInvoiceSubmission.ts index 315445cbaadf..c80a4b1c3e39 100644 --- a/src/pages/iou/request/step/confirmation/submission/useInvoiceSubmission.ts +++ b/src/pages/iou/request/step/confirmation/submission/useInvoiceSubmission.ts @@ -73,7 +73,7 @@ function useInvoiceSubmission({ sendInvoice({ getCurrencyDecimals, writeBarrier, - currentUserAccountID: currentUserPersonalDetails.accountID, + currentUserPersonalDetails, transaction, policyRecentlyUsedCurrencies, invoiceChatReport, diff --git a/tests/actions/IOUTest/SendInvoiceTest.ts b/tests/actions/IOUTest/SendInvoiceTest.ts index 5f5ff14723c3..6bec6a152526 100644 --- a/tests/actions/IOUTest/SendInvoiceTest.ts +++ b/tests/actions/IOUTest/SendInvoiceTest.ts @@ -4,6 +4,7 @@ import type {RenderAPI} from '@testing-library/react-native'; import {getReceiverType, getSendInvoiceInformation, sendInvoice} from '@libs/actions/IOU/SendInvoice'; import initOnyxDerivedValues from '@libs/actions/OnyxDerived'; import {WRITE_COMMANDS} from '@libs/API/types'; +import {buildSearchQueryJSON} from '@libs/SearchQueryUtils'; import CONST from '@src/CONST'; import IntlStore from '@src/languages/IntlStore'; @@ -206,7 +207,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation with policyRecentlyUsedCategories const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: undefined, @@ -234,7 +235,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction: {...baseTransaction, currency: CONST.CURRENCY.EUR}, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: initialCurrencies, invoiceChatReport: undefined, receiptFile: undefined, @@ -288,7 +289,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: undefined, @@ -337,7 +338,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: undefined, @@ -378,7 +379,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: existingInvoiceChatReportFixture, receiptFile: undefined, @@ -416,7 +417,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID: 123, + currentUserPersonalDetails: {accountID: 123}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: undefined, @@ -459,7 +460,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation with existing chat report const result = getSendInvoiceInformation({ transaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: existingInvoiceChatReportFixture, receiptFile: undefined, @@ -498,7 +499,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation with receipt const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: mockReceipt, @@ -550,7 +551,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation with minimal data const result = getSendInvoiceInformation({ transaction: mockTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, receiptFile: undefined, @@ -582,7 +583,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: undefined, invoiceChatReportID: preGeneratedReportID, @@ -635,7 +636,7 @@ describe('actions/SendInvoice', () => { const result = getSendInvoiceInformation({ transaction: baseTransaction, - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, policyRecentlyUsedCurrencies: [], invoiceChatReport: existingInvoiceChatReport, invoiceChatReportID: preGeneratedReportID, @@ -699,7 +700,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation with senderPolicyTags read from Onyx const result = getSendInvoiceInformation({ transaction: mockTransaction, - currentUserAccountID: 123, + currentUserPersonalDetails: {accountID: 123}, policyRecentlyUsedCurrencies: [], policyRecentlyUsedTags, senderPolicyTags: senderPolicyTags ?? {}, @@ -750,7 +751,7 @@ describe('actions/SendInvoice', () => { // When: Call getSendInvoiceInformation without a tag on the transaction const result = getSendInvoiceInformation({ transaction: mockTransaction, - currentUserAccountID: 123, + currentUserPersonalDetails: {accountID: 123}, policyRecentlyUsedCurrencies: [], senderPolicyTags: senderPolicyTags ?? {}, formatPhoneNumber, @@ -781,7 +782,7 @@ describe('actions/SendInvoice', () => { // When the user sends a new invoice to an individual sendInvoice({ - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, transaction, policyRecentlyUsedCurrencies: [], policy, @@ -828,7 +829,7 @@ describe('actions/SendInvoice', () => { }); sendInvoice({ - currentUserAccountID: 123, + currentUserPersonalDetails: {accountID: 123}, transaction, policyRecentlyUsedCurrencies: [], senderPolicyTags: undefined, @@ -857,6 +858,67 @@ describe('actions/SendInvoice', () => { } }); + it('writes the passed sender details into the invoice search snapshot', async () => { + // Given a loaded type:invoice snapshot, so getSearchOnyxUpdate has a recorded query to patch + const queryJSON = buildSearchQueryJSON('type:invoice'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.SNAPSHOT}${queryJSON?.hash}`, {search: {inputQuery: 'type:invoice'}}); + await waitForBatchedUpdates(); + + const writeSpy = jest.fn(); + const apiSpies = spyOnApiWrites(writeSpy); + + // And sender details that differ from what Onyx holds for the session user, so the Onyx fallback can't satisfy the assertion + const senderAccountID = 32; + const currentUserPersonalDetails = {accountID: senderAccountID, displayName: 'Sender', login: 'sender@test.com', avatar: 'https://avatar.png'}; + const transaction = createMock({ + transactionID: 'transaction_sender_snapshot', + reportID: 'report_sender_snapshot', + amount: 100, + currency: 'USD', + created: '2024-02-01', + merchant: 'Sender Invoice', + participants: [ + {accountID: senderAccountID, isSender: true, policyID: 'workspace_test'}, + {accountID: 456, isSender: false, login: 'receiver@test.com'}, + ], + }); + + // When the invoice is sent + sendInvoice({ + currentUserPersonalDetails, + transaction, + policyRecentlyUsedCurrencies: [], + senderPolicyTags: undefined, + formatPhoneNumber, + delegateAccountID: undefined, + getCurrencyDecimals: getCurrencyDecimalsLocal, + }); + + // Then the snapshot gets the sender from the passed details, not from the Onyx fallback + expect(writeSpy).toHaveBeenCalledWith( + WRITE_COMMANDS.SEND_INVOICE, + expect.anything(), + expect.objectContaining({ + optimisticData: expect.arrayContaining([ + expect.objectContaining({ + key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${queryJSON?.hash}`, + value: expect.objectContaining({ + data: expect.objectContaining({ + [ONYXKEYS.PERSONAL_DETAILS_LIST]: expect.objectContaining({ + [senderAccountID]: expect.objectContaining({displayName: 'Sender', avatar: 'https://avatar.png', login: 'sender@test.com'}), + }), + }), + }), + }), + ]), + }), + ); + + for (const spy of apiSpies) { + spy.mockRestore(); + } + }); + it('should not clear transaction pending action when send invoice fails', async () => { const testCurrency = CONST.CURRENCY.EUR; const transaction: Transaction = { @@ -868,7 +930,7 @@ describe('actions/SendInvoice', () => { mockFetch.pause(); sendInvoice({ - currentUserAccountID: 1, + currentUserPersonalDetails: {accountID: 1}, transaction, policyRecentlyUsedCurrencies: initialCurrencies, senderPolicyTags: undefined, @@ -909,7 +971,7 @@ describe('actions/SendInvoice', () => { // When sending an invoice sendInvoice({ - currentUserAccountID, + currentUserPersonalDetails: {accountID: currentUserAccountID}, transaction, policyRecentlyUsedCurrencies: [], policyRecentlyUsedCategories, @@ -948,7 +1010,7 @@ describe('actions/SendInvoice', () => { }; // When sending an invoice sendInvoice({ - currentUserAccountID: 1, + currentUserPersonalDetails: {accountID: 1}, transaction, policyRecentlyUsedCurrencies: [], policyRecentlyUsedTags, @@ -993,7 +1055,7 @@ describe('actions/SendInvoice', () => { const apiSpies = spyOnApiWrites(writeSpy); sendInvoice({ - currentUserAccountID: 123, + currentUserPersonalDetails: {accountID: 123}, transaction, policyRecentlyUsedCurrencies: [], invoiceChatReportID: preGeneratedReportID,