notifkit 0.1.14 → 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.
Files changed (89) hide show
  1. package/dashboard/dist/assets/index-C-DJYu0_.css +1 -0
  2. package/dashboard/dist/assets/index-CzFZcEon.js +318 -0
  3. package/dashboard/dist/index.html +2 -2
  4. package/dist/{bootstrap-m107TukN.mjs → bootstrap-BMDRPjgp.mjs} +1 -1
  5. package/dist/{bulk-xAd_biYV.mjs → bulk-CJ5QXiPa.mjs} +1 -1
  6. package/dist/index.d.mts +71 -27
  7. package/dist/index.mjs +2 -2
  8. package/dist/{main-CWIBUEc7.mjs → main-6_bXQBDH.mjs} +21 -41
  9. package/dist/{main-WkxXwP5q.mjs → main-8nP3qmCT.mjs} +29 -49
  10. package/dist/{main-HT5c6jyf.mjs → main-B82bsFoy.mjs} +183 -334
  11. package/dist/{main-C87TSAqM.mjs → main-CpaHF1Zw.mjs} +13 -15
  12. package/dist/{main-DRHL2bst.mjs → main-DULD8-th.mjs} +8 -6
  13. package/dist/{main-D_62f9fH.mjs → main-ZW5RaiOK.mjs} +77 -64
  14. package/dist/{main-5kVwbbnW.mjs → main-g1KEcXXp.mjs} +676 -582
  15. package/dist/{main-C0MjK1Au.mjs → main-mGIT8jv4.mjs} +4 -4
  16. package/dist/schedule-or-dispatch-Cd7GLfc3.mjs +52 -0
  17. package/dist/{src-DYqdD694.mjs → src-BE_EUpZ6.mjs} +93 -93
  18. package/package.json +1 -1
  19. package/scripts/create-admin.mjs +0 -0
  20. package/scripts/create-project.mjs +6 -2
  21. package/scripts/migrate.mjs +0 -0
  22. package/scripts/notifkit.mjs +0 -0
  23. package/src/client.ts +3 -3
  24. package/src/config/index.ts +1 -0
  25. package/src/contracts/common.ts +2 -0
  26. package/src/contracts/envelope.ts +1 -1
  27. package/src/contracts/events/notification-created.ts +2 -0
  28. package/src/contracts/events/notification-delivered.ts +1 -1
  29. package/src/contracts/events/notification-failed.ts +1 -1
  30. package/src/contracts/events/notification-requested.ts +3 -2
  31. package/src/contracts/helpers.ts +1 -2
  32. package/src/contracts/index.ts +1 -0
  33. package/src/contracts/registry.ts +1 -0
  34. package/src/contracts/sdk.ts +8 -37
  35. package/src/db/schema.ts +8 -13
  36. package/src/idempotency/index.ts +6 -8
  37. package/src/metrics/index.ts +2 -0
  38. package/src/rate-limiter/api-rate-limiter.ts +208 -0
  39. package/src/rate-limiter/index.ts +3 -411
  40. package/src/rate-limiter/project-settings-cache.ts +99 -0
  41. package/src/rate-limiter/user-throttle.ts +100 -0
  42. package/src/redis/index.ts +10 -3
  43. package/src/repositories/admin-user.ts +83 -0
  44. package/src/repositories/contact.ts +359 -0
  45. package/src/repositories/index.ts +9 -1416
  46. package/src/repositories/preference.ts +35 -0
  47. package/src/repositories/project.ts +110 -0
  48. package/src/repositories/segment.ts +16 -0
  49. package/src/repositories/template.ts +72 -0
  50. package/src/repositories/types.ts +49 -0
  51. package/src/repositories/user.ts +644 -0
  52. package/src/repositories/workflow.ts +64 -0
  53. package/src/services/ai/main.ts +29 -71
  54. package/src/services/api/admin-static.ts +4 -1
  55. package/src/services/api/handlers/auth.ts +109 -0
  56. package/src/services/api/handlers/campaigns.ts +191 -0
  57. package/src/services/api/handlers/events.ts +99 -0
  58. package/src/services/api/handlers/notifications.ts +356 -0
  59. package/src/services/api/handlers/projects.ts +116 -0
  60. package/src/services/api/handlers/shared.ts +262 -0
  61. package/src/services/api/handlers/suppressions.ts +95 -0
  62. package/src/services/api/handlers/system.ts +314 -0
  63. package/src/services/api/handlers/templates.ts +97 -0
  64. package/src/services/api/handlers/unsubscribe.ts +215 -0
  65. package/src/services/api/handlers/users.ts +262 -0
  66. package/src/services/api/handlers/workflows.ts +150 -0
  67. package/src/services/api/handlers.ts +25 -2190
  68. package/src/services/api/health.ts +133 -0
  69. package/src/services/api/http.ts +12 -0
  70. package/src/services/api/main.ts +32 -322
  71. package/src/services/api/router.ts +1 -1
  72. package/src/services/api/webhooks.ts +172 -0
  73. package/src/services/delivery/main.ts +256 -498
  74. package/src/services/engine/main.ts +31 -235
  75. package/src/services/engine/quiet-hours.ts +151 -0
  76. package/src/services/engine/schedule-or-dispatch.ts +104 -0
  77. package/src/services/enricher/main.ts +9 -21
  78. package/src/services/events/main.ts +2 -2
  79. package/src/services/scheduler/main.ts +16 -25
  80. package/src/services/workflow/duration.ts +24 -0
  81. package/src/services/workflow/main.ts +6 -164
  82. package/src/services/workflow/timers.ts +157 -0
  83. package/src/shared/events.ts +3 -2
  84. package/src/templates/render.ts +1 -0
  85. package/src/workers/index.ts +7 -4
  86. package/src/workflows/sdk.ts +1 -0
  87. package/dashboard/README.md +0 -29
  88. package/dashboard/dist/assets/index-ggpJKZqZ.js +0 -315
  89. package/dashboard/dist/assets/index-s-SXGIwv.css +0 -1
