@nativesquare/soma 0.11.0 → 0.13.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 (60) hide show
  1. package/dist/client/garmin.d.ts +291 -0
  2. package/dist/client/garmin.d.ts.map +1 -0
  3. package/dist/client/garmin.js +493 -0
  4. package/dist/client/garmin.js.map +1 -0
  5. package/dist/client/index.d.ts +29 -394
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +30 -520
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/client/strava.d.ts +97 -0
  10. package/dist/client/strava.d.ts.map +1 -0
  11. package/dist/client/strava.js +160 -0
  12. package/dist/client/strava.js.map +1 -0
  13. package/dist/client/types.d.ts +238 -0
  14. package/dist/client/types.d.ts.map +1 -1
  15. package/dist/component/_generated/component.d.ts +24 -12
  16. package/dist/component/_generated/component.d.ts.map +1 -1
  17. package/dist/component/garmin/private.d.ts +53 -68
  18. package/dist/component/garmin/private.d.ts.map +1 -1
  19. package/dist/component/garmin/private.js +87 -85
  20. package/dist/component/garmin/private.js.map +1 -1
  21. package/dist/component/garmin/public.d.ts +97 -53
  22. package/dist/component/garmin/public.d.ts.map +1 -1
  23. package/dist/component/garmin/public.js +75 -148
  24. package/dist/component/garmin/public.js.map +1 -1
  25. package/dist/component/garmin/webhooks.d.ts +22 -20
  26. package/dist/component/garmin/webhooks.d.ts.map +1 -1
  27. package/dist/component/garmin/webhooks.js +115 -76
  28. package/dist/component/garmin/webhooks.js.map +1 -1
  29. package/dist/component/public.d.ts +15 -15
  30. package/dist/component/schema.d.ts +25 -25
  31. package/dist/component/strava/public.d.ts +12 -8
  32. package/dist/component/strava/public.d.ts.map +1 -1
  33. package/dist/component/strava/public.js +7 -7
  34. package/dist/component/strava/public.js.map +1 -1
  35. package/dist/component/validators/activity.d.ts +4 -4
  36. package/dist/component/validators/body.d.ts +4 -4
  37. package/dist/component/validators/daily.d.ts +4 -4
  38. package/dist/component/validators/nutrition.d.ts +3 -3
  39. package/dist/component/validators/samples.d.ts +4 -4
  40. package/dist/component/validators/shared.d.ts +13 -4
  41. package/dist/component/validators/shared.d.ts.map +1 -1
  42. package/dist/component/validators/shared.js +7 -0
  43. package/dist/component/validators/shared.js.map +1 -1
  44. package/dist/component/validators/sleep.d.ts +5 -5
  45. package/dist/validators.d.ts +41 -40
  46. package/dist/validators.d.ts.map +1 -1
  47. package/dist/validators.js +1 -0
  48. package/dist/validators.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/client/garmin.ts +692 -0
  51. package/src/client/index.ts +68 -933
  52. package/src/client/strava.ts +199 -0
  53. package/src/client/types.ts +285 -0
  54. package/src/component/_generated/component.ts +19 -32
  55. package/src/component/garmin/private.ts +1872 -1870
  56. package/src/component/garmin/public.ts +1073 -1184
  57. package/src/component/garmin/webhooks.ts +898 -857
  58. package/src/component/strava/public.ts +393 -393
  59. package/src/component/validators/shared.ts +9 -0
  60. package/src/validators.ts +1 -0
