Skip to content
Merged
54 changes: 21 additions & 33 deletions src/libs/CSVDateUtils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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') {
Expand All @@ -37,48 +29,44 @@ 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);
Comment thread
Gonals marked this conversation as resolved.
Resolved
}

// 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)) {
return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING);
}
}

// 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
Comment thread
Gonals marked this conversation as resolved.
Resolved
for (const dateFormat of CSV_DATE_FORMATS) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-3 (docs)

The for-loop that iterates over CSV_DATE_FORMATS and calls parse/isValid/format is duplicated (lines 39-43 and lines 54-58). Since this file is being newly created as a shared utility, this is a good opportunity to extract the repeated pattern into a small helper.

Suggested fix -- extract a helper function:

function tryParseWithFormats(dateString: string): string | null {
    for (const dateFormat of CSV_DATE_FORMATS) {
        const parsedDate = parse(dateString, dateFormat, new Date());
        if (isValid(parsedDate)) {
            return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING);
        }
    }
    return null;
}

Then call tryParseWithFormats(trimmedInput) and tryParseWithFormats(shortInput) instead of repeating the loop.


Reviewed at: bcec5e1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Do you think it's worth making a change here in this PR, or perhaps in a follow-up?

cc: @Gonals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. It is just a handful of lines 🤷

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-2 (docs)

The magic number 100000 is used as an upper bound for valid Excel serial date numbers without being defined as a named constant. Since this file is a new shared utility, consider extracting it to improve readability.

Suggested fix:

// Excel serial date 100000 corresponds to ~2173-10-14, well beyond any reasonable CSV date
const MAX_EXCEL_SERIAL_DATE = 100000;

Then use inputInt < MAX_EXCEL_SERIAL_DATE in the condition.


Reviewed at: bcec5e1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Do you think it's worth making a change here in this PR, or perhaps in a follow-up?

Same here @Gonals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. I think this is fine 🤷

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);
Expand Down
16 changes: 15 additions & 1 deletion src/libs/actions/CompanyCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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) ?? '';
Expand All @@ -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() ?? '') : '';
Expand Down
84 changes: 75 additions & 9 deletions tests/unit/ImportTransactions.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<ImportedSpreadsheet> as ImportedSpreadsheet;
isImportingMultiLevelTags: false,
isImportingIndependentMultiLevelTags: false,
isGLAdjacent: false,
};

const result = buildColumnLayout(spreadsheet, 'Test Card', 'USD', true, false);

Expand Down Expand Up @@ -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'],
Expand All @@ -162,7 +166,10 @@ describe('ImportTransactions', () => {
3: 'category',
},
containsHeader: true,
} as Partial<ImportedSpreadsheet> as ImportedSpreadsheet;
isImportingMultiLevelTags: false,
isImportingIndependentMultiLevelTags: false,
isGLAdjacent: false,
};

const result = buildColumnLayout(spreadsheet, 'My Card', 'EUR', false, true);

Expand All @@ -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'],
Expand All @@ -199,7 +206,10 @@ describe('ImportTransactions', () => {
2: 'amount',
},
containsHeader: false,
} as Partial<ImportedSpreadsheet> as ImportedSpreadsheet;
isImportingMultiLevelTags: false,
isImportingIndependentMultiLevelTags: false,
isGLAdjacent: false,
};

const result = buildColumnLayout(spreadsheet, 'Card', 'USD', true, false);

Expand All @@ -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'],
Expand All @@ -232,7 +242,10 @@ describe('ImportTransactions', () => {
10: 'amount', // Out of bounds
},
containsHeader: true,
} as Partial<ImportedSpreadsheet> as ImportedSpreadsheet;
isImportingMultiLevelTags: false,
isImportingIndependentMultiLevelTags: false,
isGLAdjacent: false,
};

const result = buildColumnLayout(spreadsheet, 'Card', 'USD', true, false);

Expand Down Expand Up @@ -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<ImportedSpreadsheet> 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);
});
});
});
Loading