Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

1.4 KB
Raw Blame
import { capitalize } from '@ghostfolio/common/helper';
import { ExportResponse } from '@ghostfolio/common/interfaces';
import { Type } from '@ghostfolio/prisma/enums';

import { Service } from '@angular/core';
import { format, parseISO } from 'date-fns';

@Service()
export class IcsService {
  private readonly ICS_DATE_FORMAT = 'yyyyMMdd';
  private readonly ICS_LINE_BREAK = '\r\n';

  public transformActivitiesToIcsContent(
    aActivities: ExportResponse['activities']
  ): string {
    const header = [
      'BEGIN:VCALENDAR',
      'VERSION:2.0',
      'PRODID:-//Ghostfolio//NONSGML v1.0//EN'
    ];
    const events = aActivities.map((activity) => {
      return this.getEvent({
        date: parseISO(activity.date),
        id: activity.id,
        symbol: activity.symbol,
        type: activity.type
      });
    });
    const footer = ['END:VCALENDAR'];

    return [...header, ...events, ...footer].join(this.ICS_LINE_BREAK);
  }

  private getEvent({
    date,
    id,
    symbol,
    type
  }: {
    date: Date;
    id: string;
    symbol: string;
    type: Type;
  }) {
    const today = format(new Date(), this.ICS_DATE_FORMAT);

    return [
      'BEGIN:VEVENT',
      `UID:${id}`,
      `DTSTAMP:${today}T000000`,
      `DTSTART;VALUE=DATE:${format(date, this.ICS_DATE_FORMAT)}`,
      `SUMMARY:${capitalize(type)} ${symbol}`,
      'END:VEVENT'
    ].join(this.ICS_LINE_BREAK);
  }
}