File size: 1,460 Bytes
94193b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { getSystemDatabase } from '../auth/system-database';
import type { WebhookEventType, WebhookPayload, WebhookEvent } from './types';

const WEBHOOK_URL = process.env.WEBHOOK_URL;

export function isWebhookEnabled(): boolean {
  return !!WEBHOOK_URL;
}

export function enqueueEvent(eventType: WebhookEventType, payload: WebhookPayload): void {
  if (!isWebhookEnabled()) return;

  const db = getSystemDatabase();
  db.prepare(
    'INSERT INTO webhook_outbox (event_type, payload) VALUES (?, ?)'
  ).run(eventType, JSON.stringify(payload));
}

export function getPendingEvents(limit = 50): WebhookEvent[] {
  const db = getSystemDatabase();
  return db.prepare(
    'SELECT * FROM webhook_outbox WHERE delivered = 0 AND attempts < 10 ORDER BY id ASC LIMIT ?'
  ).all(limit) as WebhookEvent[];
}

export function markDelivered(id: number): void {
  const db = getSystemDatabase();
  db.prepare(
    "UPDATE webhook_outbox SET delivered = 1, delivered_at = datetime('now') WHERE id = ?"
  ).run(id);
}

export function markFailed(id: number): void {
  const db = getSystemDatabase();
  db.prepare(
    "UPDATE webhook_outbox SET attempts = attempts + 1, last_attempted_at = datetime('now') WHERE id = ?"
  ).run(id);
}

export function pruneDelivered(olderThanDays = 7): void {
  const db = getSystemDatabase();
  db.prepare(
    "DELETE FROM webhook_outbox WHERE delivered = 1 AND delivered_at < datetime('now', ?)"
  ).run(`-${olderThanDays} days`);
}