"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KyselyWebhookEndpointRepository = void 0; const WebhookEndpoint_1 = require("../../domain/entities/WebhookEndpoint"); const UniqueId_1 = require("../../../../shared/domain/UniqueId"); const WebhookSecret_1 = require("../../domain/value-objects/WebhookSecret"); class KyselyWebhookEndpointRepository { constructor(db) { this.db = db; } async save(endpoint) { const row = { id: endpoint.id.toString(), url: endpoint.url, secret: endpoint.secret.value, enabled: endpoint.enabled ? 1 : 0, created_at: endpoint.createdAt.getTime(), last_delivered_at: endpoint.lastDeliveredAt ? endpoint.lastDeliveredAt.getTime() : null, last_status: endpoint.lastStatus ?? null, }; await this.db.insertInto('webhook_endpoints').values(row).execute(); } async findById(id) { const row = await this.db .selectFrom('webhook_endpoints') .selectAll() .where('id', '=', id) .executeTakeFirst(); return row ? this.toDomain(row) : undefined; } async findAll() { const rows = await this.db .selectFrom('webhook_endpoints') .selectAll() .orderBy('created_at', 'desc') .execute(); return rows.map(r => this.toDomain(r)); } async findEnabled() { const rows = await this.db .selectFrom('webhook_endpoints') .selectAll() .where('enabled', '=', 1) .execute(); return rows.map(r => this.toDomain(r)); } async update(endpoint) { await this.db .updateTable('webhook_endpoints') .set({ enabled: endpoint.enabled ? 1 : 0, last_delivered_at: endpoint.lastDeliveredAt ? endpoint.lastDeliveredAt.getTime() : null, last_status: endpoint.lastStatus ?? null, }) .where('id', '=', endpoint.id.toString()) .execute(); } async delete(id) { await this.db.deleteFrom('webhook_endpoints').where('id', '=', id).execute(); } toDomain(row) { const props = { url: row.url, secret: WebhookSecret_1.WebhookSecret.fromString(row.secret), enabled: row.enabled === 1, createdAt: new Date(row.created_at), lastDeliveredAt: row.last_delivered_at ? new Date(row.last_delivered_at) : undefined, lastStatus: row.last_status ?? undefined, }; return WebhookEndpoint_1.WebhookEndpoint.reconstitute(props, UniqueId_1.UniqueId.from(row.id)); } } exports.KyselyWebhookEndpointRepository = KyselyWebhookEndpointRepository;