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
|
@@ -15,18 +15,17 @@ import {
|
|
|
15
15
|
CONSUMER_GROUPS,
|
|
16
16
|
PUBSUB_CHANNELS,
|
|
17
17
|
registry,
|
|
18
|
+
type EventRegistry,
|
|
18
19
|
buildStreamEvent,
|
|
19
|
-
type NotificationEnrichedPayload,
|
|
20
20
|
type DispatchedTaskPayload,
|
|
21
21
|
type StreamEvent,
|
|
22
22
|
} from "@/index.js";
|
|
23
23
|
import { IdempotencyGuard } from "@/index.js";
|
|
24
24
|
import { UserThrottle, ProjectSettingsCache } from "@/index.js";
|
|
25
25
|
import { TemplateRepository, ContactRepository, ProjectRepository } from "@/index.js";
|
|
26
|
-
import {
|
|
26
|
+
import { suppressions } from "@/db/schema.js";
|
|
27
27
|
import { and, eq } from "drizzle-orm";
|
|
28
28
|
import {
|
|
29
|
-
getPriorityBucket,
|
|
30
29
|
globalEmitter,
|
|
31
30
|
normaliseTarget,
|
|
32
31
|
type WorkerOptions,
|
|
@@ -35,6 +34,7 @@ import {
|
|
|
35
34
|
} from "@/shared/index.js";
|
|
36
35
|
import { TemplateCache } from "@/templates/index.js";
|
|
37
36
|
import { buildUnsubscribeHeaders } from "@/unsubscribe/index.js";
|
|
37
|
+
import { scheduleOrDispatch } from "./schedule-or-dispatch.js";
|
|
38
38
|
import { startHealthReporter } from "@/workers/index.js";
|
|
39
39
|
import {
|
|
40
40
|
createPriorityProducers,
|
|
@@ -43,158 +43,9 @@ import {
|
|
|
43
43
|
shutdownWorker,
|
|
44
44
|
uniqueConsumerId,
|
|
45
45
|
} from "@/workers/bootstrap.js";
|
|
46
|
+
import { isInQuietHours } from "./quiet-hours.js";
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
year: number,
|
|
49
|
-
month: number,
|
|
50
|
-
day: number,
|
|
51
|
-
hour: number,
|
|
52
|
-
minute: number,
|
|
53
|
-
second: number,
|
|
54
|
-
timezone: string,
|
|
55
|
-
): Date {
|
|
56
|
-
let utcMs = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
57
|
-
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
58
|
-
timeZone: timezone,
|
|
59
|
-
year: "numeric",
|
|
60
|
-
month: "2-digit",
|
|
61
|
-
day: "2-digit",
|
|
62
|
-
hour: "2-digit",
|
|
63
|
-
minute: "2-digit",
|
|
64
|
-
second: "2-digit",
|
|
65
|
-
hour12: false,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
for (let iter = 0; iter < 3; iter++) {
|
|
69
|
-
const parts = formatter.formatToParts(new Date(utcMs));
|
|
70
|
-
const pYear = parseInt(parts.find((p) => p.type === "year")?.value ?? "0", 10);
|
|
71
|
-
const pMonth = parseInt(parts.find((p) => p.type === "month")?.value ?? "0", 10);
|
|
72
|
-
const pDay = parseInt(parts.find((p) => p.type === "day")?.value ?? "0", 10);
|
|
73
|
-
const pHour = parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
|
|
74
|
-
const pMin = parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
|
|
75
|
-
const pSec = parseInt(parts.find((p) => p.type === "second")?.value ?? "0", 10);
|
|
76
|
-
|
|
77
|
-
const targetMs = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
78
|
-
const actualMs = Date.UTC(pYear, pMonth - 1, pDay, pHour, pMin, pSec);
|
|
79
|
-
const diff = targetMs - actualMs;
|
|
80
|
-
if (diff === 0) break;
|
|
81
|
-
utcMs += diff;
|
|
82
|
-
}
|
|
83
|
-
return new Date(utcMs);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function isInQuietHours(
|
|
87
|
-
timezone: string,
|
|
88
|
-
quietHours: { start: string; end: string }[],
|
|
89
|
-
fromDate: Date = new Date(),
|
|
90
|
-
): { inQuietHours: boolean; nextActiveTime?: Date } {
|
|
91
|
-
if (!quietHours || quietHours.length === 0) {
|
|
92
|
-
return { inQuietHours: false };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const checkWindowAt = (date: Date) => {
|
|
96
|
-
let parts: Intl.DateTimeFormatPart[];
|
|
97
|
-
try {
|
|
98
|
-
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
99
|
-
timeZone: timezone,
|
|
100
|
-
year: "numeric",
|
|
101
|
-
month: "2-digit",
|
|
102
|
-
day: "2-digit",
|
|
103
|
-
hour: "2-digit",
|
|
104
|
-
minute: "2-digit",
|
|
105
|
-
second: "2-digit",
|
|
106
|
-
hour12: false,
|
|
107
|
-
});
|
|
108
|
-
parts = formatter.formatToParts(date);
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const year = parseInt(parts.find((p) => p.type === "year")?.value ?? "0", 10);
|
|
114
|
-
const month = parseInt(parts.find((p) => p.type === "month")?.value ?? "0", 10);
|
|
115
|
-
const day = parseInt(parts.find((p) => p.type === "day")?.value ?? "0", 10);
|
|
116
|
-
const currentHour = parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
|
|
117
|
-
const currentMin = parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
|
|
118
|
-
|
|
119
|
-
const currentMinutes = currentHour * 60 + currentMin;
|
|
120
|
-
|
|
121
|
-
for (const window of quietHours) {
|
|
122
|
-
const [startH, startM] = window.start.split(":").map(Number);
|
|
123
|
-
const [endH, endM] = window.end.split(":").map(Number);
|
|
124
|
-
|
|
125
|
-
if (startH === undefined || startM === undefined || endH === undefined || endM === undefined)
|
|
126
|
-
continue;
|
|
127
|
-
|
|
128
|
-
const startMinutes = startH * 60 + startM;
|
|
129
|
-
const endMinutes = endH * 60 + endM;
|
|
130
|
-
|
|
131
|
-
let inWindow = false;
|
|
132
|
-
if (startMinutes <= endMinutes) {
|
|
133
|
-
inWindow = currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
|
134
|
-
} else {
|
|
135
|
-
inWindow = currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (inWindow) {
|
|
139
|
-
let targetYear = year;
|
|
140
|
-
let targetMonth = month;
|
|
141
|
-
let targetDay = day;
|
|
142
|
-
|
|
143
|
-
if (currentMinutes >= endMinutes) {
|
|
144
|
-
const nextDay = new Date(Date.UTC(year, month - 1, day + 1));
|
|
145
|
-
targetYear = nextDay.getUTCFullYear();
|
|
146
|
-
targetMonth = nextDay.getUTCMonth() + 1;
|
|
147
|
-
targetDay = nextDay.getUTCDate();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
let nextActive = localTimeToUtc(
|
|
151
|
-
targetYear,
|
|
152
|
-
targetMonth,
|
|
153
|
-
targetDay,
|
|
154
|
-
endH,
|
|
155
|
-
endM,
|
|
156
|
-
0,
|
|
157
|
-
timezone,
|
|
158
|
-
);
|
|
159
|
-
// In a DST fall-back hour the end time occurs twice and localTimeToUtc
|
|
160
|
-
// picks the first, which can already be behind us; use the current
|
|
161
|
-
// UTC offset to land on the second occurrence instead.
|
|
162
|
-
if (nextActive.getTime() <= date.getTime()) {
|
|
163
|
-
const currentSec = parseInt(parts.find((p) => p.type === "second")?.value ?? "0", 10);
|
|
164
|
-
const offsetMs =
|
|
165
|
-
Date.UTC(year, month - 1, day, currentHour, currentMin, currentSec) -
|
|
166
|
-
Math.floor(date.getTime() / 1000) * 1000;
|
|
167
|
-
nextActive = new Date(
|
|
168
|
-
Date.UTC(targetYear, targetMonth - 1, targetDay, endH, endM, 0) - offsetMs,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
return { inQuietHours: true, nextActiveTime: nextActive };
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return { inQuietHours: false };
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
let initialCheck = checkWindowAt(fromDate);
|
|
178
|
-
if (!initialCheck || !initialCheck.inQuietHours) {
|
|
179
|
-
return { inQuietHours: false };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Chained / overlapping quiet hours intervals
|
|
183
|
-
let candidateTime = initialCheck.nextActiveTime!;
|
|
184
|
-
for (let i = 0; i < 10; i++) {
|
|
185
|
-
const subsequentCheck = checkWindowAt(candidateTime);
|
|
186
|
-
if (subsequentCheck && subsequentCheck.inQuietHours && subsequentCheck.nextActiveTime) {
|
|
187
|
-
if (subsequentCheck.nextActiveTime.getTime() <= candidateTime.getTime()) {
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
candidateTime = subsequentCheck.nextActiveTime;
|
|
191
|
-
} else {
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return { inQuietHours: true, nextActiveTime: candidateTime };
|
|
197
|
-
}
|
|
48
|
+
export { isInQuietHours };
|
|
198
49
|
|
|
199
50
|
/** Idempotency stand-in for inline messages; see EngineWorker.process. */
|
|
200
51
|
const NO_IDEMPOTENCY = {
|
|
@@ -222,7 +73,7 @@ let worker: BaseWorker;
|
|
|
222
73
|
let healthInterval: NodeJS.Timeout | null = null;
|
|
223
74
|
|
|
224
75
|
export interface EngineWorkerOptions extends WorkerOptions {
|
|
225
|
-
registry:
|
|
76
|
+
registry: EventRegistry;
|
|
226
77
|
idempotency: any;
|
|
227
78
|
throttle: any;
|
|
228
79
|
projectSettings: ProjectSettingsCache;
|
|
@@ -237,11 +88,10 @@ export interface EngineWorkerOptions extends WorkerOptions {
|
|
|
237
88
|
}
|
|
238
89
|
|
|
239
90
|
export class EngineWorker extends BaseWorker {
|
|
240
|
-
private readonly registry:
|
|
91
|
+
private readonly registry: EventRegistry;
|
|
241
92
|
private readonly idempotency: any;
|
|
242
93
|
private readonly throttle: any;
|
|
243
94
|
private readonly projectSettings: ProjectSettingsCache;
|
|
244
|
-
private readonly redisCli: Redis;
|
|
245
95
|
private readonly templateCache: TemplateCache;
|
|
246
96
|
private readonly aiPendingProducer: any;
|
|
247
97
|
private readonly scheduledProducer: any;
|
|
@@ -258,7 +108,6 @@ export class EngineWorker extends BaseWorker {
|
|
|
258
108
|
this.idempotency = options.idempotency;
|
|
259
109
|
this.throttle = options.throttle;
|
|
260
110
|
this.projectSettings = options.projectSettings;
|
|
261
|
-
this.redisCli = options.redis;
|
|
262
111
|
this.templateCache = options.templateCache;
|
|
263
112
|
this.aiPendingProducer = options.aiPendingProducer;
|
|
264
113
|
this.scheduledProducer = options.scheduledProducer;
|
|
@@ -363,7 +212,7 @@ export class EngineWorker extends BaseWorker {
|
|
|
363
212
|
return;
|
|
364
213
|
}
|
|
365
214
|
|
|
366
|
-
const enriched = payloadResult.data
|
|
215
|
+
const enriched = payloadResult.data;
|
|
367
216
|
|
|
368
217
|
// Idempotency
|
|
369
218
|
// The enricher emits one event per resolved contact, so two addresses on one
|
|
@@ -606,17 +455,6 @@ export class EngineWorker extends BaseWorker {
|
|
|
606
455
|
// own convention — a password reset, a receipt — and putting an
|
|
607
456
|
// unsubscribe button on those invites people to switch off mail they
|
|
608
457
|
// actually need, with no topic to scope the opt-out to anyway.
|
|
609
|
-
if (
|
|
610
|
-
enriched.channel === "email" &&
|
|
611
|
-
templateTopics.length > 0 &&
|
|
612
|
-
!config.UNSUBSCRIBE_SECRET
|
|
613
|
-
) {
|
|
614
|
-
this.logger.warn(
|
|
615
|
-
{ projectId: enriched.projectId, recipientId: enriched.recipientId },
|
|
616
|
-
"UNSUBSCRIBE_SECRET is not configured — email sent without RFC 8058 one-click unsubscribe headers",
|
|
617
|
-
);
|
|
618
|
-
}
|
|
619
|
-
|
|
620
458
|
const unsubscribeHeaders =
|
|
621
459
|
enriched.channel === "email" &&
|
|
622
460
|
templateTopics.length > 0 &&
|
|
@@ -691,74 +529,25 @@ export class EngineWorker extends BaseWorker {
|
|
|
691
529
|
continue;
|
|
692
530
|
}
|
|
693
531
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
532
|
+
await scheduleOrDispatch(
|
|
533
|
+
{
|
|
534
|
+
db: this.db,
|
|
535
|
+
scheduledProducer: this.scheduledProducer,
|
|
536
|
+
outboundProducers: this.outboundProducers,
|
|
537
|
+
logger: this.logger,
|
|
538
|
+
inlineNext: this.inlineNext,
|
|
539
|
+
},
|
|
540
|
+
taskPayload,
|
|
541
|
+
{
|
|
542
|
+
source: "engine",
|
|
543
|
+
scheduledAt: enriched.scheduledAt,
|
|
544
|
+
messageId: message.id,
|
|
545
|
+
traceId: event.metadata.traceId,
|
|
546
|
+
logLevel: "debug",
|
|
547
|
+
scheduledMessage: "task scheduled and payload cached",
|
|
548
|
+
dispatchedMessage: "task dispatched",
|
|
549
|
+
},
|
|
699
550
|
);
|
|
700
|
-
|
|
701
|
-
// Route by scheduledAt
|
|
702
|
-
const now = Date.now();
|
|
703
|
-
const scheduledAt = enriched.scheduledAt ? new Date(enriched.scheduledAt).getTime() : now;
|
|
704
|
-
|
|
705
|
-
if (scheduledAt > now) {
|
|
706
|
-
await this.db
|
|
707
|
-
.insert(scheduledPayloads)
|
|
708
|
-
.values({
|
|
709
|
-
taskId,
|
|
710
|
-
payload: {
|
|
711
|
-
...taskPayload,
|
|
712
|
-
scheduledAt: enriched.scheduledAt,
|
|
713
|
-
},
|
|
714
|
-
})
|
|
715
|
-
.onConflictDoNothing();
|
|
716
|
-
|
|
717
|
-
const scheduledEnvelope = buildStreamEvent(
|
|
718
|
-
"notification.scheduled",
|
|
719
|
-
{
|
|
720
|
-
projectId: enriched.projectId,
|
|
721
|
-
enrichedEventId: event.id,
|
|
722
|
-
taskId,
|
|
723
|
-
scheduledAt: enriched.scheduledAt!,
|
|
724
|
-
},
|
|
725
|
-
"engine",
|
|
726
|
-
event.metadata.traceId,
|
|
727
|
-
);
|
|
728
|
-
|
|
729
|
-
await this.scheduledProducer.publish(scheduledEnvelope);
|
|
730
|
-
this.logger.debug(
|
|
731
|
-
{
|
|
732
|
-
messageId: message.id,
|
|
733
|
-
taskId,
|
|
734
|
-
scheduledAt: enriched.scheduledAt,
|
|
735
|
-
traceId: event.metadata.traceId,
|
|
736
|
-
},
|
|
737
|
-
"task scheduled and payload cached",
|
|
738
|
-
);
|
|
739
|
-
} else {
|
|
740
|
-
const p = getPriorityBucket(enriched.priority);
|
|
741
|
-
const outboundProducer = this.outboundProducers[p] ?? this.outboundProducers["normal"]!;
|
|
742
|
-
|
|
743
|
-
if (this.inlineNext) {
|
|
744
|
-
await this.inlineNext({
|
|
745
|
-
...envelope,
|
|
746
|
-
id: randomUUID(),
|
|
747
|
-
timestamp: new Date().toISOString(),
|
|
748
|
-
} as StreamEvent);
|
|
749
|
-
} else {
|
|
750
|
-
await outboundProducer.publish(envelope);
|
|
751
|
-
}
|
|
752
|
-
this.logger.debug(
|
|
753
|
-
{
|
|
754
|
-
messageId: message.id,
|
|
755
|
-
taskId,
|
|
756
|
-
recipientId: enriched.recipientId,
|
|
757
|
-
traceId: event.metadata.traceId,
|
|
758
|
-
},
|
|
759
|
-
"task dispatched",
|
|
760
|
-
);
|
|
761
|
-
}
|
|
762
551
|
}
|
|
763
552
|
await idempotency.markProcessed(idempotencyKey, customTtl);
|
|
764
553
|
} catch (err) {
|
|
@@ -775,6 +564,11 @@ export async function startEngineWorker() {
|
|
|
775
564
|
templateRepo = new TemplateRepository(db);
|
|
776
565
|
templateCache = new TemplateCache(templateRepo);
|
|
777
566
|
const contactRepo = new ContactRepository(db);
|
|
567
|
+
if (!config.UNSUBSCRIBE_SECRET) {
|
|
568
|
+
logger.warn(
|
|
569
|
+
"UNSUBSCRIBE_SECRET is not configured — emails with topics are sent without RFC 8058 one-click unsubscribe headers",
|
|
570
|
+
);
|
|
571
|
+
}
|
|
778
572
|
const consumerId = uniqueConsumerId("engine");
|
|
779
573
|
({ consumer, pendingScanner } = createStreamConsumer(
|
|
780
574
|
{ redis, logger },
|
|
@@ -821,16 +615,6 @@ export async function startEngineWorker() {
|
|
|
821
615
|
projectRepo.findThrottleSettings(projectId),
|
|
822
616
|
);
|
|
823
617
|
|
|
824
|
-
// ─── Stage 2: Decision Engine ───────────────────────────────────────────────
|
|
825
|
-
//
|
|
826
|
-
// Pipeline:
|
|
827
|
-
// 1. Parse payload as notification.enriched
|
|
828
|
-
// 2. Idempotency check
|
|
829
|
-
// 3. Check user opt-in (from enriched recipient.preferences)
|
|
830
|
-
// 4. Apply per-user hourly rate limit
|
|
831
|
-
// 5. Render template using user locale
|
|
832
|
-
// 6. Route: if scheduledAt is future → SCHEDULED; else → OUTBOUND
|
|
833
|
-
|
|
834
618
|
worker = new EngineWorker({
|
|
835
619
|
consumer,
|
|
836
620
|
pendingScanner,
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
function localTimeToUtc(
|
|
2
|
+
year: number,
|
|
3
|
+
month: number,
|
|
4
|
+
day: number,
|
|
5
|
+
hour: number,
|
|
6
|
+
minute: number,
|
|
7
|
+
second: number,
|
|
8
|
+
timezone: string,
|
|
9
|
+
): Date {
|
|
10
|
+
let utcMs = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
11
|
+
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
12
|
+
timeZone: timezone,
|
|
13
|
+
year: "numeric",
|
|
14
|
+
month: "2-digit",
|
|
15
|
+
day: "2-digit",
|
|
16
|
+
hour: "2-digit",
|
|
17
|
+
minute: "2-digit",
|
|
18
|
+
second: "2-digit",
|
|
19
|
+
hour12: false,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
for (let iter = 0; iter < 3; iter++) {
|
|
23
|
+
const parts = formatter.formatToParts(new Date(utcMs));
|
|
24
|
+
const pYear = parseInt(parts.find((p) => p.type === "year")?.value ?? "0", 10);
|
|
25
|
+
const pMonth = parseInt(parts.find((p) => p.type === "month")?.value ?? "0", 10);
|
|
26
|
+
const pDay = parseInt(parts.find((p) => p.type === "day")?.value ?? "0", 10);
|
|
27
|
+
const pHour = parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
|
|
28
|
+
const pMin = parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
|
|
29
|
+
const pSec = parseInt(parts.find((p) => p.type === "second")?.value ?? "0", 10);
|
|
30
|
+
|
|
31
|
+
const targetMs = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
32
|
+
const actualMs = Date.UTC(pYear, pMonth - 1, pDay, pHour, pMin, pSec);
|
|
33
|
+
const diff = targetMs - actualMs;
|
|
34
|
+
if (diff === 0) break;
|
|
35
|
+
utcMs += diff;
|
|
36
|
+
}
|
|
37
|
+
return new Date(utcMs);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function isInQuietHours(
|
|
41
|
+
timezone: string,
|
|
42
|
+
quietHours: { start: string; end: string }[],
|
|
43
|
+
fromDate: Date = new Date(),
|
|
44
|
+
): { inQuietHours: boolean; nextActiveTime?: Date } {
|
|
45
|
+
if (!quietHours || quietHours.length === 0) {
|
|
46
|
+
return { inQuietHours: false };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const checkWindowAt = (date: Date) => {
|
|
50
|
+
let parts: Intl.DateTimeFormatPart[];
|
|
51
|
+
try {
|
|
52
|
+
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
53
|
+
timeZone: timezone,
|
|
54
|
+
year: "numeric",
|
|
55
|
+
month: "2-digit",
|
|
56
|
+
day: "2-digit",
|
|
57
|
+
hour: "2-digit",
|
|
58
|
+
minute: "2-digit",
|
|
59
|
+
second: "2-digit",
|
|
60
|
+
hour12: false,
|
|
61
|
+
});
|
|
62
|
+
parts = formatter.formatToParts(date);
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const year = parseInt(parts.find((p) => p.type === "year")?.value ?? "0", 10);
|
|
68
|
+
const month = parseInt(parts.find((p) => p.type === "month")?.value ?? "0", 10);
|
|
69
|
+
const day = parseInt(parts.find((p) => p.type === "day")?.value ?? "0", 10);
|
|
70
|
+
const currentHour = parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
|
|
71
|
+
const currentMin = parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
|
|
72
|
+
|
|
73
|
+
const currentMinutes = currentHour * 60 + currentMin;
|
|
74
|
+
|
|
75
|
+
for (const window of quietHours) {
|
|
76
|
+
const [startH, startM] = window.start.split(":").map(Number);
|
|
77
|
+
const [endH, endM] = window.end.split(":").map(Number);
|
|
78
|
+
|
|
79
|
+
if (startH === undefined || startM === undefined || endH === undefined || endM === undefined)
|
|
80
|
+
continue;
|
|
81
|
+
|
|
82
|
+
const startMinutes = startH * 60 + startM;
|
|
83
|
+
const endMinutes = endH * 60 + endM;
|
|
84
|
+
|
|
85
|
+
let inWindow = false;
|
|
86
|
+
if (startMinutes <= endMinutes) {
|
|
87
|
+
inWindow = currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
|
88
|
+
} else {
|
|
89
|
+
inWindow = currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (inWindow) {
|
|
93
|
+
let targetYear = year;
|
|
94
|
+
let targetMonth = month;
|
|
95
|
+
let targetDay = day;
|
|
96
|
+
|
|
97
|
+
if (currentMinutes >= endMinutes) {
|
|
98
|
+
const nextDay = new Date(Date.UTC(year, month - 1, day + 1));
|
|
99
|
+
targetYear = nextDay.getUTCFullYear();
|
|
100
|
+
targetMonth = nextDay.getUTCMonth() + 1;
|
|
101
|
+
targetDay = nextDay.getUTCDate();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let nextActive = localTimeToUtc(
|
|
105
|
+
targetYear,
|
|
106
|
+
targetMonth,
|
|
107
|
+
targetDay,
|
|
108
|
+
endH,
|
|
109
|
+
endM,
|
|
110
|
+
0,
|
|
111
|
+
timezone,
|
|
112
|
+
);
|
|
113
|
+
// In a DST fall-back hour the end time occurs twice and localTimeToUtc
|
|
114
|
+
// picks the first, which can already be behind us; use the current
|
|
115
|
+
// UTC offset to land on the second occurrence instead.
|
|
116
|
+
if (nextActive.getTime() <= date.getTime()) {
|
|
117
|
+
const currentSec = parseInt(parts.find((p) => p.type === "second")?.value ?? "0", 10);
|
|
118
|
+
const offsetMs =
|
|
119
|
+
Date.UTC(year, month - 1, day, currentHour, currentMin, currentSec) -
|
|
120
|
+
Math.floor(date.getTime() / 1000) * 1000;
|
|
121
|
+
nextActive = new Date(
|
|
122
|
+
Date.UTC(targetYear, targetMonth - 1, targetDay, endH, endM, 0) - offsetMs,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return { inQuietHours: true, nextActiveTime: nextActive };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return { inQuietHours: false };
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
let initialCheck = checkWindowAt(fromDate);
|
|
132
|
+
if (!initialCheck || !initialCheck.inQuietHours) {
|
|
133
|
+
return { inQuietHours: false };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Chained / overlapping quiet hours intervals
|
|
137
|
+
let candidateTime = initialCheck.nextActiveTime!;
|
|
138
|
+
for (let i = 0; i < 10; i++) {
|
|
139
|
+
const subsequentCheck = checkWindowAt(candidateTime);
|
|
140
|
+
if (subsequentCheck && subsequentCheck.inQuietHours && subsequentCheck.nextActiveTime) {
|
|
141
|
+
if (subsequentCheck.nextActiveTime.getTime() <= candidateTime.getTime()) {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
candidateTime = subsequentCheck.nextActiveTime;
|
|
145
|
+
} else {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { inQuietHours: true, nextActiveTime: candidateTime };
|
|
151
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { Logger } from "@/index.js";
|
|
3
|
+
import { buildStreamEvent, type DispatchedTaskPayload, type StreamEvent } from "@/index.js";
|
|
4
|
+
import { scheduledPayloads } from "@/db/schema.js";
|
|
5
|
+
import { getPriorityBucket } from "@/shared/index.js";
|
|
6
|
+
|
|
7
|
+
export interface ScheduleOrDispatchDeps {
|
|
8
|
+
db: any;
|
|
9
|
+
scheduledProducer: any;
|
|
10
|
+
outboundProducers: any;
|
|
11
|
+
logger: Logger;
|
|
12
|
+
/** Delivery in this process, when the pipeline is fused. */
|
|
13
|
+
inlineNext?: ((event: StreamEvent) => Promise<void>) | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ScheduleOrDispatchOptions {
|
|
17
|
+
source: string;
|
|
18
|
+
scheduledAt?: string;
|
|
19
|
+
messageId: string;
|
|
20
|
+
traceId?: string;
|
|
21
|
+
logLevel: "debug" | "info";
|
|
22
|
+
scheduledMessage: string;
|
|
23
|
+
dispatchedMessage: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Caches the payload and publishes `notification.scheduled` when the task is due
|
|
28
|
+
* later, otherwise publishes `notification.dispatched` to the outbound stream
|
|
29
|
+
* for its priority (or hands it to the fused delivery worker).
|
|
30
|
+
*/
|
|
31
|
+
export async function scheduleOrDispatch(
|
|
32
|
+
deps: ScheduleOrDispatchDeps,
|
|
33
|
+
taskPayload: DispatchedTaskPayload,
|
|
34
|
+
opts: ScheduleOrDispatchOptions,
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
const { taskId } = taskPayload;
|
|
37
|
+
const envelope = buildStreamEvent(
|
|
38
|
+
"notification.dispatched",
|
|
39
|
+
taskPayload as Record<string, unknown>,
|
|
40
|
+
opts.source,
|
|
41
|
+
opts.traceId,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
// Route by scheduledAt
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
const scheduledAt = opts.scheduledAt ? new Date(opts.scheduledAt).getTime() : now;
|
|
47
|
+
|
|
48
|
+
if (scheduledAt > now) {
|
|
49
|
+
await deps.db
|
|
50
|
+
.insert(scheduledPayloads)
|
|
51
|
+
.values({
|
|
52
|
+
taskId,
|
|
53
|
+
// The send time rides in the payload so the scheduler can re-queue
|
|
54
|
+
// this from Postgres if Redis loses its data.
|
|
55
|
+
payload: { ...taskPayload, scheduledAt: opts.scheduledAt },
|
|
56
|
+
})
|
|
57
|
+
.onConflictDoNothing();
|
|
58
|
+
|
|
59
|
+
const scheduledEnvelope = buildStreamEvent(
|
|
60
|
+
"notification.scheduled",
|
|
61
|
+
{
|
|
62
|
+
projectId: taskPayload.projectId,
|
|
63
|
+
enrichedEventId: taskPayload.enrichedEventId,
|
|
64
|
+
taskId,
|
|
65
|
+
scheduledAt: opts.scheduledAt!,
|
|
66
|
+
},
|
|
67
|
+
opts.source,
|
|
68
|
+
opts.traceId,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
await deps.scheduledProducer.publish(scheduledEnvelope);
|
|
72
|
+
deps.logger[opts.logLevel](
|
|
73
|
+
{
|
|
74
|
+
messageId: opts.messageId,
|
|
75
|
+
taskId,
|
|
76
|
+
scheduledAt: opts.scheduledAt,
|
|
77
|
+
traceId: opts.traceId,
|
|
78
|
+
},
|
|
79
|
+
opts.scheduledMessage,
|
|
80
|
+
);
|
|
81
|
+
} else {
|
|
82
|
+
const p = getPriorityBucket(taskPayload.priority);
|
|
83
|
+
const outboundProducer = deps.outboundProducers[p] ?? deps.outboundProducers["normal"]!;
|
|
84
|
+
|
|
85
|
+
if (deps.inlineNext) {
|
|
86
|
+
await deps.inlineNext({
|
|
87
|
+
...envelope,
|
|
88
|
+
id: randomUUID(),
|
|
89
|
+
timestamp: new Date().toISOString(),
|
|
90
|
+
} as StreamEvent);
|
|
91
|
+
} else {
|
|
92
|
+
await outboundProducer.publish(envelope);
|
|
93
|
+
}
|
|
94
|
+
deps.logger[opts.logLevel](
|
|
95
|
+
{
|
|
96
|
+
messageId: opts.messageId,
|
|
97
|
+
taskId,
|
|
98
|
+
recipientId: taskPayload.recipientId,
|
|
99
|
+
traceId: opts.traceId,
|
|
100
|
+
},
|
|
101
|
+
opts.dispatchedMessage,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|