@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.
Files changed (100) hide show
  1. package/bin/index.js +236 -36
  2. package/bin/mcp.js +153 -4
  3. package/dist/index.js +120 -3
  4. package/microservices/microservice-analytics/package.json +27 -0
  5. package/microservices/microservice-analytics/src/cli/index.ts +373 -0
  6. package/microservices/microservice-analytics/src/db/analytics.ts +564 -0
  7. package/microservices/microservice-analytics/src/db/database.ts +93 -0
  8. package/microservices/microservice-analytics/src/db/migrations.ts +50 -0
  9. package/microservices/microservice-analytics/src/index.ts +37 -0
  10. package/microservices/microservice-analytics/src/mcp/index.ts +334 -0
  11. package/microservices/microservice-assets/package.json +27 -0
  12. package/microservices/microservice-assets/src/cli/index.ts +375 -0
  13. package/microservices/microservice-assets/src/db/assets.ts +370 -0
  14. package/microservices/microservice-assets/src/db/database.ts +93 -0
  15. package/microservices/microservice-assets/src/db/migrations.ts +51 -0
  16. package/microservices/microservice-assets/src/index.ts +32 -0
  17. package/microservices/microservice-assets/src/mcp/index.ts +346 -0
  18. package/microservices/microservice-compliance/package.json +27 -0
  19. package/microservices/microservice-compliance/src/cli/index.ts +467 -0
  20. package/microservices/microservice-compliance/src/db/compliance.ts +633 -0
  21. package/microservices/microservice-compliance/src/db/database.ts +93 -0
  22. package/microservices/microservice-compliance/src/db/migrations.ts +63 -0
  23. package/microservices/microservice-compliance/src/index.ts +46 -0
  24. package/microservices/microservice-compliance/src/mcp/index.ts +438 -0
  25. package/microservices/microservice-habits/package.json +27 -0
  26. package/microservices/microservice-habits/src/cli/index.ts +315 -0
  27. package/microservices/microservice-habits/src/db/database.ts +93 -0
  28. package/microservices/microservice-habits/src/db/habits.ts +451 -0
  29. package/microservices/microservice-habits/src/db/migrations.ts +46 -0
  30. package/microservices/microservice-habits/src/index.ts +31 -0
  31. package/microservices/microservice-habits/src/mcp/index.ts +313 -0
  32. package/microservices/microservice-health/package.json +27 -0
  33. package/microservices/microservice-health/src/cli/index.ts +484 -0
  34. package/microservices/microservice-health/src/db/database.ts +93 -0
  35. package/microservices/microservice-health/src/db/health.ts +708 -0
  36. package/microservices/microservice-health/src/db/migrations.ts +70 -0
  37. package/microservices/microservice-health/src/index.ts +63 -0
  38. package/microservices/microservice-health/src/mcp/index.ts +437 -0
  39. package/microservices/microservice-leads/package.json +27 -0
  40. package/microservices/microservice-leads/src/cli/index.ts +596 -0
  41. package/microservices/microservice-leads/src/db/database.ts +93 -0
  42. package/microservices/microservice-leads/src/db/leads.ts +520 -0
  43. package/microservices/microservice-leads/src/db/lists.ts +151 -0
  44. package/microservices/microservice-leads/src/db/migrations.ts +93 -0
  45. package/microservices/microservice-leads/src/index.ts +65 -0
  46. package/microservices/microservice-leads/src/lib/enrichment.ts +202 -0
  47. package/microservices/microservice-leads/src/lib/scoring.ts +134 -0
  48. package/microservices/microservice-leads/src/mcp/index.ts +533 -0
  49. package/microservices/microservice-notifications/package.json +27 -0
  50. package/microservices/microservice-notifications/src/cli/index.ts +349 -0
  51. package/microservices/microservice-notifications/src/db/database.ts +93 -0
  52. package/microservices/microservice-notifications/src/db/migrations.ts +62 -0
  53. package/microservices/microservice-notifications/src/db/notifications.ts +509 -0
  54. package/microservices/microservice-notifications/src/index.ts +41 -0
  55. package/microservices/microservice-notifications/src/mcp/index.ts +422 -0
  56. package/microservices/microservice-products/package.json +27 -0
  57. package/microservices/microservice-products/src/cli/index.ts +416 -0
  58. package/microservices/microservice-products/src/db/categories.ts +154 -0
  59. package/microservices/microservice-products/src/db/database.ts +93 -0
  60. package/microservices/microservice-products/src/db/migrations.ts +58 -0
  61. package/microservices/microservice-products/src/db/pricing-tiers.ts +66 -0
  62. package/microservices/microservice-products/src/db/products.ts +452 -0
  63. package/microservices/microservice-products/src/index.ts +53 -0
  64. package/microservices/microservice-products/src/mcp/index.ts +453 -0
  65. package/microservices/microservice-projects/package.json +27 -0
  66. package/microservices/microservice-projects/src/cli/index.ts +480 -0
  67. package/microservices/microservice-projects/src/db/database.ts +93 -0
  68. package/microservices/microservice-projects/src/db/migrations.ts +65 -0
  69. package/microservices/microservice-projects/src/db/projects.ts +715 -0
  70. package/microservices/microservice-projects/src/index.ts +57 -0
  71. package/microservices/microservice-projects/src/mcp/index.ts +501 -0
  72. package/microservices/microservice-proposals/package.json +27 -0
  73. package/microservices/microservice-proposals/src/cli/index.ts +400 -0
  74. package/microservices/microservice-proposals/src/db/database.ts +93 -0
  75. package/microservices/microservice-proposals/src/db/migrations.ts +52 -0
  76. package/microservices/microservice-proposals/src/db/proposals.ts +532 -0
  77. package/microservices/microservice-proposals/src/index.ts +37 -0
  78. package/microservices/microservice-proposals/src/mcp/index.ts +375 -0
  79. package/microservices/microservice-reading/package.json +27 -0
  80. package/microservices/microservice-reading/src/cli/index.ts +464 -0
  81. package/microservices/microservice-reading/src/db/database.ts +93 -0
  82. package/microservices/microservice-reading/src/db/migrations.ts +59 -0
  83. package/microservices/microservice-reading/src/db/reading.ts +524 -0
  84. package/microservices/microservice-reading/src/index.ts +51 -0
  85. package/microservices/microservice-reading/src/mcp/index.ts +368 -0
  86. package/microservices/microservice-travel/package.json +27 -0
  87. package/microservices/microservice-travel/src/cli/index.ts +505 -0
  88. package/microservices/microservice-travel/src/db/database.ts +93 -0
  89. package/microservices/microservice-travel/src/db/migrations.ts +77 -0
  90. package/microservices/microservice-travel/src/db/travel.ts +802 -0
  91. package/microservices/microservice-travel/src/index.ts +60 -0
  92. package/microservices/microservice-travel/src/mcp/index.ts +495 -0
  93. package/microservices/microservice-wiki/package.json +27 -0
  94. package/microservices/microservice-wiki/src/cli/index.ts +345 -0
  95. package/microservices/microservice-wiki/src/db/database.ts +93 -0
  96. package/microservices/microservice-wiki/src/db/migrations.ts +55 -0
  97. package/microservices/microservice-wiki/src/db/wiki.ts +395 -0
  98. package/microservices/microservice-wiki/src/index.ts +32 -0
  99. package/microservices/microservice-wiki/src/mcp/index.ts +344 -0
  100. package/package.json +1 -1
