Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/libs/actions/IOU/SendInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -79,7 +80,7 @@ type SendInvoiceInformation = {

type SendInvoiceOptions = {
transaction: OnyxEntry<OnyxTypes.Transaction>;
currentUserAccountID: number;
currentUserPersonalDetails: CurrentUserPersonalDetails;
Comment thread
parasharrajat marked this conversation as resolved.
Resolved
policyRecentlyUsedCurrencies: string[];
invoiceChatReport?: OnyxEntry<OnyxTypes.Report>;
invoiceChatReportID?: string;
Expand Down Expand Up @@ -127,6 +128,7 @@ type BuildOnyxDataForInvoiceParams = {
companyName?: string;
companyWebsite?: string;
participant?: Participant;
currentUserPersonalDetails: CurrentUserPersonalDetails;
};

/** Builds the Onyx data for an invoice */
Expand All @@ -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]));
Expand Down Expand Up @@ -580,6 +582,7 @@ function buildOnyxDataForInvoice(
const searchUpdate = getSearchOnyxUpdate({
transaction,
participant,
currentUserPersonalDetails,
iouReport: iou.report,
iouAction: iou.action,
policy: policyParams.policy,
Expand Down Expand Up @@ -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,
Expand All @@ -638,6 +641,7 @@ function getSendInvoiceInformation({
delegateAccountID,
getCurrencyDecimals,
}: SendInvoiceOptions): SendInvoiceInformation {
const currentUserAccountID = currentUserPersonalDetails.accountID;
const {
amount = 0,
currency = '',
Expand Down Expand Up @@ -767,6 +771,7 @@ function getSendInvoiceInformation({
policyRecentlyUsedTags: optimisticPolicyRecentlyUsedTags,
},
participant: receiver,
currentUserPersonalDetails,
companyName,
companyWebsite,
});
Expand All @@ -788,7 +793,7 @@ function getSendInvoiceInformation({
}

function sendInvoice({
currentUserAccountID,
currentUserPersonalDetails,
transaction,
policyRecentlyUsedCurrencies,
invoiceChatReport,
Expand Down Expand Up @@ -828,7 +833,7 @@ function sendInvoice({
onyxData,
} = getSendInvoiceInformation({
transaction,
currentUserAccountID,
currentUserPersonalDetails,
policyRecentlyUsedCurrencies,
invoiceChatReportID,
invoiceChatReport,
Expand All @@ -851,7 +856,7 @@ function sendInvoice({
createdReportActionIDForThread,
reportActionID,
senderWorkspaceID,
accountID: currentUserAccountID,
accountID: currentUserPersonalDetails.accountID,
amount: transaction?.amount ?? 0,
currency: transaction?.currency ?? '',
comment: parsedComment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function useInvoiceSubmission({
sendInvoice({
getCurrencyDecimals,
writeBarrier,
currentUserAccountID: currentUserPersonalDetails.accountID,
currentUserPersonalDetails,
transaction,
policyRecentlyUsedCurrencies,
invoiceChatReport,
Expand Down
100 changes: 81 additions & 19 deletions tests/actions/IOUTest/SendInvoiceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -288,7 +289,7 @@ describe('actions/SendInvoice', () => {
// When: Call getSendInvoiceInformation
const result = getSendInvoiceInformation({
transaction: baseTransaction,
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: undefined,
receiptFile: undefined,
Expand Down Expand Up @@ -337,7 +338,7 @@ describe('actions/SendInvoice', () => {

const result = getSendInvoiceInformation({
transaction: baseTransaction,
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: undefined,
receiptFile: undefined,
Expand Down Expand Up @@ -378,7 +379,7 @@ describe('actions/SendInvoice', () => {

const result = getSendInvoiceInformation({
transaction,
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: existingInvoiceChatReportFixture,
receiptFile: undefined,
Expand Down Expand Up @@ -416,7 +417,7 @@ describe('actions/SendInvoice', () => {

const result = getSendInvoiceInformation({
transaction: baseTransaction,
currentUserAccountID: 123,
currentUserPersonalDetails: {accountID: 123},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: undefined,
receiptFile: undefined,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -582,7 +583,7 @@ describe('actions/SendInvoice', () => {

const result = getSendInvoiceInformation({
transaction: baseTransaction,
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: undefined,
invoiceChatReportID: preGeneratedReportID,
Expand Down Expand Up @@ -635,7 +636,7 @@ describe('actions/SendInvoice', () => {

const result = getSendInvoiceInformation({
transaction: baseTransaction,
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
policyRecentlyUsedCurrencies: [],
invoiceChatReport: existingInvoiceChatReport,
invoiceChatReportID: preGeneratedReportID,
Expand Down Expand Up @@ -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 ?? {},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -781,7 +782,7 @@ describe('actions/SendInvoice', () => {

// When the user sends a new invoice to an individual
sendInvoice({
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
Comment thread
parasharrajat marked this conversation as resolved.
Resolved
transaction,
policyRecentlyUsedCurrencies: [],
policy,
Expand Down Expand Up @@ -828,7 +829,7 @@ describe('actions/SendInvoice', () => {
});

sendInvoice({
currentUserAccountID: 123,
currentUserPersonalDetails: {accountID: 123},
transaction,
policyRecentlyUsedCurrencies: [],
senderPolicyTags: undefined,
Expand Down Expand Up @@ -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<Transaction>({
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 = {
Expand All @@ -868,7 +930,7 @@ describe('actions/SendInvoice', () => {

mockFetch.pause();
sendInvoice({
currentUserAccountID: 1,
currentUserPersonalDetails: {accountID: 1},
transaction,
policyRecentlyUsedCurrencies: initialCurrencies,
senderPolicyTags: undefined,
Expand Down Expand Up @@ -909,7 +971,7 @@ describe('actions/SendInvoice', () => {

// When sending an invoice
sendInvoice({
currentUserAccountID,
currentUserPersonalDetails: {accountID: currentUserAccountID},
transaction,
policyRecentlyUsedCurrencies: [],
policyRecentlyUsedCategories,
Expand Down Expand Up @@ -948,7 +1010,7 @@ describe('actions/SendInvoice', () => {
};
// When sending an invoice
sendInvoice({
currentUserAccountID: 1,
currentUserPersonalDetails: {accountID: 1},
transaction,
policyRecentlyUsedCurrencies: [],
policyRecentlyUsedTags,
Expand Down Expand Up @@ -993,7 +1055,7 @@ describe('actions/SendInvoice', () => {
const apiSpies = spyOnApiWrites(writeSpy);

sendInvoice({
currentUserAccountID: 123,
currentUserPersonalDetails: {accountID: 123},
transaction,
policyRecentlyUsedCurrencies: [],
invoiceChatReportID: preGeneratedReportID,
Expand Down
Loading