diff --git a/src/libs/API/parameters/SetCompanyCardTransactionLiability.ts b/src/libs/API/parameters/SetCompanyCardTransactionLiability.ts index 92b7af078dd7..650eeb962fbd 100644 --- a/src/libs/API/parameters/SetCompanyCardTransactionLiability.ts +++ b/src/libs/API/parameters/SetCompanyCardTransactionLiability.ts @@ -1,5 +1,7 @@ type SetCompanyCardTransactionLiability = { + policyID: string; bankName: string; + domainAccountID: number; liabilityType: string; }; diff --git a/src/libs/actions/CompanyCards.ts b/src/libs/actions/CompanyCards.ts index b91de34f42f8..a402b12f3d9b 100644 --- a/src/libs/actions/CompanyCards.ts +++ b/src/libs/actions/CompanyCards.ts @@ -345,6 +345,7 @@ function setWorkspaceCompanyCardTransactionLiability(domainOrWorkspaceAccountID: const parameters = { policyID, + domainAccountID: domainOrWorkspaceAccountID, bankName, liabilityType, }; diff --git a/tests/actions/CompanyCardsLiabilityTest.ts b/tests/actions/CompanyCardsLiabilityTest.ts new file mode 100644 index 000000000000..b2657f7c980c --- /dev/null +++ b/tests/actions/CompanyCardsLiabilityTest.ts @@ -0,0 +1,41 @@ +import {setWorkspaceCompanyCardTransactionLiability} from '@libs/actions/CompanyCards'; +import {WRITE_COMMANDS} from '@libs/API/types'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; + +const POLICY_ID = 'A1B2C3D4E5F60718'; +const DOMAIN_ACCOUNT_ID = 1234567; +const FEED = CONST.COMPANY_CARD.FEED_BANK_NAME.VISA; + +describe('actions/CompanyCards setWorkspaceCompanyCardTransactionLiability', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('targets the feed-owning domain account when restricting deletion on a domain feed', () => { + // Given a domain feed, whose settings live on the +@domain account rather than the workspace account + const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve()); + + // When an admin turns off deleting transactions for that feed + setWorkspaceCompanyCardTransactionLiability(DOMAIN_ACCOUNT_ID, POLICY_ID, FEED, CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.RESTRICT); + + // Then the domainAccountID is forwarded to the backend, so the setting is written to the correct account, and the optimistic Onyx write targets that same account. + expect(apiWriteSpy).toHaveBeenCalledWith( + WRITE_COMMANDS.SET_COMPANY_CARD_TRANSACTION_LIABILITY, + expect.objectContaining({ + policyID: POLICY_ID, + bankName: FEED, + liabilityType: CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.RESTRICT, + domainAccountID: DOMAIN_ACCOUNT_ID, + }), + expect.objectContaining({ + optimisticData: expect.arrayContaining([ + expect.objectContaining({ + key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${DOMAIN_ACCOUNT_ID}`, + }), + ]), + }), + ); + }); +}); diff --git a/tests/unit/CardUtilsTest.ts b/tests/unit/CardUtilsTest.ts index 2f5a5cfee9a0..e3a7e5b2958b 100644 --- a/tests/unit/CardUtilsTest.ts +++ b/tests/unit/CardUtilsTest.ts @@ -45,6 +45,7 @@ import { getDisplayableExpensifyCards, getDisplayableThirdPartyCards, getDomainByFundID, + getDomainOrWorkspaceAccountID, getEligibleBankAccountsForCard, getEligibleBankAccountsForUkEuCard, getFeedNameForDisplay, @@ -1412,6 +1413,23 @@ describe('CardUtils', () => { }); }); + describe('getDomainOrWorkspaceAccountID', () => { + it('Should return the domain account that owns a domain feed', () => { + const accountID = getDomainOrWorkspaceAccountID(7654321, {domainID: 1234567}); + expect(accountID).toBe(1234567); + }); + + it('Should return the workspace account when the feed has no domain', () => { + const accountID = getDomainOrWorkspaceAccountID(7654321, {}); + expect(accountID).toBe(7654321); + }); + + it('Should return the workspace account when there is no feed data', () => { + const accountID = getDomainOrWorkspaceAccountID(7654321, undefined); + expect(accountID).toBe(7654321); + }); + }); + describe('getCustomOrFormattedFeedName', () => { beforeAll(() => { IntlStore.load(CONST.LOCALES.EN);