@nativesquare/soma 0.3.0 → 0.5.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 (101) hide show
  1. package/dist/client/index.d.ts +283 -0
  2. package/dist/client/index.d.ts.map +1 -1
  3. package/dist/client/index.js +328 -0
  4. package/dist/client/index.js.map +1 -1
  5. package/dist/component/_generated/api.d.ts +2 -0
  6. package/dist/component/_generated/api.d.ts.map +1 -1
  7. package/dist/component/_generated/api.js.map +1 -1
  8. package/dist/component/_generated/component.d.ts +77 -0
  9. package/dist/component/_generated/component.d.ts.map +1 -1
  10. package/dist/component/garmin.d.ts +164 -0
  11. package/dist/component/garmin.d.ts.map +1 -0
  12. package/dist/component/garmin.js +609 -0
  13. package/dist/component/garmin.js.map +1 -0
  14. package/dist/component/public.d.ts +761 -761
  15. package/dist/component/schema.d.ts +405 -388
  16. package/dist/component/schema.d.ts.map +1 -1
  17. package/dist/component/schema.js +14 -2
  18. package/dist/component/schema.js.map +1 -1
  19. package/dist/component/strava.d.ts +5 -4
  20. package/dist/component/strava.d.ts.map +1 -1
  21. package/dist/component/strava.js +18 -1
  22. package/dist/component/strava.js.map +1 -1
  23. package/dist/component/validators/activity.d.ts +42 -42
  24. package/dist/component/validators/body.d.ts +47 -47
  25. package/dist/component/validators/daily.d.ts +17 -17
  26. package/dist/component/validators/plannedWorkout.d.ts +5 -5
  27. package/dist/component/validators/samples.d.ts +2 -2
  28. package/dist/component/validators/shared.d.ts +17 -17
  29. package/dist/component/validators/sleep.d.ts +17 -17
  30. package/dist/garmin/activity.d.ts +101 -0
  31. package/dist/garmin/activity.d.ts.map +1 -0
  32. package/dist/garmin/activity.js +207 -0
  33. package/dist/garmin/activity.js.map +1 -0
  34. package/dist/garmin/auth.d.ts +65 -0
  35. package/dist/garmin/auth.d.ts.map +1 -0
  36. package/dist/garmin/auth.js +155 -0
  37. package/dist/garmin/auth.js.map +1 -0
  38. package/dist/garmin/body.d.ts +26 -0
  39. package/dist/garmin/body.d.ts.map +1 -0
  40. package/dist/garmin/body.js +44 -0
  41. package/dist/garmin/body.js.map +1 -0
  42. package/dist/garmin/client.d.ts +99 -0
  43. package/dist/garmin/client.d.ts.map +1 -0
  44. package/dist/garmin/client.js +153 -0
  45. package/dist/garmin/client.js.map +1 -0
  46. package/dist/garmin/daily.d.ts +74 -0
  47. package/dist/garmin/daily.d.ts.map +1 -0
  48. package/dist/garmin/daily.js +143 -0
  49. package/dist/garmin/daily.js.map +1 -0
  50. package/dist/garmin/index.d.ts +20 -0
  51. package/dist/garmin/index.d.ts.map +1 -0
  52. package/dist/garmin/index.js +21 -0
  53. package/dist/garmin/index.js.map +1 -0
  54. package/dist/garmin/maps/activity-type.d.ts +7 -0
  55. package/dist/garmin/maps/activity-type.d.ts.map +1 -0
  56. package/dist/garmin/maps/activity-type.js +98 -0
  57. package/dist/garmin/maps/activity-type.js.map +1 -0
  58. package/dist/garmin/maps/sleep-level.d.ts +6 -0
  59. package/dist/garmin/maps/sleep-level.d.ts.map +1 -0
  60. package/dist/garmin/maps/sleep-level.js +21 -0
  61. package/dist/garmin/maps/sleep-level.js.map +1 -0
  62. package/dist/garmin/menstruation.d.ts +23 -0
  63. package/dist/garmin/menstruation.d.ts.map +1 -0
  64. package/dist/garmin/menstruation.js +34 -0
  65. package/dist/garmin/menstruation.js.map +1 -0
  66. package/dist/garmin/sleep.d.ts +62 -0
  67. package/dist/garmin/sleep.d.ts.map +1 -0
  68. package/dist/garmin/sleep.js +125 -0
  69. package/dist/garmin/sleep.js.map +1 -0
  70. package/dist/garmin/sync.d.ts +39 -0
  71. package/dist/garmin/sync.d.ts.map +1 -0
  72. package/dist/garmin/sync.js +175 -0
  73. package/dist/garmin/sync.js.map +1 -0
  74. package/dist/garmin/types.d.ts +212 -0
  75. package/dist/garmin/types.d.ts.map +1 -0
  76. package/dist/garmin/types.js +8 -0
  77. package/dist/garmin/types.js.map +1 -0
  78. package/dist/validators.d.ts +331 -331
  79. package/package.json +5 -1
  80. package/src/client/index.ts +446 -1
  81. package/src/component/_generated/api.ts +2 -0
  82. package/src/component/_generated/component.ts +89 -0
  83. package/src/component/garmin.ts +711 -0
  84. package/src/component/schema.ts +15 -2
  85. package/src/component/strava.ts +23 -1
  86. package/src/garmin/activity.test.ts +178 -0
  87. package/src/garmin/activity.ts +272 -0
  88. package/src/garmin/auth.test.ts +128 -0
  89. package/src/garmin/auth.ts +249 -0
  90. package/src/garmin/body.ts +59 -0
  91. package/src/garmin/client.ts +254 -0
  92. package/src/garmin/daily.ts +211 -0
  93. package/src/garmin/index.ts +76 -0
  94. package/src/garmin/maps/activity-type.test.ts +78 -0
  95. package/src/garmin/maps/activity-type.ts +116 -0
  96. package/src/garmin/maps/sleep-level.ts +22 -0
  97. package/src/garmin/menstruation.ts +42 -0
  98. package/src/garmin/sleep.test.ts +110 -0
  99. package/src/garmin/sleep.ts +170 -0
  100. package/src/garmin/sync.ts +223 -0
  101. package/src/garmin/types.ts +338 -0
