notifkit 0.1.15 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dashboard/dist/assets/index-C-DJYu0_.css +1 -0
- package/dashboard/dist/assets/index-CzFZcEon.js +318 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/{bootstrap-BOmT69OH.mjs → bootstrap-BMDRPjgp.mjs} +1 -1
- package/dist/{bulk-ZPiOJRx-.mjs → bulk-CJ5QXiPa.mjs} +1 -1
- package/dist/index.d.mts +71 -27
- package/dist/index.mjs +2 -2
- package/dist/{main-CCqcFDDe.mjs → main-6_bXQBDH.mjs} +21 -41
- package/dist/{main-BdZvBgV7.mjs → main-8nP3qmCT.mjs} +22 -48
- package/dist/{main-C0m9-Eos.mjs → main-B82bsFoy.mjs} +183 -334
- package/dist/{main-CTjaZ7ZD.mjs → main-CpaHF1Zw.mjs} +13 -15
- package/dist/{main-JEXBLP2A.mjs → main-DULD8-th.mjs} +8 -6
- package/dist/{main-C6S3EsJ3.mjs → main-ZW5RaiOK.mjs} +77 -64
- package/dist/{main-g33jpia4.mjs → main-g1KEcXXp.mjs} +676 -582
- package/dist/{main-24J4U8yP.mjs → main-mGIT8jv4.mjs} +4 -4
- package/dist/schedule-or-dispatch-Cd7GLfc3.mjs +52 -0
- package/dist/{src-9LXITcNi.mjs → src-BE_EUpZ6.mjs} +93 -93
- package/package.json +1 -1
- package/scripts/create-project.mjs +6 -2
- package/src/client.ts +3 -3
- package/src/config/index.ts +1 -0
- package/src/contracts/common.ts +2 -0
- package/src/contracts/envelope.ts +1 -1
- package/src/contracts/events/notification-created.ts +2 -0
- package/src/contracts/events/notification-delivered.ts +1 -1
- package/src/contracts/events/notification-failed.ts +1 -1
- package/src/contracts/events/notification-requested.ts +3 -2
- package/src/contracts/helpers.ts +1 -2
- package/src/contracts/index.ts +1 -0
- package/src/contracts/registry.ts +1 -0
- package/src/contracts/sdk.ts +8 -37
- package/src/db/schema.ts +8 -13
- package/src/idempotency/index.ts +6 -8
- package/src/metrics/index.ts +2 -0
- package/src/rate-limiter/api-rate-limiter.ts +208 -0
- package/src/rate-limiter/index.ts +3 -411
- package/src/rate-limiter/project-settings-cache.ts +99 -0
- package/src/rate-limiter/user-throttle.ts +100 -0
- package/src/redis/index.ts +10 -3
- package/src/repositories/admin-user.ts +83 -0
- package/src/repositories/contact.ts +359 -0
- package/src/repositories/index.ts +9 -1416
- package/src/repositories/preference.ts +35 -0
- package/src/repositories/project.ts +110 -0
- package/src/repositories/segment.ts +16 -0
- package/src/repositories/template.ts +72 -0
- package/src/repositories/types.ts +49 -0
- package/src/repositories/user.ts +644 -0
- package/src/repositories/workflow.ts +64 -0
- package/src/services/ai/main.ts +29 -71
- package/src/services/api/admin-static.ts +4 -1
- package/src/services/api/handlers/auth.ts +109 -0
- package/src/services/api/handlers/campaigns.ts +191 -0
- package/src/services/api/handlers/events.ts +99 -0
- package/src/services/api/handlers/notifications.ts +356 -0
- package/src/services/api/handlers/projects.ts +116 -0
- package/src/services/api/handlers/shared.ts +262 -0
- package/src/services/api/handlers/suppressions.ts +95 -0
- package/src/services/api/handlers/system.ts +314 -0
- package/src/services/api/handlers/templates.ts +97 -0
- package/src/services/api/handlers/unsubscribe.ts +215 -0
- package/src/services/api/handlers/users.ts +262 -0
- package/src/services/api/handlers/workflows.ts +150 -0
- package/src/services/api/handlers.ts +25 -2190
- package/src/services/api/health.ts +133 -0
- package/src/services/api/http.ts +12 -0
- package/src/services/api/main.ts +32 -322
- package/src/services/api/router.ts +1 -1
- package/src/services/api/webhooks.ts +172 -0
- package/src/services/delivery/main.ts +256 -498
- package/src/services/engine/main.ts +31 -247
- package/src/services/engine/quiet-hours.ts +151 -0
- package/src/services/engine/schedule-or-dispatch.ts +104 -0
- package/src/services/enricher/main.ts +9 -21
- package/src/services/events/main.ts +2 -2
- package/src/services/scheduler/main.ts +16 -25
- package/src/services/workflow/duration.ts +24 -0
- package/src/services/workflow/main.ts +6 -164
- package/src/services/workflow/timers.ts +157 -0
- package/src/shared/events.ts +3 -2
- package/src/templates/render.ts +1 -0
- package/src/workers/index.ts +7 -4
- package/src/workflows/sdk.ts +1 -0
- package/dashboard/dist/assets/index-ggpJKZqZ.js +0 -315
- package/dashboard/dist/assets/index-s-SXGIwv.css +0 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { inArray } from "drizzle-orm";
|
|
3
|
+
import type { Logger } from "@/logger/index.js";
|
|
4
|
+
import type { Transport } from "@/transport/index.js";
|
|
5
|
+
import { messageLogs, suppressions } from "@/db/schema.js";
|
|
6
|
+
import { normaliseTarget } from "@/shared/index.js";
|
|
7
|
+
import { readRawBody, sendJson } from "./http.js";
|
|
8
|
+
|
|
9
|
+
/** The POST route a provider delivers webhook events to. */
|
|
10
|
+
export function createWebhookHandler(
|
|
11
|
+
transport: Transport,
|
|
12
|
+
channel: string,
|
|
13
|
+
deps: { db: any; logger: Logger },
|
|
14
|
+
) {
|
|
15
|
+
const { db, logger } = deps;
|
|
16
|
+
return async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
|
|
17
|
+
let deadline: NodeJS.Timeout | undefined;
|
|
18
|
+
try {
|
|
19
|
+
await Promise.race([
|
|
20
|
+
(async () => {
|
|
21
|
+
const rawBody = await readRawBody(req);
|
|
22
|
+
if (!transport.verifyWebhook) {
|
|
23
|
+
if (!res.headersSent)
|
|
24
|
+
sendJson(res, 501, {
|
|
25
|
+
error: "not_implemented",
|
|
26
|
+
message: "Webhook signature verification is not implemented for this provider",
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const isValid = await transport.verifyWebhook(rawBody, req.headers);
|
|
32
|
+
if (!isValid) {
|
|
33
|
+
if (!res.headersSent)
|
|
34
|
+
sendJson(res, 401, {
|
|
35
|
+
error: "unauthorized",
|
|
36
|
+
message: "Invalid webhook signature",
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let body;
|
|
41
|
+
try {
|
|
42
|
+
body = rawBody ? JSON.parse(rawBody) : {};
|
|
43
|
+
} catch {
|
|
44
|
+
body = {};
|
|
45
|
+
}
|
|
46
|
+
const events = await transport.parseWebhook!(body, rawBody, req.headers);
|
|
47
|
+
|
|
48
|
+
if (events.length > 0) {
|
|
49
|
+
const providerIds = events.map((e) => e.providerMessageId).filter(Boolean) as string[];
|
|
50
|
+
const logsToProject = new Map<string, string>();
|
|
51
|
+
const logsToTask = new Map<string, string>();
|
|
52
|
+
const logsToCampaign = new Map<string, string | null>();
|
|
53
|
+
if (providerIds.length > 0) {
|
|
54
|
+
const existing = await db
|
|
55
|
+
.select({
|
|
56
|
+
providerMessageId: messageLogs.providerMessageId,
|
|
57
|
+
projectId: messageLogs.projectId,
|
|
58
|
+
taskId: messageLogs.taskId,
|
|
59
|
+
campaignId: messageLogs.campaignId,
|
|
60
|
+
})
|
|
61
|
+
.from(messageLogs)
|
|
62
|
+
.where(inArray(messageLogs.providerMessageId, providerIds));
|
|
63
|
+
for (const row of existing) {
|
|
64
|
+
if (row.providerMessageId) {
|
|
65
|
+
logsToProject.set(row.providerMessageId, row.projectId);
|
|
66
|
+
logsToTask.set(row.providerMessageId, row.taskId);
|
|
67
|
+
// Inherit the campaign from the delivery this event is
|
|
68
|
+
// about, or an open never counts toward the send that
|
|
69
|
+
// earned it.
|
|
70
|
+
logsToCampaign.set(row.providerMessageId, row.campaignId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Only record events we can attribute to a real message. An
|
|
76
|
+
// unresolvable providerMessageId used to be written against a
|
|
77
|
+
// zero-UUID project, which is unjoinable and leaks across tenants.
|
|
78
|
+
const attributable = events.filter(
|
|
79
|
+
(e) => e.providerMessageId && logsToProject.has(e.providerMessageId),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const rows = attributable.map((e) => ({
|
|
83
|
+
projectId: logsToProject.get(e.providerMessageId)!,
|
|
84
|
+
taskId: logsToTask.get(e.providerMessageId)!,
|
|
85
|
+
providerMessageId: e.providerMessageId,
|
|
86
|
+
channel: transport.channel,
|
|
87
|
+
// Engagement events are not delivery attempts.
|
|
88
|
+
attempt: 0,
|
|
89
|
+
kind: e.status,
|
|
90
|
+
status: e.status,
|
|
91
|
+
campaignId: logsToCampaign.get(e.providerMessageId) ?? null,
|
|
92
|
+
metadata: e.metadata ?? null,
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
const skipped = events.length - rows.length;
|
|
96
|
+
if (skipped > 0) {
|
|
97
|
+
logger.warn(
|
|
98
|
+
{ channel, skipped },
|
|
99
|
+
"webhook events skipped: unknown providerMessageId",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (rows.length > 0) {
|
|
104
|
+
// Providers retry on any non-2xx, so redelivery of an event we have
|
|
105
|
+
// already recorded must be a no-op rather than a constraint error.
|
|
106
|
+
await db.insert(messageLogs).values(rows).onConflictDoNothing();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// An unsubscribe, a spam complaint, or a permanently dead
|
|
110
|
+
// address must stop future sends — logging it is not enough.
|
|
111
|
+
// Soft bounces are excluded: the address is still good.
|
|
112
|
+
const suppressionRows = attributable
|
|
113
|
+
.filter(
|
|
114
|
+
(e) =>
|
|
115
|
+
e.recipient &&
|
|
116
|
+
(e.status === "unsubscribed" ||
|
|
117
|
+
e.status === "complained" ||
|
|
118
|
+
(e.status === "bounced" && e.bounceType === "hard")),
|
|
119
|
+
)
|
|
120
|
+
.map((e) => ({
|
|
121
|
+
projectId: logsToProject.get(e.providerMessageId)!,
|
|
122
|
+
channel: transport.channel,
|
|
123
|
+
target: normaliseTarget(e.recipient!),
|
|
124
|
+
reason: e.status,
|
|
125
|
+
source: channel,
|
|
126
|
+
taskId: logsToTask.get(e.providerMessageId)!,
|
|
127
|
+
}));
|
|
128
|
+
|
|
129
|
+
if (suppressionRows.length > 0) {
|
|
130
|
+
// First reason wins: a later bounce should not overwrite the
|
|
131
|
+
// record that the person actively unsubscribed.
|
|
132
|
+
await db.insert(suppressions).values(suppressionRows).onConflictDoNothing();
|
|
133
|
+
logger.info(
|
|
134
|
+
{ channel, count: suppressionRows.length },
|
|
135
|
+
"addresses suppressed from provider webhook",
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const unsuppressable = attributable.filter(
|
|
140
|
+
(e) =>
|
|
141
|
+
!e.recipient &&
|
|
142
|
+
(e.status === "unsubscribed" ||
|
|
143
|
+
e.status === "complained" ||
|
|
144
|
+
e.status === "bounced"),
|
|
145
|
+
).length;
|
|
146
|
+
if (unsuppressable > 0) {
|
|
147
|
+
// Worth shouting about: the event arrived, we logged it, and
|
|
148
|
+
// we still cannot stop mailing the person.
|
|
149
|
+
logger.warn(
|
|
150
|
+
{ channel, count: unsuppressable },
|
|
151
|
+
"suppression events carried no recipient address — transport must set WebhookEvent.recipient",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!res.headersSent) sendJson(res, 200, { success: true });
|
|
156
|
+
})(),
|
|
157
|
+
new Promise((_, reject) => {
|
|
158
|
+
deadline = setTimeout(() => reject(new Error("Webhook processing timeout")), 15000);
|
|
159
|
+
}),
|
|
160
|
+
]);
|
|
161
|
+
} catch (err) {
|
|
162
|
+
// The detail is ours to log, not the provider's: providers keep
|
|
163
|
+
// response bodies in their delivery logs.
|
|
164
|
+
logger.error({ err, channel }, "webhook processing error or timeout");
|
|
165
|
+
if (!res.headersSent) {
|
|
166
|
+
sendJson(res, 500, { error: "internal_error" });
|
|
167
|
+
}
|
|
168
|
+
} finally {
|
|
169
|
+
clearTimeout(deadline);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|