diff --git a/apps/client/src/app/services/import-activities.service.ts b/apps/client/src/app/services/import-activities.service.ts index 02eeb7e03..a02c4248f 100644 --- a/apps/client/src/app/services/import-activities.service.ts +++ b/apps/client/src/app/services/import-activities.service.ts @@ -4,11 +4,11 @@ import { CreateAccountDto } from '@ghostfolio/api/app/account/create-account.dto import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto'; import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; import { Account, DataSource, Type } from '@prisma/client'; -import { isMatch, parse, parseISO } from 'date-fns'; import { isFinite } from 'lodash'; import { parse as csvToJson } from 'papaparse'; import { EMPTY } from 'rxjs'; import { catchError } from 'rxjs/operators'; +import { parseDate as parseDateHelper } from '@ghostfolio/common/helper'; @Injectable({ providedIn: 'root' @@ -219,30 +219,11 @@ export class ImportActivitiesService { item: any; }) { item = this.lowercaseKeys(item); - let date: string; - for (const key of ImportActivitiesService.DATE_KEYS) { if (item[key]) { - if (isMatch(item[key], 'dd-MM-yyyy') && item[key].length === 10) { - // Check length to only match yyyy (and not yy) - date = parse(item[key], 'dd-MM-yyyy', new Date()).toISOString(); - } else if ( - isMatch(item[key], 'dd/MM/yyyy') && - item[key].length === 10 - ) { - // Check length to only match yyyy (and not yy) - date = parse(item[key], 'dd/MM/yyyy', new Date()).toISOString(); - } else if (isMatch(item[key], 'yyyyMMdd') && item[key].length === 8) { - // Check length to only match yyyy (and not yy) - date = parse(item[key], 'yyyyMMdd', new Date()).toISOString(); - } else { - try { - date = parseISO(item[key]).toISOString(); - } catch {} - } - - if (date) { - return date; + const parsedDate = parseDateHelper(item[key]); + if (parsedDate !== null) { + return parsedDate.toISOString(); } } } diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index 43cc6d9f0..1b0365b1a 100644 --- a/libs/common/src/lib/helper.ts +++ b/libs/common/src/lib/helper.ts @@ -1,7 +1,15 @@ import * as currencies from '@dinero.js/currencies'; import { DataSource } from '@prisma/client'; import Big from 'big.js'; -import { getDate, getMonth, getYear, parse, subDays } from 'date-fns'; +import { + getDate, + getMonth, + getYear, + parse, + subDays, + isMatch, + parseISO +} from 'date-fns'; import { de, es, fr, it, nl, pt, tr } from 'date-fns/locale'; import { ghostfolioScraperApiSymbolPrefix, locale } from './config'; @@ -284,8 +292,33 @@ export const DATE_FORMAT = 'yyyy-MM-dd'; export const DATE_FORMAT_MONTHLY = 'MMMM yyyy'; export const DATE_FORMAT_YEARLY = 'yyyy'; -export function parseDate(date: string) { - return parse(date, DATE_FORMAT, new Date()); +// Define the supported date format patterns +const DATE_FORMATS = [ + 'dd-MM-yyyy', + 'dd/MM/yyyy', + 'dd.MM.yyyy', + 'yyyy-MM-dd', + 'yyyy.MM.dd', + 'yyyy/MM/dd' +]; + +// Helper function to parse a date string +export function parseDate(date: string): Date | null { + const matchingFormat = DATE_FORMATS.find( + (format) => isMatch(date, format) && date.length === format.length + ); + + if (matchingFormat) { + return parse(date, matchingFormat, new Date()); + } + + try { + return parseISO(date); + } catch (error) { + console.error(`Error parsing date: ${error}`); + // Return null to indicate parsing failure + return null; + } } export function prettifySymbol(aSymbol: string): string {