@hasna/microservices 0.0.10 → 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 (90) hide show
  1. package/bin/index.js +86 -1
  2. package/bin/mcp.js +86 -1
  3. package/dist/index.js +86 -1
  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-notifications/package.json +27 -0
  40. package/microservices/microservice-notifications/src/cli/index.ts +349 -0
  41. package/microservices/microservice-notifications/src/db/database.ts +93 -0
  42. package/microservices/microservice-notifications/src/db/migrations.ts +62 -0
  43. package/microservices/microservice-notifications/src/db/notifications.ts +509 -0
  44. package/microservices/microservice-notifications/src/index.ts +41 -0
  45. package/microservices/microservice-notifications/src/mcp/index.ts +422 -0
  46. package/microservices/microservice-products/package.json +27 -0
  47. package/microservices/microservice-products/src/cli/index.ts +416 -0
  48. package/microservices/microservice-products/src/db/categories.ts +154 -0
  49. package/microservices/microservice-products/src/db/database.ts +93 -0
  50. package/microservices/microservice-products/src/db/migrations.ts +58 -0
  51. package/microservices/microservice-products/src/db/pricing-tiers.ts +66 -0
  52. package/microservices/microservice-products/src/db/products.ts +452 -0
  53. package/microservices/microservice-products/src/index.ts +53 -0
  54. package/microservices/microservice-products/src/mcp/index.ts +453 -0
  55. package/microservices/microservice-projects/package.json +27 -0
  56. package/microservices/microservice-projects/src/cli/index.ts +480 -0
  57. package/microservices/microservice-projects/src/db/database.ts +93 -0
  58. package/microservices/microservice-projects/src/db/migrations.ts +65 -0
  59. package/microservices/microservice-projects/src/db/projects.ts +715 -0
  60. package/microservices/microservice-projects/src/index.ts +57 -0
  61. package/microservices/microservice-projects/src/mcp/index.ts +501 -0
  62. package/microservices/microservice-proposals/package.json +27 -0
  63. package/microservices/microservice-proposals/src/cli/index.ts +400 -0
  64. package/microservices/microservice-proposals/src/db/database.ts +93 -0
  65. package/microservices/microservice-proposals/src/db/migrations.ts +52 -0
  66. package/microservices/microservice-proposals/src/db/proposals.ts +532 -0
  67. package/microservices/microservice-proposals/src/index.ts +37 -0
  68. package/microservices/microservice-proposals/src/mcp/index.ts +375 -0
  69. package/microservices/microservice-reading/package.json +27 -0
  70. package/microservices/microservice-reading/src/cli/index.ts +464 -0
  71. package/microservices/microservice-reading/src/db/database.ts +93 -0
  72. package/microservices/microservice-reading/src/db/migrations.ts +59 -0
  73. package/microservices/microservice-reading/src/db/reading.ts +524 -0
  74. package/microservices/microservice-reading/src/index.ts +51 -0
  75. package/microservices/microservice-reading/src/mcp/index.ts +368 -0
  76. package/microservices/microservice-travel/package.json +27 -0
  77. package/microservices/microservice-travel/src/cli/index.ts +505 -0
  78. package/microservices/microservice-travel/src/db/database.ts +93 -0
  79. package/microservices/microservice-travel/src/db/migrations.ts +77 -0
  80. package/microservices/microservice-travel/src/db/travel.ts +802 -0
  81. package/microservices/microservice-travel/src/index.ts +60 -0
  82. package/microservices/microservice-travel/src/mcp/index.ts +495 -0
  83. package/microservices/microservice-wiki/package.json +27 -0
  84. package/microservices/microservice-wiki/src/cli/index.ts +345 -0
  85. package/microservices/microservice-wiki/src/db/database.ts +93 -0
  86. package/microservices/microservice-wiki/src/db/migrations.ts +55 -0
  87. package/microservices/microservice-wiki/src/db/wiki.ts +395 -0
  88. package/microservices/microservice-wiki/src/index.ts +32 -0
  89. package/microservices/microservice-wiki/src/mcp/index.ts +344 -0
  90. package/package.json +1 -1
