forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhookService.js
More file actions
178 lines (156 loc) · 4.42 KB
/
Copy pathwebhookService.js
File metadata and controls
178 lines (156 loc) · 4.42 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import crypto from 'crypto';
import prisma from '../lib/prisma.js';
import { withTenantScopeBypassed } from '../lib/tenantContext.js';
import { enqueueWebhookDelivery } from '../queues/webhookQueue.js';
const SIGNATURE_HEADER = 'X-Webhook-Signature';
const DELIVERY_ID_HEADER = 'X-Webhook-Delivery-Id';
const EVENT_TYPE_HEADER = 'X-Webhook-Event-Type';
const DEFAULT_RETRY_ATTEMPTS = 5;
const DEFAULT_BACKOFF_DELAY_MS = 5000;
function buildWebhookPayload(eventType, payload, deliveryId) {
return {
eventType,
deliveryId,
timestamp: new Date().toISOString(),
data: payload,
};
}
function generateSecret() {
return crypto.randomBytes(32).toString('hex');
}
function signPayload(secret, payload) {
return crypto.createHmac('sha256', secret).update(JSON.stringify(payload)).digest('hex');
}
async function createSubscription({ url, eventTypes, createdBy }) {
const subscriptionSecret = generateSecret();
const subscription = await prisma.webhookSubscription.create({
data: {
url: String(url).trim(),
eventTypes,
secret: subscriptionSecret,
createdBy: createdBy || null,
isActive: true,
},
select: {
id: true,
url: true,
eventTypes: true,
createdAt: true,
updatedAt: true,
},
});
return { ...subscription, secret: subscriptionSecret };
}
async function listSubscriptions({ createdBy }) {
return prisma.webhookSubscription.findMany({
where: { createdBy },
orderBy: { createdAt: 'desc' },
select: {
id: true,
url: true,
eventTypes: true,
isActive: true,
createdAt: true,
updatedAt: true,
},
});
}
async function deleteSubscription({ id, createdBy }) {
const deleted = await prisma.webhookSubscription.deleteMany({
where: { id, createdBy },
});
return deleted.count > 0;
}
async function getDeliveryHistory({ subscriptionId, createdBy, page = 1, limit = 30 }) {
const skip = (page - 1) * limit;
const [deliveries, total] = await Promise.all([
prisma.webhookDelivery.findMany({
where: { subscription: { id: subscriptionId, createdBy } },
orderBy: { createdAt: 'desc' },
skip,
take: limit,
select: {
id: true,
eventType: true,
status: true,
attempts: true,
responseCode: true,
errorMessage: true,
lastAttemptAt: true,
createdAt: true,
},
}),
prisma.webhookDelivery.count({
where: { subscription: { id: subscriptionId, createdBy } },
}),
]);
return {
page,
limit,
total,
deliveries,
};
}
async function queueSubscriptionWebhook(subscription, payload, eventType) {
const delivery = await prisma.webhookDelivery.create({
data: {
subscription: { connect: { id: subscription.id } },
eventType,
payload: payload,
status: 'pending',
},
});
const signedPayload = buildWebhookPayload(eventType, payload, delivery.id);
const signature = signPayload(subscription.secret, signedPayload);
const headers = {
'Content-Type': 'application/json',
[SIGNATURE_HEADER]: signature,
[DELIVERY_ID_HEADER]: delivery.id,
[EVENT_TYPE_HEADER]: eventType,
};
await prisma.webhookDelivery.update({
where: { id: delivery.id },
data: { payload: signedPayload },
});
await enqueueWebhookDelivery(delivery.id, subscription.url, signedPayload, headers, {
attempts: DEFAULT_RETRY_ATTEMPTS,
backoff: { type: 'exponential', delay: DEFAULT_BACKOFF_DELAY_MS },
});
return delivery;
}
async function queueEventWebhooks(eventType, payload) {
const subscriptions = await withTenantScopeBypassed(() =>
prisma.webhookSubscription.findMany({
where: { eventTypes: { has: eventType }, isActive: true },
}),
);
if (subscriptions.length === 0) {
return { queued: 0 };
}
const queued = [];
for (const subscription of subscriptions) {
const delivery = await queueSubscriptionWebhook(subscription, payload, eventType);
queued.push({ subscriptionId: subscription.id, deliveryId: delivery.id });
}
return { queued: queued.length, deliveries: queued };
}
export {
createSubscription,
listSubscriptions,
deleteSubscription,
getDeliveryHistory,
queueEventWebhooks,
signPayload,
buildWebhookPayload,
SIGNATURE_HEADER,
DELIVERY_ID_HEADER,
EVENT_TYPE_HEADER,
};
export default {
createSubscription,
listSubscriptions,
deleteSubscription,
getDeliveryHistory,
queueEventWebhooks,
signPayload,
};