@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,349 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import {
|
|
5
|
+
sendNotification,
|
|
6
|
+
listNotifications,
|
|
7
|
+
getNotification,
|
|
8
|
+
markRead,
|
|
9
|
+
markAllRead,
|
|
10
|
+
createRule,
|
|
11
|
+
listRules,
|
|
12
|
+
enableRule,
|
|
13
|
+
disableRule,
|
|
14
|
+
deleteRule,
|
|
15
|
+
createTemplate,
|
|
16
|
+
listTemplates,
|
|
17
|
+
deleteTemplate,
|
|
18
|
+
processEvent,
|
|
19
|
+
getNotificationStats,
|
|
20
|
+
} from "../db/notifications.js";
|
|
21
|
+
|
|
22
|
+
const program = new Command();
|
|
23
|
+
|
|
24
|
+
program
|
|
25
|
+
.name("microservice-notifications")
|
|
26
|
+
.description("Notification management microservice")
|
|
27
|
+
.version("0.0.1");
|
|
28
|
+
|
|
29
|
+
// --- Notifications ---
|
|
30
|
+
|
|
31
|
+
program
|
|
32
|
+
.command("send")
|
|
33
|
+
.description("Send a notification")
|
|
34
|
+
.requiredOption("--channel <channel>", "Channel (email, slack, sms, webhook, in_app)")
|
|
35
|
+
.requiredOption("--to <recipient>", "Recipient")
|
|
36
|
+
.option("--subject <subject>", "Subject")
|
|
37
|
+
.option("--body <body>", "Body")
|
|
38
|
+
.option("--priority <priority>", "Priority (low, normal, high, urgent)", "normal")
|
|
39
|
+
.option("--source-service <service>", "Source service")
|
|
40
|
+
.option("--source-event <event>", "Source event")
|
|
41
|
+
.option("--json", "Output as JSON", false)
|
|
42
|
+
.action((opts) => {
|
|
43
|
+
const notification = sendNotification({
|
|
44
|
+
channel: opts.channel,
|
|
45
|
+
recipient: opts.to,
|
|
46
|
+
subject: opts.subject,
|
|
47
|
+
body: opts.body,
|
|
48
|
+
priority: opts.priority,
|
|
49
|
+
source_service: opts.sourceService,
|
|
50
|
+
source_event: opts.sourceEvent,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (opts.json) {
|
|
54
|
+
console.log(JSON.stringify(notification, null, 2));
|
|
55
|
+
} else {
|
|
56
|
+
console.log(`Sent notification: ${notification.channel} -> ${notification.recipient} (${notification.id})`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
program
|
|
61
|
+
.command("list")
|
|
62
|
+
.description("List notifications")
|
|
63
|
+
.option("--status <status>", "Filter by status (pending, sent, failed, read)")
|
|
64
|
+
.option("--channel <channel>", "Filter by channel")
|
|
65
|
+
.option("--priority <priority>", "Filter by priority")
|
|
66
|
+
.option("--recipient <recipient>", "Filter by recipient")
|
|
67
|
+
.option("--limit <n>", "Limit results")
|
|
68
|
+
.option("--json", "Output as JSON", false)
|
|
69
|
+
.action((opts) => {
|
|
70
|
+
const notifications = listNotifications({
|
|
71
|
+
status: opts.status,
|
|
72
|
+
channel: opts.channel,
|
|
73
|
+
priority: opts.priority,
|
|
74
|
+
recipient: opts.recipient,
|
|
75
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (opts.json) {
|
|
79
|
+
console.log(JSON.stringify(notifications, null, 2));
|
|
80
|
+
} else {
|
|
81
|
+
if (notifications.length === 0) {
|
|
82
|
+
console.log("No notifications found.");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
for (const n of notifications) {
|
|
86
|
+
const subject = n.subject ? ` — ${n.subject}` : "";
|
|
87
|
+
console.log(` [${n.status}] ${n.channel} -> ${n.recipient}${subject} (${n.priority})`);
|
|
88
|
+
}
|
|
89
|
+
console.log(`\n${notifications.length} notification(s)`);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
program
|
|
94
|
+
.command("read")
|
|
95
|
+
.description("Mark a notification as read")
|
|
96
|
+
.argument("<id>", "Notification ID")
|
|
97
|
+
.option("--json", "Output as JSON", false)
|
|
98
|
+
.action((id, opts) => {
|
|
99
|
+
const notification = markRead(id);
|
|
100
|
+
if (!notification) {
|
|
101
|
+
console.error(`Notification '${id}' not found.`);
|
|
102
|
+
process.exit(1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (opts.json) {
|
|
106
|
+
console.log(JSON.stringify(notification, null, 2));
|
|
107
|
+
} else {
|
|
108
|
+
console.log(`Marked as read: ${notification.id}`);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
program
|
|
113
|
+
.command("read-all")
|
|
114
|
+
.description("Mark all notifications as read for a recipient")
|
|
115
|
+
.requiredOption("--recipient <recipient>", "Recipient")
|
|
116
|
+
.option("--json", "Output as JSON", false)
|
|
117
|
+
.action((opts) => {
|
|
118
|
+
const count = markAllRead(opts.recipient);
|
|
119
|
+
|
|
120
|
+
if (opts.json) {
|
|
121
|
+
console.log(JSON.stringify({ recipient: opts.recipient, marked_read: count }));
|
|
122
|
+
} else {
|
|
123
|
+
console.log(`Marked ${count} notification(s) as read for ${opts.recipient}`);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// --- Rules ---
|
|
128
|
+
|
|
129
|
+
const ruleCmd = program
|
|
130
|
+
.command("rule")
|
|
131
|
+
.description("Notification rule management");
|
|
132
|
+
|
|
133
|
+
ruleCmd
|
|
134
|
+
.command("create")
|
|
135
|
+
.description("Create a notification rule")
|
|
136
|
+
.requiredOption("--event <event>", "Trigger event")
|
|
137
|
+
.requiredOption("--channel <channel>", "Notification channel")
|
|
138
|
+
.requiredOption("--to <recipient>", "Recipient")
|
|
139
|
+
.option("--name <name>", "Rule name")
|
|
140
|
+
.option("--template <id>", "Template ID")
|
|
141
|
+
.option("--json", "Output as JSON", false)
|
|
142
|
+
.action((opts) => {
|
|
143
|
+
const rule = createRule({
|
|
144
|
+
name: opts.name,
|
|
145
|
+
trigger_event: opts.event,
|
|
146
|
+
channel: opts.channel,
|
|
147
|
+
recipient: opts.to,
|
|
148
|
+
template_id: opts.template,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
if (opts.json) {
|
|
152
|
+
console.log(JSON.stringify(rule, null, 2));
|
|
153
|
+
} else {
|
|
154
|
+
console.log(`Created rule: ${rule.trigger_event} -> ${rule.channel}:${rule.recipient} (${rule.id})`);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
ruleCmd
|
|
159
|
+
.command("list")
|
|
160
|
+
.description("List notification rules")
|
|
161
|
+
.option("--json", "Output as JSON", false)
|
|
162
|
+
.action((opts) => {
|
|
163
|
+
const rules = listRules();
|
|
164
|
+
|
|
165
|
+
if (opts.json) {
|
|
166
|
+
console.log(JSON.stringify(rules, null, 2));
|
|
167
|
+
} else {
|
|
168
|
+
if (rules.length === 0) {
|
|
169
|
+
console.log("No rules found.");
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
for (const r of rules) {
|
|
173
|
+
const name = r.name ? ` (${r.name})` : "";
|
|
174
|
+
const status = r.enabled ? "enabled" : "disabled";
|
|
175
|
+
console.log(` [${status}] ${r.trigger_event} -> ${r.channel}:${r.recipient}${name}`);
|
|
176
|
+
}
|
|
177
|
+
console.log(`\n${rules.length} rule(s)`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
ruleCmd
|
|
182
|
+
.command("enable")
|
|
183
|
+
.description("Enable a rule")
|
|
184
|
+
.argument("<id>", "Rule ID")
|
|
185
|
+
.action((id) => {
|
|
186
|
+
const rule = enableRule(id);
|
|
187
|
+
if (!rule) {
|
|
188
|
+
console.error(`Rule '${id}' not found.`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
console.log(`Enabled rule ${id}`);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
ruleCmd
|
|
195
|
+
.command("disable")
|
|
196
|
+
.description("Disable a rule")
|
|
197
|
+
.argument("<id>", "Rule ID")
|
|
198
|
+
.action((id) => {
|
|
199
|
+
const rule = disableRule(id);
|
|
200
|
+
if (!rule) {
|
|
201
|
+
console.error(`Rule '${id}' not found.`);
|
|
202
|
+
process.exit(1);
|
|
203
|
+
}
|
|
204
|
+
console.log(`Disabled rule ${id}`);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
ruleCmd
|
|
208
|
+
.command("delete")
|
|
209
|
+
.description("Delete a rule")
|
|
210
|
+
.argument("<id>", "Rule ID")
|
|
211
|
+
.action((id) => {
|
|
212
|
+
const deleted = deleteRule(id);
|
|
213
|
+
if (deleted) {
|
|
214
|
+
console.log(`Deleted rule ${id}`);
|
|
215
|
+
} else {
|
|
216
|
+
console.error(`Rule '${id}' not found.`);
|
|
217
|
+
process.exit(1);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// --- Templates ---
|
|
222
|
+
|
|
223
|
+
const templateCmd = program
|
|
224
|
+
.command("template")
|
|
225
|
+
.description("Notification template management");
|
|
226
|
+
|
|
227
|
+
templateCmd
|
|
228
|
+
.command("create")
|
|
229
|
+
.description("Create a notification template")
|
|
230
|
+
.requiredOption("--name <name>", "Template name")
|
|
231
|
+
.option("--channel <channel>", "Channel")
|
|
232
|
+
.option("--subject <subject>", "Subject template")
|
|
233
|
+
.option("--body <body>", "Body template")
|
|
234
|
+
.option("--variables <vars>", "Comma-separated variable names")
|
|
235
|
+
.option("--json", "Output as JSON", false)
|
|
236
|
+
.action((opts) => {
|
|
237
|
+
const template = createTemplate({
|
|
238
|
+
name: opts.name,
|
|
239
|
+
channel: opts.channel,
|
|
240
|
+
subject_template: opts.subject,
|
|
241
|
+
body_template: opts.body,
|
|
242
|
+
variables: opts.variables ? opts.variables.split(",").map((v: string) => v.trim()) : undefined,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
if (opts.json) {
|
|
246
|
+
console.log(JSON.stringify(template, null, 2));
|
|
247
|
+
} else {
|
|
248
|
+
console.log(`Created template: ${template.name} (${template.id})`);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
templateCmd
|
|
253
|
+
.command("list")
|
|
254
|
+
.description("List notification templates")
|
|
255
|
+
.option("--json", "Output as JSON", false)
|
|
256
|
+
.action((opts) => {
|
|
257
|
+
const templates = listTemplates();
|
|
258
|
+
|
|
259
|
+
if (opts.json) {
|
|
260
|
+
console.log(JSON.stringify(templates, null, 2));
|
|
261
|
+
} else {
|
|
262
|
+
if (templates.length === 0) {
|
|
263
|
+
console.log("No templates found.");
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
for (const t of templates) {
|
|
267
|
+
const channel = t.channel ? ` [${t.channel}]` : "";
|
|
268
|
+
console.log(` ${t.name}${channel} (${t.id})`);
|
|
269
|
+
}
|
|
270
|
+
console.log(`\n${templates.length} template(s)`);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
templateCmd
|
|
275
|
+
.command("delete")
|
|
276
|
+
.description("Delete a template")
|
|
277
|
+
.argument("<id>", "Template ID")
|
|
278
|
+
.action((id) => {
|
|
279
|
+
const deleted = deleteTemplate(id);
|
|
280
|
+
if (deleted) {
|
|
281
|
+
console.log(`Deleted template ${id}`);
|
|
282
|
+
} else {
|
|
283
|
+
console.error(`Template '${id}' not found.`);
|
|
284
|
+
process.exit(1);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// --- Event Processing ---
|
|
289
|
+
|
|
290
|
+
program
|
|
291
|
+
.command("process")
|
|
292
|
+
.description("Process an event and trigger matching rules")
|
|
293
|
+
.requiredOption("--event <event>", "Event name")
|
|
294
|
+
.option("--data <json>", "Event data as JSON", "{}")
|
|
295
|
+
.option("--json", "Output as JSON", false)
|
|
296
|
+
.action((opts) => {
|
|
297
|
+
let data: Record<string, string>;
|
|
298
|
+
try {
|
|
299
|
+
data = JSON.parse(opts.data);
|
|
300
|
+
} catch {
|
|
301
|
+
console.error("Invalid JSON data");
|
|
302
|
+
process.exit(1);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const notifications = processEvent(opts.event, data);
|
|
306
|
+
|
|
307
|
+
if (opts.json) {
|
|
308
|
+
console.log(JSON.stringify(notifications, null, 2));
|
|
309
|
+
} else {
|
|
310
|
+
if (notifications.length === 0) {
|
|
311
|
+
console.log(`No rules matched event '${opts.event}'.`);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
console.log(`Processed event '${opts.event}' — created ${notifications.length} notification(s):`);
|
|
315
|
+
for (const n of notifications) {
|
|
316
|
+
console.log(` ${n.channel} -> ${n.recipient}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// --- Stats ---
|
|
322
|
+
|
|
323
|
+
program
|
|
324
|
+
.command("stats")
|
|
325
|
+
.description("Show notification statistics")
|
|
326
|
+
.option("--json", "Output as JSON", false)
|
|
327
|
+
.action((opts) => {
|
|
328
|
+
const stats = getNotificationStats();
|
|
329
|
+
|
|
330
|
+
if (opts.json) {
|
|
331
|
+
console.log(JSON.stringify(stats, null, 2));
|
|
332
|
+
} else {
|
|
333
|
+
console.log(`Total notifications: ${stats.total}`);
|
|
334
|
+
console.log("\nBy channel:");
|
|
335
|
+
for (const [channel, count] of Object.entries(stats.by_channel)) {
|
|
336
|
+
console.log(` ${channel}: ${count}`);
|
|
337
|
+
}
|
|
338
|
+
console.log("\nBy status:");
|
|
339
|
+
for (const [status, count] of Object.entries(stats.by_status)) {
|
|
340
|
+
console.log(` ${status}: ${count}`);
|
|
341
|
+
}
|
|
342
|
+
console.log("\nBy priority:");
|
|
343
|
+
for (const [priority, count] of Object.entries(stats.by_priority)) {
|
|
344
|
+
console.log(` ${priority}: ${count}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection for microservice-notifications
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Database } from "bun:sqlite";
|
|
6
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { MIGRATIONS } from "./migrations.js";
|
|
9
|
+
|
|
10
|
+
let _db: Database | null = null;
|
|
11
|
+
|
|
12
|
+
function getDbPath(): string {
|
|
13
|
+
// Environment variable override
|
|
14
|
+
if (process.env["MICROSERVICES_DIR"]) {
|
|
15
|
+
return join(process.env["MICROSERVICES_DIR"], "microservice-notifications", "data.db");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Check for .microservices in current or parent directories
|
|
19
|
+
let dir = resolve(process.cwd());
|
|
20
|
+
while (true) {
|
|
21
|
+
const candidate = join(dir, ".microservices", "microservice-notifications", "data.db");
|
|
22
|
+
const msDir = join(dir, ".microservices");
|
|
23
|
+
if (existsSync(msDir)) return candidate;
|
|
24
|
+
const parent = dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Global fallback
|
|
30
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
|
|
31
|
+
return join(home, ".microservices", "microservice-notifications", "data.db");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function ensureDir(filePath: string): void {
|
|
35
|
+
const dir = dirname(resolve(filePath));
|
|
36
|
+
if (!existsSync(dir)) {
|
|
37
|
+
mkdirSync(dir, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getDatabase(): Database {
|
|
42
|
+
if (_db) return _db;
|
|
43
|
+
|
|
44
|
+
const dbPath = getDbPath();
|
|
45
|
+
ensureDir(dbPath);
|
|
46
|
+
|
|
47
|
+
_db = new Database(dbPath);
|
|
48
|
+
_db.exec("PRAGMA journal_mode = WAL");
|
|
49
|
+
_db.exec("PRAGMA foreign_keys = ON");
|
|
50
|
+
|
|
51
|
+
// Create migrations table
|
|
52
|
+
_db.exec(`
|
|
53
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
54
|
+
id INTEGER PRIMARY KEY,
|
|
55
|
+
name TEXT NOT NULL,
|
|
56
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
57
|
+
)
|
|
58
|
+
`);
|
|
59
|
+
|
|
60
|
+
// Apply pending migrations
|
|
61
|
+
const applied = _db
|
|
62
|
+
.query("SELECT id FROM _migrations ORDER BY id")
|
|
63
|
+
.all() as { id: number }[];
|
|
64
|
+
const appliedIds = new Set(applied.map((r) => r.id));
|
|
65
|
+
|
|
66
|
+
for (const migration of MIGRATIONS) {
|
|
67
|
+
if (appliedIds.has(migration.id)) continue;
|
|
68
|
+
|
|
69
|
+
_db.exec("BEGIN");
|
|
70
|
+
try {
|
|
71
|
+
_db.exec(migration.sql);
|
|
72
|
+
_db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
|
|
73
|
+
migration.id,
|
|
74
|
+
migration.name
|
|
75
|
+
);
|
|
76
|
+
_db.exec("COMMIT");
|
|
77
|
+
} catch (error) {
|
|
78
|
+
_db.exec("ROLLBACK");
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return _db;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function closeDatabase(): void {
|
|
89
|
+
if (_db) {
|
|
90
|
+
_db.close();
|
|
91
|
+
_db = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface MigrationEntry {
|
|
2
|
+
id: number;
|
|
3
|
+
name: string;
|
|
4
|
+
sql: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const MIGRATIONS: MigrationEntry[] = [
|
|
8
|
+
{
|
|
9
|
+
id: 1,
|
|
10
|
+
name: "initial_schema",
|
|
11
|
+
sql: `
|
|
12
|
+
CREATE TABLE IF NOT EXISTS notifications (
|
|
13
|
+
id TEXT PRIMARY KEY,
|
|
14
|
+
channel TEXT NOT NULL CHECK(channel IN ('email','slack','sms','webhook','in_app')),
|
|
15
|
+
recipient TEXT NOT NULL,
|
|
16
|
+
subject TEXT,
|
|
17
|
+
body TEXT,
|
|
18
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','sent','failed','read')),
|
|
19
|
+
source_service TEXT,
|
|
20
|
+
source_event TEXT,
|
|
21
|
+
priority TEXT NOT NULL DEFAULT 'normal' CHECK(priority IN ('low','normal','high','urgent')),
|
|
22
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
23
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
24
|
+
sent_at TEXT
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_status ON notifications(status);
|
|
28
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_channel ON notifications(channel);
|
|
29
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_recipient ON notifications(recipient);
|
|
30
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_priority ON notifications(priority);
|
|
31
|
+
CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at);
|
|
32
|
+
|
|
33
|
+
CREATE TABLE IF NOT EXISTS notification_rules (
|
|
34
|
+
id TEXT PRIMARY KEY,
|
|
35
|
+
name TEXT,
|
|
36
|
+
trigger_event TEXT NOT NULL,
|
|
37
|
+
channel TEXT NOT NULL,
|
|
38
|
+
recipient TEXT NOT NULL,
|
|
39
|
+
template_id TEXT,
|
|
40
|
+
enabled INTEGER NOT NULL DEFAULT 1,
|
|
41
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
42
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_rules_trigger ON notification_rules(trigger_event);
|
|
46
|
+
CREATE INDEX IF NOT EXISTS idx_rules_enabled ON notification_rules(enabled);
|
|
47
|
+
|
|
48
|
+
CREATE TABLE IF NOT EXISTS notification_templates (
|
|
49
|
+
id TEXT PRIMARY KEY,
|
|
50
|
+
name TEXT NOT NULL,
|
|
51
|
+
channel TEXT,
|
|
52
|
+
subject_template TEXT,
|
|
53
|
+
body_template TEXT,
|
|
54
|
+
variables TEXT NOT NULL DEFAULT '[]',
|
|
55
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_templates_name ON notification_templates(name);
|
|
59
|
+
CREATE INDEX IF NOT EXISTS idx_templates_channel ON notification_templates(channel);
|
|
60
|
+
`,
|
|
61
|
+
},
|
|
62
|
+
];
|