@hasna/microservices 0.0.15 → 0.0.17
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 +120 -138
- package/bin/index.js +703 -12728
- package/bin/mcp.js +250 -9726
- package/dist/index.js +170 -9407
- package/package.json +25 -25
- package/microservices/microservice-ads/package.json +0 -27
- package/microservices/microservice-ads/src/cli/index.ts +0 -605
- package/microservices/microservice-ads/src/db/campaigns.ts +0 -797
- package/microservices/microservice-ads/src/db/database.ts +0 -93
- package/microservices/microservice-ads/src/db/migrations.ts +0 -60
- package/microservices/microservice-ads/src/index.ts +0 -39
- package/microservices/microservice-ads/src/mcp/index.ts +0 -480
- package/microservices/microservice-analytics/package.json +0 -27
- package/microservices/microservice-analytics/src/cli/index.ts +0 -373
- package/microservices/microservice-analytics/src/db/analytics.ts +0 -564
- package/microservices/microservice-analytics/src/db/database.ts +0 -93
- package/microservices/microservice-analytics/src/db/migrations.ts +0 -50
- package/microservices/microservice-analytics/src/index.ts +0 -37
- package/microservices/microservice-analytics/src/mcp/index.ts +0 -334
- package/microservices/microservice-assets/package.json +0 -27
- package/microservices/microservice-assets/src/cli/index.ts +0 -375
- package/microservices/microservice-assets/src/db/assets.ts +0 -370
- package/microservices/microservice-assets/src/db/database.ts +0 -93
- package/microservices/microservice-assets/src/db/migrations.ts +0 -51
- package/microservices/microservice-assets/src/index.ts +0 -32
- package/microservices/microservice-assets/src/mcp/index.ts +0 -346
- package/microservices/microservice-bookkeeping/package.json +0 -27
- package/microservices/microservice-bookkeeping/src/cli/index.ts +0 -386
- package/microservices/microservice-bookkeeping/src/db/bookkeeping.ts +0 -591
- package/microservices/microservice-bookkeeping/src/db/database.ts +0 -93
- package/microservices/microservice-bookkeeping/src/db/migrations.ts +0 -52
- package/microservices/microservice-bookkeeping/src/index.ts +0 -32
- package/microservices/microservice-bookkeeping/src/mcp/index.ts +0 -284
- package/microservices/microservice-calendar/package.json +0 -27
- package/microservices/microservice-calendar/src/cli/index.ts +0 -287
- package/microservices/microservice-calendar/src/db/calendar.ts +0 -328
- package/microservices/microservice-calendar/src/db/database.ts +0 -82
- package/microservices/microservice-calendar/src/db/migrations.ts +0 -47
- package/microservices/microservice-calendar/src/index.ts +0 -24
- package/microservices/microservice-calendar/src/mcp/index.ts +0 -226
- package/microservices/microservice-company/package.json +0 -27
- package/microservices/microservice-company/src/cli/index.ts +0 -1126
- package/microservices/microservice-company/src/db/company.ts +0 -854
- package/microservices/microservice-company/src/db/database.ts +0 -93
- package/microservices/microservice-company/src/db/migrations.ts +0 -214
- package/microservices/microservice-company/src/db/workflow-migrations.ts +0 -44
- package/microservices/microservice-company/src/index.ts +0 -60
- package/microservices/microservice-company/src/lib/audit.ts +0 -168
- package/microservices/microservice-company/src/lib/finance.ts +0 -299
- package/microservices/microservice-company/src/lib/settings.ts +0 -85
- package/microservices/microservice-company/src/lib/workflows.ts +0 -698
- package/microservices/microservice-company/src/mcp/index.ts +0 -991
- package/microservices/microservice-compliance/package.json +0 -27
- package/microservices/microservice-compliance/src/cli/index.ts +0 -467
- package/microservices/microservice-compliance/src/db/compliance.ts +0 -633
- package/microservices/microservice-compliance/src/db/database.ts +0 -93
- package/microservices/microservice-compliance/src/db/migrations.ts +0 -63
- package/microservices/microservice-compliance/src/index.ts +0 -46
- package/microservices/microservice-compliance/src/mcp/index.ts +0 -438
- package/microservices/microservice-contacts/package.json +0 -27
- package/microservices/microservice-contacts/src/cli/index.ts +0 -393
- package/microservices/microservice-contacts/src/db/companies.ts +0 -167
- package/microservices/microservice-contacts/src/db/contacts.ts +0 -249
- package/microservices/microservice-contacts/src/db/database.ts +0 -93
- package/microservices/microservice-contacts/src/db/migrations.ts +0 -71
- package/microservices/microservice-contacts/src/db/relationships.ts +0 -53
- package/microservices/microservice-contacts/src/index.ts +0 -42
- package/microservices/microservice-contacts/src/mcp/index.ts +0 -303
- package/microservices/microservice-contracts/package.json +0 -27
- package/microservices/microservice-contracts/src/cli/index.ts +0 -770
- package/microservices/microservice-contracts/src/db/contracts.ts +0 -925
- package/microservices/microservice-contracts/src/db/database.ts +0 -93
- package/microservices/microservice-contracts/src/db/migrations.ts +0 -141
- package/microservices/microservice-contracts/src/index.ts +0 -43
- package/microservices/microservice-contracts/src/mcp/index.ts +0 -617
- package/microservices/microservice-crm/package.json +0 -27
- package/microservices/microservice-crm/src/cli/index.ts +0 -396
- package/microservices/microservice-crm/src/db/database.ts +0 -82
- package/microservices/microservice-crm/src/db/migrations.ts +0 -66
- package/microservices/microservice-crm/src/db/pipeline.ts +0 -397
- package/microservices/microservice-crm/src/index.ts +0 -34
- package/microservices/microservice-crm/src/mcp/index.ts +0 -294
- package/microservices/microservice-documents/package.json +0 -27
- package/microservices/microservice-documents/src/cli/index.ts +0 -246
- package/microservices/microservice-documents/src/db/database.ts +0 -82
- package/microservices/microservice-documents/src/db/documents.ts +0 -316
- package/microservices/microservice-documents/src/db/migrations.ts +0 -49
- package/microservices/microservice-documents/src/index.ts +0 -24
- package/microservices/microservice-documents/src/mcp/index.ts +0 -202
- package/microservices/microservice-domains/package.json +0 -27
- package/microservices/microservice-domains/src/cli/index.ts +0 -1111
- package/microservices/microservice-domains/src/db/database.ts +0 -93
- package/microservices/microservice-domains/src/db/domains.ts +0 -1164
- package/microservices/microservice-domains/src/db/migrations.ts +0 -60
- package/microservices/microservice-domains/src/index.ts +0 -65
- package/microservices/microservice-domains/src/lib/brandsight.ts +0 -350
- package/microservices/microservice-domains/src/lib/godaddy.ts +0 -338
- package/microservices/microservice-domains/src/lib/namecheap.ts +0 -262
- package/microservices/microservice-domains/src/lib/registrar.ts +0 -355
- package/microservices/microservice-domains/src/mcp/index.ts +0 -781
- package/microservices/microservice-expenses/package.json +0 -27
- package/microservices/microservice-expenses/src/cli/index.ts +0 -267
- package/microservices/microservice-expenses/src/db/database.ts +0 -82
- package/microservices/microservice-expenses/src/db/expenses.ts +0 -345
- package/microservices/microservice-expenses/src/db/migrations.ts +0 -45
- package/microservices/microservice-expenses/src/index.ts +0 -25
- package/microservices/microservice-expenses/src/mcp/index.ts +0 -196
- package/microservices/microservice-habits/package.json +0 -27
- package/microservices/microservice-habits/src/cli/index.ts +0 -315
- package/microservices/microservice-habits/src/db/database.ts +0 -93
- package/microservices/microservice-habits/src/db/habits.ts +0 -451
- package/microservices/microservice-habits/src/db/migrations.ts +0 -46
- package/microservices/microservice-habits/src/index.ts +0 -31
- package/microservices/microservice-habits/src/mcp/index.ts +0 -313
- package/microservices/microservice-health/package.json +0 -27
- package/microservices/microservice-health/src/cli/index.ts +0 -484
- package/microservices/microservice-health/src/db/database.ts +0 -93
- package/microservices/microservice-health/src/db/health.ts +0 -708
- package/microservices/microservice-health/src/db/migrations.ts +0 -70
- package/microservices/microservice-health/src/index.ts +0 -63
- package/microservices/microservice-health/src/mcp/index.ts +0 -437
- package/microservices/microservice-hiring/package.json +0 -27
- package/microservices/microservice-hiring/src/cli/index.ts +0 -741
- package/microservices/microservice-hiring/src/db/database.ts +0 -93
- package/microservices/microservice-hiring/src/db/hiring.ts +0 -1085
- package/microservices/microservice-hiring/src/db/migrations.ts +0 -89
- package/microservices/microservice-hiring/src/index.ts +0 -80
- package/microservices/microservice-hiring/src/lib/scoring.ts +0 -206
- package/microservices/microservice-hiring/src/mcp/index.ts +0 -709
- package/microservices/microservice-inventory/package.json +0 -27
- package/microservices/microservice-inventory/src/cli/index.ts +0 -365
- package/microservices/microservice-inventory/src/db/database.ts +0 -82
- package/microservices/microservice-inventory/src/db/inventory.ts +0 -393
- package/microservices/microservice-inventory/src/db/migrations.ts +0 -54
- package/microservices/microservice-inventory/src/index.ts +0 -28
- package/microservices/microservice-inventory/src/mcp/index.ts +0 -250
- package/microservices/microservice-invoices/dashboard/dist/assets/index-Bngq7FNM.css +0 -1
- package/microservices/microservice-invoices/dashboard/dist/assets/index-aHW4ARZR.js +0 -124
- package/microservices/microservice-invoices/dashboard/dist/index.html +0 -13
- package/microservices/microservice-invoices/dashboard/index.html +0 -12
- package/microservices/microservice-invoices/dashboard/package.json +0 -29
- package/microservices/microservice-invoices/dashboard/tsconfig.json +0 -14
- package/microservices/microservice-invoices/dashboard/vite.config.ts +0 -15
- package/microservices/microservice-invoices/package.json +0 -30
- package/microservices/microservice-invoices/src/cli/index.ts +0 -308
- package/microservices/microservice-invoices/src/db/business.ts +0 -241
- package/microservices/microservice-invoices/src/db/clients.ts +0 -127
- package/microservices/microservice-invoices/src/db/database.ts +0 -82
- package/microservices/microservice-invoices/src/db/invoices.ts +0 -345
- package/microservices/microservice-invoices/src/db/migrations.ts +0 -184
- package/microservices/microservice-invoices/src/index.ts +0 -56
- package/microservices/microservice-invoices/src/mcp/index.ts +0 -242
- package/microservices/microservice-invoices/src/server/index.ts +0 -162
- package/microservices/microservice-leads/package.json +0 -27
- package/microservices/microservice-leads/src/cli/index.ts +0 -596
- package/microservices/microservice-leads/src/db/database.ts +0 -93
- package/microservices/microservice-leads/src/db/leads.ts +0 -520
- package/microservices/microservice-leads/src/db/lists.ts +0 -151
- package/microservices/microservice-leads/src/db/migrations.ts +0 -93
- package/microservices/microservice-leads/src/index.ts +0 -65
- package/microservices/microservice-leads/src/lib/enrichment.ts +0 -202
- package/microservices/microservice-leads/src/lib/scoring.ts +0 -134
- package/microservices/microservice-leads/src/mcp/index.ts +0 -533
- package/microservices/microservice-notes/package.json +0 -27
- package/microservices/microservice-notes/src/cli/index.ts +0 -63
- package/microservices/microservice-notes/src/db/database.ts +0 -93
- package/microservices/microservice-notes/src/db/migrations.ts +0 -40
- package/microservices/microservice-notes/src/db/notes.ts +0 -114
- package/microservices/microservice-notes/src/index.ts +0 -2
- package/microservices/microservice-notes/src/mcp/index.ts +0 -37
- package/microservices/microservice-notifications/package.json +0 -27
- package/microservices/microservice-notifications/src/cli/index.ts +0 -349
- package/microservices/microservice-notifications/src/db/database.ts +0 -93
- package/microservices/microservice-notifications/src/db/migrations.ts +0 -62
- package/microservices/microservice-notifications/src/db/notifications.ts +0 -509
- package/microservices/microservice-notifications/src/index.ts +0 -41
- package/microservices/microservice-notifications/src/mcp/index.ts +0 -422
- package/microservices/microservice-payments/package.json +0 -27
- package/microservices/microservice-payments/src/cli/index.ts +0 -609
- package/microservices/microservice-payments/src/db/database.ts +0 -93
- package/microservices/microservice-payments/src/db/migrations.ts +0 -81
- package/microservices/microservice-payments/src/db/payments.ts +0 -1204
- package/microservices/microservice-payments/src/index.ts +0 -51
- package/microservices/microservice-payments/src/mcp/index.ts +0 -683
- package/microservices/microservice-payroll/package.json +0 -27
- package/microservices/microservice-payroll/src/cli/index.ts +0 -643
- package/microservices/microservice-payroll/src/db/database.ts +0 -93
- package/microservices/microservice-payroll/src/db/migrations.ts +0 -95
- package/microservices/microservice-payroll/src/db/payroll.ts +0 -1377
- package/microservices/microservice-payroll/src/index.ts +0 -48
- package/microservices/microservice-payroll/src/mcp/index.ts +0 -666
- package/microservices/microservice-products/package.json +0 -27
- package/microservices/microservice-products/src/cli/index.ts +0 -416
- package/microservices/microservice-products/src/db/categories.ts +0 -154
- package/microservices/microservice-products/src/db/database.ts +0 -93
- package/microservices/microservice-products/src/db/migrations.ts +0 -58
- package/microservices/microservice-products/src/db/pricing-tiers.ts +0 -66
- package/microservices/microservice-products/src/db/products.ts +0 -452
- package/microservices/microservice-products/src/index.ts +0 -53
- package/microservices/microservice-products/src/mcp/index.ts +0 -453
- package/microservices/microservice-projects/package.json +0 -27
- package/microservices/microservice-projects/src/cli/index.ts +0 -480
- package/microservices/microservice-projects/src/db/database.ts +0 -93
- package/microservices/microservice-projects/src/db/migrations.ts +0 -65
- package/microservices/microservice-projects/src/db/projects.ts +0 -715
- package/microservices/microservice-projects/src/index.ts +0 -57
- package/microservices/microservice-projects/src/mcp/index.ts +0 -501
- package/microservices/microservice-proposals/package.json +0 -27
- package/microservices/microservice-proposals/src/cli/index.ts +0 -400
- package/microservices/microservice-proposals/src/db/database.ts +0 -93
- package/microservices/microservice-proposals/src/db/migrations.ts +0 -52
- package/microservices/microservice-proposals/src/db/proposals.ts +0 -532
- package/microservices/microservice-proposals/src/index.ts +0 -37
- package/microservices/microservice-proposals/src/mcp/index.ts +0 -375
- package/microservices/microservice-reading/package.json +0 -27
- package/microservices/microservice-reading/src/cli/index.ts +0 -464
- package/microservices/microservice-reading/src/db/database.ts +0 -93
- package/microservices/microservice-reading/src/db/migrations.ts +0 -59
- package/microservices/microservice-reading/src/db/reading.ts +0 -524
- package/microservices/microservice-reading/src/index.ts +0 -51
- package/microservices/microservice-reading/src/mcp/index.ts +0 -368
- package/microservices/microservice-shipping/package.json +0 -27
- package/microservices/microservice-shipping/src/cli/index.ts +0 -606
- package/microservices/microservice-shipping/src/db/database.ts +0 -93
- package/microservices/microservice-shipping/src/db/migrations.ts +0 -69
- package/microservices/microservice-shipping/src/db/shipping.ts +0 -1093
- package/microservices/microservice-shipping/src/index.ts +0 -53
- package/microservices/microservice-shipping/src/mcp/index.ts +0 -533
- package/microservices/microservice-social/package.json +0 -28
- package/microservices/microservice-social/src/cli/index.ts +0 -1583
- package/microservices/microservice-social/src/db/database.ts +0 -93
- package/microservices/microservice-social/src/db/migrations.ts +0 -160
- package/microservices/microservice-social/src/db/social.ts +0 -1076
- package/microservices/microservice-social/src/index.ts +0 -46
- package/microservices/microservice-social/src/lib/audience.ts +0 -353
- package/microservices/microservice-social/src/lib/content-ai.ts +0 -278
- package/microservices/microservice-social/src/lib/media.ts +0 -311
- package/microservices/microservice-social/src/lib/mentions.ts +0 -434
- package/microservices/microservice-social/src/lib/metrics-sync.ts +0 -264
- package/microservices/microservice-social/src/lib/publisher.ts +0 -377
- package/microservices/microservice-social/src/lib/scheduler.ts +0 -229
- package/microservices/microservice-social/src/lib/sentiment.ts +0 -256
- package/microservices/microservice-social/src/lib/threads.ts +0 -291
- package/microservices/microservice-social/src/mcp/index.ts +0 -1425
- package/microservices/microservice-social/src/server/index.ts +0 -441
- package/microservices/microservice-subscriptions/package.json +0 -27
- package/microservices/microservice-subscriptions/src/cli/index.ts +0 -715
- package/microservices/microservice-subscriptions/src/db/database.ts +0 -93
- package/microservices/microservice-subscriptions/src/db/migrations.ts +0 -125
- package/microservices/microservice-subscriptions/src/db/subscriptions.ts +0 -1256
- package/microservices/microservice-subscriptions/src/index.ts +0 -41
- package/microservices/microservice-subscriptions/src/mcp/index.ts +0 -631
- package/microservices/microservice-timesheets/package.json +0 -27
- package/microservices/microservice-timesheets/src/cli/index.ts +0 -373
- package/microservices/microservice-timesheets/src/db/database.ts +0 -82
- package/microservices/microservice-timesheets/src/db/locale.ts +0 -217
- package/microservices/microservice-timesheets/src/db/migrations.ts +0 -74
- package/microservices/microservice-timesheets/src/db/timesheets.ts +0 -447
- package/microservices/microservice-timesheets/src/index.ts +0 -44
- package/microservices/microservice-timesheets/src/mcp/index.ts +0 -269
- package/microservices/microservice-transcriber/package.json +0 -28
- package/microservices/microservice-transcriber/src/cli/index.ts +0 -1593
- package/microservices/microservice-transcriber/src/db/annotations.ts +0 -37
- package/microservices/microservice-transcriber/src/db/comments.ts +0 -166
- package/microservices/microservice-transcriber/src/db/database.ts +0 -82
- package/microservices/microservice-transcriber/src/db/migrations.ts +0 -118
- package/microservices/microservice-transcriber/src/db/proofread.ts +0 -119
- package/microservices/microservice-transcriber/src/db/transcripts.ts +0 -395
- package/microservices/microservice-transcriber/src/index.ts +0 -43
- package/microservices/microservice-transcriber/src/lib/config.ts +0 -77
- package/microservices/microservice-transcriber/src/lib/diff.ts +0 -91
- package/microservices/microservice-transcriber/src/lib/downloader.ts +0 -638
- package/microservices/microservice-transcriber/src/lib/feeds.ts +0 -62
- package/microservices/microservice-transcriber/src/lib/live.ts +0 -94
- package/microservices/microservice-transcriber/src/lib/notion.ts +0 -129
- package/microservices/microservice-transcriber/src/lib/proofread.ts +0 -296
- package/microservices/microservice-transcriber/src/lib/providers.ts +0 -713
- package/microservices/microservice-transcriber/src/lib/summarizer.ts +0 -147
- package/microservices/microservice-transcriber/src/lib/translator.ts +0 -75
- package/microservices/microservice-transcriber/src/lib/webhook.ts +0 -37
- package/microservices/microservice-transcriber/src/mcp/index.ts +0 -1330
- package/microservices/microservice-transcriber/src/server/index.ts +0 -199
- package/microservices/microservice-travel/package.json +0 -27
- package/microservices/microservice-travel/src/cli/index.ts +0 -505
- package/microservices/microservice-travel/src/db/database.ts +0 -93
- package/microservices/microservice-travel/src/db/migrations.ts +0 -77
- package/microservices/microservice-travel/src/db/travel.ts +0 -802
- package/microservices/microservice-travel/src/index.ts +0 -60
- package/microservices/microservice-travel/src/mcp/index.ts +0 -495
- package/microservices/microservice-wiki/package.json +0 -27
- package/microservices/microservice-wiki/src/cli/index.ts +0 -345
- package/microservices/microservice-wiki/src/db/database.ts +0 -93
- package/microservices/microservice-wiki/src/db/migrations.ts +0 -55
- package/microservices/microservice-wiki/src/db/wiki.ts +0 -395
- package/microservices/microservice-wiki/src/index.ts +0 -32
- package/microservices/microservice-wiki/src/mcp/index.ts +0 -344
|
@@ -1,93 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
];
|
|
@@ -1,509 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
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";
|