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: 7 additions & 10 deletions src/pages/workspace/companyCards/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ExportAccountOption = {
/** Text shown for this account */
label: string;

/** Option key, which QBO and QBD key by label because they stored display names before they stored ids */
/** Option key. Always the id, because an integration can expose two accounts with the same display name */
keyForList: string;
};

Expand Down Expand Up @@ -135,12 +135,9 @@ type CardExportAccountSelection = {
programAccountID?: string;
};

/**
* Puts an integration's accounts into the shared shape. QBO and QBD keyed their options by display name before ids
* were stored, so those two pass `shouldKeyByLabel`.
*/
function normalizeAccounts(accounts: Array<{id: string; name: string}> | undefined, shouldKeyByLabel = false): ExportAccountOption[] {
return (accounts ?? []).map(({id, name}) => ({id, label: name, keyForList: shouldKeyByLabel ? name : id}));
/** Puts an integration's accounts into the shared shape. */
function normalizeAccounts(accounts: Array<{id: string; name: string}> | undefined): ExportAccountOption[] {
return (accounts ?? []).map(({id, name}) => ({id, label: name, keyForList: id}));
}

/**
Expand Down Expand Up @@ -257,7 +254,7 @@ function getPolicyCardExportSettings(
accountSelection: {
type: CONST.COMPANY_CARDS.EXPORT_RESOLVER.SINGLE_ACCOUNT,
nvpKey: CONST.COMPANY_CARDS.EXPORT_CARD_TYPES.NVP_QUICKBOOKS_ONLINE_EXPORT_ACCOUNT,
accounts: normalizeAccounts(creditCards, true),
accounts: normalizeAccounts(creditCards),
defaultLabel,
workspaceDefaultAccountID,
},
Expand All @@ -271,7 +268,7 @@ function getPolicyCardExportSettings(
accountSelection: {
type: CONST.COMPANY_CARDS.EXPORT_RESOLVER.SINGLE_ACCOUNT,
nvpKey: CONST.COMPANY_CARDS.EXPORT_CARD_TYPES.NVP_QUICKBOOKS_ONLINE_EXPORT_ACCOUNT_DEBIT,
accounts: normalizeAccounts(quickbooksOnlineBankAccounts, true),
accounts: normalizeAccounts(quickbooksOnlineBankAccounts),
defaultLabel,
workspaceDefaultAccountID,
},
Expand Down Expand Up @@ -420,7 +417,7 @@ function getPolicyCardExportSettings(
accountSelection: {
type: CONST.COMPANY_CARDS.EXPORT_RESOLVER.SINGLE_ACCOUNT,
nvpKey: CONST.COMPANY_CARDS.EXPORT_CARD_TYPES.NVP_QUICKBOOKS_DESKTOP_EXPORT_ACCOUNT_CREDIT,
accounts: normalizeAccounts(creditCardAccounts, true),
accounts: normalizeAccounts(creditCardAccounts),
defaultLabel: getDefaultExportLabel(qbdConfig),
// Classic saved an account id in this NVP while NewDot used to save the display name, so a
// missed id match is retried against the labels.
Expand Down
26 changes: 23 additions & 3 deletions tests/unit/CompanyCardExportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const QBD_CREDIT_CARD_ACCOUNTS = [
{id: '80000104-1746639411', name: 'Visa Business (92000)', currency: 'USD'},
];

function createQBDPolicy(overrides?: Partial<Policy>, nonReimbursableAccount = '80000103-1746639410'): Policy {
function createQBDPolicy(overrides?: Partial<Policy>, nonReimbursableAccount = '80000103-1746639410', creditCardAccounts = QBD_CREDIT_CARD_ACCOUNTS): Policy {
return createMock<Policy>({
id: MOCK_POLICY_ID,
name: 'Test Policy',
Expand All @@ -72,7 +72,7 @@ function createQBDPolicy(overrides?: Partial<Policy>, nonReimbursableAccount = '
},
},
data: {
creditCardAccounts: QBD_CREDIT_CARD_ACCOUNTS,
creditCardAccounts,
},
},
},
Expand Down Expand Up @@ -174,6 +174,26 @@ describe('getExportMenuItem - QBD credit card account resolution', () => {
expect(selectedOption?.text).toBe(defaultCard);
});

it('keys each option by id when two accounts share a display name', () => {
Comment thread
NicolasBonet marked this conversation as resolved.
Resolved
// Given a QuickBooks Desktop chart of accounts holding two credit card accounts under the same display name,
// which QBD allows whenever they sit under different parents
const duplicateNameAccounts = [
{id: '80000105-1746639412', name: 'Corporate Card', currency: 'USD'},
{id: '80000106-1746639413', name: 'Corporate Card', currency: 'USD'},
];
const policy = createQBDPolicy(undefined, '80000105-1746639412', duplicateNameAccounts);
const card = createCard('80000106-1746639413');

// When the export account options are built for a card pointing at the second of those two accounts
const result = getExportMenuItem(CONST.POLICY.CONNECTIONS.NAME.QBD, MOCK_POLICY_ID, translate, themeStyles, policy, card);

// Then every option carries a distinct key, because rows sharing one key make the list drop one of them and
// leave that account impossible to pick
const keys = result?.data?.map((item) => item.keyForList) ?? [];
expect(keys).toHaveLength(duplicateNameAccounts.length + 1);
expect(new Set(keys).size).toBe(keys.length);
});

it('uses card.id (not card.name) as the option value for all items', () => {
const policy = createQBDPolicy();
const card = createCard('80000103-1746639410');
Expand Down Expand Up @@ -333,7 +353,7 @@ describe('getExportMenuItem - QBO', () => {
const selectedOption = result?.data?.find((item) => item.isSelected);
expect(selectedOption?.value).toBe('qbo-cc-1');
expect(selectedOption?.text).toBe('Amex Corporate');
expect(selectedOption?.keyForList).toBe('Amex Corporate');
expect(selectedOption?.keyForList).toBe('qbo-cc-1');
});

it('resolves a debit card export against the bank account list and its own NVP', () => {
Expand Down
Loading