import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { AlertIntegrationConfig, AlertIntegrationsRepository, AlertTrace, } from '@task-stream/api/tracker/application'; import { Model } from 'mongoose'; import { ReadModels } from './read-model.schemas'; type Stored = T & { _id?: unknown }; @Injectable() export class MongoAlertIntegrationsRepository extends AlertIntegrationsRepository { constructor( @InjectModel(ReadModels.AlertIntegration) private readonly configs: Model>, @InjectModel(ReadModels.AlertTrace) private readonly traces: Model>, ) { super(); } async find(projectId: string): Promise { const doc = await this.configs .findOne({ projectId }) .lean>(); if (!doc) return null; const { _id: _ignored, ...config } = doc; return config; } async save(config: AlertIntegrationConfig): Promise { await this.configs.updateOne( { projectId: config.projectId }, { $set: config }, { upsert: true }, ); } async remove(projectId: string): Promise { // The traces stay: turned on again, repeats still find their bug. await this.configs.deleteOne({ projectId }); } async trace( projectId: string, fingerprint: string, ): Promise { const doc = await this.traces .findOne({ projectId, fingerprint }) .lean>(); if (!doc) return null; const { _id: _ignored, ...trace } = doc; return trace; } async saveTrace(trace: AlertTrace): Promise { await this.traces.updateOne( { projectId: trace.projectId, fingerprint: trace.fingerprint }, { $set: trace }, { upsert: true }, ); } }