@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,708 @@
1
+ /**
2
+ * Health CRUD operations for metrics, medications, appointments, and fitness logs
3
+ */
4
+
5
+ import { getDatabase } from "./database.js";
6
+
7
+ // ============================================================
8
+ // Metrics
9
+ // ============================================================
10
+
11
+ export interface Metric {
12
+ id: string;
13
+ type: string;
14
+ value: number;
15
+ unit: string | null;
16
+ notes: string | null;
17
+ recorded_at: string;
18
+ metadata: Record<string, unknown>;
19
+ created_at: string;
20
+ }
21
+
22
+ interface MetricRow {
23
+ id: string;
24
+ type: string;
25
+ value: number;
26
+ unit: string | null;
27
+ notes: string | null;
28
+ recorded_at: string;
29
+ metadata: string;
30
+ created_at: string;
31
+ }
32
+
33
+ function rowToMetric(row: MetricRow): Metric {
34
+ return {
35
+ ...row,
36
+ metadata: JSON.parse(row.metadata || "{}"),
37
+ };
38
+ }
39
+
40
+ export interface CreateMetricInput {
41
+ type: string;
42
+ value: number;
43
+ unit?: string;
44
+ notes?: string;
45
+ recorded_at?: string;
46
+ metadata?: Record<string, unknown>;
47
+ }
48
+
49
+ export function createMetric(input: CreateMetricInput): Metric {
50
+ const db = getDatabase();
51
+ const id = crypto.randomUUID();
52
+ const metadata = JSON.stringify(input.metadata || {});
53
+ const recorded_at = input.recorded_at || new Date().toISOString();
54
+
55
+ db.prepare(
56
+ `INSERT INTO metrics (id, type, value, unit, notes, recorded_at, metadata)
57
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
58
+ ).run(id, input.type, input.value, input.unit || null, input.notes || null, recorded_at, metadata);
59
+
60
+ return getMetric(id)!;
61
+ }
62
+
63
+ export function getMetric(id: string): Metric | null {
64
+ const db = getDatabase();
65
+ const row = db.prepare("SELECT * FROM metrics WHERE id = ?").get(id) as MetricRow | null;
66
+ return row ? rowToMetric(row) : null;
67
+ }
68
+
69
+ export interface ListMetricsOptions {
70
+ type?: string;
71
+ limit?: number;
72
+ offset?: number;
73
+ }
74
+
75
+ export function listMetrics(options: ListMetricsOptions = {}): Metric[] {
76
+ const db = getDatabase();
77
+ const conditions: string[] = [];
78
+ const params: unknown[] = [];
79
+
80
+ if (options.type) {
81
+ conditions.push("type = ?");
82
+ params.push(options.type);
83
+ }
84
+
85
+ let sql = "SELECT * FROM metrics";
86
+ if (conditions.length > 0) {
87
+ sql += " WHERE " + conditions.join(" AND ");
88
+ }
89
+ sql += " ORDER BY recorded_at DESC";
90
+
91
+ if (options.limit) {
92
+ sql += " LIMIT ?";
93
+ params.push(options.limit);
94
+ }
95
+ if (options.offset) {
96
+ sql += " OFFSET ?";
97
+ params.push(options.offset);
98
+ }
99
+
100
+ const rows = db.prepare(sql).all(...params) as MetricRow[];
101
+ return rows.map(rowToMetric);
102
+ }
103
+
104
+ export function deleteMetric(id: string): boolean {
105
+ const db = getDatabase();
106
+ const result = db.prepare("DELETE FROM metrics WHERE id = ?").run(id);
107
+ return result.changes > 0;
108
+ }
109
+
110
+ export interface MetricTrendPoint {
111
+ date: string;
112
+ value: number;
113
+ }
114
+
115
+ export function getMetricTrend(type: string, days: number): MetricTrendPoint[] {
116
+ const db = getDatabase();
117
+ const rows = db
118
+ .prepare(
119
+ `SELECT date(recorded_at) as date, AVG(value) as value
120
+ FROM metrics
121
+ WHERE type = ? AND recorded_at >= datetime('now', '-' || ? || ' days')
122
+ GROUP BY date(recorded_at)
123
+ ORDER BY date(recorded_at) ASC`
124
+ )
125
+ .all(type, days) as MetricTrendPoint[];
126
+ return rows;
127
+ }
128
+
129
+ // ============================================================
130
+ // Medications
131
+ // ============================================================
132
+
133
+ export interface Medication {
134
+ id: string;
135
+ name: string;
136
+ dosage: string | null;
137
+ frequency: string | null;
138
+ start_date: string | null;
139
+ end_date: string | null;
140
+ refill_date: string | null;
141
+ notes: string | null;
142
+ active: boolean;
143
+ created_at: string;
144
+ }
145
+
146
+ interface MedicationRow {
147
+ id: string;
148
+ name: string;
149
+ dosage: string | null;
150
+ frequency: string | null;
151
+ start_date: string | null;
152
+ end_date: string | null;
153
+ refill_date: string | null;
154
+ notes: string | null;
155
+ active: number;
156
+ created_at: string;
157
+ }
158
+
159
+ function rowToMedication(row: MedicationRow): Medication {
160
+ return {
161
+ ...row,
162
+ active: row.active === 1,
163
+ };
164
+ }
165
+
166
+ export interface CreateMedicationInput {
167
+ name: string;
168
+ dosage?: string;
169
+ frequency?: string;
170
+ start_date?: string;
171
+ end_date?: string;
172
+ refill_date?: string;
173
+ notes?: string;
174
+ active?: boolean;
175
+ }
176
+
177
+ export function createMedication(input: CreateMedicationInput): Medication {
178
+ const db = getDatabase();
179
+ const id = crypto.randomUUID();
180
+ const active = input.active !== undefined ? (input.active ? 1 : 0) : 1;
181
+
182
+ db.prepare(
183
+ `INSERT INTO medications (id, name, dosage, frequency, start_date, end_date, refill_date, notes, active)
184
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
185
+ ).run(
186
+ id,
187
+ input.name,
188
+ input.dosage || null,
189
+ input.frequency || null,
190
+ input.start_date || null,
191
+ input.end_date || null,
192
+ input.refill_date || null,
193
+ input.notes || null,
194
+ active
195
+ );
196
+
197
+ return getMedication(id)!;
198
+ }
199
+
200
+ export function getMedication(id: string): Medication | null {
201
+ const db = getDatabase();
202
+ const row = db.prepare("SELECT * FROM medications WHERE id = ?").get(id) as MedicationRow | null;
203
+ return row ? rowToMedication(row) : null;
204
+ }
205
+
206
+ export interface ListMedicationsOptions {
207
+ active?: boolean;
208
+ search?: string;
209
+ limit?: number;
210
+ offset?: number;
211
+ }
212
+
213
+ export function listMedications(options: ListMedicationsOptions = {}): Medication[] {
214
+ const db = getDatabase();
215
+ const conditions: string[] = [];
216
+ const params: unknown[] = [];
217
+
218
+ if (options.active !== undefined) {
219
+ conditions.push("active = ?");
220
+ params.push(options.active ? 1 : 0);
221
+ }
222
+
223
+ if (options.search) {
224
+ conditions.push("(name LIKE ? OR dosage LIKE ? OR notes LIKE ?)");
225
+ const q = `%${options.search}%`;
226
+ params.push(q, q, q);
227
+ }
228
+
229
+ let sql = "SELECT * FROM medications";
230
+ if (conditions.length > 0) {
231
+ sql += " WHERE " + conditions.join(" AND ");
232
+ }
233
+ sql += " ORDER BY name ASC";
234
+
235
+ if (options.limit) {
236
+ sql += " LIMIT ?";
237
+ params.push(options.limit);
238
+ }
239
+ if (options.offset) {
240
+ sql += " OFFSET ?";
241
+ params.push(options.offset);
242
+ }
243
+
244
+ const rows = db.prepare(sql).all(...params) as MedicationRow[];
245
+ return rows.map(rowToMedication);
246
+ }
247
+
248
+ export interface UpdateMedicationInput {
249
+ name?: string;
250
+ dosage?: string;
251
+ frequency?: string;
252
+ start_date?: string;
253
+ end_date?: string;
254
+ refill_date?: string;
255
+ notes?: string;
256
+ active?: boolean;
257
+ }
258
+
259
+ export function updateMedication(id: string, input: UpdateMedicationInput): Medication | null {
260
+ const db = getDatabase();
261
+ const existing = getMedication(id);
262
+ if (!existing) return null;
263
+
264
+ const sets: string[] = [];
265
+ const params: unknown[] = [];
266
+
267
+ if (input.name !== undefined) {
268
+ sets.push("name = ?");
269
+ params.push(input.name);
270
+ }
271
+ if (input.dosage !== undefined) {
272
+ sets.push("dosage = ?");
273
+ params.push(input.dosage);
274
+ }
275
+ if (input.frequency !== undefined) {
276
+ sets.push("frequency = ?");
277
+ params.push(input.frequency);
278
+ }
279
+ if (input.start_date !== undefined) {
280
+ sets.push("start_date = ?");
281
+ params.push(input.start_date);
282
+ }
283
+ if (input.end_date !== undefined) {
284
+ sets.push("end_date = ?");
285
+ params.push(input.end_date);
286
+ }
287
+ if (input.refill_date !== undefined) {
288
+ sets.push("refill_date = ?");
289
+ params.push(input.refill_date);
290
+ }
291
+ if (input.notes !== undefined) {
292
+ sets.push("notes = ?");
293
+ params.push(input.notes);
294
+ }
295
+ if (input.active !== undefined) {
296
+ sets.push("active = ?");
297
+ params.push(input.active ? 1 : 0);
298
+ }
299
+
300
+ if (sets.length === 0) return existing;
301
+
302
+ params.push(id);
303
+ db.prepare(`UPDATE medications SET ${sets.join(", ")} WHERE id = ?`).run(...params);
304
+
305
+ return getMedication(id);
306
+ }
307
+
308
+ export function deactivateMedication(id: string): Medication | null {
309
+ return updateMedication(id, { active: false });
310
+ }
311
+
312
+ export function deleteMedication(id: string): boolean {
313
+ const db = getDatabase();
314
+ const result = db.prepare("DELETE FROM medications WHERE id = ?").run(id);
315
+ return result.changes > 0;
316
+ }
317
+
318
+ export function getMedicationSchedule(): Medication[] {
319
+ return listMedications({ active: true });
320
+ }
321
+
322
+ // ============================================================
323
+ // Appointments
324
+ // ============================================================
325
+
326
+ export interface Appointment {
327
+ id: string;
328
+ provider: string;
329
+ specialty: string | null;
330
+ location: string | null;
331
+ scheduled_at: string;
332
+ status: "scheduled" | "completed" | "cancelled" | "rescheduled";
333
+ notes: string | null;
334
+ follow_up_date: string | null;
335
+ metadata: Record<string, unknown>;
336
+ created_at: string;
337
+ }
338
+
339
+ interface AppointmentRow {
340
+ id: string;
341
+ provider: string;
342
+ specialty: string | null;
343
+ location: string | null;
344
+ scheduled_at: string;
345
+ status: string;
346
+ notes: string | null;
347
+ follow_up_date: string | null;
348
+ metadata: string;
349
+ created_at: string;
350
+ }
351
+
352
+ function rowToAppointment(row: AppointmentRow): Appointment {
353
+ return {
354
+ ...row,
355
+ status: row.status as Appointment["status"],
356
+ metadata: JSON.parse(row.metadata || "{}"),
357
+ };
358
+ }
359
+
360
+ export interface CreateAppointmentInput {
361
+ provider: string;
362
+ specialty?: string;
363
+ location?: string;
364
+ scheduled_at: string;
365
+ status?: Appointment["status"];
366
+ notes?: string;
367
+ follow_up_date?: string;
368
+ metadata?: Record<string, unknown>;
369
+ }
370
+
371
+ export function createAppointment(input: CreateAppointmentInput): Appointment {
372
+ const db = getDatabase();
373
+ const id = crypto.randomUUID();
374
+ const metadata = JSON.stringify(input.metadata || {});
375
+ const status = input.status || "scheduled";
376
+
377
+ db.prepare(
378
+ `INSERT INTO appointments (id, provider, specialty, location, scheduled_at, status, notes, follow_up_date, metadata)
379
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
380
+ ).run(
381
+ id,
382
+ input.provider,
383
+ input.specialty || null,
384
+ input.location || null,
385
+ input.scheduled_at,
386
+ status,
387
+ input.notes || null,
388
+ input.follow_up_date || null,
389
+ metadata
390
+ );
391
+
392
+ return getAppointment(id)!;
393
+ }
394
+
395
+ export function getAppointment(id: string): Appointment | null {
396
+ const db = getDatabase();
397
+ const row = db.prepare("SELECT * FROM appointments WHERE id = ?").get(id) as AppointmentRow | null;
398
+ return row ? rowToAppointment(row) : null;
399
+ }
400
+
401
+ export interface ListAppointmentsOptions {
402
+ status?: string;
403
+ provider?: string;
404
+ limit?: number;
405
+ offset?: number;
406
+ }
407
+
408
+ export function listAppointments(options: ListAppointmentsOptions = {}): Appointment[] {
409
+ const db = getDatabase();
410
+ const conditions: string[] = [];
411
+ const params: unknown[] = [];
412
+
413
+ if (options.status) {
414
+ conditions.push("status = ?");
415
+ params.push(options.status);
416
+ }
417
+ if (options.provider) {
418
+ conditions.push("provider LIKE ?");
419
+ params.push(`%${options.provider}%`);
420
+ }
421
+
422
+ let sql = "SELECT * FROM appointments";
423
+ if (conditions.length > 0) {
424
+ sql += " WHERE " + conditions.join(" AND ");
425
+ }
426
+ sql += " ORDER BY scheduled_at DESC";
427
+
428
+ if (options.limit) {
429
+ sql += " LIMIT ?";
430
+ params.push(options.limit);
431
+ }
432
+ if (options.offset) {
433
+ sql += " OFFSET ?";
434
+ params.push(options.offset);
435
+ }
436
+
437
+ const rows = db.prepare(sql).all(...params) as AppointmentRow[];
438
+ return rows.map(rowToAppointment);
439
+ }
440
+
441
+ export interface UpdateAppointmentInput {
442
+ provider?: string;
443
+ specialty?: string;
444
+ location?: string;
445
+ scheduled_at?: string;
446
+ status?: Appointment["status"];
447
+ notes?: string;
448
+ follow_up_date?: string;
449
+ metadata?: Record<string, unknown>;
450
+ }
451
+
452
+ export function updateAppointment(id: string, input: UpdateAppointmentInput): Appointment | null {
453
+ const db = getDatabase();
454
+ const existing = getAppointment(id);
455
+ if (!existing) return null;
456
+
457
+ const sets: string[] = [];
458
+ const params: unknown[] = [];
459
+
460
+ if (input.provider !== undefined) {
461
+ sets.push("provider = ?");
462
+ params.push(input.provider);
463
+ }
464
+ if (input.specialty !== undefined) {
465
+ sets.push("specialty = ?");
466
+ params.push(input.specialty);
467
+ }
468
+ if (input.location !== undefined) {
469
+ sets.push("location = ?");
470
+ params.push(input.location);
471
+ }
472
+ if (input.scheduled_at !== undefined) {
473
+ sets.push("scheduled_at = ?");
474
+ params.push(input.scheduled_at);
475
+ }
476
+ if (input.status !== undefined) {
477
+ sets.push("status = ?");
478
+ params.push(input.status);
479
+ }
480
+ if (input.notes !== undefined) {
481
+ sets.push("notes = ?");
482
+ params.push(input.notes);
483
+ }
484
+ if (input.follow_up_date !== undefined) {
485
+ sets.push("follow_up_date = ?");
486
+ params.push(input.follow_up_date);
487
+ }
488
+ if (input.metadata !== undefined) {
489
+ sets.push("metadata = ?");
490
+ params.push(JSON.stringify(input.metadata));
491
+ }
492
+
493
+ if (sets.length === 0) return existing;
494
+
495
+ params.push(id);
496
+ db.prepare(`UPDATE appointments SET ${sets.join(", ")} WHERE id = ?`).run(...params);
497
+
498
+ return getAppointment(id);
499
+ }
500
+
501
+ export function completeAppointment(id: string): Appointment | null {
502
+ return updateAppointment(id, { status: "completed" });
503
+ }
504
+
505
+ export function cancelAppointment(id: string): Appointment | null {
506
+ return updateAppointment(id, { status: "cancelled" });
507
+ }
508
+
509
+ export function deleteAppointment(id: string): boolean {
510
+ const db = getDatabase();
511
+ const result = db.prepare("DELETE FROM appointments WHERE id = ?").run(id);
512
+ return result.changes > 0;
513
+ }
514
+
515
+ export function getUpcomingAppointments(days: number): Appointment[] {
516
+ const db = getDatabase();
517
+ const rows = db
518
+ .prepare(
519
+ `SELECT * FROM appointments
520
+ WHERE status = 'scheduled'
521
+ AND scheduled_at >= datetime('now')
522
+ AND scheduled_at <= datetime('now', '+' || ? || ' days')
523
+ ORDER BY scheduled_at ASC`
524
+ )
525
+ .all(days) as AppointmentRow[];
526
+ return rows.map(rowToAppointment);
527
+ }
528
+
529
+ // ============================================================
530
+ // Fitness Logs
531
+ // ============================================================
532
+
533
+ export interface FitnessLog {
534
+ id: string;
535
+ activity: string;
536
+ duration_min: number | null;
537
+ calories_burned: number | null;
538
+ distance: number | null;
539
+ notes: string | null;
540
+ logged_at: string;
541
+ created_at: string;
542
+ }
543
+
544
+ export interface CreateFitnessLogInput {
545
+ activity: string;
546
+ duration_min?: number;
547
+ calories_burned?: number;
548
+ distance?: number;
549
+ notes?: string;
550
+ logged_at?: string;
551
+ }
552
+
553
+ export function createFitnessLog(input: CreateFitnessLogInput): FitnessLog {
554
+ const db = getDatabase();
555
+ const id = crypto.randomUUID();
556
+ const logged_at = input.logged_at || new Date().toISOString();
557
+
558
+ db.prepare(
559
+ `INSERT INTO fitness_logs (id, activity, duration_min, calories_burned, distance, notes, logged_at)
560
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
561
+ ).run(
562
+ id,
563
+ input.activity,
564
+ input.duration_min ?? null,
565
+ input.calories_burned ?? null,
566
+ input.distance ?? null,
567
+ input.notes || null,
568
+ logged_at
569
+ );
570
+
571
+ return getFitnessLog(id)!;
572
+ }
573
+
574
+ export function getFitnessLog(id: string): FitnessLog | null {
575
+ const db = getDatabase();
576
+ const row = db.prepare("SELECT * FROM fitness_logs WHERE id = ?").get(id) as FitnessLog | null;
577
+ return row;
578
+ }
579
+
580
+ export interface ListFitnessLogsOptions {
581
+ activity?: string;
582
+ limit?: number;
583
+ offset?: number;
584
+ }
585
+
586
+ export function listFitnessLogs(options: ListFitnessLogsOptions = {}): FitnessLog[] {
587
+ const db = getDatabase();
588
+ const conditions: string[] = [];
589
+ const params: unknown[] = [];
590
+
591
+ if (options.activity) {
592
+ conditions.push("activity LIKE ?");
593
+ params.push(`%${options.activity}%`);
594
+ }
595
+
596
+ let sql = "SELECT * FROM fitness_logs";
597
+ if (conditions.length > 0) {
598
+ sql += " WHERE " + conditions.join(" AND ");
599
+ }
600
+ sql += " ORDER BY logged_at DESC";
601
+
602
+ if (options.limit) {
603
+ sql += " LIMIT ?";
604
+ params.push(options.limit);
605
+ }
606
+ if (options.offset) {
607
+ sql += " OFFSET ?";
608
+ params.push(options.offset);
609
+ }
610
+
611
+ const rows = db.prepare(sql).all(...params) as FitnessLog[];
612
+ return rows;
613
+ }
614
+
615
+ export function deleteFitnessLog(id: string): boolean {
616
+ const db = getDatabase();
617
+ const result = db.prepare("DELETE FROM fitness_logs WHERE id = ?").run(id);
618
+ return result.changes > 0;
619
+ }
620
+
621
+ export interface FitnessStats {
622
+ total_sessions: number;
623
+ total_minutes: number;
624
+ total_calories: number;
625
+ avg_duration: number;
626
+ }
627
+
628
+ export function getFitnessStats(days: number): FitnessStats {
629
+ const db = getDatabase();
630
+ const row = db
631
+ .prepare(
632
+ `SELECT
633
+ COUNT(*) as total_sessions,
634
+ COALESCE(SUM(duration_min), 0) as total_minutes,
635
+ COALESCE(SUM(calories_burned), 0) as total_calories,
636
+ COALESCE(AVG(duration_min), 0) as avg_duration
637
+ FROM fitness_logs
638
+ WHERE logged_at >= datetime('now', '-' || ? || ' days')`
639
+ )
640
+ .get(days) as FitnessStats;
641
+ return {
642
+ total_sessions: row.total_sessions,
643
+ total_minutes: row.total_minutes,
644
+ total_calories: row.total_calories,
645
+ avg_duration: Math.round(row.avg_duration * 100) / 100,
646
+ };
647
+ }
648
+
649
+ // ============================================================
650
+ // Health Summary
651
+ // ============================================================
652
+
653
+ export interface HealthSummary {
654
+ metrics: { total: number; types: string[] };
655
+ medications: { total: number; active: number };
656
+ appointments: { total: number; upcoming: number; completed: number };
657
+ fitness: { total_sessions: number; recent_stats: FitnessStats };
658
+ }
659
+
660
+ export function getHealthSummary(): HealthSummary {
661
+ const db = getDatabase();
662
+
663
+ // Metrics summary
664
+ const metricTotal = (
665
+ db.prepare("SELECT COUNT(*) as count FROM metrics").get() as { count: number }
666
+ ).count;
667
+ const metricTypes = (
668
+ db.prepare("SELECT DISTINCT type FROM metrics ORDER BY type").all() as { type: string }[]
669
+ ).map((r) => r.type);
670
+
671
+ // Medications summary
672
+ const medTotal = (
673
+ db.prepare("SELECT COUNT(*) as count FROM medications").get() as { count: number }
674
+ ).count;
675
+ const medActive = (
676
+ db.prepare("SELECT COUNT(*) as count FROM medications WHERE active = 1").get() as {
677
+ count: number;
678
+ }
679
+ ).count;
680
+
681
+ // Appointments summary
682
+ const apptTotal = (
683
+ db.prepare("SELECT COUNT(*) as count FROM appointments").get() as { count: number }
684
+ ).count;
685
+ const apptUpcoming = (
686
+ db.prepare(
687
+ "SELECT COUNT(*) as count FROM appointments WHERE status = 'scheduled' AND scheduled_at >= datetime('now')"
688
+ ).get() as { count: number }
689
+ ).count;
690
+ const apptCompleted = (
691
+ db.prepare(
692
+ "SELECT COUNT(*) as count FROM appointments WHERE status = 'completed'"
693
+ ).get() as { count: number }
694
+ ).count;
695
+
696
+ // Fitness summary
697
+ const fitnessTotal = (
698
+ db.prepare("SELECT COUNT(*) as count FROM fitness_logs").get() as { count: number }
699
+ ).count;
700
+ const recentStats = getFitnessStats(30);
701
+
702
+ return {
703
+ metrics: { total: metricTotal, types: metricTypes },
704
+ medications: { total: medTotal, active: medActive },
705
+ appointments: { total: apptTotal, upcoming: apptUpcoming, completed: apptCompleted },
706
+ fitness: { total_sessions: fitnessTotal, recent_stats: recentStats },
707
+ };
708
+ }