@@ -0,0 +1,505 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from "commander";
4
+ import {
5
+ createTrip,
6
+ getTrip,
7
+ listTrips,
8
+ updateTrip,
9
+ deleteTrip,
10
+ getUpcomingTrips,
11
+ getTripBudgetVsActual,
12
+ getTravelStats,
13
+ } from "../db/travel.js";
14
+ import {
15
+ createBooking,
16
+ listBookings,
17
+ cancelBooking,
18
+ } from "../db/travel.js";
19
+ import {
20
+ createDocument,
21
+ listDocuments,
22
+ getExpiringDocuments,
23
+ } from "../db/travel.js";
24
+ import {
25
+ createLoyaltyProgram,
26
+ listLoyaltyPrograms,
27
+ getLoyaltyPointsSummary,
28
+ } from "../db/travel.js";
29
+
30
+ const program = new Command();
31
+
32
+ program
33
+ .name("microservice-travel")
34
+ .description("Travel management microservice")
35
+ .version("0.0.1");
36
+
37
+ // --- Trips ---
38
+
39
+ const tripCmd = program
40
+ .command("trip")
41
+ .description("Trip management");
42
+
43
+ tripCmd
44
+ .command("create")
45
+ .description("Create a new trip")
46
+ .requiredOption("--name <name>", "Trip name")
47
+ .option("--destination <destination>", "Destination")
48
+ .option("--start-date <date>", "Start date (YYYY-MM-DD)")
49
+ .option("--end-date <date>", "End date (YYYY-MM-DD)")
50
+ .option("--status <status>", "Status (planning|booked|in_progress|completed|cancelled)")
51
+ .option("--budget <amount>", "Budget amount")
52
+ .option("--currency <code>", "Currency code (default: USD)")
53
+ .option("--notes <notes>", "Notes")
54
+ .option("--json", "Output as JSON", false)
55
+ .action((opts) => {
56
+ const trip = createTrip({
57
+ name: opts.name,
58
+ destination: opts.destination,
59
+ start_date: opts.startDate,
60
+ end_date: opts.endDate,
61
+ status: opts.status,
62
+ budget: opts.budget ? parseFloat(opts.budget) : undefined,
63
+ currency: opts.currency,
64
+ notes: opts.notes,
65
+ });
66
+
67
+ if (opts.json) {
68
+ console.log(JSON.stringify(trip, null, 2));
69
+ } else {
70
+ console.log(`Created trip: ${trip.name} (${trip.id})`);
71
+ }
72
+ });
73
+
74
+ tripCmd
75
+ .command("list")
76
+ .description("List trips")
77
+ .option("--search <query>", "Search by name, destination, or notes")
78
+ .option("--status <status>", "Filter by status")
79
+ .option("--destination <dest>", "Filter by destination")
80
+ .option("--limit <n>", "Limit results")
81
+ .option("--json", "Output as JSON", false)
82
+ .action((opts) => {
83
+ const trips = listTrips({
84
+ search: opts.search,
85
+ status: opts.status,
86
+ destination: opts.destination,
87
+ limit: opts.limit ? parseInt(opts.limit) : undefined,
88
+ });
89
+
90
+ if (opts.json) {
91
+ console.log(JSON.stringify(trips, null, 2));
92
+ } else {
93
+ if (trips.length === 0) {
94
+ console.log("No trips found.");
95
+ return;
96
+ }
97
+ for (const t of trips) {
98
+ const dest = t.destination ? ` -> ${t.destination}` : "";
99
+ const dates = t.start_date ? ` (${t.start_date}${t.end_date ? ` to ${t.end_date}` : ""})` : "";
100
+ console.log(` [${t.status}] ${t.name}${dest}${dates}`);
101
+ }
102
+ console.log(`\n${trips.length} trip(s)`);
103
+ }
104
+ });
105
+
106
+ tripCmd
107
+ .command("get")
108
+ .description("Get a trip by ID")
109
+ .argument("<id>", "Trip ID")
110
+ .option("--json", "Output as JSON", false)
111
+ .action((id, opts) => {
112
+ const trip = getTrip(id);
113
+ if (!trip) {
114
+ console.error(`Trip '${id}' not found.`);
115
+ process.exit(1);
116
+ }
117
+
118
+ if (opts.json) {
119
+ console.log(JSON.stringify(trip, null, 2));
120
+ } else {
121
+ console.log(`${trip.name} [${trip.status}]`);
122
+ if (trip.destination) console.log(` Destination: ${trip.destination}`);
123
+ if (trip.start_date) console.log(` Dates: ${trip.start_date}${trip.end_date ? ` to ${trip.end_date}` : ""}`);
124
+ if (trip.budget !== null) console.log(` Budget: ${trip.currency} ${trip.budget} (spent: ${trip.spent})`);
125
+ if (trip.notes) console.log(` Notes: ${trip.notes}`);
126
+ }
127
+ });
128
+
129
+ tripCmd
130
+ .command("update")
131
+ .description("Update a trip")
132
+ .argument("<id>", "Trip ID")
133
+ .option("--name <name>", "Trip name")
134
+ .option("--destination <destination>", "Destination")
135
+ .option("--start-date <date>", "Start date")
136
+ .option("--end-date <date>", "End date")
137
+ .option("--status <status>", "Status")
138
+ .option("--budget <amount>", "Budget")
139
+ .option("--currency <code>", "Currency")
140
+ .option("--notes <notes>", "Notes")
141
+ .option("--json", "Output as JSON", false)
142
+ .action((id, opts) => {
143
+ const input: Record<string, unknown> = {};
144
+ if (opts.name !== undefined) input.name = opts.name;
145
+ if (opts.destination !== undefined) input.destination = opts.destination;
146
+ if (opts.startDate !== undefined) input.start_date = opts.startDate;
147
+ if (opts.endDate !== undefined) input.end_date = opts.endDate;
148
+ if (opts.status !== undefined) input.status = opts.status;
149
+ if (opts.budget !== undefined) input.budget = parseFloat(opts.budget);
150
+ if (opts.currency !== undefined) input.currency = opts.currency;
151
+ if (opts.notes !== undefined) input.notes = opts.notes;
152
+
153
+ const trip = updateTrip(id, input);
154
+ if (!trip) {
155
+ console.error(`Trip '${id}' not found.`);
156
+ process.exit(1);
157
+ }
158
+
159
+ if (opts.json) {
160
+ console.log(JSON.stringify(trip, null, 2));
161
+ } else {
162
+ console.log(`Updated: ${trip.name}`);
163
+ }
164
+ });
165
+
166
+ tripCmd
167
+ .command("delete")
168
+ .description("Delete a trip")
169
+ .argument("<id>", "Trip ID")
170
+ .action((id) => {
171
+ const deleted = deleteTrip(id);
172
+ if (deleted) {
173
+ console.log(`Deleted trip ${id}`);
174
+ } else {
175
+ console.error(`Trip '${id}' not found.`);
176
+ process.exit(1);
177
+ }
178
+ });
179
+
180
+ // --- Bookings ---
181
+
182
+ const bookingCmd = program
183
+ .command("booking")
184
+ .description("Booking management");
185
+
186
+ bookingCmd
187
+ .command("add")
188
+ .description("Add a booking to a trip")
189
+ .requiredOption("--trip <id>", "Trip ID")
190
+ .requiredOption("--type <type>", "Type (flight|hotel|car|train|activity)")
191
+ .option("--provider <provider>", "Provider name")
192
+ .option("--confirmation <code>", "Confirmation code")
193
+ .option("--check-in <date>", "Check-in date")
194
+ .option("--check-out <date>", "Check-out date")
195
+ .option("--cost <amount>", "Cost")
196
+ .option("--json", "Output as JSON", false)
197
+ .action((opts) => {
198
+ const booking = createBooking({
199
+ trip_id: opts.trip,
200
+ type: opts.type,
201
+ provider: opts.provider,
202
+ confirmation_code: opts.confirmation,
203
+ check_in: opts.checkIn,
204
+ check_out: opts.checkOut,
205
+ cost: opts.cost ? parseFloat(opts.cost) : undefined,
206
+ });
207
+
208
+ if (opts.json) {
209
+ console.log(JSON.stringify(booking, null, 2));
210
+ } else {
211
+ console.log(`Added ${booking.type} booking (${booking.id})`);
212
+ }
213
+ });
214
+
215
+ bookingCmd
216
+ .command("list")
217
+ .description("List bookings")
218
+ .option("--trip <id>", "Filter by trip")
219
+ .option("--type <type>", "Filter by type")
220
+ .option("--json", "Output as JSON", false)
221
+ .action((opts) => {
222
+ const bookings = listBookings({
223
+ trip_id: opts.trip,
224
+ type: opts.type,
225
+ });
226
+
227
+ if (opts.json) {
228
+ console.log(JSON.stringify(bookings, null, 2));
229
+ } else {
230
+ if (bookings.length === 0) {
231
+ console.log("No bookings found.");
232
+ return;
233
+ }
234
+ for (const b of bookings) {
235
+ const provider = b.provider ? ` via ${b.provider}` : "";
236
+ const cost = b.cost !== null ? ` ($${b.cost})` : "";
237
+ console.log(` [${b.status}] ${b.type}${provider}${cost}`);
238
+ }
239
+ console.log(`\n${bookings.length} booking(s)`);
240
+ }
241
+ });
242
+
243
+ bookingCmd
244
+ .command("cancel")
245
+ .description("Cancel a booking")
246
+ .argument("<id>", "Booking ID")
247
+ .option("--json", "Output as JSON", false)
248
+ .action((id, opts) => {
249
+ const booking = cancelBooking(id);
250
+ if (!booking) {
251
+ console.error(`Booking '${id}' not found.`);
252
+ process.exit(1);
253
+ }
254
+
255
+ if (opts.json) {
256
+ console.log(JSON.stringify(booking, null, 2));
257
+ } else {
258
+ console.log(`Cancelled booking ${id}`);
259
+ }
260
+ });
261
+
262
+ // --- Documents ---
263
+
264
+ const docCmd = program
265
+ .command("document")
266
+ .alias("doc")
267
+ .description("Travel document management");
268
+
269
+ docCmd
270
+ .command("add")
271
+ .description("Add a travel document")
272
+ .requiredOption("--trip <id>", "Trip ID")
273
+ .requiredOption("--type <type>", "Type (passport|visa|insurance|ticket|voucher)")
274
+ .requiredOption("--name <name>", "Document name")
275
+ .option("--number <number>", "Document number")
276
+ .option("--expires <date>", "Expiry date (YYYY-MM-DD)")
277
+ .option("--file <path>", "File path")
278
+ .option("--json", "Output as JSON", false)
279
+ .action((opts) => {
280
+ const doc = createDocument({
281
+ trip_id: opts.trip,
282
+ type: opts.type,
283
+ name: opts.name,
284
+ number: opts.number,
285
+ expires_at: opts.expires,
286
+ file_path: opts.file,
287
+ });
288
+
289
+ if (opts.json) {
290
+ console.log(JSON.stringify(doc, null, 2));
291
+ } else {
292
+ console.log(`Added document: ${doc.name} (${doc.id})`);
293
+ }
294
+ });
295
+
296
+ docCmd
297
+ .command("list")
298
+ .description("List documents")
299
+ .option("--trip <id>", "Filter by trip")
300
+ .option("--type <type>", "Filter by type")
301
+ .option("--json", "Output as JSON", false)
302
+ .action((opts) => {
303
+ const docs = listDocuments({
304
+ trip_id: opts.trip,
305
+ type: opts.type,
306
+ });
307
+
308
+ if (opts.json) {
309
+ console.log(JSON.stringify(docs, null, 2));
310
+ } else {
311
+ if (docs.length === 0) {
312
+ console.log("No documents found.");
313
+ return;
314
+ }
315
+ for (const d of docs) {
316
+ const expires = d.expires_at ? ` (expires: ${d.expires_at})` : "";
317
+ console.log(` [${d.type}] ${d.name}${expires}`);
318
+ }
319
+ console.log(`\n${docs.length} document(s)`);
320
+ }
321
+ });
322
+
323
+ docCmd
324
+ .command("expiring")
325
+ .description("List documents expiring soon")
326
+ .option("--days <n>", "Days ahead to check", "90")
327
+ .option("--json", "Output as JSON", false)
328
+ .action((opts) => {
329
+ const docs = getExpiringDocuments(parseInt(opts.days));
330
+
331
+ if (opts.json) {
332
+ console.log(JSON.stringify(docs, null, 2));
333
+ } else {
334
+ if (docs.length === 0) {
335
+ console.log(`No documents expiring in the next ${opts.days} days.`);
336
+ return;
337
+ }
338
+ for (const d of docs) {
339
+ console.log(` [${d.type}] ${d.name} — expires ${d.expires_at}`);
340
+ }
341
+ }
342
+ });
343
+
344
+ // --- Loyalty Programs ---
345
+
346
+ const loyaltyCmd = program
347
+ .command("loyalty")
348
+ .description("Loyalty program management");
349
+
350
+ loyaltyCmd
351
+ .command("add")
352
+ .description("Add a loyalty program")
353
+ .requiredOption("--program <name>", "Program name")
354
+ .option("--member-id <id>", "Member ID")
355
+ .option("--tier <tier>", "Tier level")
356
+ .option("--points <n>", "Points")
357
+ .option("--miles <n>", "Miles")
358
+ .option("--expires <date>", "Expiry date")
359
+ .option("--json", "Output as JSON", false)
360
+ .action((opts) => {
361
+ const loyalty = createLoyaltyProgram({
362
+ program_name: opts.program,
363
+ member_id: opts.memberId,
364
+ tier: opts.tier,
365
+ points: opts.points ? parseInt(opts.points) : undefined,
366
+ miles: opts.miles ? parseInt(opts.miles) : undefined,
367
+ expires_at: opts.expires,
368
+ });
369
+
370
+ if (opts.json) {
371
+ console.log(JSON.stringify(loyalty, null, 2));
372
+ } else {
373
+ console.log(`Added loyalty program: ${loyalty.program_name} (${loyalty.id})`);
374
+ }
375
+ });
376
+
377
+ loyaltyCmd
378
+ .command("list")
379
+ .description("List loyalty programs")
380
+ .option("--json", "Output as JSON", false)
381
+ .action((opts) => {
382
+ const programs = listLoyaltyPrograms();
383
+
384
+ if (opts.json) {
385
+ console.log(JSON.stringify(programs, null, 2));
386
+ } else {
387
+ if (programs.length === 0) {
388
+ console.log("No loyalty programs found.");
389
+ return;
390
+ }
391
+ for (const p of programs) {
392
+ const tier = p.tier ? ` [${p.tier}]` : "";
393
+ const pts = p.points > 0 ? ` ${p.points} pts` : "";
394
+ const mi = p.miles > 0 ? ` ${p.miles} mi` : "";
395
+ console.log(` ${p.program_name}${tier}${pts}${mi}`);
396
+ }
397
+ }
398
+ });
399
+
400
+ loyaltyCmd
401
+ .command("summary")
402
+ .description("Show loyalty points summary")
403
+ .option("--json", "Output as JSON", false)
404
+ .action((opts) => {
405
+ const summary = getLoyaltyPointsSummary();
406
+
407
+ if (opts.json) {
408
+ console.log(JSON.stringify(summary, null, 2));
409
+ } else {
410
+ console.log(`Total points: ${summary.total_points}`);
411
+ console.log(`Total miles: ${summary.total_miles}`);
412
+ if (summary.programs.length > 0) {
413
+ console.log("\nPrograms:");
414
+ for (const p of summary.programs) {
415
+ const tier = p.tier ? ` [${p.tier}]` : "";
416
+ console.log(` ${p.program_name}${tier}: ${p.points} pts, ${p.miles} mi`);
417
+ }
418
+ }
419
+ }
420
+ });
421
+
422
+ // --- Special Commands ---
423
+
424
+ program
425
+ .command("upcoming")
426
+ .description("Show upcoming trips")
427
+ .option("--days <n>", "Days ahead to check", "30")
428
+ .option("--json", "Output as JSON", false)
429
+ .action((opts) => {
430
+ const trips = getUpcomingTrips(parseInt(opts.days));
431
+
432
+ if (opts.json) {
433
+ console.log(JSON.stringify(trips, null, 2));
434
+ } else {
435
+ if (trips.length === 0) {
436
+ console.log(`No upcoming trips in the next ${opts.days} days.`);
437
+ return;
438
+ }
439
+ for (const t of trips) {
440
+ const dest = t.destination ? ` -> ${t.destination}` : "";
441
+ console.log(` ${t.start_date}: ${t.name}${dest} [${t.status}]`);
442
+ }
443
+ }
444
+ });
445
+
446
+ program
447
+ .command("budget")
448
+ .description("Show budget vs actual for a trip")
449
+ .argument("<trip-id>", "Trip ID")
450
+ .option("--json", "Output as JSON", false)
451
+ .action((tripId, opts) => {
452
+ const budget = getTripBudgetVsActual(tripId);
453
+ if (!budget) {
454
+ console.error(`Trip '${tripId}' not found.`);
455
+ process.exit(1);
456
+ }
457
+
458
+ if (opts.json) {
459
+ console.log(JSON.stringify(budget, null, 2));
460
+ } else {
461
+ console.log(`${budget.trip_name}`);
462
+ console.log(` Budget: ${budget.currency} ${budget.budget ?? "N/A"}`);
463
+ console.log(` Spent: ${budget.currency} ${budget.spent}`);
464
+ if (budget.remaining !== null) {
465
+ console.log(` Remaining: ${budget.currency} ${budget.remaining}`);
466
+ if (budget.over_budget) console.log(` ** OVER BUDGET **`);
467
+ }
468
+ if (Object.keys(budget.bookings_by_type).length > 0) {
469
+ console.log("\n By type:");
470
+ for (const [type, cost] of Object.entries(budget.bookings_by_type)) {
471
+ console.log(` ${type}: ${budget.currency} ${cost}`);
472
+ }
473
+ }
474
+ }
475
+ });
476
+
477
+ program
478
+ .command("stats")
479
+ .description("Show travel statistics")
480
+ .option("--year <year>", "Filter by year")
481
+ .option("--json", "Output as JSON", false)
482
+ .action((opts) => {
483
+ const stats = getTravelStats(opts.year ? parseInt(opts.year) : undefined);
484
+
485
+ if (opts.json) {
486
+ console.log(JSON.stringify(stats, null, 2));
487
+ } else {
488
+ console.log(`Trips taken: ${stats.trips_taken}`);
489
+ console.log(`Total spent: $${stats.total_spent}`);
490
+ if (Object.keys(stats.by_destination).length > 0) {
491
+ console.log("\nBy destination:");
492
+ for (const [dest, count] of Object.entries(stats.by_destination)) {
493
+ console.log(` ${dest}: ${count} trip(s)`);
494
+ }
495
+ }
496
+ if (Object.keys(stats.by_booking_type).length > 0) {
497
+ console.log("\nBy booking type:");
498
+ for (const [type, count] of Object.entries(stats.by_booking_type)) {
499
+ console.log(` ${type}: ${count}`);
500
+ }
501
+ }
502
+ }
503
+ });
504
+
505
+ program.parse(process.argv);
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Database connection for microservice-travel
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-travel", "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-travel", "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-travel", "data.db");
32
+ }
33
+
34
+ function ensureDir(filePath: string): void {
35
+ const dir = dirname(resolve(filePath));
36
+ if (!existsSync(dir)) {
37
+ mkdirSync(dir, { recursive: true });
38
+ }
39
+ }
40
+
41
+ export function getDatabase(): Database {
42
+ if (_db) return _db;
43
+
44
+ const dbPath = getDbPath();
45
+ ensureDir(dbPath);
46
+
47
+ _db = new Database(dbPath);
48
+ _db.exec("PRAGMA journal_mode = WAL");
49
+ _db.exec("PRAGMA foreign_keys = ON");
50
+
51
+ // Create migrations table
52
+ _db.exec(`
53
+ CREATE TABLE IF NOT EXISTS _migrations (
54
+ id INTEGER PRIMARY KEY,
55
+ name TEXT NOT NULL,
56
+ applied_at TEXT NOT NULL DEFAULT (datetime('now'))
57
+ )
58
+ `);
59
+
60
+ // Apply pending migrations
61
+ const applied = _db
62
+ .query("SELECT id FROM _migrations ORDER BY id")
63
+ .all() as { id: number }[];
64
+ const appliedIds = new Set(applied.map((r) => r.id));
65
+
66
+ for (const migration of MIGRATIONS) {
67
+ if (appliedIds.has(migration.id)) continue;
68
+
69
+ _db.exec("BEGIN");
70
+ try {
71
+ _db.exec(migration.sql);
72
+ _db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
73
+ migration.id,
74
+ migration.name
75
+ );
76
+ _db.exec("COMMIT");
77
+ } catch (error) {
78
+ _db.exec("ROLLBACK");
79
+ throw new Error(
80
+ `Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
81
+ );
82
+ }
83
+ }
84
+
85
+ return _db;
86
+ }
87
+
88
+ export function closeDatabase(): void {
89
+ if (_db) {
90
+ _db.close();
91
+ _db = null;
92
+ }
93
+ }
@@ -0,0 +1,77 @@
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 trips (
13
+ id TEXT PRIMARY KEY,
14
+ name TEXT NOT NULL,
15
+ destination TEXT,
16
+ start_date TEXT,
17
+ end_date TEXT,
18
+ status TEXT NOT NULL DEFAULT 'planning' CHECK(status IN ('planning','booked','in_progress','completed','cancelled')),
19
+ budget REAL,
20
+ spent REAL NOT NULL DEFAULT 0,
21
+ currency TEXT NOT NULL DEFAULT 'USD',
22
+ notes TEXT,
23
+ metadata TEXT NOT NULL DEFAULT '{}',
24
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
25
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
26
+ );
27
+
28
+ CREATE TABLE IF NOT EXISTS bookings (
29
+ id TEXT PRIMARY KEY,
30
+ trip_id TEXT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
31
+ type TEXT NOT NULL CHECK(type IN ('flight','hotel','car','train','activity')),
32
+ provider TEXT,
33
+ confirmation_code TEXT,
34
+ status TEXT NOT NULL DEFAULT 'confirmed' CHECK(status IN ('confirmed','pending','cancelled')),
35
+ check_in TEXT,
36
+ check_out TEXT,
37
+ cost REAL,
38
+ details TEXT NOT NULL DEFAULT '{}',
39
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
40
+ );
41
+
42
+ CREATE TABLE IF NOT EXISTS documents (
43
+ id TEXT PRIMARY KEY,
44
+ trip_id TEXT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
45
+ type TEXT NOT NULL CHECK(type IN ('passport','visa','insurance','ticket','voucher')),
46
+ name TEXT NOT NULL,
47
+ number TEXT,
48
+ expires_at TEXT,
49
+ file_path TEXT,
50
+ metadata TEXT NOT NULL DEFAULT '{}',
51
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
52
+ );
53
+
54
+ CREATE TABLE IF NOT EXISTS loyalty_programs (
55
+ id TEXT PRIMARY KEY,
56
+ program_name TEXT NOT NULL,
57
+ member_id TEXT,
58
+ tier TEXT,
59
+ points INTEGER NOT NULL DEFAULT 0,
60
+ miles INTEGER NOT NULL DEFAULT 0,
61
+ expires_at TEXT,
62
+ metadata TEXT NOT NULL DEFAULT '{}',
63
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
64
+ );
65
+
66
+ CREATE INDEX IF NOT EXISTS idx_trips_status ON trips(status);
67
+ CREATE INDEX IF NOT EXISTS idx_trips_destination ON trips(destination);
68
+ CREATE INDEX IF NOT EXISTS idx_trips_start_date ON trips(start_date);
69
+ CREATE INDEX IF NOT EXISTS idx_bookings_trip ON bookings(trip_id);
70
+ CREATE INDEX IF NOT EXISTS idx_bookings_type ON bookings(type);
71
+ CREATE INDEX IF NOT EXISTS idx_documents_trip ON documents(trip_id);
72
+ CREATE INDEX IF NOT EXISTS idx_documents_type ON documents(type);
73
+ CREATE INDEX IF NOT EXISTS idx_documents_expires ON documents(expires_at);
74
+ CREATE INDEX IF NOT EXISTS idx_loyalty_program_name ON loyalty_programs(program_name);
75
+ `,
76
+ },
77
+ ];