import { CommandRunner } from '@task-stream/api/shared/application'; import { Injectable } from '@nestjs/common'; import { QueryBus } from '@nestjs/cqrs'; import { CreateCalendarFeedCommand, GetCalendarIcsQuery, GetMyCalendarFeedQuery, RevokeCalendarFeedCommand, } from './calendar/calendar'; import { CalendarFeed } from './ports/calendar-feeds.repository'; /** A person's calendar subscription (iCalendar), behind a secret URL. */ @Injectable() export class CalendarService { constructor( private readonly commands: CommandRunner, private readonly queryBus: QueryBus, ) {} mine(userId: string): Promise { return this.queryBus.execute(new GetMyCalendarFeedQuery(userId)); } /** A new secret URL; the previous one, if any, stops working. */ create(userId: string): Promise { return this.commands.execute(new CreateCalendarFeedCommand(userId)); } revoke(userId: string): Promise { return this.commands.execute(new RevokeCalendarFeedCommand(userId)); } /** The calendar behind `token`, or null if no feed has it. */ ics(token: string, webUrl: string): Promise { return this.queryBus.execute(new GetCalendarIcsQuery(token, webUrl)); } }