import { pool } from '../../../lib/postgres/connection.js'; import { camelCase } from '../../../lib/util/camelCase.js'; import { getValue } from '../../../lib/util/registry.js'; interface Filter { key: string; operation: string; value: unknown; } /** * The deliveries of one webhook, newest first, with the standard * filter / pagination machinery (registry key `webhookDeliveryCollectionFilters`). * Same shape as the blog collections: GraphQL calls `items()` and `total()`. */ export class WebhookDeliveryCollection { private baseQuery: any; private totalQuery: any; public currentFilters: Filter[] = []; public currentPage = 1; constructor(baseQuery: any) { this.baseQuery = baseQuery; this.baseQuery.orderBy('webhook_delivery.webhook_delivery_id', 'DESC'); } async init(filters: Filter[] = []): Promise { const currentFilters: Filter[] = []; const registered = await getValue( 'webhookDeliveryCollectionFilters', [] ); registered.forEach((filter: any) => { const check = filters.find( (f) => f.key === filter.key && filter.operation.includes(f.operation) ); if (filter.key === '*' || check) { filter.callback( this.baseQuery, check?.operation, check?.value, currentFilters ); } }); const totalQuery = this.baseQuery.clone(); totalQuery.select('COUNT(webhook_delivery.webhook_delivery_id)', 'total'); totalQuery.removeOrderBy(); totalQuery.removeLimit(); this.currentFilters = currentFilters; const pageFilter = currentFilters.find((f) => f.key === 'page'); this.currentPage = pageFilter ? parseInt(String(pageFilter.value), 10) || 1 : 1; this.totalQuery = totalQuery; } async items(): Promise>> { const items = await this.baseQuery.execute(pool); return items.map((row: Record) => camelCase(row)); } async total(): Promise { const total = await this.totalQuery.execute(pool); return total[0].total; } }