@@ -0,0 +1,711 @@
1
+ // ─── Garmin Component Actions ────────────────────────────────────────────────
2
+ // Public actions that handle the full Garmin OAuth + sync lifecycle.
3
+ // The host app calls these through the Soma class, which threads the
4
+ // credentials automatically from env vars or constructor config.
5
+ //
6
+ // Internal mutations manage the providerTokens table (token CRUD).
7
+
8
+ import { v } from "convex/values";
9
+ import { anyApi } from "convex/server";
10
+ import {
11
+ action,
12
+ internalMutation,
13
+ internalQuery,
14
+ } from "./_generated/server.js";
15
+ import { getRequestToken, getAccessToken } from "../garmin/auth.js";
16
+ import { GarminClient } from "../garmin/client.js";
17
+ import { transformActivity } from "../garmin/activity.js";
18
+ import { transformDaily } from "../garmin/daily.js";
19
+ import { transformSleep } from "../garmin/sleep.js";
20
+ import { transformBody } from "../garmin/body.js";
21
+ import { transformMenstruation } from "../garmin/menstruation.js";
22
+
23
+ // Use anyApi to avoid circular type references between this file and _generated/api.ts.
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ const publicApi: any = anyApi;
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ const internalApi: any = anyApi;
28
+
29
+ // Default sync window: last 30 days
30
+ const DEFAULT_SYNC_DAYS = 30;
31
+
32
+ // ─── Internal Pending OAuth CRUD ─────────────────────────────────────────────
33
+ // Temporary storage for in-progress Garmin OAuth 1.0a flows.
34
+ // Bridges Step 1 (getGarminRequestToken) and Step 3 (completeGarminOAuth).
35
+
36
+ export const storePendingOAuth = internalMutation({
37
+ args: {
38
+ provider: v.string(),
39
+ oauthToken: v.string(),
40
+ tokenSecret: v.string(),
41
+ userId: v.string(),
42
+ },
43
+ returns: v.null(),
44
+ handler: async (ctx, args) => {
45
+ await ctx.db.insert("pendingOAuth", {
46
+ ...args,
47
+ createdAt: Date.now(),
48
+ });
49
+ return null;
50
+ },
51
+ });
52
+
53
+ export const getPendingOAuth = internalQuery({
54
+ args: { oauthToken: v.string() },
55
+ returns: v.union(
56
+ v.object({
57
+ _id: v.id("pendingOAuth"),
58
+ _creationTime: v.number(),
59
+ provider: v.string(),
60
+ oauthToken: v.string(),
61
+ tokenSecret: v.string(),
62
+ userId: v.string(),
63
+ createdAt: v.number(),
64
+ }),
65
+ v.null(),
66
+ ),
67
+ handler: async (ctx, args) => {
68
+ return await ctx.db
69
+ .query("pendingOAuth")
70
+ .withIndex("by_oauthToken", (q) => q.eq("oauthToken", args.oauthToken))
71
+ .first();
72
+ },
73
+ });
74
+
75
+ export const deletePendingOAuth = internalMutation({
76
+ args: { oauthToken: v.string() },
77
+ returns: v.null(),
78
+ handler: async (ctx, args) => {
79
+ const pending = await ctx.db
80
+ .query("pendingOAuth")
81
+ .withIndex("by_oauthToken", (q) => q.eq("oauthToken", args.oauthToken))
82
+ .first();
83
+ if (pending) {
84
+ await ctx.db.delete(pending._id);
85
+ }
86
+ return null;
87
+ },
88
+ });
89
+
90
+ // ─── Internal Token CRUD ─────────────────────────────────────────────────────
91
+
92
+ /**
93
+ * Store OAuth 1.0a tokens for a Garmin connection.
94
+ * Upserts by connectionId — one token record per connection.
95
+ */
96
+ export const storeTokens = internalMutation({
97
+ args: {
98
+ connectionId: v.id("connections"),
99
+ accessToken: v.string(),
100
+ tokenSecret: v.string(),
101
+ },
102
+ returns: v.null(),
103
+ handler: async (ctx, args) => {
104
+ const existing = await ctx.db
105
+ .query("providerTokens")
106
+ .withIndex("by_connectionId", (q) =>
107
+ q.eq("connectionId", args.connectionId),
108
+ )
109
+ .first();
110
+
111
+ if (existing) {
112
+ await ctx.db.patch(existing._id, {
113
+ accessToken: args.accessToken,
114
+ tokenSecret: args.tokenSecret,
115
+ });
116
+ return null;
117
+ }
118
+
119
+ await ctx.db.insert("providerTokens", {
120
+ connectionId: args.connectionId,
121
+ accessToken: args.accessToken,
122
+ tokenSecret: args.tokenSecret,
123
+ });
124
+ return null;
125
+ },
126
+ });
127
+
128
+ /**
129
+ * Get stored tokens for a connection.
130
+ */
131
+ export const getTokens = internalQuery({
132
+ args: { connectionId: v.id("connections") },
133
+ returns: v.union(
134
+ v.object({
135
+ _id: v.id("providerTokens"),
136
+ _creationTime: v.number(),
137
+ connectionId: v.id("connections"),
138
+ accessToken: v.string(),
139
+ refreshToken: v.optional(v.string()),
140
+ tokenSecret: v.optional(v.string()),
141
+ expiresAt: v.optional(v.number()),
142
+ }),
143
+ v.null(),
144
+ ),
145
+ handler: async (ctx, args) => {
146
+ return await ctx.db
147
+ .query("providerTokens")
148
+ .withIndex("by_connectionId", (q) =>
149
+ q.eq("connectionId", args.connectionId),
150
+ )
151
+ .first();
152
+ },
153
+ });
154
+
155
+ /**
156
+ * Delete stored tokens for a connection.
157
+ */
158
+ export const deleteTokens = internalMutation({
159
+ args: { connectionId: v.id("connections") },
160
+ returns: v.null(),
161
+ handler: async (ctx, args) => {
162
+ const existing = await ctx.db
163
+ .query("providerTokens")
164
+ .withIndex("by_connectionId", (q) =>
165
+ q.eq("connectionId", args.connectionId),
166
+ )
167
+ .first();
168
+
169
+ if (existing) {
170
+ await ctx.db.delete(existing._id);
171
+ }
172
+ return null;
173
+ },
174
+ });
175
+
176
+ // ─── Public Actions ──────────────────────────────────────────────────────────
177
+
178
+ /**
179
+ * Step 1 of OAuth: obtain a request token and return the authorization URL.
180
+ *
181
+ * If `userId` is provided, the request token and secret are stored in the
182
+ * component's `pendingOAuth` table so that `completeGarminOAuth` can look
183
+ * them up automatically when the callback fires. This is the recommended
184
+ * flow when using `registerRoutes`.
185
+ *
186
+ * If `userId` is omitted, the host app must store the returned `token`
187
+ * and `tokenSecret` itself and pass them to `connectGarmin` manually.
188
+ */
189
+ export const getGarminRequestToken = action({
190
+ args: {
191
+ consumerKey: v.string(),
192
+ consumerSecret: v.string(),
193
+ callbackUrl: v.optional(v.string()),
194
+ userId: v.optional(v.string()),
195
+ },
196
+ returns: v.object({
197
+ token: v.string(),
198
+ tokenSecret: v.string(),
199
+ authUrl: v.string(),
200
+ }),
201
+ handler: async (ctx, args) => {
202
+ const result = await getRequestToken({
203
+ consumerKey: args.consumerKey,
204
+ consumerSecret: args.consumerSecret,
205
+ callbackUrl: args.callbackUrl,
206
+ });
207
+
208
+ if (args.userId) {
209
+ await ctx.runMutation(internalApi.garmin.storePendingOAuth, {
210
+ provider: "GARMIN",
211
+ oauthToken: result.oauthToken,
212
+ tokenSecret: result.oauthTokenSecret,
213
+ userId: args.userId,
214
+ });
215
+ }
216
+
217
+ return {
218
+ token: result.oauthToken,
219
+ tokenSecret: result.oauthTokenSecret,
220
+ authUrl: result.authUrl,
221
+ };
222
+ },
223
+ });
224
+
225
+ /**
226
+ * Step 3 of OAuth + initial sync.
227
+ *
228
+ * Exchanges the request token + verifier for permanent access tokens,
229
+ * creates/reactivates the Soma connection, stores tokens, and syncs
230
+ * the last 30 days of all data types.
231
+ */
232
+ export const connectGarmin = action({
233
+ args: {
234
+ userId: v.string(),
235
+ consumerKey: v.string(),
236
+ consumerSecret: v.string(),
237
+ token: v.string(),
238
+ tokenSecret: v.string(),
239
+ verifier: v.string(),
240
+ },
241
+ returns: v.object({
242
+ connectionId: v.string(),
243
+ synced: v.object({
244
+ activities: v.number(),
245
+ dailies: v.number(),
246
+ sleep: v.number(),
247
+ body: v.number(),
248
+ menstruation: v.number(),
249
+ }),
250
+ errors: v.array(
251
+ v.object({ type: v.string(), id: v.string(), error: v.string() }),
252
+ ),
253
+ }),
254
+ handler: async (ctx, args) => {
255
+ // 1. Exchange request token for permanent access token
256
+ const accessTokenResult = await getAccessToken({
257
+ consumerKey: args.consumerKey,
258
+ consumerSecret: args.consumerSecret,
259
+ token: args.token,
260
+ tokenSecret: args.tokenSecret,
261
+ verifier: args.verifier,
262
+ });
263
+
264
+ // 2. Create/reactivate the Soma connection
265
+ const connectionId = await ctx.runMutation(publicApi.public.connect, {
266
+ userId: args.userId,
267
+ provider: "GARMIN",
268
+ });
269
+
270
+ // 3. Store permanent OAuth tokens
271
+ await ctx.runMutation(internalApi.garmin.storeTokens, {
272
+ connectionId,
273
+ accessToken: accessTokenResult.oauthToken,
274
+ tokenSecret: accessTokenResult.oauthTokenSecret,
275
+ });
276
+
277
+ // 4. Sync last 30 days of all data types
278
+ const client = new GarminClient({
279
+ consumerKey: args.consumerKey,
280
+ consumerSecret: args.consumerSecret,
281
+ accessToken: accessTokenResult.oauthToken,
282
+ tokenSecret: accessTokenResult.oauthTokenSecret,
283
+ });
284
+
285
+ const now = Math.floor(Date.now() / 1000);
286
+ const thirtyDaysAgo = now - DEFAULT_SYNC_DAYS * 86400;
287
+ const timeRange = {
288
+ uploadStartTimeInSeconds: thirtyDaysAgo,
289
+ uploadEndTimeInSeconds: now,
290
+ };
291
+
292
+ const result = await syncAllTypes(ctx, client, {
293
+ connectionId,
294
+ userId: args.userId,
295
+ consumerKey: args.consumerKey,
296
+ consumerSecret: args.consumerSecret,
297
+ timeRange,
298
+ });
299
+
300
+ // 5. Update lastDataUpdate timestamp
301
+ await ctx.runMutation(publicApi.public.updateConnection, {
302
+ connectionId,
303
+ lastDataUpdate: new Date().toISOString(),
304
+ });
305
+
306
+ return {
307
+ connectionId,
308
+ synced: result.synced,
309
+ errors: result.errors,
310
+ };
311
+ },
312
+ });
313
+
314
+ /**
315
+ * Complete a Garmin OAuth flow using stored pending state.
316
+ *
317
+ * Used by `registerRoutes` — the callback handler calls this with the
318
+ * `oauth_token` and `oauth_verifier` from the redirect. The action looks
319
+ * up the pending state (tokenSecret, userId) stored during Step 1,
320
+ * exchanges for permanent tokens, creates the connection, syncs data,
321
+ * and cleans up the pending entry.
322
+ */
323
+ export const completeGarminOAuth = action({
324
+ args: {
325
+ oauthToken: v.string(),
326
+ oauthVerifier: v.string(),
327
+ consumerKey: v.string(),
328
+ consumerSecret: v.string(),
329
+ },
330
+ returns: v.object({
331
+ connectionId: v.string(),
332
+ synced: v.object({
333
+ activities: v.number(),
334
+ dailies: v.number(),
335
+ sleep: v.number(),
336
+ body: v.number(),
337
+ menstruation: v.number(),
338
+ }),
339
+ errors: v.array(
340
+ v.object({ type: v.string(), id: v.string(), error: v.string() }),
341
+ ),
342
+ }),
343
+ handler: async (ctx, args) => {
344
+ // 1. Look up pending state
345
+ const pending = await ctx.runQuery(internalApi.garmin.getPendingOAuth, {
346
+ oauthToken: args.oauthToken,
347
+ });
348
+ if (!pending) {
349
+ throw new Error(
350
+ "No pending Garmin OAuth state found for this token. " +
351
+ "The request token may have expired or was already used.",
352
+ );
353
+ }
354
+
355
+ // 2. Exchange request token for permanent access token
356
+ const accessTokenResult = await getAccessToken({
357
+ consumerKey: args.consumerKey,
358
+ consumerSecret: args.consumerSecret,
359
+ token: args.oauthToken,
360
+ tokenSecret: pending.tokenSecret,
361
+ verifier: args.oauthVerifier,
362
+ });
363
+
364
+ // 3. Delete pending state (no longer needed)
365
+ await ctx.runMutation(internalApi.garmin.deletePendingOAuth, {
366
+ oauthToken: args.oauthToken,
367
+ });
368
+
369
+ // 4. Create/reactivate the Soma connection
370
+ const connectionId = await ctx.runMutation(publicApi.public.connect, {
371
+ userId: pending.userId,
372
+ provider: "GARMIN",
373
+ });
374
+
375
+ // 5. Store permanent OAuth tokens
376
+ await ctx.runMutation(internalApi.garmin.storeTokens, {
377
+ connectionId,
378
+ accessToken: accessTokenResult.oauthToken,
379
+ tokenSecret: accessTokenResult.oauthTokenSecret,
380
+ });
381
+
382
+ // 6. Sync last 30 days of all data types
383
+ const client = new GarminClient({
384
+ consumerKey: args.consumerKey,
385
+ consumerSecret: args.consumerSecret,
386
+ accessToken: accessTokenResult.oauthToken,
387
+ tokenSecret: accessTokenResult.oauthTokenSecret,
388
+ });
389
+
390
+ const now = Math.floor(Date.now() / 1000);
391
+ const thirtyDaysAgo = now - DEFAULT_SYNC_DAYS * 86400;
392
+ const timeRange = {
393
+ uploadStartTimeInSeconds: thirtyDaysAgo,
394
+ uploadEndTimeInSeconds: now,
395
+ };
396
+
397
+ const result = await syncAllTypes(ctx, client, {
398
+ connectionId,
399
+ userId: pending.userId,
400
+ consumerKey: args.consumerKey,
401
+ consumerSecret: args.consumerSecret,
402
+ timeRange,
403
+ });
404
+
405
+ // 7. Update lastDataUpdate timestamp
406
+ await ctx.runMutation(publicApi.public.updateConnection, {
407
+ connectionId,
408
+ lastDataUpdate: new Date().toISOString(),
409
+ });
410
+
411
+ return {
412
+ connectionId,
413
+ synced: result.synced,
414
+ errors: result.errors,
415
+ };
416
+ },
417
+ });
418
+
419
+ /**
420
+ * Incremental Garmin sync for an already-connected user.
421
+ *
422
+ * Looks up the stored tokens and syncs all data types for the specified
423
+ * time range (defaults to last 30 days).
424
+ */
425
+ export const syncGarmin = action({
426
+ args: {
427
+ userId: v.string(),
428
+ consumerKey: v.string(),
429
+ consumerSecret: v.string(),
430
+ startTimeInSeconds: v.optional(v.number()),
431
+ endTimeInSeconds: v.optional(v.number()),
432
+ },
433
+ returns: v.object({
434
+ synced: v.object({
435
+ activities: v.number(),
436
+ dailies: v.number(),
437
+ sleep: v.number(),
438
+ body: v.number(),
439
+ menstruation: v.number(),
440
+ }),
441
+ errors: v.array(
442
+ v.object({ type: v.string(), id: v.string(), error: v.string() }),
443
+ ),
444
+ }),
445
+ handler: async (ctx, args) => {
446
+ // 1. Look up connection
447
+ const connection = await ctx.runQuery(
448
+ internalApi.private.getConnectionByProvider,
449
+ { userId: args.userId, provider: "GARMIN" },
450
+ );
451
+ if (!connection) {
452
+ throw new Error(
453
+ `No Garmin connection found for user "${args.userId}". ` +
454
+ "Call connectGarmin first.",
455
+ );
456
+ }
457
+ if (!connection.active) {
458
+ throw new Error(
459
+ `Garmin connection for user "${args.userId}" is inactive. Reconnect first.`,
460
+ );
461
+ }
462
+
463
+ const connectionId = connection._id;
464
+
465
+ // 2. Get stored tokens
466
+ const tokenDoc = await ctx.runQuery(internalApi.garmin.getTokens, {
467
+ connectionId,
468
+ });
469
+ if (!tokenDoc || !tokenDoc.tokenSecret) {
470
+ throw new Error(
471
+ "No Garmin tokens found for this connection. " +
472
+ "The connection may have been created before token storage was available.",
473
+ );
474
+ }
475
+
476
+ // 3. Create client and sync
477
+ const client = new GarminClient({
478
+ consumerKey: args.consumerKey,
479
+ consumerSecret: args.consumerSecret,
480
+ accessToken: tokenDoc.accessToken,
481
+ tokenSecret: tokenDoc.tokenSecret,
482
+ });
483
+
484
+ const now = Math.floor(Date.now() / 1000);
485
+ const timeRange = {
486
+ uploadStartTimeInSeconds:
487
+ args.startTimeInSeconds ?? now - DEFAULT_SYNC_DAYS * 86400,
488
+ uploadEndTimeInSeconds: args.endTimeInSeconds ?? now,
489
+ };
490
+
491
+ const result = await syncAllTypes(ctx, client, {
492
+ connectionId,
493
+ userId: args.userId,
494
+ consumerKey: args.consumerKey,
495
+ consumerSecret: args.consumerSecret,
496
+ timeRange,
497
+ });
498
+
499
+ // 4. Update lastDataUpdate timestamp
500
+ await ctx.runMutation(publicApi.public.updateConnection, {
501
+ connectionId,
502
+ lastDataUpdate: new Date().toISOString(),
503
+ });
504
+
505
+ return result;
506
+ },
507
+ });
508
+
509
+ /**
510
+ * Disconnect a user from Garmin.
511
+ *
512
+ * Deletes stored tokens and sets the connection to inactive.
513
+ * Garmin OAuth 1.0a tokens can't be revoked via API, so we just clean up locally.
514
+ */
515
+ export const disconnectGarmin = action({
516
+ args: {
517
+ userId: v.string(),
518
+ },
519
+ returns: v.null(),
520
+ handler: async (ctx, args) => {
521
+ // 1. Look up connection
522
+ const connection = await ctx.runQuery(
523
+ internalApi.private.getConnectionByProvider,
524
+ { userId: args.userId, provider: "GARMIN" },
525
+ );
526
+ if (!connection) {
527
+ throw new Error(
528
+ `No Garmin connection found for user "${args.userId}".`,
529
+ );
530
+ }
531
+
532
+ const connectionId = connection._id;
533
+
534
+ // 2. Delete stored tokens
535
+ await ctx.runMutation(internalApi.garmin.deleteTokens, { connectionId });
536
+
537
+ // 3. Set connection inactive
538
+ await ctx.runMutation(publicApi.public.disconnect, {
539
+ userId: args.userId,
540
+ provider: "GARMIN",
541
+ });
542
+
543
+ return null;
544
+ },
545
+ });
546
+
547
+ // ─── Internal Helpers ────────────────────────────────────────────────────────
548
+
549
+ interface SyncAllConfig {
550
+ connectionId: string;
551
+ userId: string;
552
+ consumerKey: string;
553
+ consumerSecret: string;
554
+ timeRange: { uploadStartTimeInSeconds: number; uploadEndTimeInSeconds: number };
555
+ }
556
+
557
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
558
+ type ActionContext = { runMutation: (ref: any, args: any) => Promise<any> };
559
+
560
+ async function syncAllTypes(
561
+ ctx: ActionContext,
562
+ client: GarminClient,
563
+ config: SyncAllConfig,
564
+ ) {
565
+ const { connectionId, userId, timeRange } = config;
566
+
567
+ const synced = { activities: 0, dailies: 0, sleep: 0, body: 0, menstruation: 0 };
568
+ const errors: Array<{ type: string; id: string; error: string }> = [];
569
+
570
+ // ── Activities ──────────────────────────────────────────────────────────
571
+ try {
572
+ const activities = await client.getActivities(timeRange);
573
+ for (const activity of activities) {
574
+ try {
575
+ const data = transformActivity(activity);
576
+ await ctx.runMutation(publicApi.public.ingestActivity, {
577
+ connectionId,
578
+ userId,
579
+ ...data,
580
+ } as never);
581
+ synced.activities++;
582
+ } catch (err) {
583
+ errors.push({
584
+ type: "activity",
585
+ id: activity.summaryId ?? String(activity.activityId),
586
+ error: err instanceof Error ? err.message : String(err),
587
+ });
588
+ }
589
+ }
590
+ } catch (err) {
591
+ errors.push({
592
+ type: "activity",
593
+ id: "fetch",
594
+ error: err instanceof Error ? err.message : String(err),
595
+ });
596
+ }
597
+
598
+ // ── Dailies ─────────────────────────────────────────────────────────────
599
+ try {
600
+ const dailies = await client.getDailies(timeRange);
601
+ for (const daily of dailies) {
602
+ try {
603
+ const data = transformDaily(daily);
604
+ await ctx.runMutation(publicApi.public.ingestDaily, {
605
+ connectionId,
606
+ userId,
607
+ ...data,
608
+ } as never);
609
+ synced.dailies++;
610
+ } catch (err) {
611
+ errors.push({
612
+ type: "daily",
613
+ id: daily.summaryId ?? daily.calendarDate,
614
+ error: err instanceof Error ? err.message : String(err),
615
+ });
616
+ }
617
+ }
618
+ } catch (err) {
619
+ errors.push({
620
+ type: "daily",
621
+ id: "fetch",
622
+ error: err instanceof Error ? err.message : String(err),
623
+ });
624
+ }
625
+
626
+ // ── Sleep ───────────────────────────────────────────────────────────────
627
+ try {
628
+ const sleeps = await client.getSleeps(timeRange);
629
+ for (const sleep of sleeps) {
630
+ try {
631
+ const data = transformSleep(sleep);
632
+ await ctx.runMutation(publicApi.public.ingestSleep, {
633
+ connectionId,
634
+ userId,
635
+ ...data,
636
+ } as never);
637
+ synced.sleep++;
638
+ } catch (err) {
639
+ errors.push({
640
+ type: "sleep",
641
+ id: sleep.summaryId ?? sleep.calendarDate,
642
+ error: err instanceof Error ? err.message : String(err),
643
+ });
644
+ }
645
+ }
646
+ } catch (err) {
647
+ errors.push({
648
+ type: "sleep",
649
+ id: "fetch",
650
+ error: err instanceof Error ? err.message : String(err),
651
+ });
652
+ }
653
+
654
+ // ── Body ────────────────────────────────────────────────────────────────
655
+ try {
656
+ const bodyComps = await client.getBodyCompositions(timeRange);
657
+ for (const body of bodyComps) {
658
+ try {
659
+ const data = transformBody(body);
660
+ await ctx.runMutation(publicApi.public.ingestBody, {
661
+ connectionId,
662
+ userId,
663
+ ...data,
664
+ } as never);
665
+ synced.body++;
666
+ } catch (err) {
667
+ errors.push({
668
+ type: "body",
669
+ id: body.summaryId ?? String(body.measurementTimeInSeconds),
670
+ error: err instanceof Error ? err.message : String(err),
671
+ });
672
+ }
673
+ }
674
+ } catch (err) {
675
+ errors.push({
676
+ type: "body",
677
+ id: "fetch",
678
+ error: err instanceof Error ? err.message : String(err),
679
+ });
680
+ }
681
+
682
+ // ── Menstruation ────────────────────────────────────────────────────────
683
+ try {
684
+ const records = await client.getMenstrualCycleData(timeRange);
685
+ for (const record of records) {
686
+ try {
687
+ const data = transformMenstruation(record);
688
+ await ctx.runMutation(publicApi.public.ingestMenstruation, {
689
+ connectionId,
690
+ userId,
691
+ ...data,
692
+ } as never);
693
+ synced.menstruation++;
694
+ } catch (err) {
695
+ errors.push({
696
+ type: "menstruation",
697
+ id: record.summaryId ?? record.calendarDate,
698
+ error: err instanceof Error ? err.message : String(err),
699
+ });
700
+ }
701
+ }
702
+ } catch (err) {
703
+ errors.push({
704
+ type: "menstruation",
705
+ id: "fetch",
706
+ error: err instanceof Error ? err.message : String(err),
707
+ });
708
+ }
709
+
710
+ return { synced, errors };
711
+ }