@hasna/microservices 0.0.3 → 0.0.4

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 (67) hide show
  1. package/bin/index.js +63 -0
  2. package/bin/mcp.js +63 -0
  3. package/dist/index.js +63 -0
  4. package/microservices/microservice-ads/package.json +27 -0
  5. package/microservices/microservice-ads/src/cli/index.ts +407 -0
  6. package/microservices/microservice-ads/src/db/campaigns.ts +493 -0
  7. package/microservices/microservice-ads/src/db/database.ts +93 -0
  8. package/microservices/microservice-ads/src/db/migrations.ts +60 -0
  9. package/microservices/microservice-ads/src/index.ts +39 -0
  10. package/microservices/microservice-ads/src/mcp/index.ts +320 -0
  11. package/microservices/microservice-contracts/package.json +27 -0
  12. package/microservices/microservice-contracts/src/cli/index.ts +383 -0
  13. package/microservices/microservice-contracts/src/db/contracts.ts +496 -0
  14. package/microservices/microservice-contracts/src/db/database.ts +93 -0
  15. package/microservices/microservice-contracts/src/db/migrations.ts +58 -0
  16. package/microservices/microservice-contracts/src/index.ts +43 -0
  17. package/microservices/microservice-contracts/src/mcp/index.ts +308 -0
  18. package/microservices/microservice-domains/package.json +27 -0
  19. package/microservices/microservice-domains/src/cli/index.ts +438 -0
  20. package/microservices/microservice-domains/src/db/database.ts +93 -0
  21. package/microservices/microservice-domains/src/db/domains.ts +551 -0
  22. package/microservices/microservice-domains/src/db/migrations.ts +60 -0
  23. package/microservices/microservice-domains/src/index.ts +44 -0
  24. package/microservices/microservice-domains/src/mcp/index.ts +368 -0
  25. package/microservices/microservice-hiring/package.json +27 -0
  26. package/microservices/microservice-hiring/src/cli/index.ts +431 -0
  27. package/microservices/microservice-hiring/src/db/database.ts +93 -0
  28. package/microservices/microservice-hiring/src/db/hiring.ts +582 -0
  29. package/microservices/microservice-hiring/src/db/migrations.ts +68 -0
  30. package/microservices/microservice-hiring/src/index.ts +51 -0
  31. package/microservices/microservice-hiring/src/mcp/index.ts +464 -0
  32. package/microservices/microservice-payments/package.json +27 -0
  33. package/microservices/microservice-payments/src/cli/index.ts +357 -0
  34. package/microservices/microservice-payments/src/db/database.ts +93 -0
  35. package/microservices/microservice-payments/src/db/migrations.ts +63 -0
  36. package/microservices/microservice-payments/src/db/payments.ts +652 -0
  37. package/microservices/microservice-payments/src/index.ts +51 -0
  38. package/microservices/microservice-payments/src/mcp/index.ts +460 -0
  39. package/microservices/microservice-payroll/package.json +27 -0
  40. package/microservices/microservice-payroll/src/cli/index.ts +374 -0
  41. package/microservices/microservice-payroll/src/db/database.ts +93 -0
  42. package/microservices/microservice-payroll/src/db/migrations.ts +69 -0
  43. package/microservices/microservice-payroll/src/db/payroll.ts +741 -0
  44. package/microservices/microservice-payroll/src/index.ts +48 -0
  45. package/microservices/microservice-payroll/src/mcp/index.ts +420 -0
  46. package/microservices/microservice-shipping/package.json +27 -0
  47. package/microservices/microservice-shipping/src/cli/index.ts +398 -0
  48. package/microservices/microservice-shipping/src/db/database.ts +93 -0
  49. package/microservices/microservice-shipping/src/db/migrations.ts +61 -0
  50. package/microservices/microservice-shipping/src/db/shipping.ts +643 -0
  51. package/microservices/microservice-shipping/src/index.ts +53 -0
  52. package/microservices/microservice-shipping/src/mcp/index.ts +385 -0
  53. package/microservices/microservice-social/package.json +27 -0
  54. package/microservices/microservice-social/src/cli/index.ts +447 -0
  55. package/microservices/microservice-social/src/db/database.ts +93 -0
  56. package/microservices/microservice-social/src/db/migrations.ts +55 -0
  57. package/microservices/microservice-social/src/db/social.ts +672 -0
  58. package/microservices/microservice-social/src/index.ts +46 -0
  59. package/microservices/microservice-social/src/mcp/index.ts +435 -0
  60. package/microservices/microservice-subscriptions/package.json +27 -0
  61. package/microservices/microservice-subscriptions/src/cli/index.ts +400 -0
  62. package/microservices/microservice-subscriptions/src/db/database.ts +93 -0
  63. package/microservices/microservice-subscriptions/src/db/migrations.ts +57 -0
  64. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +692 -0
  65. package/microservices/microservice-subscriptions/src/index.ts +41 -0
  66. package/microservices/microservice-subscriptions/src/mcp/index.ts +365 -0
  67. package/package.json +1 -1
