From 949623c1e2ef57a7c17c7ef443798c23647cfafc Mon Sep 17 00:00:00 2001 From: Mukher Date: Fri, 18 Sep 2026 15:47:37 +0500 Subject: [PATCH] Read the CSV card identity from the mapped column instead of the row's last cell buildOptimisticCompanyCardCSVTransactions looked up the Card number column only, so with that column unmapped the index was -1 and row.at(-1) read whatever cell sat last in the row. With Unique ID mapped, that is the last optional column the user chose (Tag, Description, Category); a blank cell there made the row fail the identity check, drop out of the success count, and skip the yyyy-MM-dd date normalization even though it was still sent. Resolve the identity column once, Card number then Card name, and read it only when it resolved. --- src/libs/actions/CompanyCards.ts | 4 ++- tests/actions/CompanyCardsImportTest.ts | 38 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/CompanyCards.ts b/src/libs/actions/CompanyCards.ts index b91de34f42f8..7dfcf1479c01 100644 --- a/src/libs/actions/CompanyCards.ts +++ b/src/libs/actions/CompanyCards.ts @@ -119,6 +119,8 @@ function buildOptimisticCompanyCardCSVTransactions( const externalIDColumnIndex = isExternalIDColumnMapped ? mappedExternalIDColumnIndex : normalizedColumnMappings.length - 1; const cardNumberColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.CARD_NUMBER); + const cardNameColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.CARD_NAME); + const cardIdentityColumnIndex = cardNumberColumnIndex >= 0 ? cardNumberColumnIndex : cardNameColumnIndex; const postedDateColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.POSTED_DATE); const originalTransactionDateColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.ORIGINAL_TRANSACTION_DATE); const merchantColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.MERCHANT); @@ -137,7 +139,7 @@ function buildOptimisticCompanyCardCSVTransactions( row[externalIDColumnIndex] = transactionID; } - const cardName = row.at(cardNumberColumnIndex)?.trim(); + const cardName = cardIdentityColumnIndex >= 0 ? row.at(cardIdentityColumnIndex)?.trim() : undefined; const rawPostedDate = row.at(postedDateColumnIndex)?.trim(); const created = rawPostedDate ? parseCSVDate(rawPostedDate) : null; const merchant = row.at(merchantColumnIndex)?.trim() ?? ''; diff --git a/tests/actions/CompanyCardsImportTest.ts b/tests/actions/CompanyCardsImportTest.ts index f1d4a26c34a0..7c7b74d7bdd4 100644 --- a/tests/actions/CompanyCardsImportTest.ts +++ b/tests/actions/CompanyCardsImportTest.ts @@ -111,6 +111,44 @@ describe('actions/CompanyCards importCSVCompanyCards', () => { apiWriteSpy.mockRestore(); }); + it('counts every row that has a card identity when the Card number column is not mapped', () => { + // Given an import that maps Card name (not Card number) plus a mapped Unique ID and a trailing optional column + const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve()); + + // When rows leave that trailing optional column empty + importCSVCompanyCards({ + policyID: POLICY_ID, + domainAccountID: DOMAIN_ACCOUNT_ID, + layoutName: 'My Layout', + layoutType: CSV_FEED, + columnMappings: ['externalID', 'cardName', 'postedDate', 'merchant', 'amount', 'currency', 'tag'], + csvData: [ + ['Unique ID', 'Card Name', 'Date', 'Merchant', 'Amount', 'Currency', 'Tag'], + ['txn-1', 'Finance Card', '08/05/2026', 'Coffee Shop', '-6.25', 'USD', ''], + ['txn-2', 'Finance Card', '08/06/2026', 'Book Store', '-14.00', 'USD', ''], + ['txn-3', 'Travel Card', '08/07/2026', 'Grocery Mart', '-28.40', 'USD', 'Travel'], + ], + workspaceCardFeeds: undefined, + }); + + // Then all three rows are counted, because the card identity comes from Card name rather than the last cell in the row + expect(apiWriteSpy).toHaveBeenCalledWith( + WRITE_COMMANDS.IMPORT_CSV_COMPANY_CARDS, + expect.anything(), + expect.objectContaining({ + successData: [ + expect.objectContaining({ + value: expect.objectContaining({ + importFinalModal: expect.objectContaining({promptKeyParams: {count: 3}}), + }), + }), + ], + }), + ); + + apiWriteSpy.mockRestore(); + }); + describe('externalID', () => { const sentImports: ImportCSVCompanyCardsParams[] = [];