@nativesquare/soma 0.14.1 → 0.16.0

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