@@ -1,858 +1,899 @@
1
- // ─── Garmin Webhook Handlers (Push Mode) ─────────────────────────────────────
2
- // Each handler receives full Garmin data objects from push-mode webhooks.
3
- // Separate actions per data type because the Garmin developer portal
4
- // configures separate URLs per type.
5
-
6
- import { v } from "convex/values";
7
- import { action, type ActionCtx } from "../_generated/server";
8
- import { api, internal } from "../_generated/api";
9
- import {
10
- garminSkinTemperaturePingPayloadSchema,
11
- garminSkinTemperaturePushPayloadSchema,
12
- } from "./schemas/skinTemperature.js";
13
- import {
14
- garminSleepsPingPayloadSchema,
15
- garminSleepsPushPayloadSchema,
16
- } from "./schemas/sleeps.js";
17
- import {
18
- garminStressPingPayloadSchema,
19
- garminStressPushPayloadSchema,
20
- } from "./schemas/stress.js";
21
- import {
22
- garminActivityPingPayloadSchema,
23
- garminActivityPushPayloadSchema,
24
- } from "./schemas/activity.js";
25
- import {
26
- garminActivityDetailsPingPayloadSchema,
27
- garminActivityDetailsPushPayloadSchema,
28
- } from "./schemas/activityDetails.js";
29
- import {
30
- garminManuallyUpdatedActivitiesPingPayloadSchema,
31
- garminManuallyUpdatedActivitiesPushPayloadSchema,
32
- } from "./schemas/manuallyUpdatedActivities.js";
33
- import {
34
- garminMoveIQPingPayloadSchema,
35
- garminMoveIQPushPayloadSchema,
36
- } from "./schemas/moveIQ.js";
37
- import {
38
- garminBloodPressurePingPayloadSchema,
39
- garminBloodPressurePushPayloadSchema,
40
- } from "./schemas/bloodPressure.js";
41
- import {
42
- garminBodyCompositionsPingPayloadSchema,
43
- garminBodyCompositionsPushPayloadSchema,
44
- } from "./schemas/bodyCompositions.js";
45
- import {
46
- garminDailiesPingPayloadSchema,
47
- garminDailiesPushPayloadSchema,
48
- } from "./schemas/dailies.js";
49
- import {
50
- garminEpochPingPayloadSchema,
51
- garminEpochPushPayloadSchema,
52
- } from "./schemas/epochs.js";
53
- import {
54
- garminHealthSnapshotPingPayloadSchema,
55
- garminHealthSnapshotPushPayloadSchema,
56
- } from "./schemas/healthSnapshot.js";
57
- import {
58
- garminHRVSummaryPingPayloadSchema,
59
- garminHRVSummaryPushPayloadSchema,
60
- } from "./schemas/hrvSummary.js";
61
- import {
62
- garminPulseOxPingPayloadSchema,
63
- garminPulseOxPushPayloadSchema,
64
- } from "./schemas/pulseOx.js";
65
- import {
66
- garminRespirationPingPayloadSchema,
67
- garminRespirationPushPayloadSchema,
68
- } from "./schemas/respiration.js";
69
- import {
70
- garminUserMetricsPingPayloadSchema,
71
- garminUserMetricsPushPayloadSchema,
72
- } from "./schemas/userMetrics.js";
73
- import {
74
- garminMenstrualCycleTrackingPingPayloadSchema,
75
- garminMenstrualCycleTrackingPushPayloadSchema,
76
- } from "./schemas/menstrualCycleTracking.js";
77
-
78
- /**
79
- * Discriminate push vs ping webhook payloads.
80
- *
81
- * Per the Garmin Activity API docs:
82
- * - Ping items always carry `callbackURL` (absent only for deregistrations)
83
- * - Push items never carry `callbackURL`, they contain the full summary data
84
- *
85
- * Zod schema validation alone can't distinguish the two because both schemas
86
- * have all-optional fields and would accept either payload shape.
87
- */
88
- function isWebhookPushMode(payload: unknown): boolean {
89
- if (payload == null || typeof payload !== "object") return false;
90
-
91
- // The payload is either a keyed object like { activities: [...] }
92
- // or a flat array of items. Extract the first item either way.
93
- let firstItem: unknown;
94
- if (Array.isArray(payload)) {
95
- firstItem = payload[0];
96
- } else {
97
- const key = Object.keys(payload)[0];
98
- const list = (payload as Record<string, unknown>)[key];
99
- firstItem = Array.isArray(list) ? list[0] : undefined;
100
- }
101
-
102
- if (firstItem == null || typeof firstItem !== "object") return false;
103
- return !("callbackURL" in firstItem);
104
- }
105
-
106
- type WebhookResult = {
107
- processed: number;
108
- errors: Array<{ type: string; id: string; error: string }>;
109
- affectedUsers: Array<{ userId: string; connectionId: string }>;
110
- };
111
-
112
- type ProcessResult = {
113
- items: Array<{ connectionId: string; userId: string; data: Record<string, unknown> }>;
114
- errors: Array<{ type: string; id: string; error: string }>;
115
- };
116
-
117
- /**
118
- * Ingest transformed items from a private handler and update connections.
119
- * Shared orchestration logic for all push-mode webhook handlers.
120
- */
121
- async function ingestAndUpdate(
122
- ctx: ActionCtx,
123
- result: ProcessResult,
124
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
125
- ingestMutation: any,
126
- ): Promise<WebhookResult> {
127
- let processed = 0;
128
- const errors = [...result.errors];
129
- const connectionsToUpdate = new Set<string>();
130
- const userMap = new Map<string, { userId: string; connectionId: string }>();
131
-
132
- for (const item of result.items) {
133
- try {
134
- await ctx.runMutation(ingestMutation, {
135
- connectionId: item.connectionId,
136
- userId: item.userId,
137
- ...item.data,
138
- } as never);
139
- processed++;
140
- connectionsToUpdate.add(item.connectionId);
141
- userMap.set(`${item.userId}:${item.connectionId}`, {
142
- userId: item.userId,
143
- connectionId: item.connectionId,
144
- });
145
- } catch (err) {
146
- errors.push({
147
- type: "ingest",
148
- id: "unknown",
149
- error: err instanceof Error ? err.message : String(err),
150
- });
151
- }
152
- }
153
-
154
- for (const connectionId of connectionsToUpdate) {
155
- await ctx.runMutation(api.public.updateConnection, {
156
- connectionId,
157
- lastDataUpdate: new Date().toISOString(),
158
- } as never);
159
- }
160
-
161
- return { processed, errors, affectedUsers: [...userMap.values()] };
162
- }
163
-
164
- // ─── Webhook Handlers ────────────────────────────────────────────────────────
165
-
166
- /**
167
- * Handle a webhook for Garmin activities (push or ping mode).
168
- */
169
- export const handleGarminWebhookActivities = action({
170
- args: { payload: v.any() },
171
- handler: async (ctx, args): Promise<WebhookResult> => {
172
- if (isWebhookPushMode(args.payload)) {
173
- const pushResult = garminActivityPushPayloadSchema.safeParse(args.payload);
174
- if (pushResult.success && pushResult.data.activities.length > 0) {
175
- const result: ProcessResult = await ctx.runAction(
176
- internal.garmin.private.processActivityPushPayload,
177
- { payload: args.payload },
178
- );
179
- return await ingestAndUpdate(ctx, result, api.public.ingestActivity);
180
- }
181
- } else {
182
- const pingResult = garminActivityPingPayloadSchema.safeParse(args.payload);
183
- if (pingResult.success && pingResult.data.activities.length > 0) {
184
- return await ctx.runAction(
185
- internal.garmin.private.processActivityPingPayload,
186
- { payload: args.payload },
187
- );
188
- }
189
- }
190
-
191
- console.warn(
192
- `[garmin:webhook:activities] Payload matched neither ping nor push schema`,
193
- );
194
- return { processed: 0, errors: [], affectedUsers: [] };
195
- },
196
- });
197
-
198
- /**
199
- * Handle a webhook for Garmin activity details (push or ping mode).
200
- */
201
- export const handleGarminWebhookActivityDetails = action({
202
- args: { payload: v.any() },
203
- handler: async (ctx, args): Promise<WebhookResult> => {
204
- if (isWebhookPushMode(args.payload)) {
205
- const pushResult =
206
- garminActivityDetailsPushPayloadSchema.safeParse(args.payload);
207
- if (
208
- pushResult.success &&
209
- pushResult.data.activityDetails.length > 0
210
- ) {
211
- const result: ProcessResult = await ctx.runAction(
212
- internal.garmin.private.processActivityDetailsPushPayload,
213
- { payload: args.payload },
214
- );
215
- return await ingestAndUpdate(ctx, result, api.public.ingestActivity);
216
- }
217
- } else {
218
- const pingResult =
219
- garminActivityDetailsPingPayloadSchema.safeParse(args.payload);
220
- if (
221
- pingResult.success &&
222
- pingResult.data.activityDetails.length > 0
223
- ) {
224
- return await ctx.runAction(
225
- internal.garmin.private.processActivityDetailsPingPayload,
226
- { payload: args.payload },
227
- );
228
- }
229
- }
230
-
231
- console.warn(
232
- `[garmin:webhook:activityDetails] Payload matched neither ping nor push schema`,
233
- );
234
- return { processed: 0, errors: [], affectedUsers: [] };
235
- },
236
- });
237
-
238
- /**
239
- * Handle a webhook for Garmin manually updated activities (push or ping mode).
240
- */
241
- export const handleGarminWebhookManuallyUpdatedActivities = action({
242
- args: { payload: v.any() },
243
- handler: async (ctx, args): Promise<WebhookResult> => {
244
- if (isWebhookPushMode(args.payload)) {
245
- const pushResult =
246
- garminManuallyUpdatedActivitiesPushPayloadSchema.safeParse(
247
- args.payload,
248
- );
249
- if (
250
- pushResult.success &&
251
- pushResult.data.manuallyUpdatedActivities.length > 0
252
- ) {
253
- const result: ProcessResult = await ctx.runAction(
254
- internal.garmin.private
255
- .processManuallyUpdatedActivitiesPushPayload,
256
- { payload: args.payload },
257
- );
258
- return await ingestAndUpdate(ctx, result, api.public.ingestActivity);
259
- }
260
- } else {
261
- const pingResult =
262
- garminManuallyUpdatedActivitiesPingPayloadSchema.safeParse(
263
- args.payload,
264
- );
265
- if (
266
- pingResult.success &&
267
- pingResult.data.manuallyUpdatedActivities.length > 0
268
- ) {
269
- return await ctx.runAction(
270
- internal.garmin.private
271
- .processManuallyUpdatedActivitiesPingPayload,
272
- { payload: args.payload },
273
- );
274
- }
275
- }
276
-
277
- console.warn(
278
- `[garmin:webhook:manuallyUpdatedActivities] Payload matched neither ping nor push schema`,
279
- );
280
- return { processed: 0, errors: [], affectedUsers: [] };
281
- },
282
- });
283
-
284
- /**
285
- * Handle a webhook for Garmin Move IQ auto-detected activities (push or ping mode).
286
- */
287
- export const handleGarminWebhookMoveIQ = action({
288
- args: { payload: v.any() },
289
- handler: async (ctx, args): Promise<WebhookResult> => {
290
- if (isWebhookPushMode(args.payload)) {
291
- const pushResult =
292
- garminMoveIQPushPayloadSchema.safeParse(args.payload);
293
- if (
294
- pushResult.success &&
295
- pushResult.data.moveIQActivities.length > 0
296
- ) {
297
- const result: ProcessResult = await ctx.runAction(
298
- internal.garmin.private.processMoveIQPushPayload,
299
- { payload: args.payload },
300
- );
301
- return await ingestAndUpdate(ctx, result, api.public.ingestActivity);
302
- }
303
- } else {
304
- const pingResult =
305
- garminMoveIQPingPayloadSchema.safeParse(args.payload);
306
- if (
307
- pingResult.success &&
308
- pingResult.data.moveIQActivities.length > 0
309
- ) {
310
- return await ctx.runAction(
311
- internal.garmin.private.processMoveIQPingPayload,
312
- { payload: args.payload },
313
- );
314
- }
315
- }
316
-
317
- console.warn(
318
- `[garmin:webhook:moveIQ] Payload matched neither ping nor push schema`,
319
- );
320
- return { processed: 0, errors: [], affectedUsers: [] };
321
- },
322
- });
323
-
324
- /**
325
- * Handle a webhook for Garmin blood pressure summaries (push or ping mode).
326
- */
327
- export const handleGarminWebhookBloodPressures = action({
328
- args: { payload: v.any() },
329
- handler: async (ctx, args): Promise<WebhookResult> => {
330
- if (isWebhookPushMode(args.payload)) {
331
- const pushResult =
332
- garminBloodPressurePushPayloadSchema.safeParse(args.payload);
333
- if (
334
- pushResult.success &&
335
- pushResult.data.bloodPressures.length > 0
336
- ) {
337
- const result: ProcessResult = await ctx.runAction(
338
- internal.garmin.private.processBloodPressurePushPayload,
339
- { payload: args.payload },
340
- );
341
- return await ingestAndUpdate(ctx, result, api.public.ingestBody);
342
- }
343
- } else {
344
- const pingResult =
345
- garminBloodPressurePingPayloadSchema.safeParse(args.payload);
346
- if (
347
- pingResult.success &&
348
- pingResult.data.bloodPressures.length > 0
349
- ) {
350
- return await ctx.runAction(
351
- internal.garmin.private.processBloodPressurePingPayload,
352
- { payload: args.payload },
353
- );
354
- }
355
- }
356
-
357
- console.warn(
358
- `[garmin:webhook:bloodPressures] Payload matched neither ping nor push schema`,
359
- );
360
- return { processed: 0, errors: [], affectedUsers: [] };
361
- },
362
- });
363
-
364
- /**
365
- * Handle a webhook for Garmin body composition summaries (push or ping mode).
366
- */
367
- export const handleGarminWebhookBodyCompositions = action({
368
- args: { payload: v.any() },
369
- handler: async (ctx, args): Promise<WebhookResult> => {
370
- if (isWebhookPushMode(args.payload)) {
371
- const pushResult =
372
- garminBodyCompositionsPushPayloadSchema.safeParse(args.payload);
373
- if (
374
- pushResult.success &&
375
- pushResult.data.bodyComps.length > 0
376
- ) {
377
- const result: ProcessResult = await ctx.runAction(
378
- internal.garmin.private.processBodyCompositionsPushPayload,
379
- { payload: args.payload },
380
- );
381
- return await ingestAndUpdate(ctx, result, api.public.ingestBody);
382
- }
383
- } else {
384
- const pingResult =
385
- garminBodyCompositionsPingPayloadSchema.safeParse(args.payload);
386
- if (
387
- pingResult.success &&
388
- pingResult.data.bodyComps.length > 0
389
- ) {
390
- return await ctx.runAction(
391
- internal.garmin.private.processBodyCompositionsPingPayload,
392
- { payload: args.payload },
393
- );
394
- }
395
- }
396
-
397
- console.warn(
398
- `[garmin:webhook:bodyCompositions] Payload matched neither ping nor push schema`,
399
- );
400
- return { processed: 0, errors: [], affectedUsers: [] };
401
- },
402
- });
403
-
404
- /**
405
- * Handle a webhook for Garmin daily summaries (push or ping mode).
406
- */
407
- export const handleGarminWebhookDailies = action({
408
- args: { payload: v.any() },
409
- handler: async (ctx, args): Promise<WebhookResult> => {
410
- if (isWebhookPushMode(args.payload)) {
411
- const pushResult =
412
- garminDailiesPushPayloadSchema.safeParse(args.payload);
413
- if (
414
- pushResult.success &&
415
- pushResult.data.dailies.length > 0
416
- ) {
417
- const result: ProcessResult = await ctx.runAction(
418
- internal.garmin.private.processDailiesPushPayload,
419
- { payload: args.payload },
420
- );
421
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
422
- }
423
- } else {
424
- const pingResult =
425
- garminDailiesPingPayloadSchema.safeParse(args.payload);
426
- if (
427
- pingResult.success &&
428
- pingResult.data.dailies.length > 0
429
- ) {
430
- return await ctx.runAction(
431
- internal.garmin.private.processDailiesPingPayload,
432
- { payload: args.payload },
433
- );
434
- }
435
- }
436
-
437
- console.warn(
438
- `[garmin:webhook:dailies] Payload matched neither ping nor push schema`,
439
- );
440
- return { processed: 0, errors: [], affectedUsers: [] };
441
- },
442
- });
443
-
444
- /**
445
- * Handle a webhook for Garmin epoch summaries (push or ping mode).
446
- */
447
- export const handleGarminWebhookEpochs = action({
448
- args: { payload: v.any() },
449
- handler: async (ctx, args): Promise<WebhookResult> => {
450
- if (isWebhookPushMode(args.payload)) {
451
- const pushResult =
452
- garminEpochPushPayloadSchema.safeParse(args.payload);
453
- if (
454
- pushResult.success &&
455
- pushResult.data.epochs.length > 0
456
- ) {
457
- const result: ProcessResult = await ctx.runAction(
458
- internal.garmin.private.processEpochPushPayload,
459
- { payload: args.payload },
460
- );
461
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
462
- }
463
- } else {
464
- const pingResult =
465
- garminEpochPingPayloadSchema.safeParse(args.payload);
466
- if (
467
- pingResult.success &&
468
- pingResult.data.epochs.length > 0
469
- ) {
470
- return await ctx.runAction(
471
- internal.garmin.private.processEpochPingPayload,
472
- { payload: args.payload },
473
- );
474
- }
475
- }
476
-
477
- console.warn(
478
- `[garmin:webhook:epochs] Payload matched neither ping nor push schema`,
479
- );
480
- return { processed: 0, errors: [], affectedUsers: [] };
481
- },
482
- });
483
-
484
- /**
485
- * Handle a webhook for Garmin health snapshot summaries (push or ping mode).
486
- */
487
- export const handleGarminWebhookHealthSnapshot = action({
488
- args: { payload: v.any() },
489
- handler: async (ctx, args): Promise<WebhookResult> => {
490
- if (isWebhookPushMode(args.payload)) {
491
- const pushResult =
492
- garminHealthSnapshotPushPayloadSchema.safeParse(args.payload);
493
- if (
494
- pushResult.success &&
495
- pushResult.data.healthSnapshot.length > 0
496
- ) {
497
- const result: ProcessResult = await ctx.runAction(
498
- internal.garmin.private.processHealthSnapshotPushPayload,
499
- { payload: args.payload },
500
- );
501
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
502
- }
503
- } else {
504
- const pingResult =
505
- garminHealthSnapshotPingPayloadSchema.safeParse(args.payload);
506
- if (
507
- pingResult.success &&
508
- pingResult.data.healthSnapshot.length > 0
509
- ) {
510
- return await ctx.runAction(
511
- internal.garmin.private.processHealthSnapshotPingPayload,
512
- { payload: args.payload },
513
- );
514
- }
515
- }
516
-
517
- console.warn(
518
- `[garmin:webhook:healthSnapshot] Payload matched neither ping nor push schema`,
519
- );
520
- return { processed: 0, errors: [], affectedUsers: [] };
521
- },
522
- });
523
-
524
- /**
525
- * Handle a webhook for Garmin sleep summaries (push or ping mode).
526
- * Follows the structured dispatch pattern: validates with Zod, then delegates
527
- * to internal actions for processing.
528
- */
529
- export const handleGarminWebhookSleeps = action({
530
- args: { payload: v.any() },
531
- handler: async (ctx, args): Promise<WebhookResult> => {
532
- if (isWebhookPushMode(args.payload)) {
533
- const pushResult =
534
- garminSleepsPushPayloadSchema.safeParse(args.payload);
535
- if (
536
- pushResult.success &&
537
- pushResult.data.sleeps.length > 0
538
- ) {
539
- const result: ProcessResult = await ctx.runAction(
540
- internal.garmin.private.processSleepsPushPayload,
541
- { payload: args.payload },
542
- );
543
- return await ingestAndUpdate(ctx, result, api.public.ingestSleep);
544
- }
545
- } else {
546
- const pingResult =
547
- garminSleepsPingPayloadSchema.safeParse(args.payload);
548
- if (
549
- pingResult.success &&
550
- pingResult.data.sleeps.length > 0
551
- ) {
552
- return await ctx.runAction(
553
- internal.garmin.private.processSleepsPingPayload,
554
- { payload: args.payload },
555
- );
556
- }
557
- }
558
-
559
- console.warn(
560
- `[garmin:webhook:sleeps] Payload matched neither ping nor push schema`,
561
- );
562
- return { processed: 0, errors: [], affectedUsers: [] };
563
- },
564
- });
565
-
566
- /**
567
- * Handle a webhook for Garmin skin temperature summaries (push or ping mode).
568
- * Follows the structured dispatch pattern: validates with Zod, then delegates
569
- * to internal actions for processing.
570
- */
571
- export const handleGarminWebhookSkinTemp = action({
572
- args: { payload: v.any() },
573
- handler: async (ctx, args): Promise<WebhookResult> => {
574
- if (isWebhookPushMode(args.payload)) {
575
- const pushResult =
576
- garminSkinTemperaturePushPayloadSchema.safeParse(args.payload);
577
- if (
578
- pushResult.success &&
579
- pushResult.data.skinTemp.length > 0
580
- ) {
581
- const result: ProcessResult = await ctx.runAction(
582
- internal.garmin.private.processSkinTemperaturePushPayload,
583
- { payload: args.payload },
584
- );
585
- return await ingestAndUpdate(ctx, result, api.public.ingestBody);
586
- }
587
- } else {
588
- const pingResult =
589
- garminSkinTemperaturePingPayloadSchema.safeParse(args.payload);
590
- if (
591
- pingResult.success &&
592
- pingResult.data.skinTemp.length > 0
593
- ) {
594
- return await ctx.runAction(
595
- internal.garmin.private.processSkinTemperaturePingPayload,
596
- { payload: args.payload },
597
- );
598
- }
599
- }
600
-
601
- console.warn(
602
- `[garmin:webhook:skinTemperature] Payload matched neither ping nor push schema`,
603
- );
604
- return { processed: 0, errors: [], affectedUsers: [] };
605
- },
606
- });
607
-
608
- /**
609
- * Handle a webhook for Garmin user metrics (push or ping mode).
610
- * Follows the structured dispatch pattern: validates with Zod, then delegates
611
- * to internal actions for processing.
612
- */
613
- export const handleGarminWebhookUserMetrics = action({
614
- args: { payload: v.any() },
615
- handler: async (ctx, args): Promise<WebhookResult> => {
616
- if (isWebhookPushMode(args.payload)) {
617
- const pushResult =
618
- garminUserMetricsPushPayloadSchema.safeParse(args.payload);
619
- if (
620
- pushResult.success &&
621
- pushResult.data.userMetrics.length > 0
622
- ) {
623
- const result: ProcessResult = await ctx.runAction(
624
- internal.garmin.private.processUserMetricsPushPayload,
625
- { payload: args.payload },
626
- );
627
- return await ingestAndUpdate(ctx, result, api.public.ingestBody);
628
- }
629
- } else {
630
- const pingResult =
631
- garminUserMetricsPingPayloadSchema.safeParse(args.payload);
632
- if (
633
- pingResult.success &&
634
- pingResult.data.userMetrics.length > 0
635
- ) {
636
- return await ctx.runAction(
637
- internal.garmin.private.processUserMetricsPingPayload,
638
- { payload: args.payload },
639
- );
640
- }
641
- }
642
-
643
- console.warn(
644
- `[garmin:webhook:userMetrics] Payload matched neither ping nor push schema`,
645
- );
646
- return { processed: 0, errors: [], affectedUsers: [] };
647
- },
648
- });
649
-
650
- /**
651
- * Handle a webhook for Garmin menstrual cycle tracking (push or ping mode).
652
- * Follows the structured dispatch pattern: validates with Zod, then delegates
653
- * to internal actions for processing.
654
- */
655
- export const handleGarminWebhookMenstrualCycleTracking = action({
656
- args: { payload: v.any() },
657
- handler: async (ctx, args): Promise<WebhookResult> => {
658
- if (isWebhookPushMode(args.payload)) {
659
- const pushResult =
660
- garminMenstrualCycleTrackingPushPayloadSchema.safeParse(args.payload);
661
- if (
662
- pushResult.success &&
663
- pushResult.data.mct.length > 0
664
- ) {
665
- const result: ProcessResult = await ctx.runAction(
666
- internal.garmin.private.processMenstrualCycleTrackingPushPayload,
667
- { payload: args.payload },
668
- );
669
- return await ingestAndUpdate(ctx, result, api.public.ingestMenstruation);
670
- }
671
- } else {
672
- const pingResult =
673
- garminMenstrualCycleTrackingPingPayloadSchema.safeParse(args.payload);
674
- if (
675
- pingResult.success &&
676
- pingResult.data.mct.length > 0
677
- ) {
678
- return await ctx.runAction(
679
- internal.garmin.private.processMenstrualCycleTrackingPingPayload,
680
- { payload: args.payload },
681
- );
682
- }
683
- }
684
-
685
- console.warn(
686
- `[garmin:webhook:menstrualCycleTracking] Payload matched neither ping nor push schema`,
687
- );
688
- return { processed: 0, errors: [], affectedUsers: [] };
689
- },
690
- });
691
-
692
- /**
693
- * Handle a webhook for Garmin HRV summaries (push or ping mode).
694
- * Follows the structured dispatch pattern: validates with Zod, then delegates
695
- * to internal actions for processing.
696
- */
697
- export const handleGarminWebhookHRVSummary = action({
698
- args: { payload: v.any() },
699
- handler: async (ctx, args): Promise<WebhookResult> => {
700
- if (isWebhookPushMode(args.payload)) {
701
- const pushResult =
702
- garminHRVSummaryPushPayloadSchema.safeParse(args.payload);
703
- if (
704
- pushResult.success &&
705
- pushResult.data.hrv.length > 0
706
- ) {
707
- const result: ProcessResult = await ctx.runAction(
708
- internal.garmin.private.processHRVSummaryPushPayload,
709
- { payload: args.payload },
710
- );
711
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
712
- }
713
- } else {
714
- const pingResult =
715
- garminHRVSummaryPingPayloadSchema.safeParse(args.payload);
716
- if (
717
- pingResult.success &&
718
- pingResult.data.hrv.length > 0
719
- ) {
720
- return await ctx.runAction(
721
- internal.garmin.private.processHRVSummaryPingPayload,
722
- { payload: args.payload },
723
- );
724
- }
725
- }
726
-
727
- console.warn(
728
- `[garmin:webhook:hrvSummary] Payload matched neither ping nor push schema`,
729
- );
730
- return { processed: 0, errors: [], affectedUsers: [] };
731
- },
732
- });
733
-
734
- /**
735
- * Handle a webhook for Garmin stress detail summaries (push or ping mode).
736
- * Follows the structured dispatch pattern: validates with Zod, then delegates
737
- * to internal actions for processing.
738
- */
739
- export const handleGarminWebhookStress = action({
740
- args: { payload: v.any() },
741
- handler: async (ctx, args): Promise<WebhookResult> => {
742
- if (isWebhookPushMode(args.payload)) {
743
- const pushResult =
744
- garminStressPushPayloadSchema.safeParse(args.payload);
745
- if (
746
- pushResult.success &&
747
- pushResult.data.stressDetails.length > 0
748
- ) {
749
- const result: ProcessResult = await ctx.runAction(
750
- internal.garmin.private.processStressPushPayload,
751
- { payload: args.payload },
752
- );
753
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
754
- }
755
- } else {
756
- const pingResult =
757
- garminStressPingPayloadSchema.safeParse(args.payload);
758
- if (
759
- pingResult.success &&
760
- pingResult.data.stressDetails.length > 0
761
- ) {
762
- return await ctx.runAction(
763
- internal.garmin.private.processStressPingPayload,
764
- { payload: args.payload },
765
- );
766
- }
767
- }
768
-
769
- console.warn(
770
- `[garmin:webhook:stressDetails] Payload matched neither ping nor push schema`,
771
- );
772
- return { processed: 0, errors: [], affectedUsers: [] };
773
- },
774
- });
775
-
776
- /**
777
- * Handle a webhook for Garmin pulse oximetry (SpO2) summaries (push or ping mode).
778
- * Follows the structured dispatch pattern: validates with Zod, then delegates
779
- * to internal actions for processing.
780
- */
781
- export const handleGarminWebhookPulseOx = action({
782
- args: { payload: v.any() },
783
- handler: async (ctx, args): Promise<WebhookResult> => {
784
- if (isWebhookPushMode(args.payload)) {
785
- const pushResult =
786
- garminPulseOxPushPayloadSchema.safeParse(args.payload);
787
- if (
788
- pushResult.success &&
789
- pushResult.data.pulseox.length > 0
790
- ) {
791
- const result: ProcessResult = await ctx.runAction(
792
- internal.garmin.private.processPulseOxPushPayload,
793
- { payload: args.payload },
794
- );
795
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
796
- }
797
- } else {
798
- const pingResult =
799
- garminPulseOxPingPayloadSchema.safeParse(args.payload);
800
- if (
801
- pingResult.success &&
802
- pingResult.data.pulseox.length > 0
803
- ) {
804
- return await ctx.runAction(
805
- internal.garmin.private.processPulseOxPingPayload,
806
- { payload: args.payload },
807
- );
808
- }
809
- }
810
-
811
- console.warn(
812
- `[garmin:webhook:pulseOx] Payload matched neither ping nor push schema`,
813
- );
814
- return { processed: 0, errors: [], affectedUsers: [] };
815
- },
816
- });
817
-
818
- /**
819
- * Handle a webhook for Garmin respiration summaries (push or ping mode).
820
- * Follows the structured dispatch pattern: validates with Zod, then delegates
821
- * to internal actions for processing.
822
- */
823
- export const handleGarminWebhookRespiration = action({
824
- args: { payload: v.any() },
825
- handler: async (ctx, args): Promise<WebhookResult> => {
826
- if (isWebhookPushMode(args.payload)) {
827
- const pushResult =
828
- garminRespirationPushPayloadSchema.safeParse(args.payload);
829
- if (
830
- pushResult.success &&
831
- pushResult.data.allDayRespiration.length > 0
832
- ) {
833
- const result: ProcessResult = await ctx.runAction(
834
- internal.garmin.private.processRespirationPushPayload,
835
- { payload: args.payload },
836
- );
837
- return await ingestAndUpdate(ctx, result, api.public.ingestDaily);
838
- }
839
- } else {
840
- const pingResult =
841
- garminRespirationPingPayloadSchema.safeParse(args.payload);
842
- if (
843
- pingResult.success &&
844
- pingResult.data.allDayRespiration.length > 0
845
- ) {
846
- return await ctx.runAction(
847
- internal.garmin.private.processRespirationPingPayload,
848
- { payload: args.payload },
849
- );
850
- }
851
- }
852
-
853
- console.warn(
854
- `[garmin:webhook:respiration] Payload matched neither ping nor push schema`,
855
- );
856
- return { processed: 0, errors: [], affectedUsers: [] };
857
- },
1
+ // ─── Garmin Webhook Handlers (Push Mode) ─────────────────────────────────────
2
+ // Each handler receives full Garmin data objects from push-mode webhooks.
3
+ // Separate actions per data type because the Garmin developer portal
4
+ // configures separate URLs per type.
5
+
6
+ import { v } from "convex/values";
7
+ import { action, type ActionCtx } from "../_generated/server";
8
+ import { api, internal } from "../_generated/api";
9
+ import {
10
+ garminSkinTemperaturePingPayloadSchema,
11
+ garminSkinTemperaturePushPayloadSchema,
12
+ } from "./schemas/skinTemperature.js";
13
+ import {
14
+ garminSleepsPingPayloadSchema,
15
+ garminSleepsPushPayloadSchema,
16
+ } from "./schemas/sleeps.js";
17
+ import {
18
+ garminStressPingPayloadSchema,
19
+ garminStressPushPayloadSchema,
20
+ } from "./schemas/stress.js";
21
+ import {
22
+ garminActivityPingPayloadSchema,
23
+ garminActivityPushPayloadSchema,
24
+ } from "./schemas/activity.js";
25
+ import {
26
+ garminActivityDetailsPingPayloadSchema,
27
+ garminActivityDetailsPushPayloadSchema,
28
+ } from "./schemas/activityDetails.js";
29
+ import {
30
+ garminManuallyUpdatedActivitiesPingPayloadSchema,
31
+ garminManuallyUpdatedActivitiesPushPayloadSchema,
32
+ } from "./schemas/manuallyUpdatedActivities.js";
33
+ import {
34
+ garminMoveIQPingPayloadSchema,
35
+ garminMoveIQPushPayloadSchema,
36
+ } from "./schemas/moveIQ.js";
37
+ import {
38
+ garminBloodPressurePingPayloadSchema,
39
+ garminBloodPressurePushPayloadSchema,
40
+ } from "./schemas/bloodPressure.js";
41
+ import {
42
+ garminBodyCompositionsPingPayloadSchema,
43
+ garminBodyCompositionsPushPayloadSchema,
44
+ } from "./schemas/bodyCompositions.js";
45
+ import {
46
+ garminDailiesPingPayloadSchema,
47
+ garminDailiesPushPayloadSchema,
48
+ } from "./schemas/dailies.js";
49
+ import {
50
+ garminEpochPingPayloadSchema,
51
+ garminEpochPushPayloadSchema,
52
+ } from "./schemas/epochs.js";
53
+ import {
54
+ garminHealthSnapshotPingPayloadSchema,
55
+ garminHealthSnapshotPushPayloadSchema,
56
+ } from "./schemas/healthSnapshot.js";
57
+ import {
58
+ garminHRVSummaryPingPayloadSchema,
59
+ garminHRVSummaryPushPayloadSchema,
60
+ } from "./schemas/hrvSummary.js";
61
+ import {
62
+ garminPulseOxPingPayloadSchema,
63
+ garminPulseOxPushPayloadSchema,
64
+ } from "./schemas/pulseOx.js";
65
+ import {
66
+ garminRespirationPingPayloadSchema,
67
+ garminRespirationPushPayloadSchema,
68
+ } from "./schemas/respiration.js";
69
+ import {
70
+ garminUserMetricsPingPayloadSchema,
71
+ garminUserMetricsPushPayloadSchema,
72
+ } from "./schemas/userMetrics.js";
73
+ import {
74
+ garminMenstrualCycleTrackingPingPayloadSchema,
75
+ garminMenstrualCycleTrackingPushPayloadSchema,
76
+ } from "./schemas/menstrualCycleTracking.js";
77
+
78
+ /**
79
+ * Discriminate push vs ping webhook payloads.
80
+ *
81
+ * Per the Garmin Activity API docs:
82
+ * - Ping items always carry `callbackURL` (absent only for deregistrations)
83
+ * - Push items never carry `callbackURL`, they contain the full summary data
84
+ *
85
+ * Zod schema validation alone can't distinguish the two because both schemas
86
+ * have all-optional fields and would accept either payload shape.
87
+ */
88
+ function isWebhookPushMode(payload: unknown): boolean {
89
+ if (payload == null || typeof payload !== "object") return false;
90
+
91
+ // The payload is either a keyed object like { activities: [...] }
92
+ // or a flat array of items. Extract the first item either way.
93
+ let firstItem: unknown;
94
+ if (Array.isArray(payload)) {
95
+ firstItem = payload[0];
96
+ } else {
97
+ const key = Object.keys(payload)[0];
98
+ const list = (payload as Record<string, unknown>)[key];
99
+ firstItem = Array.isArray(list) ? list[0] : undefined;
100
+ }
101
+
102
+ if (firstItem == null || typeof firstItem !== "object") return false;
103
+ return !("callbackURL" in firstItem);
104
+ }
105
+
106
+ /** Shape returned by every public webhook handler action to the HTTP layer. */
107
+ type WebhookResult = {
108
+ errors: Array<{ type: string; id: string; message: string }>;
109
+ items: Array<{ connectionId: string; userId: string; data: Record<string, unknown> }>;
110
+ };
111
+
112
+ /** Shape returned by internal push-processing actions (transform only, no DB writes). */
113
+ type ProcessResult = {
114
+ items: Array<{ connectionId: string; userId: string; data: Record<string, unknown> }>;
115
+ errors: Array<{ type: string; id: string; message: string }>;
116
+ };
117
+
118
+ /**
119
+ * Build a WebhookResult from a ProcessResult without writing to the database.
120
+ * Used when `autoIngest` is disabled — the host app still gets the full set of
121
+ * transformed items in its callbacks, but no data is persisted.
122
+ */
123
+ function toWebhookResult(result: ProcessResult): WebhookResult {
124
+ return {
125
+ errors: result.errors,
126
+ items: result.items,
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Ingest transformed items from a private handler and update connections.
132
+ * Shared orchestration logic for all push-mode webhook handlers.
133
+ */
134
+ async function ingestAndUpdate(
135
+ ctx: ActionCtx,
136
+ result: ProcessResult,
137
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ ingestMutation: any,
139
+ ): Promise<WebhookResult> {
140
+ const errors = [...result.errors];
141
+ const connectionsToUpdate = new Set<string>();
142
+
143
+ for (const item of result.items) {
144
+ try {
145
+ await ctx.runMutation(ingestMutation, {
146
+ connectionId: item.connectionId,
147
+ userId: item.userId,
148
+ ...item.data,
149
+ });
150
+ connectionsToUpdate.add(item.connectionId);
151
+ } catch (err) {
152
+ errors.push({
153
+ type: "ingest",
154
+ id: "unknown",
155
+ message: err instanceof Error ? err.message : String(err),
156
+ });
157
+ }
158
+ }
159
+
160
+ for (const connectionId of connectionsToUpdate) {
161
+ await ctx.runMutation(api.public.updateConnection, {
162
+ connectionId,
163
+ lastDataUpdate: new Date().toISOString(),
164
+ } as never);
165
+ }
166
+
167
+ return { errors, items: result.items };
168
+ }
169
+
170
+ // ─── Webhook Handlers ────────────────────────────────────────────────────────
171
+
172
+ /**
173
+ * Handle a webhook for Garmin activities (push or ping mode).
174
+ */
175
+ export const handleGarminWebhookActivities = action({
176
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
177
+ handler: async (ctx, args): Promise<WebhookResult> => {
178
+ const shouldIngest = args.autoIngest !== false;
179
+ if (isWebhookPushMode(args.payload)) {
180
+ const pushResult = garminActivityPushPayloadSchema.safeParse(args.payload);
181
+ if (pushResult.success && pushResult.data.activities.length > 0) {
182
+ const result: ProcessResult = await ctx.runAction(
183
+ internal.garmin.private.processActivityPushPayload,
184
+ { payload: args.payload },
185
+ );
186
+ return shouldIngest
187
+ ? await ingestAndUpdate(ctx, result, api.public.ingestActivity)
188
+ : toWebhookResult(result);
189
+ }
190
+ } else {
191
+ const pingResult = garminActivityPingPayloadSchema.safeParse(args.payload);
192
+ if (pingResult.success && pingResult.data.activities.length > 0) {
193
+ return await ctx.runAction(
194
+ internal.garmin.private.processActivityPingPayload,
195
+ { payload: args.payload },
196
+ );
197
+ }
198
+ }
199
+
200
+ console.warn(
201
+ `[garmin:webhook:activities] Payload matched neither ping nor push schema`,
202
+ );
203
+ return { errors: [], items: [] };
204
+ },
205
+ });
206
+
207
+ /**
208
+ * Handle a webhook for Garmin activity details (push or ping mode).
209
+ */
210
+ export const handleGarminWebhookActivityDetails = action({
211
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
212
+ handler: async (ctx, args): Promise<WebhookResult> => {
213
+ const shouldIngest = args.autoIngest !== false;
214
+ if (isWebhookPushMode(args.payload)) {
215
+ const pushResult =
216
+ garminActivityDetailsPushPayloadSchema.safeParse(args.payload);
217
+ if (
218
+ pushResult.success &&
219
+ pushResult.data.activityDetails.length > 0
220
+ ) {
221
+ const result: ProcessResult = await ctx.runAction(
222
+ internal.garmin.private.processActivityDetailsPushPayload,
223
+ { payload: args.payload },
224
+ );
225
+ return shouldIngest
226
+ ? await ingestAndUpdate(ctx, result, api.public.ingestActivity)
227
+ : toWebhookResult(result);
228
+ }
229
+ } else {
230
+ const pingResult =
231
+ garminActivityDetailsPingPayloadSchema.safeParse(args.payload);
232
+ if (
233
+ pingResult.success &&
234
+ pingResult.data.activityDetails.length > 0
235
+ ) {
236
+ return await ctx.runAction(
237
+ internal.garmin.private.processActivityDetailsPingPayload,
238
+ { payload: args.payload },
239
+ );
240
+ }
241
+ }
242
+
243
+ console.warn(
244
+ `[garmin:webhook:activityDetails] Payload matched neither ping nor push schema`,
245
+ );
246
+ return { errors: [], items: [] };
247
+ },
248
+ });
249
+
250
+ /**
251
+ * Handle a webhook for Garmin manually updated activities (push or ping mode).
252
+ */
253
+ export const handleGarminWebhookManuallyUpdatedActivities = action({
254
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
255
+ handler: async (ctx, args): Promise<WebhookResult> => {
256
+ const shouldIngest = args.autoIngest !== false;
257
+ if (isWebhookPushMode(args.payload)) {
258
+ const pushResult =
259
+ garminManuallyUpdatedActivitiesPushPayloadSchema.safeParse(
260
+ args.payload,
261
+ );
262
+ if (
263
+ pushResult.success &&
264
+ pushResult.data.manuallyUpdatedActivities.length > 0
265
+ ) {
266
+ const result: ProcessResult = await ctx.runAction(
267
+ internal.garmin.private
268
+ .processManuallyUpdatedActivitiesPushPayload,
269
+ { payload: args.payload },
270
+ );
271
+ return shouldIngest
272
+ ? await ingestAndUpdate(ctx, result, api.public.ingestActivity)
273
+ : toWebhookResult(result);
274
+ }
275
+ } else {
276
+ const pingResult =
277
+ garminManuallyUpdatedActivitiesPingPayloadSchema.safeParse(
278
+ args.payload,
279
+ );
280
+ if (
281
+ pingResult.success &&
282
+ pingResult.data.manuallyUpdatedActivities.length > 0
283
+ ) {
284
+ return await ctx.runAction(
285
+ internal.garmin.private
286
+ .processManuallyUpdatedActivitiesPingPayload,
287
+ { payload: args.payload },
288
+ );
289
+ }
290
+ }
291
+
292
+ console.warn(
293
+ `[garmin:webhook:manuallyUpdatedActivities] Payload matched neither ping nor push schema`,
294
+ );
295
+ return { errors: [], items: [] };
296
+ },
297
+ });
298
+
299
+ /**
300
+ * Handle a webhook for Garmin Move IQ auto-detected activities (push or ping mode).
301
+ */
302
+ export const handleGarminWebhookMoveIQ = action({
303
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
304
+ handler: async (ctx, args): Promise<WebhookResult> => {
305
+ const shouldIngest = args.autoIngest !== false;
306
+ if (isWebhookPushMode(args.payload)) {
307
+ const pushResult =
308
+ garminMoveIQPushPayloadSchema.safeParse(args.payload);
309
+ if (
310
+ pushResult.success &&
311
+ pushResult.data.moveIQActivities.length > 0
312
+ ) {
313
+ const result: ProcessResult = await ctx.runAction(
314
+ internal.garmin.private.processMoveIQPushPayload,
315
+ { payload: args.payload },
316
+ );
317
+ return shouldIngest
318
+ ? await ingestAndUpdate(ctx, result, api.public.ingestActivity)
319
+ : toWebhookResult(result);
320
+ }
321
+ } else {
322
+ const pingResult =
323
+ garminMoveIQPingPayloadSchema.safeParse(args.payload);
324
+ if (
325
+ pingResult.success &&
326
+ pingResult.data.moveIQActivities.length > 0
327
+ ) {
328
+ return await ctx.runAction(
329
+ internal.garmin.private.processMoveIQPingPayload,
330
+ { payload: args.payload },
331
+ );
332
+ }
333
+ }
334
+
335
+ console.warn(
336
+ `[garmin:webhook:moveIQ] Payload matched neither ping nor push schema`,
337
+ );
338
+ return { errors: [], items: [] };
339
+ },
340
+ });
341
+
342
+ /**
343
+ * Handle a webhook for Garmin blood pressure summaries (push or ping mode).
344
+ */
345
+ export const handleGarminWebhookBloodPressures = action({
346
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
347
+ handler: async (ctx, args): Promise<WebhookResult> => {
348
+ const shouldIngest = args.autoIngest !== false;
349
+ if (isWebhookPushMode(args.payload)) {
350
+ const pushResult =
351
+ garminBloodPressurePushPayloadSchema.safeParse(args.payload);
352
+ if (
353
+ pushResult.success &&
354
+ pushResult.data.bloodPressures.length > 0
355
+ ) {
356
+ const result: ProcessResult = await ctx.runAction(
357
+ internal.garmin.private.processBloodPressurePushPayload,
358
+ { payload: args.payload },
359
+ );
360
+ return shouldIngest
361
+ ? await ingestAndUpdate(ctx, result, api.public.ingestBody)
362
+ : toWebhookResult(result);
363
+ }
364
+ } else {
365
+ const pingResult =
366
+ garminBloodPressurePingPayloadSchema.safeParse(args.payload);
367
+ if (
368
+ pingResult.success &&
369
+ pingResult.data.bloodPressures.length > 0
370
+ ) {
371
+ return await ctx.runAction(
372
+ internal.garmin.private.processBloodPressurePingPayload,
373
+ { payload: args.payload },
374
+ );
375
+ }
376
+ }
377
+
378
+ console.warn(
379
+ `[garmin:webhook:bloodPressures] Payload matched neither ping nor push schema`,
380
+ );
381
+ return { errors: [], items: [] };
382
+ },
383
+ });
384
+
385
+ /**
386
+ * Handle a webhook for Garmin body composition summaries (push or ping mode).
387
+ */
388
+ export const handleGarminWebhookBodyCompositions = action({
389
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
390
+ handler: async (ctx, args): Promise<WebhookResult> => {
391
+ const shouldIngest = args.autoIngest !== false;
392
+ if (isWebhookPushMode(args.payload)) {
393
+ const pushResult =
394
+ garminBodyCompositionsPushPayloadSchema.safeParse(args.payload);
395
+ if (
396
+ pushResult.success &&
397
+ pushResult.data.bodyComps.length > 0
398
+ ) {
399
+ const result: ProcessResult = await ctx.runAction(
400
+ internal.garmin.private.processBodyCompositionsPushPayload,
401
+ { payload: args.payload },
402
+ );
403
+ return shouldIngest
404
+ ? await ingestAndUpdate(ctx, result, api.public.ingestBody)
405
+ : toWebhookResult(result);
406
+ }
407
+ } else {
408
+ const pingResult =
409
+ garminBodyCompositionsPingPayloadSchema.safeParse(args.payload);
410
+ if (
411
+ pingResult.success &&
412
+ pingResult.data.bodyComps.length > 0
413
+ ) {
414
+ return await ctx.runAction(
415
+ internal.garmin.private.processBodyCompositionsPingPayload,
416
+ { payload: args.payload },
417
+ );
418
+ }
419
+ }
420
+
421
+ console.warn(
422
+ `[garmin:webhook:bodyCompositions] Payload matched neither ping nor push schema`,
423
+ );
424
+ return { errors: [], items: [] };
425
+ },
426
+ });
427
+
428
+ /**
429
+ * Handle a webhook for Garmin daily summaries (push or ping mode).
430
+ */
431
+ export const handleGarminWebhookDailies = action({
432
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
433
+ handler: async (ctx, args): Promise<WebhookResult> => {
434
+ const shouldIngest = args.autoIngest !== false;
435
+ if (isWebhookPushMode(args.payload)) {
436
+ const pushResult =
437
+ garminDailiesPushPayloadSchema.safeParse(args.payload);
438
+ if (
439
+ pushResult.success &&
440
+ pushResult.data.dailies.length > 0
441
+ ) {
442
+ const result: ProcessResult = await ctx.runAction(
443
+ internal.garmin.private.processDailiesPushPayload,
444
+ { payload: args.payload },
445
+ );
446
+ return shouldIngest
447
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
448
+ : toWebhookResult(result);
449
+ }
450
+ } else {
451
+ const pingResult =
452
+ garminDailiesPingPayloadSchema.safeParse(args.payload);
453
+ if (
454
+ pingResult.success &&
455
+ pingResult.data.dailies.length > 0
456
+ ) {
457
+ return await ctx.runAction(
458
+ internal.garmin.private.processDailiesPingPayload,
459
+ { payload: args.payload },
460
+ );
461
+ }
462
+ }
463
+
464
+ console.warn(
465
+ `[garmin:webhook:dailies] Payload matched neither ping nor push schema`,
466
+ );
467
+ return { errors: [], items: [] };
468
+ },
469
+ });
470
+
471
+ /**
472
+ * Handle a webhook for Garmin epoch summaries (push or ping mode).
473
+ */
474
+ export const handleGarminWebhookEpochs = action({
475
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
476
+ handler: async (ctx, args): Promise<WebhookResult> => {
477
+ const shouldIngest = args.autoIngest !== false;
478
+ if (isWebhookPushMode(args.payload)) {
479
+ const pushResult =
480
+ garminEpochPushPayloadSchema.safeParse(args.payload);
481
+ if (
482
+ pushResult.success &&
483
+ pushResult.data.epochs.length > 0
484
+ ) {
485
+ const result: ProcessResult = await ctx.runAction(
486
+ internal.garmin.private.processEpochPushPayload,
487
+ { payload: args.payload },
488
+ );
489
+ return shouldIngest
490
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
491
+ : toWebhookResult(result);
492
+ }
493
+ } else {
494
+ const pingResult =
495
+ garminEpochPingPayloadSchema.safeParse(args.payload);
496
+ if (
497
+ pingResult.success &&
498
+ pingResult.data.epochs.length > 0
499
+ ) {
500
+ return await ctx.runAction(
501
+ internal.garmin.private.processEpochPingPayload,
502
+ { payload: args.payload },
503
+ );
504
+ }
505
+ }
506
+
507
+ console.warn(
508
+ `[garmin:webhook:epochs] Payload matched neither ping nor push schema`,
509
+ );
510
+ return { errors: [], items: [] };
511
+ },
512
+ });
513
+
514
+ /**
515
+ * Handle a webhook for Garmin health snapshot summaries (push or ping mode).
516
+ */
517
+ export const handleGarminWebhookHealthSnapshot = action({
518
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
519
+ handler: async (ctx, args): Promise<WebhookResult> => {
520
+ const shouldIngest = args.autoIngest !== false;
521
+ if (isWebhookPushMode(args.payload)) {
522
+ const pushResult =
523
+ garminHealthSnapshotPushPayloadSchema.safeParse(args.payload);
524
+ if (
525
+ pushResult.success &&
526
+ pushResult.data.healthSnapshot.length > 0
527
+ ) {
528
+ const result: ProcessResult = await ctx.runAction(
529
+ internal.garmin.private.processHealthSnapshotPushPayload,
530
+ { payload: args.payload },
531
+ );
532
+ return shouldIngest
533
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
534
+ : toWebhookResult(result);
535
+ }
536
+ } else {
537
+ const pingResult =
538
+ garminHealthSnapshotPingPayloadSchema.safeParse(args.payload);
539
+ if (
540
+ pingResult.success &&
541
+ pingResult.data.healthSnapshot.length > 0
542
+ ) {
543
+ return await ctx.runAction(
544
+ internal.garmin.private.processHealthSnapshotPingPayload,
545
+ { payload: args.payload },
546
+ );
547
+ }
548
+ }
549
+
550
+ console.warn(
551
+ `[garmin:webhook:healthSnapshot] Payload matched neither ping nor push schema`,
552
+ );
553
+ return { errors: [], items: [] };
554
+ },
555
+ });
556
+
557
+ /**
558
+ * Handle a webhook for Garmin sleep summaries (push or ping mode).
559
+ */
560
+ export const handleGarminWebhookSleeps = action({
561
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
562
+ handler: async (ctx, args): Promise<WebhookResult> => {
563
+ const shouldIngest = args.autoIngest !== false;
564
+ if (isWebhookPushMode(args.payload)) {
565
+ const pushResult =
566
+ garminSleepsPushPayloadSchema.safeParse(args.payload);
567
+ if (
568
+ pushResult.success &&
569
+ pushResult.data.sleeps.length > 0
570
+ ) {
571
+ const result: ProcessResult = await ctx.runAction(
572
+ internal.garmin.private.processSleepsPushPayload,
573
+ { payload: args.payload },
574
+ );
575
+ return shouldIngest
576
+ ? await ingestAndUpdate(ctx, result, api.public.ingestSleep)
577
+ : toWebhookResult(result);
578
+ }
579
+ } else {
580
+ const pingResult =
581
+ garminSleepsPingPayloadSchema.safeParse(args.payload);
582
+ if (
583
+ pingResult.success &&
584
+ pingResult.data.sleeps.length > 0
585
+ ) {
586
+ return await ctx.runAction(
587
+ internal.garmin.private.processSleepsPingPayload,
588
+ { payload: args.payload },
589
+ );
590
+ }
591
+ }
592
+
593
+ console.warn(
594
+ `[garmin:webhook:sleeps] Payload matched neither ping nor push schema`,
595
+ );
596
+ return { errors: [], items: [] };
597
+ },
598
+ });
599
+
600
+ /**
601
+ * Handle a webhook for Garmin skin temperature summaries (push or ping mode).
602
+ */
603
+ export const handleGarminWebhookSkinTemp = action({
604
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
605
+ handler: async (ctx, args): Promise<WebhookResult> => {
606
+ const shouldIngest = args.autoIngest !== false;
607
+ if (isWebhookPushMode(args.payload)) {
608
+ const pushResult =
609
+ garminSkinTemperaturePushPayloadSchema.safeParse(args.payload);
610
+ if (
611
+ pushResult.success &&
612
+ pushResult.data.skinTemp.length > 0
613
+ ) {
614
+ const result: ProcessResult = await ctx.runAction(
615
+ internal.garmin.private.processSkinTemperaturePushPayload,
616
+ { payload: args.payload },
617
+ );
618
+ return shouldIngest
619
+ ? await ingestAndUpdate(ctx, result, api.public.ingestBody)
620
+ : toWebhookResult(result);
621
+ }
622
+ } else {
623
+ const pingResult =
624
+ garminSkinTemperaturePingPayloadSchema.safeParse(args.payload);
625
+ if (
626
+ pingResult.success &&
627
+ pingResult.data.skinTemp.length > 0
628
+ ) {
629
+ return await ctx.runAction(
630
+ internal.garmin.private.processSkinTemperaturePingPayload,
631
+ { payload: args.payload },
632
+ );
633
+ }
634
+ }
635
+
636
+ console.warn(
637
+ `[garmin:webhook:skinTemperature] Payload matched neither ping nor push schema`,
638
+ );
639
+ return { errors: [], items: [] };
640
+ },
641
+ });
642
+
643
+ /**
644
+ * Handle a webhook for Garmin user metrics (push or ping mode).
645
+ */
646
+ export const handleGarminWebhookUserMetrics = action({
647
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
648
+ handler: async (ctx, args): Promise<WebhookResult> => {
649
+ const shouldIngest = args.autoIngest !== false;
650
+ if (isWebhookPushMode(args.payload)) {
651
+ const pushResult =
652
+ garminUserMetricsPushPayloadSchema.safeParse(args.payload);
653
+ if (
654
+ pushResult.success &&
655
+ pushResult.data.userMetrics.length > 0
656
+ ) {
657
+ const result: ProcessResult = await ctx.runAction(
658
+ internal.garmin.private.processUserMetricsPushPayload,
659
+ { payload: args.payload },
660
+ );
661
+ return shouldIngest
662
+ ? await ingestAndUpdate(ctx, result, api.public.ingestBody)
663
+ : toWebhookResult(result);
664
+ }
665
+ } else {
666
+ const pingResult =
667
+ garminUserMetricsPingPayloadSchema.safeParse(args.payload);
668
+ if (
669
+ pingResult.success &&
670
+ pingResult.data.userMetrics.length > 0
671
+ ) {
672
+ return await ctx.runAction(
673
+ internal.garmin.private.processUserMetricsPingPayload,
674
+ { payload: args.payload },
675
+ );
676
+ }
677
+ }
678
+
679
+ console.warn(
680
+ `[garmin:webhook:userMetrics] Payload matched neither ping nor push schema`,
681
+ );
682
+ return { errors: [], items: [] };
683
+ },
684
+ });
685
+
686
+ /**
687
+ * Handle a webhook for Garmin menstrual cycle tracking (push or ping mode).
688
+ */
689
+ export const handleGarminWebhookMenstrualCycleTracking = action({
690
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
691
+ handler: async (ctx, args): Promise<WebhookResult> => {
692
+ const shouldIngest = args.autoIngest !== false;
693
+ if (isWebhookPushMode(args.payload)) {
694
+ const pushResult =
695
+ garminMenstrualCycleTrackingPushPayloadSchema.safeParse(args.payload);
696
+ if (
697
+ pushResult.success &&
698
+ pushResult.data.mct.length > 0
699
+ ) {
700
+ const result: ProcessResult = await ctx.runAction(
701
+ internal.garmin.private.processMenstrualCycleTrackingPushPayload,
702
+ { payload: args.payload },
703
+ );
704
+ return shouldIngest
705
+ ? await ingestAndUpdate(ctx, result, api.public.ingestMenstruation)
706
+ : toWebhookResult(result);
707
+ }
708
+ } else {
709
+ const pingResult =
710
+ garminMenstrualCycleTrackingPingPayloadSchema.safeParse(args.payload);
711
+ if (
712
+ pingResult.success &&
713
+ pingResult.data.mct.length > 0
714
+ ) {
715
+ return await ctx.runAction(
716
+ internal.garmin.private.processMenstrualCycleTrackingPingPayload,
717
+ { payload: args.payload },
718
+ );
719
+ }
720
+ }
721
+
722
+ console.warn(
723
+ `[garmin:webhook:menstrualCycleTracking] Payload matched neither ping nor push schema`,
724
+ );
725
+ return { errors: [], items: [] };
726
+ },
727
+ });
728
+
729
+ /**
730
+ * Handle a webhook for Garmin HRV summaries (push or ping mode).
731
+ */
732
+ export const handleGarminWebhookHRVSummary = action({
733
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
734
+ handler: async (ctx, args): Promise<WebhookResult> => {
735
+ const shouldIngest = args.autoIngest !== false;
736
+ if (isWebhookPushMode(args.payload)) {
737
+ const pushResult =
738
+ garminHRVSummaryPushPayloadSchema.safeParse(args.payload);
739
+ if (
740
+ pushResult.success &&
741
+ pushResult.data.hrv.length > 0
742
+ ) {
743
+ const result: ProcessResult = await ctx.runAction(
744
+ internal.garmin.private.processHRVSummaryPushPayload,
745
+ { payload: args.payload },
746
+ );
747
+ return shouldIngest
748
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
749
+ : toWebhookResult(result);
750
+ }
751
+ } else {
752
+ const pingResult =
753
+ garminHRVSummaryPingPayloadSchema.safeParse(args.payload);
754
+ if (
755
+ pingResult.success &&
756
+ pingResult.data.hrv.length > 0
757
+ ) {
758
+ return await ctx.runAction(
759
+ internal.garmin.private.processHRVSummaryPingPayload,
760
+ { payload: args.payload },
761
+ );
762
+ }
763
+ }
764
+
765
+ console.warn(
766
+ `[garmin:webhook:hrvSummary] Payload matched neither ping nor push schema`,
767
+ );
768
+ return { errors: [], items: [] };
769
+ },
770
+ });
771
+
772
+ /**
773
+ * Handle a webhook for Garmin stress detail summaries (push or ping mode).
774
+ */
775
+ export const handleGarminWebhookStress = action({
776
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
777
+ handler: async (ctx, args): Promise<WebhookResult> => {
778
+ const shouldIngest = args.autoIngest !== false;
779
+ if (isWebhookPushMode(args.payload)) {
780
+ const pushResult =
781
+ garminStressPushPayloadSchema.safeParse(args.payload);
782
+ if (
783
+ pushResult.success &&
784
+ pushResult.data.stressDetails.length > 0
785
+ ) {
786
+ const result: ProcessResult = await ctx.runAction(
787
+ internal.garmin.private.processStressPushPayload,
788
+ { payload: args.payload },
789
+ );
790
+ return shouldIngest
791
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
792
+ : toWebhookResult(result);
793
+ }
794
+ } else {
795
+ const pingResult =
796
+ garminStressPingPayloadSchema.safeParse(args.payload);
797
+ if (
798
+ pingResult.success &&
799
+ pingResult.data.stressDetails.length > 0
800
+ ) {
801
+ return await ctx.runAction(
802
+ internal.garmin.private.processStressPingPayload,
803
+ { payload: args.payload },
804
+ );
805
+ }
806
+ }
807
+
808
+ console.warn(
809
+ `[garmin:webhook:stressDetails] Payload matched neither ping nor push schema`,
810
+ );
811
+ return { errors: [], items: [] };
812
+ },
813
+ });
814
+
815
+ /**
816
+ * Handle a webhook for Garmin pulse oximetry (SpO2) summaries (push or ping mode).
817
+ */
818
+ export const handleGarminWebhookPulseOx = action({
819
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
820
+ handler: async (ctx, args): Promise<WebhookResult> => {
821
+ const shouldIngest = args.autoIngest !== false;
822
+ if (isWebhookPushMode(args.payload)) {
823
+ const pushResult =
824
+ garminPulseOxPushPayloadSchema.safeParse(args.payload);
825
+ if (
826
+ pushResult.success &&
827
+ pushResult.data.pulseox.length > 0
828
+ ) {
829
+ const result: ProcessResult = await ctx.runAction(
830
+ internal.garmin.private.processPulseOxPushPayload,
831
+ { payload: args.payload },
832
+ );
833
+ return shouldIngest
834
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
835
+ : toWebhookResult(result);
836
+ }
837
+ } else {
838
+ const pingResult =
839
+ garminPulseOxPingPayloadSchema.safeParse(args.payload);
840
+ if (
841
+ pingResult.success &&
842
+ pingResult.data.pulseox.length > 0
843
+ ) {
844
+ return await ctx.runAction(
845
+ internal.garmin.private.processPulseOxPingPayload,
846
+ { payload: args.payload },
847
+ );
848
+ }
849
+ }
850
+
851
+ console.warn(
852
+ `[garmin:webhook:pulseOx] Payload matched neither ping nor push schema`,
853
+ );
854
+ return { errors: [], items: [] };
855
+ },
856
+ });
857
+
858
+ /**
859
+ * Handle a webhook for Garmin respiration summaries (push or ping mode).
860
+ */
861
+ export const handleGarminWebhookRespiration = action({
862
+ args: { payload: v.any(), autoIngest: v.optional(v.boolean()) },
863
+ handler: async (ctx, args): Promise<WebhookResult> => {
864
+ const shouldIngest = args.autoIngest !== false;
865
+ if (isWebhookPushMode(args.payload)) {
866
+ const pushResult =
867
+ garminRespirationPushPayloadSchema.safeParse(args.payload);
868
+ if (
869
+ pushResult.success &&
870
+ pushResult.data.allDayRespiration.length > 0
871
+ ) {
872
+ const result: ProcessResult = await ctx.runAction(
873
+ internal.garmin.private.processRespirationPushPayload,
874
+ { payload: args.payload },
875
+ );
876
+ return shouldIngest
877
+ ? await ingestAndUpdate(ctx, result, api.public.ingestDaily)
878
+ : toWebhookResult(result);
879
+ }
880
+ } else {
881
+ const pingResult =
882
+ garminRespirationPingPayloadSchema.safeParse(args.payload);
883
+ if (
884
+ pingResult.success &&
885
+ pingResult.data.allDayRespiration.length > 0
886
+ ) {
887
+ return await ctx.runAction(
888
+ internal.garmin.private.processRespirationPingPayload,
889
+ { payload: args.payload },
890
+ );
891
+ }
892
+ }
893
+
894
+ console.warn(
895
+ `[garmin:webhook:respiration] Payload matched neither ping nor push schema`,
896
+ );
897
+ return { errors: [], items: [] };
898
+ },
858
899
  });