diff --git a/src/libs/CSVDateUtils.ts b/src/libs/CSVDateUtils.ts index a06bb4fc5db6..ddf5759eb242 100644 --- a/src/libs/CSVDateUtils.ts +++ b/src/libs/CSVDateUtils.ts @@ -1,10 +1,8 @@ import {addDays, format, isValid, parse} from 'date-fns'; import CONST from '@src/CONST'; -/** - * Common date formats to try when parsing CSV dates. Order matters: more - * specific / more common formats are tried first. - */ +// Common date formats to try when parsing CSV dates +// Order matters - more specific/common formats first const CSV_DATE_FORMATS = [ 'yyyy-MM-dd', // ISO format: 2025-11-02 'MM/dd/yyyy', // US format: 11/02/2025 @@ -22,13 +20,7 @@ const CSV_DATE_FORMATS = [ ]; /** - * Parses a date string from a variety of common CSV formats and returns it in - * `yyyy-MM-dd`. Returns null when the input cannot be parsed as a date. - * - * The yyyy-MM-dd format is tried via `date-fns.parse` before falling back to - * the native Date constructor so that bare ISO dates (e.g. `2024-01-15`) are - * interpreted in the local timezone instead of being shifted by UTC midnight - * in zones west of UTC. + * Parses a date string from various formats and returns it in yyyy-MM-dd format */ function parseCSVDate(input: string): string | null { if (!input || typeof input !== 'string') { @@ -37,11 +29,13 @@ function parseCSVDate(input: string): string | null { const trimmedInput = input.trim(); - // Try the explicit formats first. The native `Date` constructor parses - // bare ISO dates (e.g. "2024-01-15") as UTC midnight, which would shift to - // the previous day in any zone west of UTC once formatted back. The - // format list starts with `yyyy-MM-dd` so those inputs are interpreted in - // local time and round-trip cleanly. + // Try native Date parsing first (handles ISO and some other formats) + let date = new Date(trimmedInput); + if (isValid(date) && !Number.isNaN(date.getTime())) { + return format(date, CONST.DATE.FNS_FORMAT_STRING); + } + + // Try parsing with common date formats using date-fns for (const dateFormat of CSV_DATE_FORMATS) { const parsedDate = parse(trimmedInput, dateFormat, new Date()); if (isValid(parsedDate)) { @@ -49,36 +43,30 @@ function parseCSVDate(input: string): string | null { } } - // Fall back to the native Date constructor for anything else (ISO 8601 - // date-time strings, RFC 2822, etc). - let date = new Date(trimmedInput); - if (isValid(date) && !Number.isNaN(date.getTime())) { - return format(date, CONST.DATE.FNS_FORMAT_STRING); - } - - // If still unparsed, try just the first 10 characters in case the input - // is a longer string with a date prefix. + // If the date didn't parse, try taking just the first 10 characters if (trimmedInput.length > 10) { const shortInput = trimmedInput.substring(0, 10); + date = new Date(shortInput); + if (isValid(date) && !Number.isNaN(date.getTime())) { + return format(date, CONST.DATE.FNS_FORMAT_STRING); + } + + // Also try format parsing on the shortened input for (const dateFormat of CSV_DATE_FORMATS) { const parsedDate = parse(shortInput, dateFormat, new Date()); if (isValid(parsedDate)) { return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING); } } - date = new Date(shortInput); - if (isValid(date) && !Number.isNaN(date.getTime())) { - return format(date, CONST.DATE.FNS_FORMAT_STRING); - } } - // Maybe it's an Excel serial date number. Excel stores dates serialized - // from January 1st 1900 (with 1/1/1900 being 1), and incorrectly treats - // 1900 as a leap year, so we subtract 2 days when converting. + // If it didn't parse, maybe it's an Excel date number + // Excel stores dates serialized from January 1st, 1900 (with 1/1/1900 being 1) + // Excel thinks that 1900 was a leap year and adds an extra day to account for that if (/^\d+$/.test(trimmedInput)) { const inputInt = parseInt(trimmedInput, 10); if (inputInt > 0 && inputInt < 100000) { - const excelEpoch = new Date(1900, 0, 1); + const excelEpoch = new Date(1900, 0, 1); // January 1, 1900 const parsedDate = addDays(excelEpoch, inputInt - 2); if (isValid(parsedDate)) { return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING); diff --git a/src/libs/actions/CompanyCards.ts b/src/libs/actions/CompanyCards.ts index d3b26bd8a93e..9e13eff4397d 100644 --- a/src/libs/actions/CompanyCards.ts +++ b/src/libs/actions/CompanyCards.ts @@ -18,6 +18,7 @@ import type { import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import * as CardUtils from '@libs/CardUtils'; import {getCardFeedWithDomainID} from '@libs/CardUtils'; +import parseCSVDate from '@libs/CSVDateUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import {rand64} from '@libs/NumberUtils'; @@ -106,6 +107,7 @@ function buildOptimisticCompanyCardCSVTransactions( const cardNumberColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.CARD_NUMBER); 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); const amountColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.AMOUNT); const currencyColumnIndex = getColumnIndex(normalizedColumnMappings, CONST.CSV_IMPORT_COLUMNS.CURRENCY); @@ -119,7 +121,8 @@ function buildOptimisticCompanyCardCSVTransactions( row[externalIDColumnIndex] = transactionID; const cardName = row.at(cardNumberColumnIndex)?.trim(); - const created = row.at(postedDateColumnIndex)?.trim(); + const rawPostedDate = row.at(postedDateColumnIndex)?.trim(); + const created = rawPostedDate ? parseCSVDate(rawPostedDate) : null; const merchant = row.at(merchantColumnIndex)?.trim() ?? ''; const currency = row.at(currencyColumnIndex)?.trim(); const amountValue = row.at(amountColumnIndex) ?? ''; @@ -129,6 +132,17 @@ function buildOptimisticCompanyCardCSVTransactions( continue; } + // The backend expects yyyy-MM-dd dates, so write the normalized values back into + // the row before it gets serialized and sent to Auth/Scrapers. + row[postedDateColumnIndex] = created; + if (originalTransactionDateColumnIndex >= 0) { + const rawOriginalDate = row.at(originalTransactionDateColumnIndex)?.trim(); + const parsedOriginalDate = rawOriginalDate ? parseCSVDate(rawOriginalDate) : null; + if (parsedOriginalDate) { + row[originalTransactionDateColumnIndex] = parsedOriginalDate; + } + } + const category = categoryColumnIndex >= 0 ? (row.at(categoryColumnIndex)?.trim() ?? '') : ''; const tag = tagColumnIndex >= 0 ? (row.at(tagColumnIndex)?.trim() ?? '') : ''; const comment = commentColumnIndex >= 0 ? (row.at(commentColumnIndex)?.trim() ?? '') : ''; diff --git a/tests/unit/ImportTransactions.test.ts b/tests/unit/ImportTransactions.test.ts index 411d57a55f68..a61342b84a00 100644 --- a/tests/unit/ImportTransactions.test.ts +++ b/tests/unit/ImportTransactions.test.ts @@ -1,7 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ import Onyx from 'react-native-onyx'; import {applySavedColumnMappings, getImportFinalModalOnyxData} from '@libs/actions/ImportSpreadsheet'; -import {buildColumnLayout, buildTransactionListFromSpreadsheet, getColumnIndexes} from '@libs/actions/ImportTransactions'; +import importTransactionsFromCSV, {buildColumnLayout, buildTransactionListFromSpreadsheet, getColumnIndexes} from '@libs/actions/ImportTransactions'; +import * as API from '@libs/API'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type ImportedSpreadsheet from '@src/types/onyx/ImportedSpreadsheet'; @@ -108,11 +109,14 @@ describe('ImportTransactions', () => { describe('buildColumnLayout', () => { it('should build a basic layout with no column mappings', () => { - const spreadsheet = { + const spreadsheet: ImportedSpreadsheet = { data: [], columns: {}, containsHeader: true, - } as Partial as ImportedSpreadsheet; + isImportingMultiLevelTags: false, + isImportingIndependentMultiLevelTags: false, + isGLAdjacent: false, + }; const result = buildColumnLayout(spreadsheet, 'Test Card', 'USD', true, false); @@ -148,7 +152,7 @@ describe('ImportTransactions', () => { }); it('should build layout with column mappings and extract header names', () => { - const spreadsheet = { + const spreadsheet: ImportedSpreadsheet = { data: [ ['Date', '2024-01-01', '2024-01-02'], ['Merchant', 'Store A', 'Store B'], @@ -162,7 +166,10 @@ describe('ImportTransactions', () => { 3: 'category', }, containsHeader: true, - } as Partial as ImportedSpreadsheet; + isImportingMultiLevelTags: false, + isImportingIndependentMultiLevelTags: false, + isGLAdjacent: false, + }; const result = buildColumnLayout(spreadsheet, 'My Card', 'EUR', false, true); @@ -187,7 +194,7 @@ describe('ImportTransactions', () => { }); it('should handle missing headers when containsHeader is false', () => { - const spreadsheet = { + const spreadsheet: ImportedSpreadsheet = { data: [ ['2024-01-01', '2024-01-02'], ['Store A', 'Store B'], @@ -199,7 +206,10 @@ describe('ImportTransactions', () => { 2: 'amount', }, containsHeader: false, - } as Partial as ImportedSpreadsheet; + isImportingMultiLevelTags: false, + isImportingIndependentMultiLevelTags: false, + isGLAdjacent: false, + }; const result = buildColumnLayout(spreadsheet, 'Card', 'USD', true, false); @@ -221,7 +231,7 @@ describe('ImportTransactions', () => { }); it('should handle out of bounds column indexes gracefully', () => { - const spreadsheet = { + const spreadsheet: ImportedSpreadsheet = { data: [ ['Date', '2024-01-01'], ['Merchant', 'Store A'], @@ -232,7 +242,10 @@ describe('ImportTransactions', () => { 10: 'amount', // Out of bounds }, containsHeader: true, - } as Partial as ImportedSpreadsheet; + isImportingMultiLevelTags: false, + isImportingIndependentMultiLevelTags: false, + isGLAdjacent: false, + }; const result = buildColumnLayout(spreadsheet, 'Card', 'USD', true, false); @@ -760,4 +773,57 @@ describe('ImportTransactions', () => { expect(Onyx.merge).not.toHaveBeenCalled(); }); }); + + describe('importTransactionsFromCSV', () => { + const CURRENT_USER_ACCOUNT_ID = 12345; + const validSpreadsheet = { + data: [ + ['Date', '2024-01-15', '2024-01-20'], + ['Merchant', 'Coffee Shop', 'Restaurant'], + ['Amount', '5.50', '25.00'], + ], + columns: { + 0: 'date', + 1: 'merchant', + 2: 'amount', + }, + containsHeader: true, + } as Partial as ImportedSpreadsheet; + + let writeSpy: jest.SpyInstance; + + beforeEach(() => { + writeSpy = jest.spyOn(API, 'write').mockRejectedValue(new Error('forced')); + }); + + afterEach(() => { + writeSpy.mockRestore(); + }); + + it('returns the failed-import modal and skips the API call when no transactions are parsed', async () => { + const result = await importTransactionsFromCSV({...validSpreadsheet, data: []}, CURRENT_USER_ACCOUNT_ID); + + expect(result).toEqual({titleKey: 'spreadsheet.importFailedTitle', promptKey: 'spreadsheet.invalidFileMessage'}); + expect(writeSpy).not.toHaveBeenCalled(); + }); + + it('calls API.write with an optimistic card when no existingCardID is passed', async () => { + await importTransactionsFromCSV(validSpreadsheet, CURRENT_USER_ACCOUNT_ID); + + expect(writeSpy).toHaveBeenCalledTimes(1); + const [command, , onyxData] = writeSpy.mock.calls.at(0) as [string, unknown, {optimisticData: Array<{key: string}>}]; + expect(command).toBe('ImportCSVTransactions'); + expect(onyxData.optimisticData.some((entry) => entry.key === ONYXKEYS.CARD_LIST)).toBe(true); + }); + + it('reuses an existingCardID without queuing an optimistic card', async () => { + const existingCardID = 987654321; + + await importTransactionsFromCSV(validSpreadsheet, CURRENT_USER_ACCOUNT_ID, existingCardID); + + const [, params, onyxData] = writeSpy.mock.calls.at(0) as [string, {cardID: number}, {optimisticData: Array<{key: string}>}]; + expect(params.cardID).toBe(existingCardID); + expect(onyxData.optimisticData.some((entry) => entry.key === ONYXKEYS.CARD_LIST)).toBe(false); + }); + }); });