import { Injectable } from '@nestjs/common'; import { CommandBus, QueryBus } from '@nestjs/cqrs'; import { WebhookDelivery } from './ports/outgoing-webhooks.repository'; import { WEBHOOK_EVENT_NAMES } from './webhooks/webhook-events'; import { CreateWebhookCommand, DeleteWebhookCommand, GetWebhooksQuery, TestWebhookCommand, UpdateWebhookCommand, WebhookView, } from './webhooks/webhooks'; /** The project's outgoing webhooks (owners and admins). */ @Injectable() export class WebhooksService { constructor( private readonly commandBus: CommandBus, private readonly queryBus: QueryBus, ) {} /** Names other systems can subscribe to (plus `family.*` and `*`). */ events(): string[] { return WEBHOOK_EVENT_NAMES; } list(viewerId: string, projectId: string): Promise { return this.queryBus.execute(new GetWebhooksQuery(viewerId, projectId)); } /** The secret comes back only here. */ create( actorId: string, projectId: string, url: string, events: string[], ): Promise<{ id: string; secret: string }> { return this.commandBus.execute( new CreateWebhookCommand(actorId, projectId, url, events), ); } update( actorId: string, webhookId: string, changes: { url?: string; events?: string[]; active?: boolean }, ): Promise { return this.commandBus.execute( new UpdateWebhookCommand(actorId, webhookId, changes), ); } delete(actorId: string, webhookId: string): Promise { return this.commandBus.execute( new DeleteWebhookCommand(actorId, webhookId), ); } test(actorId: string, webhookId: string): Promise { return this.commandBus.execute(new TestWebhookCommand(actorId, webhookId)); } }