@@ -0,0 +1,35 @@
1
+ import { eq, and } from "drizzle-orm";
2
+ import type { Db } from "@/index.js";
3
+ import { users, userTopicPreferences } from "@/db/schema.js";
4
+ import type { NotificationPreference } from "./types.js";
5
+
6
+ /** @deprecated Unused; removed in the next minor version. */
7
+ export class PreferenceRepository {
8
+ constructor(private readonly db: Db) {}
9
+
10
+ async isOptedIn(projectId: string, userId: string, eventType: string): Promise<boolean> {
11
+ const prefs = await this.findByUserId(projectId, userId);
12
+ const pref = prefs.find((p) => p.eventType === eventType);
13
+ return pref ? pref.optedIn : true;
14
+ }
15
+
16
+ async findByUserId(projectId: string, userId: string): Promise<NotificationPreference[]> {
17
+ const internalUserIdRows = await this.db
18
+ .select({ id: users.id })
19
+ .from(users)
20
+ .where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
21
+ const internalId = internalUserIdRows[0]?.id;
22
+ if (!internalId) return [];
23
+
24
+ const rows = await this.db
25
+ .select()
26
+ .from(userTopicPreferences)
27
+ .where(eq(userTopicPreferences.userId, internalId));
28
+
29
+ return rows.map((r) => ({
30
+ userId,
31
+ eventType: r.topic,
32
+ optedIn: r.enabled,
33
+ }));
34
+ }
35
+ }
@@ -0,0 +1,110 @@
1
+ import { eq, and, desc } from "drizzle-orm";
2
+ import type { Db } from "@/index.js";
3
+ import {
4
+ users,
5
+ projects,
6
+ workflowInstances,
7
+ workflowDefinitions,
8
+ projectApiKeys,
9
+ suppressions,
10
+ messageLogs,
11
+ } from "@/db/schema.js";
12
+
13
+ export class ProjectRepository {
14
+ constructor(private readonly db: Db) {}
15
+
16
+ async list(): Promise<any[]> {
17
+ return this.db
18
+ .select({
19
+ id: projects.id,
20
+ name: projects.name,
21
+ rateLimitRpm: projects.rateLimitRpm,
22
+ throttleLimit: projects.throttleLimit,
23
+ throttleWindowHours: projects.throttleWindowHours,
24
+ createdAt: projects.createdAt,
25
+ })
26
+ .from(projects)
27
+ .orderBy(desc(projects.createdAt));
28
+ }
29
+
30
+ async delete(id: string): Promise<boolean> {
31
+ return await this.db.transaction(async (tx) => {
32
+ // Contacts, segments, preferences and quiet hours cascade on users.id.
33
+ await tx.delete(users).where(eq(users.projectId, id));
34
+ await tx.delete(suppressions).where(eq(suppressions.projectId, id));
35
+ await tx.delete(messageLogs).where(eq(messageLogs.projectId, id));
36
+ await tx.delete(workflowInstances).where(eq(workflowInstances.projectId, id));
37
+ await tx.delete(workflowDefinitions).where(eq(workflowDefinitions.projectId, id));
38
+
39
+ const result = await tx.delete(projects).where(eq(projects.id, id)).returning();
40
+ return result.length > 0;
41
+ });
42
+ }
43
+
44
+ /**
45
+ * Throttle overrides only. Kept narrow because the engine calls this once per
46
+ * notification (behind a cache) and has no use for the rest of the row.
47
+ */
48
+ async findThrottleSettings(
49
+ id: string,
50
+ ): Promise<{ throttleLimit: number | null; throttleWindowHours: number | null } | null> {
51
+ const rows = await this.db
52
+ .select({
53
+ throttleLimit: projects.throttleLimit,
54
+ throttleWindowHours: projects.throttleWindowHours,
55
+ })
56
+ .from(projects)
57
+ .where(eq(projects.id, id))
58
+ .limit(1);
59
+
60
+ return rows[0] ?? null;
61
+ }
62
+
63
+ async updateSettings(
64
+ id: string,
65
+ settings: {
66
+ rateLimitRpm?: number | null;
67
+ throttleLimit?: number | null;
68
+ throttleWindowHours?: number | null;
69
+ },
70
+ ): Promise<boolean> {
71
+ const result = await this.db
72
+ .update(projects)
73
+ .set(settings)
74
+ .where(eq(projects.id, id))
75
+ .returning();
76
+ return result.length > 0;
77
+ }
78
+
79
+ async createApiKey(
80
+ projectId: string,
81
+ keyHash: string,
82
+ role: "admin" | "read_only" = "admin",
83
+ ): Promise<{ id: string }> {
84
+ const result = await this.db
85
+ .insert(projectApiKeys)
86
+ .values({ projectId, keyHash, role })
87
+ .returning();
88
+ return { id: result[0]!.id };
89
+ }
90
+
91
+ async listApiKeys(projectId: string): Promise<any[]> {
92
+ return this.db
93
+ .select({
94
+ id: projectApiKeys.id,
95
+ role: projectApiKeys.role,
96
+ createdAt: projectApiKeys.createdAt,
97
+ })
98
+ .from(projectApiKeys)
99
+ .where(eq(projectApiKeys.projectId, projectId))
100
+ .orderBy(desc(projectApiKeys.createdAt));
101
+ }
102
+
103
+ async deleteApiKey(projectId: string, keyId: string): Promise<boolean> {
104
+ const result = await this.db
105
+ .delete(projectApiKeys)
106
+ .where(and(eq(projectApiKeys.id, keyId), eq(projectApiKeys.projectId, projectId)))
107
+ .returning();
108
+ return result.length > 0;
109
+ }
110
+ }
@@ -0,0 +1,16 @@
1
+ import { sql as drizzleSql } from "drizzle-orm";
2
+ import type { Db } from "@/index.js";
3
+
4
+ export class SegmentRepository {
5
+ constructor(private readonly db: Db) {}
6
+
7
+ async listSegments(projectId: string): Promise<string[]> {
8
+ const rows = await this.db.execute(drizzleSql`
9
+ SELECT DISTINCT s.segment
10
+ FROM user_segments s
11
+ JOIN users u ON u.id = s.user_id
12
+ WHERE u.project_id = ${projectId}
13
+ `);
14
+ return (rows as any[]).map((r) => r.segment);
15
+ }
16
+ }
@@ -0,0 +1,72 @@
1
+ import { eq, and, sql as drizzleSql } from "drizzle-orm";
2
+ import type { Db } from "@/index.js";
3
+ import { templates } from "@/db/schema.js";
4
+ import type { TemplateRecord } from "./types.js";
5
+
6
+ export class TemplateRepository {
7
+ constructor(private readonly db: Db) {}
8
+
9
+ async findById(projectId: string, id: string): Promise<TemplateRecord | null> {
10
+ const rows = await this.db
11
+ .select()
12
+ .from(templates)
13
+ .where(and(eq(templates.id, id), eq(templates.projectId, projectId)))
14
+ .limit(1);
15
+ if (!rows[0]) return null;
16
+ return {
17
+ id: rows[0].id,
18
+ channel: rows[0].channel,
19
+ content: rows[0].content,
20
+ topics: (rows[0].topics ?? []) as string[],
21
+ aiPrompts: rows[0].aiPrompts as Record<string, string> | null,
22
+ };
23
+ }
24
+
25
+ async list(projectId: string): Promise<TemplateRecord[]> {
26
+ const rows = await this.db.select().from(templates).where(eq(templates.projectId, projectId));
27
+ return rows.map((r) => ({
28
+ id: r.id,
29
+ channel: r.channel,
30
+ content: r.content,
31
+ topics: (r.topics ?? []) as string[],
32
+ aiPrompts: r.aiPrompts as Record<string, string> | null,
33
+ }));
34
+ }
35
+
36
+ async upsertMany(projectId: string, templateList: any[]): Promise<number> {
37
+ if (templateList.length === 0) return 0;
38
+
39
+ const values = templateList.map((t) => ({
40
+ projectId,
41
+ id: t.id,
42
+ channel: t.channel as any,
43
+ topics: t.topics ?? [],
44
+ content: t.content,
45
+ aiPrompts: t.aiPrompts,
46
+ }));
47
+
48
+ await this.db
49
+ .insert(templates)
50
+ .values(values)
51
+ .onConflictDoUpdate({
52
+ target: [templates.projectId, templates.id],
53
+ set: {
54
+ channel: drizzleSql`excluded.channel`,
55
+ topics: drizzleSql`excluded.topics`,
56
+ content: drizzleSql`excluded.content`,
57
+ aiPrompts: drizzleSql`excluded.ai_prompts`,
58
+ updatedAt: new Date(),
59
+ },
60
+ });
61
+
62
+ return templateList.length;
63
+ }
64
+
65
+ async delete(projectId: string, id: string): Promise<boolean> {
66
+ const result = await this.db
67
+ .delete(templates)
68
+ .where(and(eq(templates.id, id), eq(templates.projectId, projectId)))
69
+ .returning();
70
+ return result.length > 0;
71
+ }
72
+ }
@@ -0,0 +1,49 @@
1
+ import type { Preferences, ContactChannel } from "@/contracts/index.js";
2
+
3
+ export interface UserProfile {
4
+ userId: string; // Maps to externalId
5
+ language?: string;
6
+ timezone?: string;
7
+ email?: string | null;
8
+ }
9
+
10
+ export interface UserRecord extends UserProfile {
11
+ segments: string[];
12
+ preferences: Preferences;
13
+ }
14
+
15
+ export interface UserContact {
16
+ id: string; // uuid
17
+ userId: string; // externalId
18
+ channel: ContactChannel;
19
+ target: string;
20
+ preferences: Preferences;
21
+ active: boolean;
22
+ }
23
+
24
+ export interface TemplateRecord {
25
+ id: string;
26
+ channel: string;
27
+ topics: string[];
28
+ content: unknown;
29
+ /** Template-level AI prompts, merged with per-request ones by the engine. */
30
+ aiPrompts?: Record<string, string> | null;
31
+ }
32
+
33
+ /** @deprecated Unused; removed in the next minor version. */
34
+ export type DevicePlatform = "fcm" | "apns" | "web";
35
+
36
+ /** @deprecated Unused; removed in the next minor version. */
37
+ export interface DeviceToken {
38
+ id: string;
39
+ userId: string;
40
+ deviceToken: string;
41
+ platform: DevicePlatform;
42
+ active: boolean;
43
+ }
44
+
45
+ export interface NotificationPreference {
46
+ userId: string;
47
+ eventType: string;
48
+ optedIn: boolean;
49
+ }