public
anord
read
Ledger
Why work hard when you can work easier?
Languages
Repository composition by tracked source files.
TypeScript
86%
CSS
10%
SQL
3%
Shell
1%
HTML
0%
Trace
apps/api/src/services/webhooks.ts
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
import crypto from "node:crypto";
2
import { Queue } from "bullmq";
3
import Redis from "ioredis";
4
import { env } from "../config/env.js";
5
import { pool } from "../db/pool.js";
7
const connection = new Redis(env.REDIS_URL, {
8
maxRetriesPerRequest: null
9
});
10
const queue = new Queue("ledger-jobs", { connection });
12
export async function enqueueWebhookEvent(
13
eventName: string,
14
data: Record<string, unknown>,
15
options?: {
16
actor?: { id: string; name: string; email: string } | null;
17
workspaceId?: string | null;
18
}
19
) {
20
const webhooks = await pool.query(
21
`SELECT id FROM webhooks WHERE is_active = true AND events @> $1::jsonb`,
22
[JSON.stringify([eventName])]
23
);
25
const payload = {
26
id: crypto.randomUUID(),
27
event: eventName,
28
createdAt: new Date().toISOString(),
29
workspaceId: options?.workspaceId ?? null,
30
actor: options?.actor ?? null,
31
data
32
};
34
for (const row of webhooks.rows) {
35
const delivery = await pool.query(
36
`INSERT INTO webhook_deliveries (webhook_id, event_name, payload)
37
VALUES ($1, $2, $3)
38
RETURNING id`,
39
[row.id, eventName, JSON.stringify(payload)]
40
);
42
await queue.add("webhook.deliver", {
43
deliveryId: delivery.rows[0].id
44
}, {
45
attempts: 3,
46
backoff: {
47
type: "exponential",
48
delay: 5000
49
}
50
});
51
}
52
}