@@ -0,0 +1,58 @@
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 categories (
13
+ id TEXT PRIMARY KEY,
14
+ name TEXT NOT NULL,
15
+ parent_id TEXT REFERENCES categories(id) ON DELETE SET NULL,
16
+ description TEXT,
17
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
18
+ );
19
+
20
+ CREATE TABLE IF NOT EXISTS products (
21
+ id TEXT PRIMARY KEY,
22
+ name TEXT NOT NULL,
23
+ description TEXT,
24
+ type TEXT NOT NULL DEFAULT 'product' CHECK(type IN ('product', 'service', 'subscription', 'digital')),
25
+ sku TEXT UNIQUE,
26
+ price REAL,
27
+ currency TEXT NOT NULL DEFAULT 'USD',
28
+ unit TEXT,
29
+ category TEXT,
30
+ status TEXT NOT NULL DEFAULT 'draft' CHECK(status IN ('active', 'draft', 'archived')),
31
+ images TEXT NOT NULL DEFAULT '[]',
32
+ variants TEXT NOT NULL DEFAULT '[]',
33
+ metadata TEXT NOT NULL DEFAULT '{}',
34
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
35
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
36
+ );
37
+
38
+ CREATE TABLE IF NOT EXISTS pricing_tiers (
39
+ id TEXT PRIMARY KEY,
40
+ product_id TEXT NOT NULL REFERENCES products(id) ON DELETE CASCADE,
41
+ name TEXT NOT NULL,
42
+ min_quantity INTEGER NOT NULL,
43
+ price REAL NOT NULL,
44
+ currency TEXT NOT NULL DEFAULT 'USD',
45
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
46
+ );
47
+
48
+ CREATE INDEX IF NOT EXISTS idx_products_sku ON products(sku);
49
+ CREATE INDEX IF NOT EXISTS idx_products_name ON products(name);
50
+ CREATE INDEX IF NOT EXISTS idx_products_type ON products(type);
51
+ CREATE INDEX IF NOT EXISTS idx_products_status ON products(status);
52
+ CREATE INDEX IF NOT EXISTS idx_products_category ON products(category);
53
+ CREATE INDEX IF NOT EXISTS idx_categories_name ON categories(name);
54
+ CREATE INDEX IF NOT EXISTS idx_categories_parent ON categories(parent_id);
55
+ CREATE INDEX IF NOT EXISTS idx_pricing_tiers_product ON pricing_tiers(product_id);
56
+ `,
57
+ },
58
+ ];
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Pricing tier operations
3
+ */
4
+
5
+ import { getDatabase } from "./database.js";
6
+
7
+ export interface PricingTier {
8
+ id: string;
9
+ product_id: string;
10
+ name: string;
11
+ min_quantity: number;
12
+ price: number;
13
+ currency: string;
14
+ created_at: string;
15
+ }
16
+
17
+ export interface CreatePricingTierInput {
18
+ product_id: string;
19
+ name: string;
20
+ min_quantity: number;
21
+ price: number;
22
+ currency?: string;
23
+ }
24
+
25
+ export function createPricingTier(input: CreatePricingTierInput): PricingTier {
26
+ const db = getDatabase();
27
+ const id = crypto.randomUUID();
28
+
29
+ db.prepare(
30
+ `INSERT INTO pricing_tiers (id, product_id, name, min_quantity, price, currency)
31
+ VALUES (?, ?, ?, ?, ?, ?)`
32
+ ).run(
33
+ id,
34
+ input.product_id,
35
+ input.name,
36
+ input.min_quantity,
37
+ input.price,
38
+ input.currency || "USD"
39
+ );
40
+
41
+ return getPricingTier(id)!;
42
+ }
43
+
44
+ export function getPricingTier(id: string): PricingTier | null {
45
+ const db = getDatabase();
46
+ return db.prepare("SELECT * FROM pricing_tiers WHERE id = ?").get(id) as PricingTier | null;
47
+ }
48
+
49
+ export function listPricingTiers(productId: string): PricingTier[] {
50
+ const db = getDatabase();
51
+ return db
52
+ .prepare("SELECT * FROM pricing_tiers WHERE product_id = ? ORDER BY min_quantity")
53
+ .all(productId) as PricingTier[];
54
+ }
55
+
56
+ export function deletePricingTier(id: string): boolean {
57
+ const db = getDatabase();
58
+ const result = db.prepare("DELETE FROM pricing_tiers WHERE id = ?").run(id);
59
+ return result.changes > 0;
60
+ }
61
+
62
+ export function deletePricingTiersByProduct(productId: string): number {
63
+ const db = getDatabase();
64
+ const result = db.prepare("DELETE FROM pricing_tiers WHERE product_id = ?").run(productId);
65
+ return result.changes;
66
+ }
@@ -0,0 +1,452 @@
1
+ /**
2
+ * Product CRUD operations
3
+ */
4
+
5
+ import { getDatabase } from "./database.js";
6
+
7
+ export interface Product {
8
+ id: string;
9
+ name: string;
10
+ description: string | null;
11
+ type: "product" | "service" | "subscription" | "digital";
12
+ sku: string | null;
13
+ price: number | null;
14
+ currency: string;
15
+ unit: string | null;
16
+ category: string | null;
17
+ status: "active" | "draft" | "archived";
18
+ images: string[];
19
+ variants: Record<string, unknown>[];
20
+ metadata: Record<string, unknown>;
21
+ created_at: string;
22
+ updated_at: string;
23
+ }
24
+
25
+ interface ProductRow {
26
+ id: string;
27
+ name: string;
28
+ description: string | null;
29
+ type: string;
30
+ sku: string | null;
31
+ price: number | null;
32
+ currency: string;
33
+ unit: string | null;
34
+ category: string | null;
35
+ status: string;
36
+ images: string;
37
+ variants: string;
38
+ metadata: string;
39
+ created_at: string;
40
+ updated_at: string;
41
+ }
42
+
43
+ function rowToProduct(row: ProductRow): Product {
44
+ return {
45
+ ...row,
46
+ type: row.type as Product["type"],
47
+ status: row.status as Product["status"],
48
+ images: JSON.parse(row.images || "[]"),
49
+ variants: JSON.parse(row.variants || "[]"),
50
+ metadata: JSON.parse(row.metadata || "{}"),
51
+ };
52
+ }
53
+
54
+ export interface CreateProductInput {
55
+ name: string;
56
+ description?: string;
57
+ type?: "product" | "service" | "subscription" | "digital";
58
+ sku?: string;
59
+ price?: number;
60
+ currency?: string;
61
+ unit?: string;
62
+ category?: string;
63
+ status?: "active" | "draft" | "archived";
64
+ images?: string[];
65
+ variants?: Record<string, unknown>[];
66
+ metadata?: Record<string, unknown>;
67
+ }
68
+
69
+ export function createProduct(input: CreateProductInput): Product {
70
+ const db = getDatabase();
71
+ const id = crypto.randomUUID();
72
+ const images = JSON.stringify(input.images || []);
73
+ const variants = JSON.stringify(input.variants || []);
74
+ const metadata = JSON.stringify(input.metadata || {});
75
+
76
+ db.prepare(
77
+ `INSERT INTO products (id, name, description, type, sku, price, currency, unit, category, status, images, variants, metadata)
78
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
79
+ ).run(
80
+ id,
81
+ input.name,
82
+ input.description || null,
83
+ input.type || "product",
84
+ input.sku || null,
85
+ input.price ?? null,
86
+ input.currency || "USD",
87
+ input.unit || null,
88
+ input.category || null,
89
+ input.status || "draft",
90
+ images,
91
+ variants,
92
+ metadata
93
+ );
94
+
95
+ return getProduct(id)!;
96
+ }
97
+
98
+ export function getProduct(id: string): Product | null {
99
+ const db = getDatabase();
100
+ const row = db.prepare("SELECT * FROM products WHERE id = ?").get(id) as ProductRow | null;
101
+ return row ? rowToProduct(row) : null;
102
+ }
103
+
104
+ export interface ListProductsOptions {
105
+ search?: string;
106
+ category?: string;
107
+ type?: string;
108
+ status?: string;
109
+ limit?: number;
110
+ offset?: number;
111
+ }
112
+
113
+ export function listProducts(options: ListProductsOptions = {}): Product[] {
114
+ const db = getDatabase();
115
+ const conditions: string[] = [];
116
+ const params: unknown[] = [];
117
+
118
+ if (options.search) {
119
+ conditions.push(
120
+ "(name LIKE ? OR description LIKE ? OR sku LIKE ?)"
121
+ );
122
+ const q = `%${options.search}%`;
123
+ params.push(q, q, q);
124
+ }
125
+
126
+ if (options.category) {
127
+ conditions.push("category = ?");
128
+ params.push(options.category);
129
+ }
130
+
131
+ if (options.type) {
132
+ conditions.push("type = ?");
133
+ params.push(options.type);
134
+ }
135
+
136
+ if (options.status) {
137
+ conditions.push("status = ?");
138
+ params.push(options.status);
139
+ }
140
+
141
+ let sql = "SELECT * FROM products";
142
+ if (conditions.length > 0) {
143
+ sql += " WHERE " + conditions.join(" AND ");
144
+ }
145
+ sql += " ORDER BY name";
146
+
147
+ if (options.limit) {
148
+ sql += " LIMIT ?";
149
+ params.push(options.limit);
150
+ }
151
+ if (options.offset) {
152
+ sql += " OFFSET ?";
153
+ params.push(options.offset);
154
+ }
155
+
156
+ const rows = db.prepare(sql).all(...params) as ProductRow[];
157
+ return rows.map(rowToProduct);
158
+ }
159
+
160
+ export interface UpdateProductInput {
161
+ name?: string;
162
+ description?: string;
163
+ type?: "product" | "service" | "subscription" | "digital";
164
+ sku?: string;
165
+ price?: number;
166
+ currency?: string;
167
+ unit?: string;
168
+ category?: string | null;
169
+ status?: "active" | "draft" | "archived";
170
+ images?: string[];
171
+ variants?: Record<string, unknown>[];
172
+ metadata?: Record<string, unknown>;
173
+ }
174
+
175
+ export function updateProduct(
176
+ id: string,
177
+ input: UpdateProductInput
178
+ ): Product | null {
179
+ const db = getDatabase();
180
+ const existing = getProduct(id);
181
+ if (!existing) return null;
182
+
183
+ const sets: string[] = [];
184
+ const params: unknown[] = [];
185
+
186
+ if (input.name !== undefined) {
187
+ sets.push("name = ?");
188
+ params.push(input.name);
189
+ }
190
+ if (input.description !== undefined) {
191
+ sets.push("description = ?");
192
+ params.push(input.description);
193
+ }
194
+ if (input.type !== undefined) {
195
+ sets.push("type = ?");
196
+ params.push(input.type);
197
+ }
198
+ if (input.sku !== undefined) {
199
+ sets.push("sku = ?");
200
+ params.push(input.sku);
201
+ }
202
+ if (input.price !== undefined) {
203
+ sets.push("price = ?");
204
+ params.push(input.price);
205
+ }
206
+ if (input.currency !== undefined) {
207
+ sets.push("currency = ?");
208
+ params.push(input.currency);
209
+ }
210
+ if (input.unit !== undefined) {
211
+ sets.push("unit = ?");
212
+ params.push(input.unit);
213
+ }
214
+ if (input.category !== undefined) {
215
+ sets.push("category = ?");
216
+ params.push(input.category);
217
+ }
218
+ if (input.status !== undefined) {
219
+ sets.push("status = ?");
220
+ params.push(input.status);
221
+ }
222
+ if (input.images !== undefined) {
223
+ sets.push("images = ?");
224
+ params.push(JSON.stringify(input.images));
225
+ }
226
+ if (input.variants !== undefined) {
227
+ sets.push("variants = ?");
228
+ params.push(JSON.stringify(input.variants));
229
+ }
230
+ if (input.metadata !== undefined) {
231
+ sets.push("metadata = ?");
232
+ params.push(JSON.stringify(input.metadata));
233
+ }
234
+
235
+ if (sets.length === 0) return existing;
236
+
237
+ sets.push("updated_at = datetime('now')");
238
+ params.push(id);
239
+
240
+ db.prepare(
241
+ `UPDATE products SET ${sets.join(", ")} WHERE id = ?`
242
+ ).run(...params);
243
+
244
+ return getProduct(id);
245
+ }
246
+
247
+ export function deleteProduct(id: string): boolean {
248
+ const db = getDatabase();
249
+ const result = db.prepare("DELETE FROM products WHERE id = ?").run(id);
250
+ return result.changes > 0;
251
+ }
252
+
253
+ export function countProducts(): number {
254
+ const db = getDatabase();
255
+ const row = db.prepare("SELECT COUNT(*) as count FROM products").get() as { count: number };
256
+ return row.count;
257
+ }
258
+
259
+ export function searchProducts(query: string): Product[] {
260
+ return listProducts({ search: query });
261
+ }
262
+
263
+ export function listByCategory(category: string): Product[] {
264
+ return listProducts({ category });
265
+ }
266
+
267
+ export function listByType(type: string): Product[] {
268
+ return listProducts({ type });
269
+ }
270
+
271
+ export function listByStatus(status: string): Product[] {
272
+ return listProducts({ status });
273
+ }
274
+
275
+ export interface ProductWithTiers extends Product {
276
+ pricing_tiers: PricingTier[];
277
+ }
278
+
279
+ export interface PricingTier {
280
+ id: string;
281
+ product_id: string;
282
+ name: string;
283
+ min_quantity: number;
284
+ price: number;
285
+ currency: string;
286
+ created_at: string;
287
+ }
288
+
289
+ export function getProductWithTiers(id: string): ProductWithTiers | null {
290
+ const product = getProduct(id);
291
+ if (!product) return null;
292
+
293
+ const db = getDatabase();
294
+ const tiers = db
295
+ .prepare("SELECT * FROM pricing_tiers WHERE product_id = ? ORDER BY min_quantity")
296
+ .all(id) as PricingTier[];
297
+
298
+ return { ...product, pricing_tiers: tiers };
299
+ }
300
+
301
+ export interface BulkImportResult {
302
+ imported: number;
303
+ skipped: number;
304
+ errors: string[];
305
+ }
306
+
307
+ export function bulkImportProducts(csv: string): BulkImportResult {
308
+ const lines = csv.trim().split("\n");
309
+ if (lines.length < 2) {
310
+ return { imported: 0, skipped: 0, errors: ["CSV must have a header row and at least one data row"] };
311
+ }
312
+
313
+ const headers = lines[0].split(",").map((h) => h.trim().toLowerCase());
314
+ const nameIdx = headers.indexOf("name");
315
+ if (nameIdx === -1) {
316
+ return { imported: 0, skipped: 0, errors: ["CSV must have a 'name' column"] };
317
+ }
318
+
319
+ const result: BulkImportResult = { imported: 0, skipped: 0, errors: [] };
320
+
321
+ for (let i = 1; i < lines.length; i++) {
322
+ const values = lines[i].split(",").map((v) => v.trim());
323
+ if (values.length < headers.length) {
324
+ result.errors.push(`Row ${i + 1}: insufficient columns`);
325
+ result.skipped++;
326
+ continue;
327
+ }
328
+
329
+ try {
330
+ const input: CreateProductInput = {
331
+ name: values[nameIdx],
332
+ };
333
+
334
+ const descIdx = headers.indexOf("description");
335
+ if (descIdx !== -1 && values[descIdx]) input.description = values[descIdx];
336
+
337
+ const typeIdx = headers.indexOf("type");
338
+ if (typeIdx !== -1 && values[typeIdx]) {
339
+ const t = values[typeIdx] as CreateProductInput["type"];
340
+ if (["product", "service", "subscription", "digital"].includes(values[typeIdx])) {
341
+ input.type = t;
342
+ }
343
+ }
344
+
345
+ const skuIdx = headers.indexOf("sku");
346
+ if (skuIdx !== -1 && values[skuIdx]) input.sku = values[skuIdx];
347
+
348
+ const priceIdx = headers.indexOf("price");
349
+ if (priceIdx !== -1 && values[priceIdx]) {
350
+ const p = parseFloat(values[priceIdx]);
351
+ if (!isNaN(p)) input.price = p;
352
+ }
353
+
354
+ const currencyIdx = headers.indexOf("currency");
355
+ if (currencyIdx !== -1 && values[currencyIdx]) input.currency = values[currencyIdx];
356
+
357
+ const unitIdx = headers.indexOf("unit");
358
+ if (unitIdx !== -1 && values[unitIdx]) input.unit = values[unitIdx];
359
+
360
+ const categoryIdx = headers.indexOf("category");
361
+ if (categoryIdx !== -1 && values[categoryIdx]) input.category = values[categoryIdx];
362
+
363
+ const statusIdx = headers.indexOf("status");
364
+ if (statusIdx !== -1 && values[statusIdx]) {
365
+ const s = values[statusIdx] as CreateProductInput["status"];
366
+ if (["active", "draft", "archived"].includes(values[statusIdx])) {
367
+ input.status = s;
368
+ }
369
+ }
370
+
371
+ createProduct(input);
372
+ result.imported++;
373
+ } catch (error) {
374
+ result.errors.push(`Row ${i + 1}: ${error instanceof Error ? error.message : String(error)}`);
375
+ result.skipped++;
376
+ }
377
+ }
378
+
379
+ return result;
380
+ }
381
+
382
+ export function exportProducts(format: "csv" | "json" = "json"): string {
383
+ const products = listProducts();
384
+
385
+ if (format === "json") {
386
+ return JSON.stringify(products, null, 2);
387
+ }
388
+
389
+ // CSV format
390
+ const headers = [
391
+ "id", "name", "description", "type", "sku", "price", "currency",
392
+ "unit", "category", "status", "created_at", "updated_at",
393
+ ];
394
+ const rows = products.map((p) =>
395
+ headers.map((h) => {
396
+ const val = p[h as keyof Product];
397
+ if (val === null || val === undefined) return "";
398
+ if (typeof val === "string" && val.includes(",")) return `"${val}"`;
399
+ return String(val);
400
+ }).join(",")
401
+ );
402
+
403
+ return [headers.join(","), ...rows].join("\n");
404
+ }
405
+
406
+ export interface ProductStats {
407
+ total: number;
408
+ by_status: Record<string, number>;
409
+ by_type: Record<string, number>;
410
+ by_category: Record<string, number>;
411
+ avg_price: number | null;
412
+ min_price: number | null;
413
+ max_price: number | null;
414
+ }
415
+
416
+ export function getProductStats(): ProductStats {
417
+ const db = getDatabase();
418
+
419
+ const total = (db.prepare("SELECT COUNT(*) as count FROM products").get() as { count: number }).count;
420
+
421
+ const statusRows = db
422
+ .prepare("SELECT status, COUNT(*) as count FROM products GROUP BY status")
423
+ .all() as { status: string; count: number }[];
424
+ const by_status: Record<string, number> = {};
425
+ for (const r of statusRows) by_status[r.status] = r.count;
426
+
427
+ const typeRows = db
428
+ .prepare("SELECT type, COUNT(*) as count FROM products GROUP BY type")
429
+ .all() as { type: string; count: number }[];
430
+ const by_type: Record<string, number> = {};
431
+ for (const r of typeRows) by_type[r.type] = r.count;
432
+
433
+ const catRows = db
434
+ .prepare("SELECT COALESCE(category, 'uncategorized') as category, COUNT(*) as count FROM products GROUP BY category")
435
+ .all() as { category: string; count: number }[];
436
+ const by_category: Record<string, number> = {};
437
+ for (const r of catRows) by_category[r.category] = r.count;
438
+
439
+ const priceRow = db
440
+ .prepare("SELECT AVG(price) as avg_price, MIN(price) as min_price, MAX(price) as max_price FROM products WHERE price IS NOT NULL")
441
+ .get() as { avg_price: number | null; min_price: number | null; max_price: number | null };
442
+
443
+ return {
444
+ total,
445
+ by_status,
446
+ by_type,
447
+ by_category,
448
+ avg_price: priceRow.avg_price,
449
+ min_price: priceRow.min_price,
450
+ max_price: priceRow.max_price,
451
+ };
452
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * microservice-products — Product catalog microservice
3
+ */
4
+
5
+ export {
6
+ createProduct,
7
+ getProduct,
8
+ listProducts,
9
+ updateProduct,
10
+ deleteProduct,
11
+ countProducts,
12
+ searchProducts,
13
+ listByCategory,
14
+ listByType,
15
+ listByStatus,
16
+ getProductWithTiers,
17
+ bulkImportProducts,
18
+ exportProducts,
19
+ getProductStats,
20
+ type Product,
21
+ type ProductWithTiers,
22
+ type CreateProductInput,
23
+ type UpdateProductInput,
24
+ type ListProductsOptions,
25
+ type ProductStats,
26
+ type BulkImportResult,
27
+ } from "./db/products.js";
28
+
29
+ export {
30
+ createCategory,
31
+ getCategory,
32
+ listCategories,
33
+ updateCategory,
34
+ deleteCategory,
35
+ getCategoryTree,
36
+ type Category,
37
+ type CreateCategoryInput,
38
+ type UpdateCategoryInput,
39
+ type ListCategoriesOptions,
40
+ type CategoryTreeNode,
41
+ } from "./db/categories.js";
42
+
43
+ export {
44
+ createPricingTier,
45
+ getPricingTier,
46
+ listPricingTiers,
47
+ deletePricingTier,
48
+ deletePricingTiersByProduct,
49
+ type PricingTier,
50
+ type CreatePricingTierInput,
51
+ } from "./db/pricing-tiers.js";
52
+
53
+ export { getDatabase, closeDatabase } from "./db/database.js";