notifkit 0.1.9 → 0.1.11
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/README.md +66 -72
- package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
- package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/bootstrap-GRXzgPvW.mjs +89 -0
- package/dist/bulk-BvRfz6Of.mjs +77 -0
- package/dist/index.d.mts +1107 -8885
- package/dist/index.mjs +2 -38
- package/dist/{main-DSmh1e-V.mjs → main-BENOY7HG.mjs} +49 -75
- package/dist/{main-BPbPnBIG.mjs → main-BpnuFX62.mjs} +119 -112
- package/dist/{main-4Wyge9ms.mjs → main-BqVH2BHj.mjs} +57 -54
- package/dist/{main-D19Wxs0x.mjs → main-C_5VFIwp.mjs} +135 -79
- package/dist/main-D6od9N8h.mjs +312 -0
- package/dist/main-D84OX0ux.mjs +811 -0
- package/dist/{main-JdOvJ70b.mjs → main-HH-WwsMM.mjs} +395 -184
- package/dist/{main-AO0JaY5z.mjs → main-HY_TDs61.mjs} +149 -134
- package/dist/{src-CGmXRfsM.mjs → src-CI7FGf3h.mjs} +1223 -651
- package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
- package/drizzle/0007_workflow_status_canceled.sql +3 -0
- package/drizzle/meta/_journal.json +14 -0
- package/package.json +17 -17
- package/scripts/create-admin.mjs +8 -11
- package/src/client.ts +15 -5
- package/src/config/index.ts +26 -3
- package/src/contracts/events/notification-ai-pending.ts +12 -0
- package/src/contracts/events/notification-delivered.ts +7 -0
- package/src/contracts/events/notification-dispatched.ts +21 -2
- package/src/contracts/events/notification-enriched.ts +16 -0
- package/src/contracts/events/notification-failed.ts +2 -0
- package/src/contracts/events/notification-requested.ts +2 -0
- package/src/contracts/registry.ts +2 -2
- package/src/contracts/sdk.ts +77 -29
- package/src/db/bulk.ts +132 -0
- package/src/db/index.ts +2 -2
- package/src/db/schema.ts +3 -50
- package/src/idempotency/index.ts +60 -6
- package/src/logger/index.ts +0 -12
- package/src/queue/index.ts +255 -76
- package/src/rate-limiter/index.ts +217 -16
- package/src/redis/index.ts +265 -0
- package/src/repositories/index.ts +171 -82
- package/src/server.ts +71 -41
- package/src/services/ai/main.ts +83 -70
- package/src/services/api/admin-static.ts +41 -12
- package/src/services/api/handlers.ts +357 -159
- package/src/services/api/http.ts +34 -7
- package/src/services/api/main.ts +104 -67
- package/src/services/api/router.ts +18 -5
- package/src/services/delivery/main.ts +495 -230
- package/src/services/delivery/throttle.ts +42 -59
- package/src/services/engine/main.ts +206 -152
- package/src/services/enricher/main.ts +155 -107
- package/src/services/events/main.ts +100 -55
- package/src/services/scheduler/main.ts +151 -79
- package/src/services/scheduler/send-gate.ts +117 -0
- package/src/services/workflow/main.ts +220 -100
- package/src/shared/batch-processor.ts +65 -34
- package/src/shared/dataloader.ts +58 -22
- package/src/shared/events.ts +3 -0
- package/src/shared/index.ts +0 -1
- package/src/shared/utils.ts +2 -47
- package/src/templates/index.ts +0 -67
- package/src/transport/index.ts +6 -0
- package/src/workers/bootstrap.ts +124 -0
- package/src/workers/index.ts +99 -24
- package/src/workflows/sdk.ts +2 -0
- package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/main-4Wyge9ms.mjs.map +0 -1
- package/dist/main-AO0JaY5z.mjs.map +0 -1
- package/dist/main-BMYSYcv2.mjs +0 -224
- package/dist/main-BMYSYcv2.mjs.map +0 -1
- package/dist/main-BPbPnBIG.mjs.map +0 -1
- package/dist/main-CyeQ_yDt.mjs +0 -632
- package/dist/main-CyeQ_yDt.mjs.map +0 -1
- package/dist/main-D19Wxs0x.mjs.map +0 -1
- package/dist/main-DSmh1e-V.mjs.map +0 -1
- package/dist/main-JdOvJ70b.mjs.map +0 -1
- package/dist/src-CGmXRfsM.mjs.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { eq, and, or, sql as drizzleSql, inArray, desc } from "drizzle-orm";
|
|
2
|
+
import { LRUCache } from "@/shared/index.js";
|
|
2
3
|
import type { Db } from "@/index.js";
|
|
3
4
|
import type { Preferences, ContactChannel } from "@/contracts/index.js";
|
|
4
5
|
import {
|
|
@@ -21,6 +22,15 @@ import {
|
|
|
21
22
|
adminUsers,
|
|
22
23
|
} from "@/db/schema.js";
|
|
23
24
|
|
|
25
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Applied when a profile is read, not when it is written: an upsert that
|
|
29
|
+
* leaves a field out must keep the stored value rather than reset it.
|
|
30
|
+
*/
|
|
31
|
+
const DEFAULT_LANGUAGE = "en";
|
|
32
|
+
const DEFAULT_TIMEZONE = "UTC";
|
|
33
|
+
|
|
24
34
|
// ─── Domain types ───────────────────────────────────────────────────────────
|
|
25
35
|
|
|
26
36
|
export interface UserProfile {
|
|
@@ -72,6 +82,8 @@ export interface NotificationPreference {
|
|
|
72
82
|
// ─── UserRepository ─────────────────────────────────────────────────────────
|
|
73
83
|
|
|
74
84
|
export class UserRepository {
|
|
85
|
+
private readonly recordCache = new LRUCache<string, UserRecord>(5000, 30_000);
|
|
86
|
+
|
|
75
87
|
constructor(private readonly db: Db) {}
|
|
76
88
|
|
|
77
89
|
async findById(projectId: string, userId: string): Promise<UserProfile | null> {
|
|
@@ -85,76 +97,47 @@ export class UserRepository {
|
|
|
85
97
|
const attrs = rows[0].attributes as any;
|
|
86
98
|
return {
|
|
87
99
|
userId: rows[0].externalId,
|
|
88
|
-
language: attrs.language,
|
|
89
|
-
timezone: attrs.timezone,
|
|
100
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
101
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
90
102
|
email: attrs.email,
|
|
91
103
|
};
|
|
92
104
|
}
|
|
93
105
|
|
|
94
106
|
async findRecordById(projectId: string, userId: string): Promise<UserRecord | null> {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
|
|
99
|
-
if (!userRows[0]) return null;
|
|
107
|
+
const cacheKey = `${projectId}:${userId}`;
|
|
108
|
+
const cached = this.recordCache.get(cacheKey);
|
|
109
|
+
if (cached !== undefined) return cached;
|
|
100
110
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
const records = await this.findRecordsByIds(projectId, [userId]);
|
|
112
|
+
return records[0] ?? null;
|
|
113
|
+
}
|
|
104
114
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
this.db
|
|
108
|
-
.select()
|
|
109
|
-
.from(userTopicPreferences)
|
|
110
|
-
.where(eq(userTopicPreferences.userId, internalId)),
|
|
111
|
-
this.db
|
|
112
|
-
.select()
|
|
113
|
-
.from(userChannelPreferences)
|
|
114
|
-
.where(eq(userChannelPreferences.userId, internalId)),
|
|
115
|
-
this.db.select().from(quietHours).where(eq(quietHours.userId, internalId)),
|
|
116
|
-
]);
|
|
115
|
+
async findRecordsByIds(projectId: string, userIds: string[]): Promise<UserRecord[]> {
|
|
116
|
+
if (userIds.length === 0) return [];
|
|
117
117
|
|
|
118
|
-
const
|
|
118
|
+
const results: UserRecord[] = [];
|
|
119
|
+
const missingIds: string[] = [];
|
|
119
120
|
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
for (const id of userIds) {
|
|
122
|
+
const cacheKey = `${projectId}:${id}`;
|
|
123
|
+
const cached = this.recordCache.get(cacheKey);
|
|
124
|
+
if (cached !== undefined) {
|
|
125
|
+
results.push(cached);
|
|
126
|
+
} else {
|
|
127
|
+
missingIds.push(id);
|
|
128
|
+
}
|
|
123
129
|
}
|
|
124
130
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
channels[r.channel] = r.enabled;
|
|
131
|
+
if (missingIds.length === 0) {
|
|
132
|
+
return results;
|
|
128
133
|
}
|
|
129
134
|
|
|
130
|
-
const quietHoursList = qhRows.map((r) => ({
|
|
131
|
-
start: r.startTime.slice(0, 5),
|
|
132
|
-
end: r.endTime.slice(0, 5),
|
|
133
|
-
}));
|
|
134
|
-
|
|
135
|
-
return {
|
|
136
|
-
userId: userRow.externalId,
|
|
137
|
-
language: attrs.language,
|
|
138
|
-
timezone: attrs.timezone,
|
|
139
|
-
email: attrs.email,
|
|
140
|
-
segments,
|
|
141
|
-
preferences: {
|
|
142
|
-
channels,
|
|
143
|
-
topics,
|
|
144
|
-
quietHours: quietHoursList.length > 0 ? quietHoursList : undefined,
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async findRecordsByIds(projectId: string, userIds: string[]): Promise<UserRecord[]> {
|
|
150
|
-
if (userIds.length === 0) return [];
|
|
151
|
-
|
|
152
135
|
const usersRows = await this.db
|
|
153
136
|
.select()
|
|
154
137
|
.from(users)
|
|
155
|
-
.where(and(inArray(users.externalId,
|
|
138
|
+
.where(and(inArray(users.externalId, missingIds), eq(users.projectId, projectId)));
|
|
156
139
|
|
|
157
|
-
if (usersRows.length === 0) return
|
|
140
|
+
if (usersRows.length === 0) return results;
|
|
158
141
|
|
|
159
142
|
const internalIds = usersRows.map((r) => r.id);
|
|
160
143
|
|
|
@@ -198,16 +181,14 @@ export class UserRepository {
|
|
|
198
181
|
.push({ start: r.startTime.slice(0, 5), end: r.endTime.slice(0, 5) });
|
|
199
182
|
}
|
|
200
183
|
|
|
201
|
-
const userRecords: UserRecord[] = [];
|
|
202
|
-
|
|
203
184
|
for (const userRow of usersRows) {
|
|
204
185
|
const attrs = userRow.attributes as any;
|
|
205
186
|
const internalId = userRow.id;
|
|
206
187
|
|
|
207
|
-
|
|
188
|
+
const record: UserRecord = {
|
|
208
189
|
userId: userRow.externalId,
|
|
209
|
-
language: attrs.language,
|
|
210
|
-
timezone: attrs.timezone,
|
|
190
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
191
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
211
192
|
email: attrs.email,
|
|
212
193
|
segments: segmentsByUserId.get(internalId) || [],
|
|
213
194
|
preferences: {
|
|
@@ -215,10 +196,13 @@ export class UserRepository {
|
|
|
215
196
|
topics: topicsByUserId.get(internalId) || {},
|
|
216
197
|
quietHours: qhByUserId.get(internalId),
|
|
217
198
|
},
|
|
218
|
-
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
this.recordCache.set(`${projectId}:${record.userId}`, record);
|
|
202
|
+
results.push(record);
|
|
219
203
|
}
|
|
220
204
|
|
|
221
|
-
return
|
|
205
|
+
return results;
|
|
222
206
|
}
|
|
223
207
|
|
|
224
208
|
async upsertFull(
|
|
@@ -319,6 +303,7 @@ export class UserRepository {
|
|
|
319
303
|
}
|
|
320
304
|
}
|
|
321
305
|
});
|
|
306
|
+
this.recordCache.delete(`${projectId}:${user.userId}`);
|
|
322
307
|
}
|
|
323
308
|
|
|
324
309
|
async upsertManyFull(
|
|
@@ -334,6 +319,12 @@ export class UserRepository {
|
|
|
334
319
|
): Promise<void> {
|
|
335
320
|
if (usersList.length === 0) return;
|
|
336
321
|
|
|
322
|
+
// ON CONFLICT DO UPDATE rejects the whole statement when two proposed rows
|
|
323
|
+
// hit the same key, so an import naming one user twice failed outright.
|
|
324
|
+
// The last occurrence wins, as it would had they been sent one by one.
|
|
325
|
+
const uniqueUsers = [...new Map(usersList.map((u) => [u.userId, u])).values()];
|
|
326
|
+
const winners = new Set(uniqueUsers);
|
|
327
|
+
|
|
337
328
|
let attempts = 0;
|
|
338
329
|
while (attempts < 3) {
|
|
339
330
|
try {
|
|
@@ -341,7 +332,7 @@ export class UserRepository {
|
|
|
341
332
|
await tx
|
|
342
333
|
.insert(users)
|
|
343
334
|
.values(
|
|
344
|
-
|
|
335
|
+
uniqueUsers.map((u) => ({
|
|
345
336
|
projectId,
|
|
346
337
|
externalId: u.userId,
|
|
347
338
|
attributes: {
|
|
@@ -354,7 +345,9 @@ export class UserRepository {
|
|
|
354
345
|
.onConflictDoUpdate({
|
|
355
346
|
target: [users.projectId, users.externalId],
|
|
356
347
|
set: {
|
|
357
|
-
|
|
348
|
+
// Merged, not replaced: a field the caller left out (absent
|
|
349
|
+
// from the JSON) keeps its stored value.
|
|
350
|
+
attributes: drizzleSql`${users.attributes} || excluded.attributes`,
|
|
358
351
|
updatedAt: new Date(),
|
|
359
352
|
},
|
|
360
353
|
});
|
|
@@ -405,7 +398,12 @@ export class UserRepository {
|
|
|
405
398
|
}
|
|
406
399
|
}
|
|
407
400
|
|
|
408
|
-
|
|
401
|
+
// Quiet hours are replaced, not merged, so only the winning row counts.
|
|
402
|
+
if (
|
|
403
|
+
winners.has(u) &&
|
|
404
|
+
u.preferences?.quietHours &&
|
|
405
|
+
u.preferences.quietHours.length > 0
|
|
406
|
+
) {
|
|
409
407
|
for (const window of u.preferences.quietHours) {
|
|
410
408
|
quietHoursInserts.push({
|
|
411
409
|
userId: internalId,
|
|
@@ -462,7 +460,7 @@ export class UserRepository {
|
|
|
462
460
|
});
|
|
463
461
|
}
|
|
464
462
|
|
|
465
|
-
const usersWithQuietHours =
|
|
463
|
+
const usersWithQuietHours = uniqueUsers.filter(
|
|
466
464
|
(u) => u.preferences?.quietHours !== undefined,
|
|
467
465
|
);
|
|
468
466
|
const internalIdsToClearQuietHours = usersWithQuietHours
|
|
@@ -478,6 +476,9 @@ export class UserRepository {
|
|
|
478
476
|
await tx.insert(quietHours).values(quietHoursInserts);
|
|
479
477
|
}
|
|
480
478
|
});
|
|
479
|
+
for (const u of usersList) {
|
|
480
|
+
this.recordCache.delete(`${projectId}:${u.userId}`);
|
|
481
|
+
}
|
|
481
482
|
return;
|
|
482
483
|
} catch (err: any) {
|
|
483
484
|
attempts++;
|
|
@@ -506,7 +507,8 @@ export class UserRepository {
|
|
|
506
507
|
const attrs = {
|
|
507
508
|
language: patch.language ?? existing.language,
|
|
508
509
|
timezone: patch.timezone ?? existing.timezone,
|
|
509
|
-
|
|
510
|
+
// `null` clears the email; only an absent field keeps the old one.
|
|
511
|
+
email: patch.email !== undefined ? patch.email : existing.email,
|
|
510
512
|
};
|
|
511
513
|
|
|
512
514
|
await this.db.transaction(async (tx) => {
|
|
@@ -582,10 +584,12 @@ export class UserRepository {
|
|
|
582
584
|
}
|
|
583
585
|
});
|
|
584
586
|
|
|
587
|
+
this.recordCache.delete(`${projectId}:${userId}`);
|
|
585
588
|
return true;
|
|
586
589
|
}
|
|
587
590
|
|
|
588
591
|
async delete(projectId: string, userId: string): Promise<boolean> {
|
|
592
|
+
this.recordCache.delete(`${projectId}:${userId}`);
|
|
589
593
|
const result = await this.db
|
|
590
594
|
.delete(users)
|
|
591
595
|
.where(and(eq(users.externalId, userId), eq(users.projectId, projectId)))
|
|
@@ -607,11 +611,14 @@ export class UserRepository {
|
|
|
607
611
|
): Promise<{ users: UserProfile[]; nextCursor: string | null }> {
|
|
608
612
|
const conditions = [eq(users.projectId, projectId)];
|
|
609
613
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
614
|
+
// The cursor is the id of the last row returned. Paging on created_at
|
|
615
|
+
// alone skipped rows: a bulk import writes every user in one transaction,
|
|
616
|
+
// so they share one created_at, and the millisecond cursor could not tell
|
|
617
|
+
// them apart. (created_at, id) is unique and ordered.
|
|
618
|
+
if (cursor && UUID_RE.test(cursor)) {
|
|
619
|
+
conditions.push(
|
|
620
|
+
drizzleSql`(${users.createdAt}, ${users.id}) < (SELECT c.created_at, c.id FROM ${users} c WHERE c.id = ${cursor})`,
|
|
621
|
+
);
|
|
615
622
|
}
|
|
616
623
|
|
|
617
624
|
if (filters?.language) {
|
|
@@ -623,7 +630,8 @@ export class UserRepository {
|
|
|
623
630
|
}
|
|
624
631
|
|
|
625
632
|
if (filters?.search) {
|
|
626
|
-
|
|
633
|
+
// `%`, `_` and `\` in the search are literal, not wildcards.
|
|
634
|
+
const term = `%${filters.search.trim().replace(/[\\%_]/g, "\\$&")}%`;
|
|
627
635
|
conditions.push(
|
|
628
636
|
drizzleSql`(${users.externalId} ILIKE ${term} OR (${users.attributes}->>'email') ILIKE ${term})`,
|
|
629
637
|
);
|
|
@@ -645,22 +653,21 @@ export class UserRepository {
|
|
|
645
653
|
.select()
|
|
646
654
|
.from(users)
|
|
647
655
|
.where(and(...conditions))
|
|
648
|
-
.orderBy(desc(users.createdAt))
|
|
656
|
+
.orderBy(desc(users.createdAt), desc(users.id))
|
|
649
657
|
.limit(limit);
|
|
650
658
|
|
|
651
659
|
const items = rows.map((r) => {
|
|
652
660
|
const attrs = r.attributes as any;
|
|
653
661
|
return {
|
|
654
662
|
userId: r.externalId,
|
|
655
|
-
language: attrs.language,
|
|
656
|
-
timezone: attrs.timezone,
|
|
663
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
664
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
657
665
|
email: attrs.email,
|
|
658
666
|
createdAt: r.createdAt.getTime(),
|
|
659
667
|
};
|
|
660
668
|
});
|
|
661
669
|
|
|
662
|
-
const nextCursor =
|
|
663
|
-
items.length === limit ? items[items.length - 1]!.createdAt.toString() : null;
|
|
670
|
+
const nextCursor = rows.length === limit ? rows[rows.length - 1]!.id : null;
|
|
664
671
|
return { users: items, nextCursor };
|
|
665
672
|
}
|
|
666
673
|
|
|
@@ -726,6 +733,8 @@ export class PreferenceRepository {
|
|
|
726
733
|
// ─── ContactRepository ───────────────────────────────────────────────────────
|
|
727
734
|
|
|
728
735
|
export class ContactRepository {
|
|
736
|
+
private readonly activeContactsCache = new LRUCache<string, UserContact[]>(5000, 30_000);
|
|
737
|
+
|
|
729
738
|
constructor(private readonly db: Db) {}
|
|
730
739
|
|
|
731
740
|
async findByUserId(projectId: string, userId: string): Promise<UserContact[]> {
|
|
@@ -776,6 +785,23 @@ export class ContactRepository {
|
|
|
776
785
|
const byUser = new Map<string, UserContact[]>();
|
|
777
786
|
if (userIds.length === 0) return byUser;
|
|
778
787
|
|
|
788
|
+
const missingIds: string[] = [];
|
|
789
|
+
for (const userId of userIds) {
|
|
790
|
+
const cacheKey = `${projectId}:${userId}`;
|
|
791
|
+
const cached = this.activeContactsCache.get(cacheKey);
|
|
792
|
+
if (cached !== undefined) {
|
|
793
|
+
if (cached.length > 0) {
|
|
794
|
+
byUser.set(userId, cached);
|
|
795
|
+
}
|
|
796
|
+
} else {
|
|
797
|
+
missingIds.push(userId);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
if (missingIds.length === 0) {
|
|
802
|
+
return byUser;
|
|
803
|
+
}
|
|
804
|
+
|
|
779
805
|
const rows = await this.db
|
|
780
806
|
.select({
|
|
781
807
|
userId: users.externalId,
|
|
@@ -789,23 +815,57 @@ export class ContactRepository {
|
|
|
789
815
|
.where(
|
|
790
816
|
and(
|
|
791
817
|
eq(users.projectId, projectId),
|
|
792
|
-
inArray(users.externalId,
|
|
818
|
+
inArray(users.externalId, missingIds),
|
|
793
819
|
eq(userContacts.enabled, true),
|
|
794
820
|
),
|
|
795
821
|
);
|
|
796
822
|
|
|
823
|
+
// Per-address topic opt-outs (a user who wants marketing at home but not at
|
|
824
|
+
// work). Without them the pipeline cannot honour an opt-out it accepted.
|
|
825
|
+
const topicsByContact = new Map<string, Record<string, boolean>>();
|
|
826
|
+
if (rows.length > 0) {
|
|
827
|
+
const topicRows = await this.db
|
|
828
|
+
.select()
|
|
829
|
+
.from(contactTopicPreferences)
|
|
830
|
+
.where(
|
|
831
|
+
inArray(
|
|
832
|
+
contactTopicPreferences.contactId,
|
|
833
|
+
rows.map((r) => r.id),
|
|
834
|
+
),
|
|
835
|
+
);
|
|
836
|
+
for (const t of topicRows) {
|
|
837
|
+
if (!topicsByContact.has(t.contactId)) topicsByContact.set(t.contactId, {});
|
|
838
|
+
topicsByContact.get(t.contactId)![t.topic] = t.enabled;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const fetchedByUser = new Map<string, UserContact[]>();
|
|
843
|
+
for (const id of missingIds) {
|
|
844
|
+
fetchedByUser.set(id, []);
|
|
845
|
+
}
|
|
846
|
+
|
|
797
847
|
for (const row of rows) {
|
|
798
|
-
const contacts =
|
|
848
|
+
const contacts = fetchedByUser.get(row.userId) ?? [];
|
|
849
|
+
const topics = topicsByContact.get(row.id);
|
|
799
850
|
contacts.push({
|
|
800
851
|
id: row.id,
|
|
801
852
|
userId: row.userId,
|
|
802
853
|
channel: row.channel as ContactChannel,
|
|
803
854
|
target: row.target,
|
|
804
|
-
preferences: {},
|
|
855
|
+
preferences: topics ? { topics } : {},
|
|
805
856
|
active: row.enabled,
|
|
806
857
|
});
|
|
807
|
-
|
|
858
|
+
fetchedByUser.set(row.userId, contacts);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
for (const id of missingIds) {
|
|
862
|
+
const contacts = fetchedByUser.get(id) ?? [];
|
|
863
|
+
this.activeContactsCache.set(`${projectId}:${id}`, contacts);
|
|
864
|
+
if (contacts.length > 0) {
|
|
865
|
+
byUser.set(id, contacts);
|
|
866
|
+
}
|
|
808
867
|
}
|
|
868
|
+
|
|
809
869
|
return byUser;
|
|
810
870
|
}
|
|
811
871
|
|
|
@@ -854,6 +914,7 @@ export class ContactRepository {
|
|
|
854
914
|
set: { enabled: drizzleSql`excluded.enabled` },
|
|
855
915
|
});
|
|
856
916
|
}
|
|
917
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
857
918
|
}
|
|
858
919
|
|
|
859
920
|
async upsertMany(
|
|
@@ -882,7 +943,29 @@ export class ContactRepository {
|
|
|
882
943
|
|
|
883
944
|
const idMap = new Map(internalUserIdRows.map((r) => [r.externalId, r.id]));
|
|
884
945
|
|
|
885
|
-
|
|
946
|
+
// The same address can arrive twice — as `email` and again in `contacts`
|
|
947
|
+
// — and ON CONFLICT DO UPDATE rejects a statement that touches one row
|
|
948
|
+
// twice. Keep one entry per address, merging their topic preferences.
|
|
949
|
+
const byAddress = new Map<string, (typeof contactsList)[number]>();
|
|
950
|
+
for (const c of contactsList) {
|
|
951
|
+
if (!idMap.has(c.userId)) continue;
|
|
952
|
+
const key = `${c.userId}\u0000${c.channel}\u0000${c.target}`;
|
|
953
|
+
const seen = byAddress.get(key);
|
|
954
|
+
byAddress.set(
|
|
955
|
+
key,
|
|
956
|
+
seen
|
|
957
|
+
? {
|
|
958
|
+
...c,
|
|
959
|
+
preferences: {
|
|
960
|
+
...seen.preferences,
|
|
961
|
+
...c.preferences,
|
|
962
|
+
topics: { ...seen.preferences?.topics, ...c.preferences?.topics },
|
|
963
|
+
},
|
|
964
|
+
}
|
|
965
|
+
: c,
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
const validContacts = [...byAddress.values()];
|
|
886
969
|
if (validContacts.length === 0) return;
|
|
887
970
|
|
|
888
971
|
const inserted = await this.db
|
|
@@ -931,6 +1014,9 @@ export class ContactRepository {
|
|
|
931
1014
|
set: { enabled: drizzleSql`excluded.enabled` },
|
|
932
1015
|
});
|
|
933
1016
|
}
|
|
1017
|
+
for (const c of validContacts) {
|
|
1018
|
+
this.activeContactsCache.delete(`${projectId}:${c.userId}`);
|
|
1019
|
+
}
|
|
934
1020
|
}
|
|
935
1021
|
|
|
936
1022
|
/**
|
|
@@ -944,6 +1030,7 @@ export class ContactRepository {
|
|
|
944
1030
|
channel: ContactChannel,
|
|
945
1031
|
target: string,
|
|
946
1032
|
): Promise<boolean> {
|
|
1033
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
947
1034
|
const internalUserIdRows = await this.db
|
|
948
1035
|
.select({ id: users.id })
|
|
949
1036
|
.from(users)
|
|
@@ -971,6 +1058,7 @@ export class ContactRepository {
|
|
|
971
1058
|
channel: ContactChannel,
|
|
972
1059
|
target: string,
|
|
973
1060
|
): Promise<boolean> {
|
|
1061
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
974
1062
|
const internalUserIdRows = await this.db
|
|
975
1063
|
.select({ id: users.id })
|
|
976
1064
|
.from(users)
|
|
@@ -1098,6 +1186,7 @@ export class ProjectRepository {
|
|
|
1098
1186
|
await tx.delete(suppressions).where(eq(suppressions.projectId, id));
|
|
1099
1187
|
await tx.delete(messageLogs).where(eq(messageLogs.projectId, id));
|
|
1100
1188
|
await tx.delete(workflowInstances).where(eq(workflowInstances.projectId, id));
|
|
1189
|
+
await tx.delete(workflowDefinitions).where(eq(workflowDefinitions.projectId, id));
|
|
1101
1190
|
|
|
1102
1191
|
const result = await tx.delete(projects).where(eq(projects.id, id)).returning();
|
|
1103
1192
|
return result.length > 0;
|
|
@@ -1214,7 +1303,7 @@ export class WorkflowRepository {
|
|
|
1214
1303
|
return await this.db.transaction(async (tx) => {
|
|
1215
1304
|
const result = await tx
|
|
1216
1305
|
.update(workflowInstances)
|
|
1217
|
-
.set({ status: "canceled"
|
|
1306
|
+
.set({ status: "canceled" })
|
|
1218
1307
|
.where(
|
|
1219
1308
|
and(
|
|
1220
1309
|
eq(workflowInstances.id, instanceId),
|
package/src/server.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { EventEmitter } from "node:events";
|
|
|
2
2
|
import { registerTransport, type Transport } from "./transport/index.js";
|
|
3
3
|
import { globalEmitter } from "./shared/index.js";
|
|
4
4
|
import { createLogger, type Logger } from "./logger/index.js";
|
|
5
|
+
import { setGlobalConfig, readBaseConfig, setAiConfig } from "./config/index.js";
|
|
6
|
+
import { createDatabase, runMigrations } from "./db/index.js";
|
|
5
7
|
import type { LanguageModel } from "ai";
|
|
6
8
|
|
|
7
9
|
export interface NotifkitOptions {
|
|
@@ -10,6 +12,11 @@ export interface NotifkitOptions {
|
|
|
10
12
|
port?: number;
|
|
11
13
|
logLevel?: "fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent";
|
|
12
14
|
nodeEnv?: "development" | "test" | "production";
|
|
15
|
+
adminUser?: {
|
|
16
|
+
email: string;
|
|
17
|
+
password: string;
|
|
18
|
+
username?: string;
|
|
19
|
+
};
|
|
13
20
|
services: (
|
|
14
21
|
"api" | "delivery" | "engine" | "enricher" | "scheduler" | "ai" | "workflow" | "events" | "all"
|
|
15
22
|
)[];
|
|
@@ -17,6 +24,18 @@ export interface NotifkitOptions {
|
|
|
17
24
|
autoMigrate?: boolean;
|
|
18
25
|
aiModel?: LanguageModel;
|
|
19
26
|
workerConcurrency?: number;
|
|
27
|
+
/** In-flight sends per delivery worker. Defaults to `workerConcurrency`. */
|
|
28
|
+
deliveryConcurrency?: number;
|
|
29
|
+
/**
|
|
30
|
+
* When enricher, engine and delivery all run in this process, pass
|
|
31
|
+
* single-recipient notifications between them in memory rather than through
|
|
32
|
+
* the enriched and outbound streams: two fewer write/read/ack round trips and
|
|
33
|
+
* serialisations per message. The inbound stream entry stays pending until
|
|
34
|
+
* delivery finishes, so a crash replays the chain from the enricher.
|
|
35
|
+
* Fan-outs, scheduled sends, AI and retries still use the streams.
|
|
36
|
+
* Also settable with `PIPELINE_FUSED=true`.
|
|
37
|
+
*/
|
|
38
|
+
fusedPipeline?: boolean;
|
|
20
39
|
redisOptions?: {
|
|
21
40
|
maxQueueLength?: number;
|
|
22
41
|
};
|
|
@@ -65,12 +84,20 @@ export class NotifkitServer extends EventEmitter {
|
|
|
65
84
|
|
|
66
85
|
async start() {
|
|
67
86
|
// 1. Initial configuration setup for environment overrides
|
|
68
|
-
const { setGlobalConfig, readBaseConfig } = await import("./config/index.js");
|
|
69
87
|
if (this.options.port) process.env.PORT = String(this.options.port);
|
|
70
88
|
if (this.options.logLevel) process.env.LOG_LEVEL = this.options.logLevel;
|
|
71
89
|
if (this.options.nodeEnv) process.env.NODE_ENV = this.options.nodeEnv;
|
|
90
|
+
if (this.options.adminUser?.email) process.env.ADMIN_EMAIL = this.options.adminUser.email;
|
|
91
|
+
if (this.options.adminUser?.password)
|
|
92
|
+
process.env.ADMIN_PASSWORD = this.options.adminUser.password;
|
|
93
|
+
if (this.options.adminUser?.username)
|
|
94
|
+
process.env.ADMIN_USERNAME = this.options.adminUser.username;
|
|
72
95
|
if (this.options.workerConcurrency)
|
|
73
96
|
process.env.WORKER_CONCURRENCY = String(this.options.workerConcurrency);
|
|
97
|
+
if (this.options.fusedPipeline !== undefined)
|
|
98
|
+
process.env.PIPELINE_FUSED = String(this.options.fusedPipeline);
|
|
99
|
+
if (this.options.deliveryConcurrency)
|
|
100
|
+
process.env.DELIVERY_CONCURRENCY = String(this.options.deliveryConcurrency);
|
|
74
101
|
if (this.options.redisOptions?.maxQueueLength)
|
|
75
102
|
process.env.QUEUE_MAX_LEN = String(this.options.redisOptions.maxQueueLength);
|
|
76
103
|
if (this.options.dbOptions?.maxConnections)
|
|
@@ -120,14 +147,12 @@ export class NotifkitServer extends EventEmitter {
|
|
|
120
147
|
const finalConfig = readBaseConfig();
|
|
121
148
|
setGlobalConfig(finalConfig);
|
|
122
149
|
if (this.options.aiModel) {
|
|
123
|
-
const { setAiConfig } = await import("./config/index.js");
|
|
124
150
|
setAiConfig({ aiModel: this.options.aiModel });
|
|
125
151
|
}
|
|
126
152
|
|
|
127
153
|
// 4. Run database migrations if enabled
|
|
128
154
|
if (this.options.autoMigrate !== false) {
|
|
129
155
|
this.logger.info("Running database migrations...");
|
|
130
|
-
const { createDatabase, runMigrations } = await import("./db/index.js");
|
|
131
156
|
const { db, sql } = createDatabase({ url: this.options.databaseUrl! });
|
|
132
157
|
await runMigrations(db);
|
|
133
158
|
await sql.end();
|
|
@@ -206,6 +231,29 @@ export class NotifkitServer extends EventEmitter {
|
|
|
206
231
|
}
|
|
207
232
|
|
|
208
233
|
await Promise.all(startupPromises);
|
|
234
|
+
|
|
235
|
+
if (
|
|
236
|
+
readBaseConfig().PIPELINE_FUSED &&
|
|
237
|
+
services.includes("enricher") &&
|
|
238
|
+
services.includes("engine") &&
|
|
239
|
+
services.includes("delivery")
|
|
240
|
+
) {
|
|
241
|
+
const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all(
|
|
242
|
+
[
|
|
243
|
+
import("./services/enricher/main.js"),
|
|
244
|
+
import("./services/engine/main.js"),
|
|
245
|
+
import("./services/delivery/main.js"),
|
|
246
|
+
],
|
|
247
|
+
);
|
|
248
|
+
const enricher = getEnricherWorker();
|
|
249
|
+
const engine = getEngineWorker();
|
|
250
|
+
const delivery = getDeliveryWorker();
|
|
251
|
+
if (enricher && engine && delivery) {
|
|
252
|
+
engine.setInlineNext((event) => delivery.processInline(event));
|
|
253
|
+
enricher.setInlineNext((event) => engine.processInline(event));
|
|
254
|
+
this.logger.info("pipeline fused: enricher → engine → delivery hand off in-process");
|
|
255
|
+
}
|
|
256
|
+
}
|
|
209
257
|
}
|
|
210
258
|
|
|
211
259
|
async stop() {
|
|
@@ -225,44 +273,26 @@ export class NotifkitServer extends EventEmitter {
|
|
|
225
273
|
? ["api", "delivery", "engine", "enricher", "scheduler", "ai", "workflow", "events"]
|
|
226
274
|
: this.options.services;
|
|
227
275
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
await
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if (services.includes("scheduler")) {
|
|
249
|
-
const { stopSchedulerWorker } = await import("./services/scheduler/main.js");
|
|
250
|
-
await stopSchedulerWorker();
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (services.includes("ai")) {
|
|
254
|
-
const { stopAiWorker } = await import("./services/ai/main.js");
|
|
255
|
-
await stopAiWorker();
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (services.includes("workflow")) {
|
|
259
|
-
const { stopWorkflowWorker } = await import("./services/workflow/main.js");
|
|
260
|
-
await stopWorkflowWorker();
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (services.includes("events")) {
|
|
264
|
-
const { stopEventWorker } = await import("./services/events/main.js");
|
|
265
|
-
await stopEventWorker();
|
|
276
|
+
// Upstream first. With the pipeline fused, the enricher's task runs the
|
|
277
|
+
// engine and delivery inline, so delivery's connections must outlive it;
|
|
278
|
+
// stopping delivery first failed those sends mid-flight, after the
|
|
279
|
+
// provider may already have accepted them. Events goes last because it
|
|
280
|
+
// records what delivery did.
|
|
281
|
+
const shutdownOrder: [string, () => Promise<void>][] = [
|
|
282
|
+
["api", async () => (await import("./services/api/main.js")).stopApiServer()],
|
|
283
|
+
["workflow", async () => (await import("./services/workflow/main.js")).stopWorkflowWorker()],
|
|
284
|
+
["enricher", async () => (await import("./services/enricher/main.js")).stopEnricherWorker()],
|
|
285
|
+
["ai", async () => (await import("./services/ai/main.js")).stopAiWorker()],
|
|
286
|
+
["engine", async () => (await import("./services/engine/main.js")).stopEngineWorker()],
|
|
287
|
+
[
|
|
288
|
+
"scheduler",
|
|
289
|
+
async () => (await import("./services/scheduler/main.js")).stopSchedulerWorker(),
|
|
290
|
+
],
|
|
291
|
+
["delivery", async () => (await import("./services/delivery/main.js")).stopDeliveryWorker()],
|
|
292
|
+
["events", async () => (await import("./services/events/main.js")).stopEventWorker()],
|
|
293
|
+
];
|
|
294
|
+
for (const [name, stop] of shutdownOrder) {
|
|
295
|
+
if (services.includes(name)) await stop();
|
|
266
296
|
}
|
|
267
297
|
|
|
268
298
|
if (this.pgContainer) {
|