import db from '@admin/db' import { webhookDeliveries } from '@admin/db/schema' import { and, desc, eq, notInArray } from 'drizzle-orm' const DELIVERY_LOG_LIMIT = 100 export async function pruneWebhookDeliveries(webhookId: string): Promise { const keepRows = await db .select({ id: webhookDeliveries.id }) .from(webhookDeliveries) .where(eq(webhookDeliveries.webhookId, webhookId)) .orderBy(desc(webhookDeliveries.createdAt)) .limit(DELIVERY_LOG_LIMIT) if (keepRows.length < DELIVERY_LOG_LIMIT) return await db.delete(webhookDeliveries).where( and( eq(webhookDeliveries.webhookId, webhookId), notInArray( webhookDeliveries.id, keepRows.map((row) => row.id) ) ) ) }