@@ -0,0 +1,643 @@
1
+ /**
2
+ * Shipping CRUD operations — orders, shipments, and returns
3
+ */
4
+
5
+ import { getDatabase } from "./database.js";
6
+
7
+ // ─── Types ───────────────────────────────────────────────────────────────────
8
+
9
+ export interface Address {
10
+ street: string;
11
+ city: string;
12
+ state: string;
13
+ zip: string;
14
+ country: string;
15
+ }
16
+
17
+ export interface OrderItem {
18
+ name: string;
19
+ qty: number;
20
+ weight: number;
21
+ value: number;
22
+ }
23
+
24
+ export interface Order {
25
+ id: string;
26
+ customer_name: string;
27
+ customer_email: string | null;
28
+ address: Address;
29
+ items: OrderItem[];
30
+ status: "pending" | "processing" | "shipped" | "delivered" | "returned";
31
+ total_value: number;
32
+ currency: string;
33
+ created_at: string;
34
+ updated_at: string;
35
+ }
36
+
37
+ interface OrderRow {
38
+ id: string;
39
+ customer_name: string;
40
+ customer_email: string | null;
41
+ address: string;
42
+ items: string;
43
+ status: string;
44
+ total_value: number;
45
+ currency: string;
46
+ created_at: string;
47
+ updated_at: string;
48
+ }
49
+
50
+ export interface Shipment {
51
+ id: string;
52
+ order_id: string;
53
+ carrier: "ups" | "fedex" | "usps" | "dhl";
54
+ tracking_number: string | null;
55
+ service: "ground" | "express" | "overnight";
56
+ status: "label_created" | "in_transit" | "out_for_delivery" | "delivered" | "exception";
57
+ shipped_at: string | null;
58
+ estimated_delivery: string | null;
59
+ delivered_at: string | null;
60
+ cost: number | null;
61
+ weight: number | null;
62
+ dimensions: Record<string, unknown> | null;
63
+ created_at: string;
64
+ }
65
+
66
+ interface ShipmentRow {
67
+ id: string;
68
+ order_id: string;
69
+ carrier: string;
70
+ tracking_number: string | null;
71
+ service: string;
72
+ status: string;
73
+ shipped_at: string | null;
74
+ estimated_delivery: string | null;
75
+ delivered_at: string | null;
76
+ cost: number | null;
77
+ weight: number | null;
78
+ dimensions: string | null;
79
+ created_at: string;
80
+ }
81
+
82
+ export interface Return {
83
+ id: string;
84
+ order_id: string;
85
+ reason: string | null;
86
+ status: "requested" | "approved" | "received" | "refunded";
87
+ created_at: string;
88
+ updated_at: string;
89
+ }
90
+
91
+ interface ReturnRow {
92
+ id: string;
93
+ order_id: string;
94
+ reason: string | null;
95
+ status: string;
96
+ created_at: string;
97
+ updated_at: string;
98
+ }
99
+
100
+ // ─── Row converters ──────────────────────────────────────────────────────────
101
+
102
+ function rowToOrder(row: OrderRow): Order {
103
+ return {
104
+ ...row,
105
+ address: JSON.parse(row.address || "{}"),
106
+ items: JSON.parse(row.items || "[]"),
107
+ status: row.status as Order["status"],
108
+ };
109
+ }
110
+
111
+ function rowToShipment(row: ShipmentRow): Shipment {
112
+ return {
113
+ ...row,
114
+ carrier: row.carrier as Shipment["carrier"],
115
+ service: row.service as Shipment["service"],
116
+ status: row.status as Shipment["status"],
117
+ dimensions: row.dimensions ? JSON.parse(row.dimensions) : null,
118
+ };
119
+ }
120
+
121
+ function rowToReturn(row: ReturnRow): Return {
122
+ return {
123
+ ...row,
124
+ status: row.status as Return["status"],
125
+ };
126
+ }
127
+
128
+ // ─── Orders ──────────────────────────────────────────────────────────────────
129
+
130
+ export interface CreateOrderInput {
131
+ customer_name: string;
132
+ customer_email?: string;
133
+ address: Address;
134
+ items: OrderItem[];
135
+ status?: Order["status"];
136
+ total_value?: number;
137
+ currency?: string;
138
+ }
139
+
140
+ export function createOrder(input: CreateOrderInput): Order {
141
+ const db = getDatabase();
142
+ const id = crypto.randomUUID();
143
+ const address = JSON.stringify(input.address);
144
+ const items = JSON.stringify(input.items);
145
+ const totalValue = input.total_value ?? input.items.reduce((sum, i) => sum + i.value * i.qty, 0);
146
+
147
+ db.prepare(
148
+ `INSERT INTO orders (id, customer_name, customer_email, address, items, status, total_value, currency)
149
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
150
+ ).run(
151
+ id,
152
+ input.customer_name,
153
+ input.customer_email || null,
154
+ address,
155
+ items,
156
+ input.status || "pending",
157
+ totalValue,
158
+ input.currency || "USD"
159
+ );
160
+
161
+ return getOrder(id)!;
162
+ }
163
+
164
+ export function getOrder(id: string): Order | null {
165
+ const db = getDatabase();
166
+ const row = db.prepare("SELECT * FROM orders WHERE id = ?").get(id) as OrderRow | null;
167
+ return row ? rowToOrder(row) : null;
168
+ }
169
+
170
+ export interface ListOrdersOptions {
171
+ status?: string;
172
+ search?: string;
173
+ limit?: number;
174
+ offset?: number;
175
+ }
176
+
177
+ export function listOrders(options: ListOrdersOptions = {}): Order[] {
178
+ const db = getDatabase();
179
+ const conditions: string[] = [];
180
+ const params: unknown[] = [];
181
+
182
+ if (options.status) {
183
+ conditions.push("status = ?");
184
+ params.push(options.status);
185
+ }
186
+
187
+ if (options.search) {
188
+ conditions.push(
189
+ "(customer_name LIKE ? OR customer_email LIKE ?)"
190
+ );
191
+ const q = `%${options.search}%`;
192
+ params.push(q, q);
193
+ }
194
+
195
+ let sql = "SELECT * FROM orders";
196
+ if (conditions.length > 0) {
197
+ sql += " WHERE " + conditions.join(" AND ");
198
+ }
199
+ sql += " ORDER BY created_at DESC";
200
+
201
+ if (options.limit) {
202
+ sql += " LIMIT ?";
203
+ params.push(options.limit);
204
+ }
205
+ if (options.offset) {
206
+ sql += " OFFSET ?";
207
+ params.push(options.offset);
208
+ }
209
+
210
+ const rows = db.prepare(sql).all(...params) as OrderRow[];
211
+ return rows.map(rowToOrder);
212
+ }
213
+
214
+ export interface UpdateOrderInput {
215
+ customer_name?: string;
216
+ customer_email?: string;
217
+ address?: Address;
218
+ items?: OrderItem[];
219
+ status?: Order["status"];
220
+ total_value?: number;
221
+ currency?: string;
222
+ }
223
+
224
+ export function updateOrder(id: string, input: UpdateOrderInput): Order | null {
225
+ const db = getDatabase();
226
+ const existing = getOrder(id);
227
+ if (!existing) return null;
228
+
229
+ const sets: string[] = [];
230
+ const params: unknown[] = [];
231
+
232
+ if (input.customer_name !== undefined) {
233
+ sets.push("customer_name = ?");
234
+ params.push(input.customer_name);
235
+ }
236
+ if (input.customer_email !== undefined) {
237
+ sets.push("customer_email = ?");
238
+ params.push(input.customer_email);
239
+ }
240
+ if (input.address !== undefined) {
241
+ sets.push("address = ?");
242
+ params.push(JSON.stringify(input.address));
243
+ }
244
+ if (input.items !== undefined) {
245
+ sets.push("items = ?");
246
+ params.push(JSON.stringify(input.items));
247
+ }
248
+ if (input.status !== undefined) {
249
+ sets.push("status = ?");
250
+ params.push(input.status);
251
+ }
252
+ if (input.total_value !== undefined) {
253
+ sets.push("total_value = ?");
254
+ params.push(input.total_value);
255
+ }
256
+ if (input.currency !== undefined) {
257
+ sets.push("currency = ?");
258
+ params.push(input.currency);
259
+ }
260
+
261
+ if (sets.length === 0) return existing;
262
+
263
+ sets.push("updated_at = datetime('now')");
264
+ params.push(id);
265
+
266
+ db.prepare(
267
+ `UPDATE orders SET ${sets.join(", ")} WHERE id = ?`
268
+ ).run(...params);
269
+
270
+ return getOrder(id);
271
+ }
272
+
273
+ export function deleteOrder(id: string): boolean {
274
+ const db = getDatabase();
275
+ const result = db.prepare("DELETE FROM orders WHERE id = ?").run(id);
276
+ return result.changes > 0;
277
+ }
278
+
279
+ export function listByStatus(status: string): Order[] {
280
+ return listOrders({ status });
281
+ }
282
+
283
+ export function searchOrders(query: string): Order[] {
284
+ return listOrders({ search: query });
285
+ }
286
+
287
+ // ─── Shipments ───────────────────────────────────────────────────────────────
288
+
289
+ export interface CreateShipmentInput {
290
+ order_id: string;
291
+ carrier: Shipment["carrier"];
292
+ tracking_number?: string;
293
+ service?: Shipment["service"];
294
+ status?: Shipment["status"];
295
+ shipped_at?: string;
296
+ estimated_delivery?: string;
297
+ cost?: number;
298
+ weight?: number;
299
+ dimensions?: Record<string, unknown>;
300
+ }
301
+
302
+ export function createShipment(input: CreateShipmentInput): Shipment {
303
+ const db = getDatabase();
304
+ const id = crypto.randomUUID();
305
+
306
+ db.prepare(
307
+ `INSERT INTO shipments (id, order_id, carrier, tracking_number, service, status, shipped_at, estimated_delivery, cost, weight, dimensions)
308
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
309
+ ).run(
310
+ id,
311
+ input.order_id,
312
+ input.carrier,
313
+ input.tracking_number || null,
314
+ input.service || "ground",
315
+ input.status || "label_created",
316
+ input.shipped_at || null,
317
+ input.estimated_delivery || null,
318
+ input.cost ?? null,
319
+ input.weight ?? null,
320
+ input.dimensions ? JSON.stringify(input.dimensions) : null
321
+ );
322
+
323
+ // Update order status to processing if it's pending
324
+ const order = getOrder(input.order_id);
325
+ if (order && order.status === "pending") {
326
+ updateOrder(input.order_id, { status: "processing" });
327
+ }
328
+
329
+ return getShipment(id)!;
330
+ }
331
+
332
+ export function getShipment(id: string): Shipment | null {
333
+ const db = getDatabase();
334
+ const row = db.prepare("SELECT * FROM shipments WHERE id = ?").get(id) as ShipmentRow | null;
335
+ return row ? rowToShipment(row) : null;
336
+ }
337
+
338
+ export function getShipmentByTracking(trackingNumber: string): Shipment | null {
339
+ const db = getDatabase();
340
+ const row = db.prepare("SELECT * FROM shipments WHERE tracking_number = ?").get(trackingNumber) as ShipmentRow | null;
341
+ return row ? rowToShipment(row) : null;
342
+ }
343
+
344
+ export interface ListShipmentsOptions {
345
+ order_id?: string;
346
+ carrier?: string;
347
+ status?: string;
348
+ limit?: number;
349
+ offset?: number;
350
+ }
351
+
352
+ export function listShipments(options: ListShipmentsOptions = {}): Shipment[] {
353
+ const db = getDatabase();
354
+ const conditions: string[] = [];
355
+ const params: unknown[] = [];
356
+
357
+ if (options.order_id) {
358
+ conditions.push("order_id = ?");
359
+ params.push(options.order_id);
360
+ }
361
+ if (options.carrier) {
362
+ conditions.push("carrier = ?");
363
+ params.push(options.carrier);
364
+ }
365
+ if (options.status) {
366
+ conditions.push("status = ?");
367
+ params.push(options.status);
368
+ }
369
+
370
+ let sql = "SELECT * FROM shipments";
371
+ if (conditions.length > 0) {
372
+ sql += " WHERE " + conditions.join(" AND ");
373
+ }
374
+ sql += " ORDER BY created_at DESC";
375
+
376
+ if (options.limit) {
377
+ sql += " LIMIT ?";
378
+ params.push(options.limit);
379
+ }
380
+ if (options.offset) {
381
+ sql += " OFFSET ?";
382
+ params.push(options.offset);
383
+ }
384
+
385
+ const rows = db.prepare(sql).all(...params) as ShipmentRow[];
386
+ return rows.map(rowToShipment);
387
+ }
388
+
389
+ export interface UpdateShipmentInput {
390
+ carrier?: Shipment["carrier"];
391
+ tracking_number?: string;
392
+ service?: Shipment["service"];
393
+ status?: Shipment["status"];
394
+ shipped_at?: string;
395
+ estimated_delivery?: string;
396
+ delivered_at?: string;
397
+ cost?: number;
398
+ weight?: number;
399
+ dimensions?: Record<string, unknown>;
400
+ }
401
+
402
+ export function updateShipment(id: string, input: UpdateShipmentInput): Shipment | null {
403
+ const db = getDatabase();
404
+ const existing = getShipment(id);
405
+ if (!existing) return null;
406
+
407
+ const sets: string[] = [];
408
+ const params: unknown[] = [];
409
+
410
+ if (input.carrier !== undefined) {
411
+ sets.push("carrier = ?");
412
+ params.push(input.carrier);
413
+ }
414
+ if (input.tracking_number !== undefined) {
415
+ sets.push("tracking_number = ?");
416
+ params.push(input.tracking_number);
417
+ }
418
+ if (input.service !== undefined) {
419
+ sets.push("service = ?");
420
+ params.push(input.service);
421
+ }
422
+ if (input.status !== undefined) {
423
+ sets.push("status = ?");
424
+ params.push(input.status);
425
+
426
+ // Update order status when shipment is delivered
427
+ if (input.status === "delivered") {
428
+ updateOrder(existing.order_id, { status: "delivered" });
429
+ }
430
+ }
431
+ if (input.shipped_at !== undefined) {
432
+ sets.push("shipped_at = ?");
433
+ params.push(input.shipped_at);
434
+ }
435
+ if (input.estimated_delivery !== undefined) {
436
+ sets.push("estimated_delivery = ?");
437
+ params.push(input.estimated_delivery);
438
+ }
439
+ if (input.delivered_at !== undefined) {
440
+ sets.push("delivered_at = ?");
441
+ params.push(input.delivered_at);
442
+ }
443
+ if (input.cost !== undefined) {
444
+ sets.push("cost = ?");
445
+ params.push(input.cost);
446
+ }
447
+ if (input.weight !== undefined) {
448
+ sets.push("weight = ?");
449
+ params.push(input.weight);
450
+ }
451
+ if (input.dimensions !== undefined) {
452
+ sets.push("dimensions = ?");
453
+ params.push(JSON.stringify(input.dimensions));
454
+ }
455
+
456
+ if (sets.length === 0) return existing;
457
+
458
+ params.push(id);
459
+
460
+ db.prepare(
461
+ `UPDATE shipments SET ${sets.join(", ")} WHERE id = ?`
462
+ ).run(...params);
463
+
464
+ return getShipment(id);
465
+ }
466
+
467
+ export function deleteShipment(id: string): boolean {
468
+ const db = getDatabase();
469
+ const result = db.prepare("DELETE FROM shipments WHERE id = ?").run(id);
470
+ return result.changes > 0;
471
+ }
472
+
473
+ // ─── Returns ─────────────────────────────────────────────────────────────────
474
+
475
+ export interface CreateReturnInput {
476
+ order_id: string;
477
+ reason?: string;
478
+ status?: Return["status"];
479
+ }
480
+
481
+ export function createReturn(input: CreateReturnInput): Return {
482
+ const db = getDatabase();
483
+ const id = crypto.randomUUID();
484
+
485
+ db.prepare(
486
+ `INSERT INTO returns (id, order_id, reason, status)
487
+ VALUES (?, ?, ?, ?)`
488
+ ).run(
489
+ id,
490
+ input.order_id,
491
+ input.reason || null,
492
+ input.status || "requested"
493
+ );
494
+
495
+ return getReturn(id)!;
496
+ }
497
+
498
+ export function getReturn(id: string): Return | null {
499
+ const db = getDatabase();
500
+ const row = db.prepare("SELECT * FROM returns WHERE id = ?").get(id) as ReturnRow | null;
501
+ return row ? rowToReturn(row) : null;
502
+ }
503
+
504
+ export interface ListReturnsOptions {
505
+ order_id?: string;
506
+ status?: string;
507
+ limit?: number;
508
+ offset?: number;
509
+ }
510
+
511
+ export function listReturns(options: ListReturnsOptions = {}): Return[] {
512
+ const db = getDatabase();
513
+ const conditions: string[] = [];
514
+ const params: unknown[] = [];
515
+
516
+ if (options.order_id) {
517
+ conditions.push("order_id = ?");
518
+ params.push(options.order_id);
519
+ }
520
+ if (options.status) {
521
+ conditions.push("status = ?");
522
+ params.push(options.status);
523
+ }
524
+
525
+ let sql = "SELECT * FROM returns";
526
+ if (conditions.length > 0) {
527
+ sql += " WHERE " + conditions.join(" AND ");
528
+ }
529
+ sql += " ORDER BY created_at DESC";
530
+
531
+ if (options.limit) {
532
+ sql += " LIMIT ?";
533
+ params.push(options.limit);
534
+ }
535
+ if (options.offset) {
536
+ sql += " OFFSET ?";
537
+ params.push(options.offset);
538
+ }
539
+
540
+ const rows = db.prepare(sql).all(...params) as ReturnRow[];
541
+ return rows.map(rowToReturn);
542
+ }
543
+
544
+ export interface UpdateReturnInput {
545
+ reason?: string;
546
+ status?: Return["status"];
547
+ }
548
+
549
+ export function updateReturn(id: string, input: UpdateReturnInput): Return | null {
550
+ const db = getDatabase();
551
+ const existing = getReturn(id);
552
+ if (!existing) return null;
553
+
554
+ const sets: string[] = [];
555
+ const params: unknown[] = [];
556
+
557
+ if (input.reason !== undefined) {
558
+ sets.push("reason = ?");
559
+ params.push(input.reason);
560
+ }
561
+ if (input.status !== undefined) {
562
+ sets.push("status = ?");
563
+ params.push(input.status);
564
+
565
+ // Update order status when return is received or refunded
566
+ if (input.status === "received" || input.status === "refunded") {
567
+ updateOrder(existing.order_id, { status: "returned" });
568
+ }
569
+ }
570
+
571
+ if (sets.length === 0) return existing;
572
+
573
+ sets.push("updated_at = datetime('now')");
574
+ params.push(id);
575
+
576
+ db.prepare(
577
+ `UPDATE returns SET ${sets.join(", ")} WHERE id = ?`
578
+ ).run(...params);
579
+
580
+ return getReturn(id);
581
+ }
582
+
583
+ export function deleteReturn(id: string): boolean {
584
+ const db = getDatabase();
585
+ const result = db.prepare("DELETE FROM returns WHERE id = ?").run(id);
586
+ return result.changes > 0;
587
+ }
588
+
589
+ // ─── Analytics ───────────────────────────────────────────────────────────────
590
+
591
+ export interface ShippingStats {
592
+ total_orders: number;
593
+ orders_by_status: Record<string, number>;
594
+ total_shipments: number;
595
+ shipments_by_status: Record<string, number>;
596
+ total_returns: number;
597
+ returns_by_status: Record<string, number>;
598
+ total_revenue: number;
599
+ total_shipping_cost: number;
600
+ }
601
+
602
+ export function getShippingStats(): ShippingStats {
603
+ const db = getDatabase();
604
+
605
+ const totalOrders = (db.prepare("SELECT COUNT(*) as count FROM orders").get() as { count: number }).count;
606
+ const ordersByStatus = db.prepare("SELECT status, COUNT(*) as count FROM orders GROUP BY status").all() as { status: string; count: number }[];
607
+
608
+ const totalShipments = (db.prepare("SELECT COUNT(*) as count FROM shipments").get() as { count: number }).count;
609
+ const shipmentsByStatus = db.prepare("SELECT status, COUNT(*) as count FROM shipments GROUP BY status").all() as { status: string; count: number }[];
610
+
611
+ const totalReturns = (db.prepare("SELECT COUNT(*) as count FROM returns").get() as { count: number }).count;
612
+ const returnsByStatus = db.prepare("SELECT status, COUNT(*) as count FROM returns GROUP BY status").all() as { status: string; count: number }[];
613
+
614
+ const totalRevenue = (db.prepare("SELECT COALESCE(SUM(total_value), 0) as total FROM orders").get() as { total: number }).total;
615
+ const totalShippingCost = (db.prepare("SELECT COALESCE(SUM(cost), 0) as total FROM shipments").get() as { total: number }).total;
616
+
617
+ return {
618
+ total_orders: totalOrders,
619
+ orders_by_status: Object.fromEntries(ordersByStatus.map((r) => [r.status, r.count])),
620
+ total_shipments: totalShipments,
621
+ shipments_by_status: Object.fromEntries(shipmentsByStatus.map((r) => [r.status, r.count])),
622
+ total_returns: totalReturns,
623
+ returns_by_status: Object.fromEntries(returnsByStatus.map((r) => [r.status, r.count])),
624
+ total_revenue: totalRevenue,
625
+ total_shipping_cost: totalShippingCost,
626
+ };
627
+ }
628
+
629
+ export interface CarrierCosts {
630
+ carrier: string;
631
+ total_cost: number;
632
+ shipment_count: number;
633
+ avg_cost: number;
634
+ }
635
+
636
+ export function getCostsByCarrier(): CarrierCosts[] {
637
+ const db = getDatabase();
638
+ const rows = db.prepare(
639
+ `SELECT carrier, COALESCE(SUM(cost), 0) as total_cost, COUNT(*) as shipment_count, COALESCE(AVG(cost), 0) as avg_cost
640
+ FROM shipments GROUP BY carrier ORDER BY total_cost DESC`
641
+ ).all() as CarrierCosts[];
642
+ return rows;
643
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * microservice-shipping — Shipping and order management microservice
3
+ */
4
+
5
+ export {
6
+ createOrder,
7
+ getOrder,
8
+ listOrders,
9
+ updateOrder,
10
+ deleteOrder,
11
+ searchOrders,
12
+ listByStatus,
13
+ type Order,
14
+ type OrderItem,
15
+ type Address,
16
+ type CreateOrderInput,
17
+ type UpdateOrderInput,
18
+ type ListOrdersOptions,
19
+ } from "./db/shipping.js";
20
+
21
+ export {
22
+ createShipment,
23
+ getShipment,
24
+ getShipmentByTracking,
25
+ listShipments,
26
+ updateShipment,
27
+ deleteShipment,
28
+ type Shipment,
29
+ type CreateShipmentInput,
30
+ type UpdateShipmentInput,
31
+ type ListShipmentsOptions,
32
+ } from "./db/shipping.js";
33
+
34
+ export {
35
+ createReturn,
36
+ getReturn,
37
+ listReturns,
38
+ updateReturn,
39
+ deleteReturn,
40
+ type Return,
41
+ type CreateReturnInput,
42
+ type UpdateReturnInput,
43
+ type ListReturnsOptions,
44
+ } from "./db/shipping.js";
45
+
46
+ export {
47
+ getShippingStats,
48
+ getCostsByCarrier,
49
+ type ShippingStats,
50
+ type CarrierCosts,
51
+ } from "./db/shipping.js";
52
+
53
+ export { getDatabase, closeDatabase } from "./db/database.js";