@hasna/microservices 0.0.9 → 0.0.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/bin/index.js +236 -36
- package/bin/mcp.js +153 -4
- package/dist/index.js +120 -3
- package/microservices/microservice-analytics/package.json +27 -0
- package/microservices/microservice-analytics/src/cli/index.ts +373 -0
- package/microservices/microservice-analytics/src/db/analytics.ts +564 -0
- package/microservices/microservice-analytics/src/db/database.ts +93 -0
- package/microservices/microservice-analytics/src/db/migrations.ts +50 -0
- package/microservices/microservice-analytics/src/index.ts +37 -0
- package/microservices/microservice-analytics/src/mcp/index.ts +334 -0
- package/microservices/microservice-assets/package.json +27 -0
- package/microservices/microservice-assets/src/cli/index.ts +375 -0
- package/microservices/microservice-assets/src/db/assets.ts +370 -0
- package/microservices/microservice-assets/src/db/database.ts +93 -0
- package/microservices/microservice-assets/src/db/migrations.ts +51 -0
- package/microservices/microservice-assets/src/index.ts +32 -0
- package/microservices/microservice-assets/src/mcp/index.ts +346 -0
- package/microservices/microservice-compliance/package.json +27 -0
- package/microservices/microservice-compliance/src/cli/index.ts +467 -0
- package/microservices/microservice-compliance/src/db/compliance.ts +633 -0
- package/microservices/microservice-compliance/src/db/database.ts +93 -0
- package/microservices/microservice-compliance/src/db/migrations.ts +63 -0
- package/microservices/microservice-compliance/src/index.ts +46 -0
- package/microservices/microservice-compliance/src/mcp/index.ts +438 -0
- package/microservices/microservice-habits/package.json +27 -0
- package/microservices/microservice-habits/src/cli/index.ts +315 -0
- package/microservices/microservice-habits/src/db/database.ts +93 -0
- package/microservices/microservice-habits/src/db/habits.ts +451 -0
- package/microservices/microservice-habits/src/db/migrations.ts +46 -0
- package/microservices/microservice-habits/src/index.ts +31 -0
- package/microservices/microservice-habits/src/mcp/index.ts +313 -0
- package/microservices/microservice-health/package.json +27 -0
- package/microservices/microservice-health/src/cli/index.ts +484 -0
- package/microservices/microservice-health/src/db/database.ts +93 -0
- package/microservices/microservice-health/src/db/health.ts +708 -0
- package/microservices/microservice-health/src/db/migrations.ts +70 -0
- package/microservices/microservice-health/src/index.ts +63 -0
- package/microservices/microservice-health/src/mcp/index.ts +437 -0
- package/microservices/microservice-leads/package.json +27 -0
- package/microservices/microservice-leads/src/cli/index.ts +596 -0
- package/microservices/microservice-leads/src/db/database.ts +93 -0
- package/microservices/microservice-leads/src/db/leads.ts +520 -0
- package/microservices/microservice-leads/src/db/lists.ts +151 -0
- package/microservices/microservice-leads/src/db/migrations.ts +93 -0
- package/microservices/microservice-leads/src/index.ts +65 -0
- package/microservices/microservice-leads/src/lib/enrichment.ts +202 -0
- package/microservices/microservice-leads/src/lib/scoring.ts +134 -0
- package/microservices/microservice-leads/src/mcp/index.ts +533 -0
- package/microservices/microservice-notifications/package.json +27 -0
- package/microservices/microservice-notifications/src/cli/index.ts +349 -0
- package/microservices/microservice-notifications/src/db/database.ts +93 -0
- package/microservices/microservice-notifications/src/db/migrations.ts +62 -0
- package/microservices/microservice-notifications/src/db/notifications.ts +509 -0
- package/microservices/microservice-notifications/src/index.ts +41 -0
- package/microservices/microservice-notifications/src/mcp/index.ts +422 -0
- package/microservices/microservice-products/package.json +27 -0
- package/microservices/microservice-products/src/cli/index.ts +416 -0
- package/microservices/microservice-products/src/db/categories.ts +154 -0
- package/microservices/microservice-products/src/db/database.ts +93 -0
- package/microservices/microservice-products/src/db/migrations.ts +58 -0
- package/microservices/microservice-products/src/db/pricing-tiers.ts +66 -0
- package/microservices/microservice-products/src/db/products.ts +452 -0
- package/microservices/microservice-products/src/index.ts +53 -0
- package/microservices/microservice-products/src/mcp/index.ts +453 -0
- package/microservices/microservice-projects/package.json +27 -0
- package/microservices/microservice-projects/src/cli/index.ts +480 -0
- package/microservices/microservice-projects/src/db/database.ts +93 -0
- package/microservices/microservice-projects/src/db/migrations.ts +65 -0
- package/microservices/microservice-projects/src/db/projects.ts +715 -0
- package/microservices/microservice-projects/src/index.ts +57 -0
- package/microservices/microservice-projects/src/mcp/index.ts +501 -0
- package/microservices/microservice-proposals/package.json +27 -0
- package/microservices/microservice-proposals/src/cli/index.ts +400 -0
- package/microservices/microservice-proposals/src/db/database.ts +93 -0
- package/microservices/microservice-proposals/src/db/migrations.ts +52 -0
- package/microservices/microservice-proposals/src/db/proposals.ts +532 -0
- package/microservices/microservice-proposals/src/index.ts +37 -0
- package/microservices/microservice-proposals/src/mcp/index.ts +375 -0
- package/microservices/microservice-reading/package.json +27 -0
- package/microservices/microservice-reading/src/cli/index.ts +464 -0
- package/microservices/microservice-reading/src/db/database.ts +93 -0
- package/microservices/microservice-reading/src/db/migrations.ts +59 -0
- package/microservices/microservice-reading/src/db/reading.ts +524 -0
- package/microservices/microservice-reading/src/index.ts +51 -0
- package/microservices/microservice-reading/src/mcp/index.ts +368 -0
- package/microservices/microservice-travel/package.json +27 -0
- package/microservices/microservice-travel/src/cli/index.ts +505 -0
- package/microservices/microservice-travel/src/db/database.ts +93 -0
- package/microservices/microservice-travel/src/db/migrations.ts +77 -0
- package/microservices/microservice-travel/src/db/travel.ts +802 -0
- package/microservices/microservice-travel/src/index.ts +60 -0
- package/microservices/microservice-travel/src/mcp/index.ts +495 -0
- package/microservices/microservice-wiki/package.json +27 -0
- package/microservices/microservice-wiki/src/cli/index.ts +345 -0
- package/microservices/microservice-wiki/src/db/database.ts +93 -0
- package/microservices/microservice-wiki/src/db/migrations.ts +55 -0
- package/microservices/microservice-wiki/src/db/wiki.ts +395 -0
- package/microservices/microservice-wiki/src/index.ts +32 -0
- package/microservices/microservice-wiki/src/mcp/index.ts +344 -0
- package/package.json +1 -1
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notification CRUD, rules, templates, and event processing
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { getDatabase } from "./database.js";
|
|
6
|
+
|
|
7
|
+
// ─── Types ───────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export type Channel = "email" | "slack" | "sms" | "webhook" | "in_app";
|
|
10
|
+
export type NotificationStatus = "pending" | "sent" | "failed" | "read";
|
|
11
|
+
export type Priority = "low" | "normal" | "high" | "urgent";
|
|
12
|
+
|
|
13
|
+
export interface Notification {
|
|
14
|
+
id: string;
|
|
15
|
+
channel: Channel;
|
|
16
|
+
recipient: string;
|
|
17
|
+
subject: string | null;
|
|
18
|
+
body: string | null;
|
|
19
|
+
status: NotificationStatus;
|
|
20
|
+
source_service: string | null;
|
|
21
|
+
source_event: string | null;
|
|
22
|
+
priority: Priority;
|
|
23
|
+
metadata: Record<string, unknown>;
|
|
24
|
+
created_at: string;
|
|
25
|
+
sent_at: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface NotificationRow {
|
|
29
|
+
id: string;
|
|
30
|
+
channel: string;
|
|
31
|
+
recipient: string;
|
|
32
|
+
subject: string | null;
|
|
33
|
+
body: string | null;
|
|
34
|
+
status: string;
|
|
35
|
+
source_service: string | null;
|
|
36
|
+
source_event: string | null;
|
|
37
|
+
priority: string;
|
|
38
|
+
metadata: string;
|
|
39
|
+
created_at: string;
|
|
40
|
+
sent_at: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function rowToNotification(row: NotificationRow): Notification {
|
|
44
|
+
return {
|
|
45
|
+
...row,
|
|
46
|
+
channel: row.channel as Channel,
|
|
47
|
+
status: row.status as NotificationStatus,
|
|
48
|
+
priority: row.priority as Priority,
|
|
49
|
+
metadata: JSON.parse(row.metadata || "{}"),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface NotificationRule {
|
|
54
|
+
id: string;
|
|
55
|
+
name: string | null;
|
|
56
|
+
trigger_event: string;
|
|
57
|
+
channel: string;
|
|
58
|
+
recipient: string;
|
|
59
|
+
template_id: string | null;
|
|
60
|
+
enabled: boolean;
|
|
61
|
+
metadata: Record<string, unknown>;
|
|
62
|
+
created_at: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface RuleRow {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string | null;
|
|
68
|
+
trigger_event: string;
|
|
69
|
+
channel: string;
|
|
70
|
+
recipient: string;
|
|
71
|
+
template_id: string | null;
|
|
72
|
+
enabled: number;
|
|
73
|
+
metadata: string;
|
|
74
|
+
created_at: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function rowToRule(row: RuleRow): NotificationRule {
|
|
78
|
+
return {
|
|
79
|
+
...row,
|
|
80
|
+
enabled: row.enabled === 1,
|
|
81
|
+
metadata: JSON.parse(row.metadata || "{}"),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface NotificationTemplate {
|
|
86
|
+
id: string;
|
|
87
|
+
name: string;
|
|
88
|
+
channel: string | null;
|
|
89
|
+
subject_template: string | null;
|
|
90
|
+
body_template: string | null;
|
|
91
|
+
variables: string[];
|
|
92
|
+
created_at: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface TemplateRow {
|
|
96
|
+
id: string;
|
|
97
|
+
name: string;
|
|
98
|
+
channel: string | null;
|
|
99
|
+
subject_template: string | null;
|
|
100
|
+
body_template: string | null;
|
|
101
|
+
variables: string;
|
|
102
|
+
created_at: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function rowToTemplate(row: TemplateRow): NotificationTemplate {
|
|
106
|
+
return {
|
|
107
|
+
...row,
|
|
108
|
+
variables: JSON.parse(row.variables || "[]"),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ─── Notifications ───────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
export interface SendNotificationInput {
|
|
115
|
+
channel: Channel;
|
|
116
|
+
recipient: string;
|
|
117
|
+
subject?: string;
|
|
118
|
+
body?: string;
|
|
119
|
+
source_service?: string;
|
|
120
|
+
source_event?: string;
|
|
121
|
+
priority?: Priority;
|
|
122
|
+
metadata?: Record<string, unknown>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function sendNotification(input: SendNotificationInput): Notification {
|
|
126
|
+
const db = getDatabase();
|
|
127
|
+
const id = crypto.randomUUID();
|
|
128
|
+
const metadata = JSON.stringify(input.metadata || {});
|
|
129
|
+
|
|
130
|
+
db.prepare(
|
|
131
|
+
`INSERT INTO notifications (id, channel, recipient, subject, body, source_service, source_event, priority, metadata)
|
|
132
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
133
|
+
).run(
|
|
134
|
+
id,
|
|
135
|
+
input.channel,
|
|
136
|
+
input.recipient,
|
|
137
|
+
input.subject || null,
|
|
138
|
+
input.body || null,
|
|
139
|
+
input.source_service || null,
|
|
140
|
+
input.source_event || null,
|
|
141
|
+
input.priority || "normal",
|
|
142
|
+
metadata
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return getNotification(id)!;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function getNotification(id: string): Notification | null {
|
|
149
|
+
const db = getDatabase();
|
|
150
|
+
const row = db.prepare("SELECT * FROM notifications WHERE id = ?").get(id) as NotificationRow | null;
|
|
151
|
+
return row ? rowToNotification(row) : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface ListNotificationsOptions {
|
|
155
|
+
status?: NotificationStatus;
|
|
156
|
+
channel?: Channel;
|
|
157
|
+
priority?: Priority;
|
|
158
|
+
recipient?: string;
|
|
159
|
+
limit?: number;
|
|
160
|
+
offset?: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function listNotifications(options: ListNotificationsOptions = {}): Notification[] {
|
|
164
|
+
const db = getDatabase();
|
|
165
|
+
const conditions: string[] = [];
|
|
166
|
+
const params: unknown[] = [];
|
|
167
|
+
|
|
168
|
+
if (options.status) {
|
|
169
|
+
conditions.push("status = ?");
|
|
170
|
+
params.push(options.status);
|
|
171
|
+
}
|
|
172
|
+
if (options.channel) {
|
|
173
|
+
conditions.push("channel = ?");
|
|
174
|
+
params.push(options.channel);
|
|
175
|
+
}
|
|
176
|
+
if (options.priority) {
|
|
177
|
+
conditions.push("priority = ?");
|
|
178
|
+
params.push(options.priority);
|
|
179
|
+
}
|
|
180
|
+
if (options.recipient) {
|
|
181
|
+
conditions.push("recipient = ?");
|
|
182
|
+
params.push(options.recipient);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let sql = "SELECT * FROM notifications";
|
|
186
|
+
if (conditions.length > 0) {
|
|
187
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
188
|
+
}
|
|
189
|
+
sql += " ORDER BY created_at DESC";
|
|
190
|
+
|
|
191
|
+
if (options.limit) {
|
|
192
|
+
sql += " LIMIT ?";
|
|
193
|
+
params.push(options.limit);
|
|
194
|
+
}
|
|
195
|
+
if (options.offset) {
|
|
196
|
+
sql += " OFFSET ?";
|
|
197
|
+
params.push(options.offset);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const rows = db.prepare(sql).all(...params) as NotificationRow[];
|
|
201
|
+
return rows.map(rowToNotification);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function markRead(id: string): Notification | null {
|
|
205
|
+
const db = getDatabase();
|
|
206
|
+
const existing = getNotification(id);
|
|
207
|
+
if (!existing) return null;
|
|
208
|
+
|
|
209
|
+
db.prepare("UPDATE notifications SET status = 'read' WHERE id = ?").run(id);
|
|
210
|
+
return getNotification(id);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function markAllRead(recipient: string): number {
|
|
214
|
+
const db = getDatabase();
|
|
215
|
+
const result = db.prepare(
|
|
216
|
+
"UPDATE notifications SET status = 'read' WHERE recipient = ? AND status != 'read'"
|
|
217
|
+
).run(recipient);
|
|
218
|
+
return result.changes;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function markSent(id: string): Notification | null {
|
|
222
|
+
const db = getDatabase();
|
|
223
|
+
const existing = getNotification(id);
|
|
224
|
+
if (!existing) return null;
|
|
225
|
+
|
|
226
|
+
db.prepare(
|
|
227
|
+
"UPDATE notifications SET status = 'sent', sent_at = datetime('now') WHERE id = ?"
|
|
228
|
+
).run(id);
|
|
229
|
+
return getNotification(id);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function markFailed(id: string): Notification | null {
|
|
233
|
+
const db = getDatabase();
|
|
234
|
+
const existing = getNotification(id);
|
|
235
|
+
if (!existing) return null;
|
|
236
|
+
|
|
237
|
+
db.prepare("UPDATE notifications SET status = 'failed' WHERE id = ?").run(id);
|
|
238
|
+
return getNotification(id);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ─── Rules ───────────────────────────────────────────────────────────────────
|
|
242
|
+
|
|
243
|
+
export interface CreateRuleInput {
|
|
244
|
+
name?: string;
|
|
245
|
+
trigger_event: string;
|
|
246
|
+
channel: string;
|
|
247
|
+
recipient: string;
|
|
248
|
+
template_id?: string;
|
|
249
|
+
metadata?: Record<string, unknown>;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function createRule(input: CreateRuleInput): NotificationRule {
|
|
253
|
+
const db = getDatabase();
|
|
254
|
+
const id = crypto.randomUUID();
|
|
255
|
+
const metadata = JSON.stringify(input.metadata || {});
|
|
256
|
+
|
|
257
|
+
db.prepare(
|
|
258
|
+
`INSERT INTO notification_rules (id, name, trigger_event, channel, recipient, template_id, metadata)
|
|
259
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
260
|
+
).run(
|
|
261
|
+
id,
|
|
262
|
+
input.name || null,
|
|
263
|
+
input.trigger_event,
|
|
264
|
+
input.channel,
|
|
265
|
+
input.recipient,
|
|
266
|
+
input.template_id || null,
|
|
267
|
+
metadata
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
return getRule(id)!;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function getRule(id: string): NotificationRule | null {
|
|
274
|
+
const db = getDatabase();
|
|
275
|
+
const row = db.prepare("SELECT * FROM notification_rules WHERE id = ?").get(id) as RuleRow | null;
|
|
276
|
+
return row ? rowToRule(row) : null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function listRules(): NotificationRule[] {
|
|
280
|
+
const db = getDatabase();
|
|
281
|
+
const rows = db.prepare("SELECT * FROM notification_rules ORDER BY created_at DESC").all() as RuleRow[];
|
|
282
|
+
return rows.map(rowToRule);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface UpdateRuleInput {
|
|
286
|
+
name?: string;
|
|
287
|
+
trigger_event?: string;
|
|
288
|
+
channel?: string;
|
|
289
|
+
recipient?: string;
|
|
290
|
+
template_id?: string | null;
|
|
291
|
+
metadata?: Record<string, unknown>;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function updateRule(id: string, input: UpdateRuleInput): NotificationRule | null {
|
|
295
|
+
const db = getDatabase();
|
|
296
|
+
const existing = getRule(id);
|
|
297
|
+
if (!existing) return null;
|
|
298
|
+
|
|
299
|
+
const sets: string[] = [];
|
|
300
|
+
const params: unknown[] = [];
|
|
301
|
+
|
|
302
|
+
if (input.name !== undefined) {
|
|
303
|
+
sets.push("name = ?");
|
|
304
|
+
params.push(input.name);
|
|
305
|
+
}
|
|
306
|
+
if (input.trigger_event !== undefined) {
|
|
307
|
+
sets.push("trigger_event = ?");
|
|
308
|
+
params.push(input.trigger_event);
|
|
309
|
+
}
|
|
310
|
+
if (input.channel !== undefined) {
|
|
311
|
+
sets.push("channel = ?");
|
|
312
|
+
params.push(input.channel);
|
|
313
|
+
}
|
|
314
|
+
if (input.recipient !== undefined) {
|
|
315
|
+
sets.push("recipient = ?");
|
|
316
|
+
params.push(input.recipient);
|
|
317
|
+
}
|
|
318
|
+
if (input.template_id !== undefined) {
|
|
319
|
+
sets.push("template_id = ?");
|
|
320
|
+
params.push(input.template_id);
|
|
321
|
+
}
|
|
322
|
+
if (input.metadata !== undefined) {
|
|
323
|
+
sets.push("metadata = ?");
|
|
324
|
+
params.push(JSON.stringify(input.metadata));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (sets.length === 0) return existing;
|
|
328
|
+
|
|
329
|
+
params.push(id);
|
|
330
|
+
db.prepare(`UPDATE notification_rules SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
331
|
+
|
|
332
|
+
return getRule(id);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function deleteRule(id: string): boolean {
|
|
336
|
+
const db = getDatabase();
|
|
337
|
+
const result = db.prepare("DELETE FROM notification_rules WHERE id = ?").run(id);
|
|
338
|
+
return result.changes > 0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function enableRule(id: string): NotificationRule | null {
|
|
342
|
+
const db = getDatabase();
|
|
343
|
+
const existing = getRule(id);
|
|
344
|
+
if (!existing) return null;
|
|
345
|
+
|
|
346
|
+
db.prepare("UPDATE notification_rules SET enabled = 1 WHERE id = ?").run(id);
|
|
347
|
+
return getRule(id);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function disableRule(id: string): NotificationRule | null {
|
|
351
|
+
const db = getDatabase();
|
|
352
|
+
const existing = getRule(id);
|
|
353
|
+
if (!existing) return null;
|
|
354
|
+
|
|
355
|
+
db.prepare("UPDATE notification_rules SET enabled = 0 WHERE id = ?").run(id);
|
|
356
|
+
return getRule(id);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ─── Templates ───────────────────────────────────────────────────────────────
|
|
360
|
+
|
|
361
|
+
export interface CreateTemplateInput {
|
|
362
|
+
name: string;
|
|
363
|
+
channel?: string;
|
|
364
|
+
subject_template?: string;
|
|
365
|
+
body_template?: string;
|
|
366
|
+
variables?: string[];
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function createTemplate(input: CreateTemplateInput): NotificationTemplate {
|
|
370
|
+
const db = getDatabase();
|
|
371
|
+
const id = crypto.randomUUID();
|
|
372
|
+
const variables = JSON.stringify(input.variables || []);
|
|
373
|
+
|
|
374
|
+
db.prepare(
|
|
375
|
+
`INSERT INTO notification_templates (id, name, channel, subject_template, body_template, variables)
|
|
376
|
+
VALUES (?, ?, ?, ?, ?, ?)`
|
|
377
|
+
).run(
|
|
378
|
+
id,
|
|
379
|
+
input.name,
|
|
380
|
+
input.channel || null,
|
|
381
|
+
input.subject_template || null,
|
|
382
|
+
input.body_template || null,
|
|
383
|
+
variables
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
return getTemplate(id)!;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function getTemplate(id: string): NotificationTemplate | null {
|
|
390
|
+
const db = getDatabase();
|
|
391
|
+
const row = db.prepare("SELECT * FROM notification_templates WHERE id = ?").get(id) as TemplateRow | null;
|
|
392
|
+
return row ? rowToTemplate(row) : null;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function listTemplates(): NotificationTemplate[] {
|
|
396
|
+
const db = getDatabase();
|
|
397
|
+
const rows = db.prepare("SELECT * FROM notification_templates ORDER BY created_at DESC").all() as TemplateRow[];
|
|
398
|
+
return rows.map(rowToTemplate);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function deleteTemplate(id: string): boolean {
|
|
402
|
+
const db = getDatabase();
|
|
403
|
+
const result = db.prepare("DELETE FROM notification_templates WHERE id = ?").run(id);
|
|
404
|
+
return result.changes > 0;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export function renderTemplate(
|
|
408
|
+
templateId: string,
|
|
409
|
+
variables: Record<string, string>
|
|
410
|
+
): { subject: string | null; body: string | null } | null {
|
|
411
|
+
const template = getTemplate(templateId);
|
|
412
|
+
if (!template) return null;
|
|
413
|
+
|
|
414
|
+
function substitute(text: string | null): string | null {
|
|
415
|
+
if (!text) return null;
|
|
416
|
+
return text.replace(/\{\{(\w+)\}\}/g, (_, key) => variables[key] ?? `{{${key}}}`);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
subject: substitute(template.subject_template),
|
|
421
|
+
body: substitute(template.body_template),
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ─── Event Processing ────────────────────────────────────────────────────────
|
|
426
|
+
|
|
427
|
+
export function processEvent(
|
|
428
|
+
event: string,
|
|
429
|
+
data: Record<string, string> = {}
|
|
430
|
+
): Notification[] {
|
|
431
|
+
const db = getDatabase();
|
|
432
|
+
const rows = db.prepare(
|
|
433
|
+
"SELECT * FROM notification_rules WHERE trigger_event = ? AND enabled = 1"
|
|
434
|
+
).all(event) as RuleRow[];
|
|
435
|
+
|
|
436
|
+
const rules = rows.map(rowToRule);
|
|
437
|
+
const notifications: Notification[] = [];
|
|
438
|
+
|
|
439
|
+
for (const rule of rules) {
|
|
440
|
+
let subject: string | null = null;
|
|
441
|
+
let body: string | null = null;
|
|
442
|
+
|
|
443
|
+
if (rule.template_id) {
|
|
444
|
+
const rendered = renderTemplate(rule.template_id, data);
|
|
445
|
+
if (rendered) {
|
|
446
|
+
subject = rendered.subject;
|
|
447
|
+
body = rendered.body;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const notification = sendNotification({
|
|
452
|
+
channel: rule.channel as Channel,
|
|
453
|
+
recipient: rule.recipient,
|
|
454
|
+
subject,
|
|
455
|
+
body,
|
|
456
|
+
source_service: undefined,
|
|
457
|
+
source_event: event,
|
|
458
|
+
priority: "normal",
|
|
459
|
+
metadata: data,
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
notifications.push(notification);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return notifications;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// ─── Stats ───────────────────────────────────────────────────────────────────
|
|
469
|
+
|
|
470
|
+
export interface NotificationStats {
|
|
471
|
+
total: number;
|
|
472
|
+
by_channel: Record<string, number>;
|
|
473
|
+
by_status: Record<string, number>;
|
|
474
|
+
by_priority: Record<string, number>;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function getNotificationStats(): NotificationStats {
|
|
478
|
+
const db = getDatabase();
|
|
479
|
+
|
|
480
|
+
const totalRow = db.prepare("SELECT COUNT(*) as count FROM notifications").get() as { count: number };
|
|
481
|
+
|
|
482
|
+
const channelRows = db.prepare(
|
|
483
|
+
"SELECT channel, COUNT(*) as count FROM notifications GROUP BY channel"
|
|
484
|
+
).all() as { channel: string; count: number }[];
|
|
485
|
+
|
|
486
|
+
const statusRows = db.prepare(
|
|
487
|
+
"SELECT status, COUNT(*) as count FROM notifications GROUP BY status"
|
|
488
|
+
).all() as { status: string; count: number }[];
|
|
489
|
+
|
|
490
|
+
const priorityRows = db.prepare(
|
|
491
|
+
"SELECT priority, COUNT(*) as count FROM notifications GROUP BY priority"
|
|
492
|
+
).all() as { priority: string; count: number }[];
|
|
493
|
+
|
|
494
|
+
const by_channel: Record<string, number> = {};
|
|
495
|
+
for (const row of channelRows) by_channel[row.channel] = row.count;
|
|
496
|
+
|
|
497
|
+
const by_status: Record<string, number> = {};
|
|
498
|
+
for (const row of statusRows) by_status[row.status] = row.count;
|
|
499
|
+
|
|
500
|
+
const by_priority: Record<string, number> = {};
|
|
501
|
+
for (const row of priorityRows) by_priority[row.priority] = row.count;
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
total: totalRow.count,
|
|
505
|
+
by_channel,
|
|
506
|
+
by_status,
|
|
507
|
+
by_priority,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* microservice-notifications — Notification management microservice
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
sendNotification,
|
|
7
|
+
getNotification,
|
|
8
|
+
listNotifications,
|
|
9
|
+
markRead,
|
|
10
|
+
markAllRead,
|
|
11
|
+
markSent,
|
|
12
|
+
markFailed,
|
|
13
|
+
createRule,
|
|
14
|
+
getRule,
|
|
15
|
+
listRules,
|
|
16
|
+
updateRule,
|
|
17
|
+
deleteRule,
|
|
18
|
+
enableRule,
|
|
19
|
+
disableRule,
|
|
20
|
+
createTemplate,
|
|
21
|
+
getTemplate,
|
|
22
|
+
listTemplates,
|
|
23
|
+
deleteTemplate,
|
|
24
|
+
renderTemplate,
|
|
25
|
+
processEvent,
|
|
26
|
+
getNotificationStats,
|
|
27
|
+
type Notification,
|
|
28
|
+
type NotificationRule,
|
|
29
|
+
type NotificationTemplate,
|
|
30
|
+
type NotificationStats,
|
|
31
|
+
type SendNotificationInput,
|
|
32
|
+
type ListNotificationsOptions,
|
|
33
|
+
type CreateRuleInput,
|
|
34
|
+
type UpdateRuleInput,
|
|
35
|
+
type CreateTemplateInput,
|
|
36
|
+
type Channel,
|
|
37
|
+
type NotificationStatus,
|
|
38
|
+
type Priority,
|
|
39
|
+
} from "./db/notifications.js";
|
|
40
|
+
|
|
41
|
+
export { getDatabase, closeDatabase } from "./db/database.js";
|