@nativesquare/soma 0.16.0 → 0.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/garmin.d.ts.map +1 -1
- package/dist/client/garmin.js +8 -0
- package/dist/client/garmin.js.map +1 -1
- package/dist/client/healthkit.d.ts +70 -24
- package/dist/client/healthkit.d.ts.map +1 -1
- package/dist/client/healthkit.js +103 -41
- package/dist/client/healthkit.js.map +1 -1
- package/package.json +1 -1
- package/src/client/garmin.ts +13 -0
- package/src/client/healthkit.ts +864 -791
package/src/client/healthkit.ts
CHANGED
|
@@ -1,791 +1,864 @@
|
|
|
1
|
-
import type { SomaComponent } from "./index.js";
|
|
2
|
-
import type { MutationCtx, SomaError } from "./types.js";
|
|
3
|
-
import type {
|
|
4
|
-
HKWorkout,
|
|
5
|
-
HKCategorySample,
|
|
6
|
-
HKQuantitySample,
|
|
7
|
-
HKActivitySummary,
|
|
8
|
-
HKCharacteristics,
|
|
9
|
-
} from "../component/healthkit/types.js";
|
|
10
|
-
import { transformWorkout } from "../component/healthkit/transform/activity.js";
|
|
11
|
-
import { transformSleep } from "../component/healthkit/transform/sleep.js";
|
|
12
|
-
import { transformBody } from "../component/healthkit/transform/body.js";
|
|
13
|
-
import {
|
|
14
|
-
transformDaily,
|
|
15
|
-
transformDailyFromSummary,
|
|
16
|
-
} from "../component/healthkit/transform/daily.js";
|
|
17
|
-
import { transformNutrition } from "../component/healthkit/transform/nutrition.js";
|
|
18
|
-
import { transformMenstruation } from "../component/healthkit/transform/menstruation.js";
|
|
19
|
-
import { transformAthlete } from "../component/healthkit/transform/athlete.js";
|
|
20
|
-
|
|
21
|
-
const PROVIDER = "APPLE";
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* app
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* });
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* @param
|
|
111
|
-
* @param args.
|
|
112
|
-
*/
|
|
113
|
-
async
|
|
114
|
-
ctx: MutationCtx,
|
|
115
|
-
args: { userId: string
|
|
116
|
-
): Promise<
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
},
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
await this.
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
await this.
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
)
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
return { data:
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
return
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
private async
|
|
617
|
-
ctx: MutationCtx,
|
|
618
|
-
connectionId: string,
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
})
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1
|
+
import type { SomaComponent } from "./index.js";
|
|
2
|
+
import type { MutationCtx, SomaError, SomaResult } from "./types.js";
|
|
3
|
+
import type {
|
|
4
|
+
HKWorkout,
|
|
5
|
+
HKCategorySample,
|
|
6
|
+
HKQuantitySample,
|
|
7
|
+
HKActivitySummary,
|
|
8
|
+
HKCharacteristics,
|
|
9
|
+
} from "../component/healthkit/types.js";
|
|
10
|
+
import { transformWorkout } from "../component/healthkit/transform/activity.js";
|
|
11
|
+
import { transformSleep } from "../component/healthkit/transform/sleep.js";
|
|
12
|
+
import { transformBody } from "../component/healthkit/transform/body.js";
|
|
13
|
+
import {
|
|
14
|
+
transformDaily,
|
|
15
|
+
transformDailyFromSummary,
|
|
16
|
+
} from "../component/healthkit/transform/daily.js";
|
|
17
|
+
import { transformNutrition } from "../component/healthkit/transform/nutrition.js";
|
|
18
|
+
import { transformMenstruation } from "../component/healthkit/transform/menstruation.js";
|
|
19
|
+
import { transformAthlete } from "../component/healthkit/transform/athlete.js";
|
|
20
|
+
|
|
21
|
+
const PROVIDER = "APPLE";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Client class for Apple HealthKit integration with Soma.
|
|
25
|
+
*
|
|
26
|
+
* Unlike {@link import("./strava.js").SomaStrava | SomaStrava} and
|
|
27
|
+
* {@link import("./garmin.js").SomaGarmin | SomaGarmin}, HealthKit is an
|
|
28
|
+
* on-device provider — data is queried locally on iOS, not fetched from a
|
|
29
|
+
* cloud API. This class wraps the transform + ingest pipeline so the host
|
|
30
|
+
* app gets the same ergonomic interface as cloud providers.
|
|
31
|
+
*
|
|
32
|
+
* Because HealthKit permissions are managed in iOS Settings, the server
|
|
33
|
+
* cannot independently verify whether the user has granted or revoked
|
|
34
|
+
* access. The `connect()` / `disconnect()` methods therefore represent
|
|
35
|
+
* assertions by the host app about the state it has observed on-device:
|
|
36
|
+
*
|
|
37
|
+
* - Call {@link SomaHealthKit.connect | connect} once the React Native
|
|
38
|
+
* HealthKit library confirms permissions were granted.
|
|
39
|
+
* - Call {@link SomaHealthKit.disconnect | disconnect} when the user
|
|
40
|
+
* turns HealthKit off in your app's settings, or when you otherwise
|
|
41
|
+
* detect that syncs are no longer returning data.
|
|
42
|
+
*
|
|
43
|
+
* Sync methods refuse to run unless an active connection exists.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Once, after the RN library confirms permissions:
|
|
48
|
+
* await soma.healthkit.connect(ctx, { userId: "user_123" });
|
|
49
|
+
*
|
|
50
|
+
* // Sync workouts received from the React Native client:
|
|
51
|
+
* const result = await soma.healthkit.syncActivities(ctx, {
|
|
52
|
+
* userId: "user_123",
|
|
53
|
+
* workouts: hkWorkouts,
|
|
54
|
+
* });
|
|
55
|
+
* // result: { data: { activities: 5 }, errors: [] }
|
|
56
|
+
*
|
|
57
|
+
* // Or sync everything at once:
|
|
58
|
+
* await soma.healthkit.syncAll(ctx, {
|
|
59
|
+
* userId: "user_123",
|
|
60
|
+
* workouts: hkWorkouts,
|
|
61
|
+
* sleepSessions: [nightSamples],
|
|
62
|
+
* bodySamples: hkBodySamples,
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // When the user disables HealthKit in your app:
|
|
66
|
+
* await soma.healthkit.disconnect(ctx, { userId: "user_123" });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export class SomaHealthKit {
|
|
70
|
+
constructor(private component: SomaComponent) { }
|
|
71
|
+
|
|
72
|
+
// ─── Connect / Disconnect ──────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Assert that HealthKit is connected for this user.
|
|
76
|
+
*
|
|
77
|
+
* Call this once after the React Native HealthKit library confirms
|
|
78
|
+
* permissions were granted. Creates the APPLE connection if missing,
|
|
79
|
+
* or re-activates it if previously disconnected. Idempotent.
|
|
80
|
+
*
|
|
81
|
+
* Because iOS permission grants happen on-device, the server cannot
|
|
82
|
+
* verify them independently — this method records the host app's
|
|
83
|
+
* assertion that the user has authorized HealthKit access.
|
|
84
|
+
*
|
|
85
|
+
* @param ctx - Mutation context from the host app
|
|
86
|
+
* @param args.userId - The host app's user identifier
|
|
87
|
+
* @returns The connection document ID
|
|
88
|
+
*/
|
|
89
|
+
async connect(
|
|
90
|
+
ctx: MutationCtx,
|
|
91
|
+
args: { userId: string },
|
|
92
|
+
): Promise<string> {
|
|
93
|
+
return (await ctx.runMutation(this.component.public.connect, {
|
|
94
|
+
userId: args.userId,
|
|
95
|
+
provider: PROVIDER,
|
|
96
|
+
})) as string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Mark the HealthKit connection inactive for this user.
|
|
101
|
+
*
|
|
102
|
+
* Call this when the user disables HealthKit in your app's settings, or
|
|
103
|
+
* when you detect that sync calls are no longer returning data (suggesting
|
|
104
|
+
* the user revoked permissions in iOS Settings). Subsequent sync methods
|
|
105
|
+
* will throw until {@link SomaHealthKit.connect | connect} is called again.
|
|
106
|
+
*
|
|
107
|
+
* Does not delete the connection row or any previously synced data —
|
|
108
|
+
* re-connecting later preserves history.
|
|
109
|
+
*
|
|
110
|
+
* @param ctx - Mutation context from the host app
|
|
111
|
+
* @param args.userId - The host app's user identifier
|
|
112
|
+
*/
|
|
113
|
+
async disconnect(
|
|
114
|
+
ctx: MutationCtx,
|
|
115
|
+
args: { userId: string },
|
|
116
|
+
): Promise<null> {
|
|
117
|
+
return await ctx.runMutation(this.component.public.disconnect, {
|
|
118
|
+
userId: args.userId,
|
|
119
|
+
provider: PROVIDER,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ─── Per-Type Sync Methods ─────────────────────────────────────────────
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Sync workout activities from HealthKit.
|
|
127
|
+
*
|
|
128
|
+
* Transforms each `HKWorkout` into the Soma Activity schema and ingests it
|
|
129
|
+
* with automatic deduplication by `workout.uuid`.
|
|
130
|
+
*
|
|
131
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
132
|
+
* @param args.userId - The host app's user identifier
|
|
133
|
+
* @param args.workouts - Array of HKWorkout objects from HealthKit
|
|
134
|
+
*/
|
|
135
|
+
async syncActivities(
|
|
136
|
+
ctx: MutationCtx,
|
|
137
|
+
args: { userId: string; workouts: HKWorkout[] },
|
|
138
|
+
): Promise<SomaResult<{ activities: number }>> {
|
|
139
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
140
|
+
const errors: SomaError[] = [];
|
|
141
|
+
let count = 0;
|
|
142
|
+
|
|
143
|
+
for (const workout of args.workouts) {
|
|
144
|
+
try {
|
|
145
|
+
const data = transformWorkout(workout);
|
|
146
|
+
await ctx.runMutation(this.component.public.ingestActivity, {
|
|
147
|
+
connectionId,
|
|
148
|
+
userId: args.userId,
|
|
149
|
+
...data,
|
|
150
|
+
});
|
|
151
|
+
count++;
|
|
152
|
+
} catch (err) {
|
|
153
|
+
errors.push({
|
|
154
|
+
type: "activity",
|
|
155
|
+
id: workout.uuid,
|
|
156
|
+
message: err instanceof Error ? err.message : String(err),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
162
|
+
return { data: { activities: count }, errors };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Sync sleep sessions from HealthKit.
|
|
167
|
+
*
|
|
168
|
+
* Each inner array represents one sleep session (all `HKCategorySample`
|
|
169
|
+
* stage records for a single night). Each session is aggregated into a
|
|
170
|
+
* single Soma Sleep document.
|
|
171
|
+
*
|
|
172
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
173
|
+
* @param args.userId - The host app's user identifier
|
|
174
|
+
* @param args.sessions - Array of sessions, each an array of sleep-stage samples
|
|
175
|
+
*/
|
|
176
|
+
async syncSleep(
|
|
177
|
+
ctx: MutationCtx,
|
|
178
|
+
args: { userId: string; sessions: HKCategorySample[][] },
|
|
179
|
+
): Promise<SomaResult<{ sleep: number }>> {
|
|
180
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
181
|
+
const errors: SomaError[] = [];
|
|
182
|
+
let count = 0;
|
|
183
|
+
|
|
184
|
+
for (const session of args.sessions) {
|
|
185
|
+
const sessionId = session[0]?.uuid ?? "unknown";
|
|
186
|
+
try {
|
|
187
|
+
const data = transformSleep(session);
|
|
188
|
+
await ctx.runMutation(this.component.public.ingestSleep, {
|
|
189
|
+
connectionId,
|
|
190
|
+
userId: args.userId,
|
|
191
|
+
...data,
|
|
192
|
+
});
|
|
193
|
+
count++;
|
|
194
|
+
} catch (err) {
|
|
195
|
+
errors.push({
|
|
196
|
+
type: "sleep",
|
|
197
|
+
id: sessionId,
|
|
198
|
+
message: err instanceof Error ? err.message : String(err),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
204
|
+
return { data: { sleep: count }, errors };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Sync body metrics from HealthKit.
|
|
209
|
+
*
|
|
210
|
+
* Accepts a mixed array of body-related quantity samples (heart rate, HRV,
|
|
211
|
+
* blood pressure, SpO2, weight, etc.) for a single time window and produces
|
|
212
|
+
* one Soma Body document.
|
|
213
|
+
*
|
|
214
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
215
|
+
* @param args.userId - The host app's user identifier
|
|
216
|
+
* @param args.samples - Array of HKQuantitySample for the desired time range
|
|
217
|
+
* @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
|
|
218
|
+
*/
|
|
219
|
+
async syncBody(
|
|
220
|
+
ctx: MutationCtx,
|
|
221
|
+
args: {
|
|
222
|
+
userId: string;
|
|
223
|
+
samples: HKQuantitySample[];
|
|
224
|
+
timeRange?: { start_time: string; end_time: string };
|
|
225
|
+
},
|
|
226
|
+
): Promise<SomaResult<{ body: number }>> {
|
|
227
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
228
|
+
const errors: SomaError[] = [];
|
|
229
|
+
let count = 0;
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
const data = transformBody(args.samples, args.timeRange);
|
|
233
|
+
await ctx.runMutation(this.component.public.ingestBody, {
|
|
234
|
+
connectionId,
|
|
235
|
+
userId: args.userId,
|
|
236
|
+
...data,
|
|
237
|
+
});
|
|
238
|
+
count++;
|
|
239
|
+
} catch (err) {
|
|
240
|
+
errors.push({
|
|
241
|
+
type: "body",
|
|
242
|
+
id: "transform",
|
|
243
|
+
message: err instanceof Error ? err.message : String(err),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
248
|
+
return { data: { body: count }, errors };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Sync daily activity data from HealthKit quantity samples.
|
|
253
|
+
*
|
|
254
|
+
* Accepts samples for a single day (steps, distance, energy, exercise time,
|
|
255
|
+
* heart rate, etc.) and produces one Soma Daily document.
|
|
256
|
+
*
|
|
257
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
258
|
+
* @param args.userId - The host app's user identifier
|
|
259
|
+
* @param args.samples - Array of HKQuantitySample for the desired day
|
|
260
|
+
* @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
|
|
261
|
+
*/
|
|
262
|
+
async syncDaily(
|
|
263
|
+
ctx: MutationCtx,
|
|
264
|
+
args: {
|
|
265
|
+
userId: string;
|
|
266
|
+
samples: HKQuantitySample[];
|
|
267
|
+
timeRange?: { start_time: string; end_time: string };
|
|
268
|
+
},
|
|
269
|
+
): Promise<SomaResult<{ daily: number }>> {
|
|
270
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
271
|
+
const errors: SomaError[] = [];
|
|
272
|
+
let count = 0;
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
const data = transformDaily(args.samples, args.timeRange);
|
|
276
|
+
await ctx.runMutation(this.component.public.ingestDaily, {
|
|
277
|
+
connectionId,
|
|
278
|
+
userId: args.userId,
|
|
279
|
+
...data,
|
|
280
|
+
});
|
|
281
|
+
count++;
|
|
282
|
+
} catch (err) {
|
|
283
|
+
errors.push({
|
|
284
|
+
type: "daily",
|
|
285
|
+
id: "transform",
|
|
286
|
+
message: err instanceof Error ? err.message : String(err),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
291
|
+
return { data: { daily: count }, errors };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Sync daily activity data from HealthKit activity ring summaries.
|
|
296
|
+
*
|
|
297
|
+
* Each `HKActivitySummary` represents one day's activity rings (Move,
|
|
298
|
+
* Exercise, Stand). Each summary produces one Soma Daily document.
|
|
299
|
+
*
|
|
300
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
301
|
+
* @param args.userId - The host app's user identifier
|
|
302
|
+
* @param args.summaries - Array of HKActivitySummary from HealthKit
|
|
303
|
+
*/
|
|
304
|
+
async syncDailyFromSummary(
|
|
305
|
+
ctx: MutationCtx,
|
|
306
|
+
args: { userId: string; summaries: HKActivitySummary[] },
|
|
307
|
+
): Promise<SomaResult<{ daily: number }>> {
|
|
308
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
309
|
+
const errors: SomaError[] = [];
|
|
310
|
+
let count = 0;
|
|
311
|
+
|
|
312
|
+
for (const summary of args.summaries) {
|
|
313
|
+
const { year, month, day } = summary.dateComponents;
|
|
314
|
+
const summaryId = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
315
|
+
try {
|
|
316
|
+
const data = transformDailyFromSummary(summary);
|
|
317
|
+
await ctx.runMutation(this.component.public.ingestDaily, {
|
|
318
|
+
connectionId,
|
|
319
|
+
userId: args.userId,
|
|
320
|
+
...data,
|
|
321
|
+
});
|
|
322
|
+
count++;
|
|
323
|
+
} catch (err) {
|
|
324
|
+
errors.push({
|
|
325
|
+
type: "daily",
|
|
326
|
+
id: summaryId,
|
|
327
|
+
message: err instanceof Error ? err.message : String(err),
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
333
|
+
return { data: { daily: count }, errors };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Sync nutrition data from HealthKit.
|
|
338
|
+
*
|
|
339
|
+
* Accepts dietary quantity samples for a single time window and produces
|
|
340
|
+
* one Soma Nutrition document with macros and micros.
|
|
341
|
+
*
|
|
342
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
343
|
+
* @param args.userId - The host app's user identifier
|
|
344
|
+
* @param args.samples - Array of HKQuantitySample with dietary type identifiers
|
|
345
|
+
* @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
|
|
346
|
+
*/
|
|
347
|
+
async syncNutrition(
|
|
348
|
+
ctx: MutationCtx,
|
|
349
|
+
args: {
|
|
350
|
+
userId: string;
|
|
351
|
+
samples: HKQuantitySample[];
|
|
352
|
+
timeRange?: { start_time: string; end_time: string };
|
|
353
|
+
},
|
|
354
|
+
): Promise<SomaResult<{ nutrition: number }>> {
|
|
355
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
356
|
+
const errors: SomaError[] = [];
|
|
357
|
+
let count = 0;
|
|
358
|
+
|
|
359
|
+
try {
|
|
360
|
+
const data = transformNutrition(args.samples, args.timeRange);
|
|
361
|
+
await ctx.runMutation(this.component.public.ingestNutrition, {
|
|
362
|
+
connectionId,
|
|
363
|
+
userId: args.userId,
|
|
364
|
+
...data,
|
|
365
|
+
});
|
|
366
|
+
count++;
|
|
367
|
+
} catch (err) {
|
|
368
|
+
errors.push({
|
|
369
|
+
type: "nutrition" as SomaError["type"],
|
|
370
|
+
id: "transform",
|
|
371
|
+
message: err instanceof Error ? err.message : String(err),
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
376
|
+
return { data: { nutrition: count }, errors };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Sync menstruation data from HealthKit.
|
|
381
|
+
*
|
|
382
|
+
* Accepts menstrual flow category samples for a single time window and
|
|
383
|
+
* produces one Soma Menstruation document.
|
|
384
|
+
*
|
|
385
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
386
|
+
* @param args.userId - The host app's user identifier
|
|
387
|
+
* @param args.samples - Array of HKCategorySample with menstrual flow values
|
|
388
|
+
* @param args.timeRange - Optional explicit time range; auto-detected from samples if omitted
|
|
389
|
+
*/
|
|
390
|
+
async syncMenstruation(
|
|
391
|
+
ctx: MutationCtx,
|
|
392
|
+
args: {
|
|
393
|
+
userId: string;
|
|
394
|
+
samples: HKCategorySample[];
|
|
395
|
+
timeRange?: { start_time: string; end_time: string };
|
|
396
|
+
},
|
|
397
|
+
): Promise<SomaResult<{ menstruation: number }>> {
|
|
398
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
399
|
+
const errors: SomaError[] = [];
|
|
400
|
+
let count = 0;
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
const data = transformMenstruation(args.samples, args.timeRange);
|
|
404
|
+
await ctx.runMutation(this.component.public.ingestMenstruation, {
|
|
405
|
+
connectionId,
|
|
406
|
+
userId: args.userId,
|
|
407
|
+
...data,
|
|
408
|
+
});
|
|
409
|
+
count++;
|
|
410
|
+
} catch (err) {
|
|
411
|
+
errors.push({
|
|
412
|
+
type: "menstruation",
|
|
413
|
+
id: "transform",
|
|
414
|
+
message: err instanceof Error ? err.message : String(err),
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
419
|
+
return { data: { menstruation: count }, errors };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Sync athlete profile from HealthKit.
|
|
424
|
+
*
|
|
425
|
+
* HealthKit exposes limited profile data (biological sex, date of birth).
|
|
426
|
+
* Produces one Soma Athlete document per connection.
|
|
427
|
+
*
|
|
428
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
429
|
+
* @param args.userId - The host app's user identifier
|
|
430
|
+
* @param args.characteristics - The HKCharacteristics from HealthKit
|
|
431
|
+
*/
|
|
432
|
+
async syncAthlete(
|
|
433
|
+
ctx: MutationCtx,
|
|
434
|
+
args: { userId: string; characteristics: HKCharacteristics },
|
|
435
|
+
): Promise<SomaResult<{ athletes: number }>> {
|
|
436
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
437
|
+
const errors: SomaError[] = [];
|
|
438
|
+
let count = 0;
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
const data = transformAthlete(args.characteristics);
|
|
442
|
+
await ctx.runMutation(this.component.public.ingestAthlete, {
|
|
443
|
+
connectionId,
|
|
444
|
+
userId: args.userId,
|
|
445
|
+
...data,
|
|
446
|
+
});
|
|
447
|
+
count++;
|
|
448
|
+
} catch (err) {
|
|
449
|
+
errors.push({
|
|
450
|
+
type: "athlete",
|
|
451
|
+
id: "transform",
|
|
452
|
+
message: err instanceof Error ? err.message : String(err),
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
457
|
+
return { data: { athletes: count }, errors };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// ─── Orchestrator ────────────────────────────────────────────────────────
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Sync all provided HealthKit data types in a single call.
|
|
464
|
+
*
|
|
465
|
+
* Only data types with values provided are synced. Errors from individual
|
|
466
|
+
* data types are collected — one failing type does not block others.
|
|
467
|
+
*
|
|
468
|
+
* @param ctx - Mutation (or action) context from the host app
|
|
469
|
+
* @param args.userId - The host app's user identifier
|
|
470
|
+
* @param args.workouts - Optional array of HKWorkout objects
|
|
471
|
+
* @param args.sleepSessions - Optional array of sleep sessions (each an array of stage samples)
|
|
472
|
+
* @param args.bodySamples - Optional body-related quantity samples
|
|
473
|
+
* @param args.dailySamples - Optional daily activity quantity samples
|
|
474
|
+
* @param args.dailySummaries - Optional daily activity ring summaries
|
|
475
|
+
* @param args.nutritionSamples - Optional dietary quantity samples
|
|
476
|
+
* @param args.menstruationSamples - Optional menstrual flow category samples
|
|
477
|
+
* @param args.characteristics - Optional user characteristics
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```ts
|
|
481
|
+
* const result = await soma.healthkit.syncAll(ctx, {
|
|
482
|
+
* userId: "user_123",
|
|
483
|
+
* workouts: hkWorkouts,
|
|
484
|
+
* sleepSessions: [nightSamples],
|
|
485
|
+
* bodySamples: hkBodySamples,
|
|
486
|
+
* dailySummaries: hkSummaries,
|
|
487
|
+
* characteristics: hkCharacteristics,
|
|
488
|
+
* });
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
491
|
+
async syncAll(
|
|
492
|
+
ctx: MutationCtx,
|
|
493
|
+
args: {
|
|
494
|
+
userId: string;
|
|
495
|
+
workouts?: HKWorkout[];
|
|
496
|
+
sleepSessions?: HKCategorySample[][];
|
|
497
|
+
bodySamples?: HKQuantitySample[];
|
|
498
|
+
bodyTimeRange?: { start_time: string; end_time: string };
|
|
499
|
+
dailySamples?: HKQuantitySample[];
|
|
500
|
+
dailyTimeRange?: { start_time: string; end_time: string };
|
|
501
|
+
dailySummaries?: HKActivitySummary[];
|
|
502
|
+
nutritionSamples?: HKQuantitySample[];
|
|
503
|
+
nutritionTimeRange?: { start_time: string; end_time: string };
|
|
504
|
+
menstruationSamples?: HKCategorySample[];
|
|
505
|
+
menstruationTimeRange?: { start_time: string; end_time: string };
|
|
506
|
+
characteristics?: HKCharacteristics;
|
|
507
|
+
},
|
|
508
|
+
): Promise<SomaResult<Record<string, number>>> {
|
|
509
|
+
const connectionId = await this.requireActiveConnection(ctx, args.userId);
|
|
510
|
+
const allErrors: SomaError[] = [];
|
|
511
|
+
const counts: Record<string, number> = {};
|
|
512
|
+
|
|
513
|
+
const run = async <T extends Record<string, number>>(
|
|
514
|
+
fn: () => Promise<SomaResult<T>>,
|
|
515
|
+
fallbackType: SomaError["type"],
|
|
516
|
+
) => {
|
|
517
|
+
try {
|
|
518
|
+
const result = await fn();
|
|
519
|
+
Object.assign(counts, result.data);
|
|
520
|
+
allErrors.push(...result.errors);
|
|
521
|
+
} catch (err) {
|
|
522
|
+
allErrors.push({
|
|
523
|
+
type: fallbackType,
|
|
524
|
+
id: "sync",
|
|
525
|
+
message: err instanceof Error ? err.message : String(err),
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
if (args.workouts) {
|
|
531
|
+
await run(
|
|
532
|
+
() => this.syncActivitiesInternal(ctx, connectionId, args.userId, args.workouts!),
|
|
533
|
+
"activity",
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
if (args.sleepSessions) {
|
|
537
|
+
await run(
|
|
538
|
+
() => this.syncSleepInternal(ctx, connectionId, args.userId, args.sleepSessions!),
|
|
539
|
+
"sleep",
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
if (args.bodySamples) {
|
|
543
|
+
await run(
|
|
544
|
+
() => this.syncBodyInternal(ctx, connectionId, args.userId, args.bodySamples!, args.bodyTimeRange),
|
|
545
|
+
"body",
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
if (args.dailySamples) {
|
|
549
|
+
await run(
|
|
550
|
+
() => this.syncDailyInternal(ctx, connectionId, args.userId, args.dailySamples!, args.dailyTimeRange),
|
|
551
|
+
"daily",
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
if (args.dailySummaries) {
|
|
555
|
+
await run(
|
|
556
|
+
() => this.syncDailyFromSummaryInternal(ctx, connectionId, args.userId, args.dailySummaries!),
|
|
557
|
+
"daily",
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
if (args.nutritionSamples) {
|
|
561
|
+
await run(
|
|
562
|
+
() => this.syncNutritionInternal(ctx, connectionId, args.userId, args.nutritionSamples!, args.nutritionTimeRange),
|
|
563
|
+
"nutrition" as SomaError["type"],
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
if (args.menstruationSamples) {
|
|
567
|
+
await run(
|
|
568
|
+
() => this.syncMenstruationInternal(ctx, connectionId, args.userId, args.menstruationSamples!, args.menstruationTimeRange),
|
|
569
|
+
"menstruation",
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
if (args.characteristics) {
|
|
573
|
+
await run(
|
|
574
|
+
() => this.syncAthleteInternal(ctx, connectionId, args.userId, args.characteristics!),
|
|
575
|
+
"athlete",
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Update lastDataUpdate once at the end
|
|
580
|
+
await this.updateLastDataUpdate(ctx, connectionId);
|
|
581
|
+
|
|
582
|
+
return { data: counts, errors: allErrors };
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// ─── Private Helpers ───────────��─────────────────────────────────────────
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Load the active APPLE connection for a user, or throw if missing/inactive.
|
|
589
|
+
*
|
|
590
|
+
* HealthKit permissions are managed on-device, so the connection state is
|
|
591
|
+
* an assertion made by the host app via {@link SomaHealthKit.connect} and
|
|
592
|
+
* {@link SomaHealthKit.disconnect}. Sync methods refuse to run without an
|
|
593
|
+
* active connection — the host app must explicitly reconnect to resume.
|
|
594
|
+
*/
|
|
595
|
+
private async requireActiveConnection(
|
|
596
|
+
ctx: MutationCtx,
|
|
597
|
+
userId: string,
|
|
598
|
+
): Promise<string> {
|
|
599
|
+
const connection = await ctx.runQuery(
|
|
600
|
+
this.component.public.getConnectionByProvider,
|
|
601
|
+
{ userId, provider: PROVIDER },
|
|
602
|
+
);
|
|
603
|
+
if (!connection) {
|
|
604
|
+
throw new Error(
|
|
605
|
+
`No HealthKit connection for user "${userId}". Call soma.healthkit.connect(ctx, { userId }) after the React Native HealthKit library confirms permissions.`,
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
if (connection.active === false) {
|
|
609
|
+
throw new Error(
|
|
610
|
+
`HealthKit connection for user "${userId}" is disconnected. Call soma.healthkit.connect(ctx, { userId }) to re-activate.`,
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
return connection._id;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
private async updateLastDataUpdate(
|
|
617
|
+
ctx: MutationCtx,
|
|
618
|
+
connectionId: string,
|
|
619
|
+
): Promise<void> {
|
|
620
|
+
await ctx.runMutation(this.component.public.updateConnection, {
|
|
621
|
+
connectionId,
|
|
622
|
+
lastDataUpdate: new Date().toISOString(),
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// ─── Internal sync methods (skip connection resolution + lastDataUpdate) ─
|
|
627
|
+
|
|
628
|
+
private async syncActivitiesInternal(
|
|
629
|
+
ctx: MutationCtx,
|
|
630
|
+
connectionId: string,
|
|
631
|
+
userId: string,
|
|
632
|
+
workouts: HKWorkout[],
|
|
633
|
+
): Promise<SomaResult<{ activities: number }>> {
|
|
634
|
+
const errors: SomaError[] = [];
|
|
635
|
+
let count = 0;
|
|
636
|
+
|
|
637
|
+
for (const workout of workouts) {
|
|
638
|
+
try {
|
|
639
|
+
const data = transformWorkout(workout);
|
|
640
|
+
await ctx.runMutation(this.component.public.ingestActivity, {
|
|
641
|
+
connectionId,
|
|
642
|
+
userId,
|
|
643
|
+
...data,
|
|
644
|
+
});
|
|
645
|
+
count++;
|
|
646
|
+
} catch (err) {
|
|
647
|
+
errors.push({
|
|
648
|
+
type: "activity",
|
|
649
|
+
id: workout.uuid,
|
|
650
|
+
message: err instanceof Error ? err.message : String(err),
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
return { data: { activities: count }, errors };
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
private async syncSleepInternal(
|
|
659
|
+
ctx: MutationCtx,
|
|
660
|
+
connectionId: string,
|
|
661
|
+
userId: string,
|
|
662
|
+
sessions: HKCategorySample[][],
|
|
663
|
+
): Promise<SomaResult<{ sleep: number }>> {
|
|
664
|
+
const errors: SomaError[] = [];
|
|
665
|
+
let count = 0;
|
|
666
|
+
|
|
667
|
+
for (const session of sessions) {
|
|
668
|
+
const sessionId = session[0]?.uuid ?? "unknown";
|
|
669
|
+
try {
|
|
670
|
+
const data = transformSleep(session);
|
|
671
|
+
await ctx.runMutation(this.component.public.ingestSleep, {
|
|
672
|
+
connectionId,
|
|
673
|
+
userId,
|
|
674
|
+
...data,
|
|
675
|
+
});
|
|
676
|
+
count++;
|
|
677
|
+
} catch (err) {
|
|
678
|
+
errors.push({
|
|
679
|
+
type: "sleep",
|
|
680
|
+
id: sessionId,
|
|
681
|
+
message: err instanceof Error ? err.message : String(err),
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
return { data: { sleep: count }, errors };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
private async syncBodyInternal(
|
|
690
|
+
ctx: MutationCtx,
|
|
691
|
+
connectionId: string,
|
|
692
|
+
userId: string,
|
|
693
|
+
samples: HKQuantitySample[],
|
|
694
|
+
timeRange?: { start_time: string; end_time: string },
|
|
695
|
+
): Promise<SomaResult<{ body: number }>> {
|
|
696
|
+
const errors: SomaError[] = [];
|
|
697
|
+
let count = 0;
|
|
698
|
+
|
|
699
|
+
try {
|
|
700
|
+
const data = transformBody(samples, timeRange);
|
|
701
|
+
await ctx.runMutation(this.component.public.ingestBody, {
|
|
702
|
+
connectionId,
|
|
703
|
+
userId,
|
|
704
|
+
...data,
|
|
705
|
+
});
|
|
706
|
+
count++;
|
|
707
|
+
} catch (err) {
|
|
708
|
+
errors.push({
|
|
709
|
+
type: "body",
|
|
710
|
+
id: "transform",
|
|
711
|
+
message: err instanceof Error ? err.message : String(err),
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return { data: { body: count }, errors };
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
private async syncDailyInternal(
|
|
719
|
+
ctx: MutationCtx,
|
|
720
|
+
connectionId: string,
|
|
721
|
+
userId: string,
|
|
722
|
+
samples: HKQuantitySample[],
|
|
723
|
+
timeRange?: { start_time: string; end_time: string },
|
|
724
|
+
): Promise<SomaResult<{ daily: number }>> {
|
|
725
|
+
const errors: SomaError[] = [];
|
|
726
|
+
let count = 0;
|
|
727
|
+
|
|
728
|
+
try {
|
|
729
|
+
const data = transformDaily(samples, timeRange);
|
|
730
|
+
await ctx.runMutation(this.component.public.ingestDaily, {
|
|
731
|
+
connectionId,
|
|
732
|
+
userId,
|
|
733
|
+
...data,
|
|
734
|
+
});
|
|
735
|
+
count++;
|
|
736
|
+
} catch (err) {
|
|
737
|
+
errors.push({
|
|
738
|
+
type: "daily",
|
|
739
|
+
id: "transform",
|
|
740
|
+
message: err instanceof Error ? err.message : String(err),
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
return { data: { daily: count }, errors };
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
private async syncDailyFromSummaryInternal(
|
|
748
|
+
ctx: MutationCtx,
|
|
749
|
+
connectionId: string,
|
|
750
|
+
userId: string,
|
|
751
|
+
summaries: HKActivitySummary[],
|
|
752
|
+
): Promise<SomaResult<{ daily: number }>> {
|
|
753
|
+
const errors: SomaError[] = [];
|
|
754
|
+
let count = 0;
|
|
755
|
+
|
|
756
|
+
for (const summary of summaries) {
|
|
757
|
+
const { year, month, day } = summary.dateComponents;
|
|
758
|
+
const summaryId = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
759
|
+
try {
|
|
760
|
+
const data = transformDailyFromSummary(summary);
|
|
761
|
+
await ctx.runMutation(this.component.public.ingestDaily, {
|
|
762
|
+
connectionId,
|
|
763
|
+
userId,
|
|
764
|
+
...data,
|
|
765
|
+
});
|
|
766
|
+
count++;
|
|
767
|
+
} catch (err) {
|
|
768
|
+
errors.push({
|
|
769
|
+
type: "daily",
|
|
770
|
+
id: summaryId,
|
|
771
|
+
message: err instanceof Error ? err.message : String(err),
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return { data: { daily: count }, errors };
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
private async syncNutritionInternal(
|
|
780
|
+
ctx: MutationCtx,
|
|
781
|
+
connectionId: string,
|
|
782
|
+
userId: string,
|
|
783
|
+
samples: HKQuantitySample[],
|
|
784
|
+
timeRange?: { start_time: string; end_time: string },
|
|
785
|
+
): Promise<SomaResult<{ nutrition: number }>> {
|
|
786
|
+
const errors: SomaError[] = [];
|
|
787
|
+
let count = 0;
|
|
788
|
+
|
|
789
|
+
try {
|
|
790
|
+
const data = transformNutrition(samples, timeRange);
|
|
791
|
+
await ctx.runMutation(this.component.public.ingestNutrition, {
|
|
792
|
+
connectionId,
|
|
793
|
+
userId,
|
|
794
|
+
...data,
|
|
795
|
+
});
|
|
796
|
+
count++;
|
|
797
|
+
} catch (err) {
|
|
798
|
+
errors.push({
|
|
799
|
+
type: "nutrition" as SomaError["type"],
|
|
800
|
+
id: "transform",
|
|
801
|
+
message: err instanceof Error ? err.message : String(err),
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return { data: { nutrition: count }, errors };
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
private async syncMenstruationInternal(
|
|
809
|
+
ctx: MutationCtx,
|
|
810
|
+
connectionId: string,
|
|
811
|
+
userId: string,
|
|
812
|
+
samples: HKCategorySample[],
|
|
813
|
+
timeRange?: { start_time: string; end_time: string },
|
|
814
|
+
): Promise<SomaResult<{ menstruation: number }>> {
|
|
815
|
+
const errors: SomaError[] = [];
|
|
816
|
+
let count = 0;
|
|
817
|
+
|
|
818
|
+
try {
|
|
819
|
+
const data = transformMenstruation(samples, timeRange);
|
|
820
|
+
await ctx.runMutation(this.component.public.ingestMenstruation, {
|
|
821
|
+
connectionId,
|
|
822
|
+
userId,
|
|
823
|
+
...data,
|
|
824
|
+
});
|
|
825
|
+
count++;
|
|
826
|
+
} catch (err) {
|
|
827
|
+
errors.push({
|
|
828
|
+
type: "menstruation",
|
|
829
|
+
id: "transform",
|
|
830
|
+
message: err instanceof Error ? err.message : String(err),
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
return { data: { menstruation: count }, errors };
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
private async syncAthleteInternal(
|
|
838
|
+
ctx: MutationCtx,
|
|
839
|
+
connectionId: string,
|
|
840
|
+
userId: string,
|
|
841
|
+
characteristics: HKCharacteristics,
|
|
842
|
+
): Promise<SomaResult<{ athletes: number }>> {
|
|
843
|
+
const errors: SomaError[] = [];
|
|
844
|
+
let count = 0;
|
|
845
|
+
|
|
846
|
+
try {
|
|
847
|
+
const data = transformAthlete(characteristics);
|
|
848
|
+
await ctx.runMutation(this.component.public.ingestAthlete, {
|
|
849
|
+
connectionId,
|
|
850
|
+
userId,
|
|
851
|
+
...data,
|
|
852
|
+
});
|
|
853
|
+
count++;
|
|
854
|
+
} catch (err) {
|
|
855
|
+
errors.push({
|
|
856
|
+
type: "athlete",
|
|
857
|
+
id: "transform",
|
|
858
|
+
message: err instanceof Error ? err.message : String(err),
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return { data: { athletes: count }, errors };
|
|
863
|
+
}
|
|
864
|
+
}
|