Browse Source

Improve supported date formats in activities import

pull/2362/head
Frane Caleta 2 years ago
committed by Thomas
parent
commit
dd2ad8c897
  1. 27
      apps/client/src/app/services/import-activities.service.ts
  2. 39
      libs/common/src/lib/helper.ts

27
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 { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
import { Account, DataSource, Type } from '@prisma/client'; import { Account, DataSource, Type } from '@prisma/client';
import { isMatch, parse, parseISO } from 'date-fns';
import { isFinite } from 'lodash'; import { isFinite } from 'lodash';
import { parse as csvToJson } from 'papaparse'; import { parse as csvToJson } from 'papaparse';
import { EMPTY } from 'rxjs'; import { EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators'; import { catchError } from 'rxjs/operators';
import { parseDate as parseDateHelper } from '@ghostfolio/common/helper';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -219,30 +219,11 @@ export class ImportActivitiesService {
item: any; item: any;
}) { }) {
item = this.lowercaseKeys(item); item = this.lowercaseKeys(item);
let date: string;
for (const key of ImportActivitiesService.DATE_KEYS) { for (const key of ImportActivitiesService.DATE_KEYS) {
if (item[key]) { if (item[key]) {
if (isMatch(item[key], 'dd-MM-yyyy') && item[key].length === 10) { const parsedDate = parseDateHelper(item[key]);
// Check length to only match yyyy (and not yy) if (parsedDate !== null) {
date = parse(item[key], 'dd-MM-yyyy', new Date()).toISOString(); return parsedDate.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;
} }
} }
} }

39
libs/common/src/lib/helper.ts

@ -1,7 +1,15 @@
import * as currencies from '@dinero.js/currencies'; import * as currencies from '@dinero.js/currencies';
import { DataSource } from '@prisma/client'; import { DataSource } from '@prisma/client';
import Big from 'big.js'; 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 { de, es, fr, it, nl, pt, tr } from 'date-fns/locale';
import { ghostfolioScraperApiSymbolPrefix, locale } from './config'; 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_MONTHLY = 'MMMM yyyy';
export const DATE_FORMAT_YEARLY = 'yyyy'; export const DATE_FORMAT_YEARLY = 'yyyy';
export function parseDate(date: string) { // Define the supported date format patterns
return parse(date, DATE_FORMAT, new Date()); 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 { export function prettifySymbol(aSymbol: string): string {

Loading…
Cancel
Save