mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat(integrations): add manual sync for Oura, Polar, Strava and Nightscout
These four were the only connected providers with no way to ask for a sync; they ran on their scheduled pass and nothing else. Each gets a POST sync route on the hardened Fitbit and Google Health template: session auth, a 5-per-minute per-user bucket, and a fixed 502 on failure. No body and no full-sync flag, because none of the four entry points has a full-history arm to call. Polar goes through the shared legs entry point so both legs run with the attribution rules built for them. The Nightscout error arm is the one that matters: a base URL carries the API token as a query parameter, so the caught message can hold the user's secret and never reaches the response body. The classified failure is already on the ledger, recorded with the provider's own redaction. Connecting also starts the first sync now. The poll handler has accepted a single-user payload since it was written and never had a producer, so a fresh connection sat empty for up to an hour waiting for the schedule. The Oura and Polar callbacks and the Nightscout connect route enqueue one targeted job each, best effort, with the hourly tick still the safety net. Strava needs none: its callback already enqueues the history backfill, which covers the recent window. The four poll-queue names move into their own module so a request-path caller can name a queue without dragging the whole worker handler graph into the request bundle, following the backfill-queue precedent. Creation, scheduling and handler binding stay in the registrar, and the dead-queue guard follows the declarations.
This commit is contained in:
1 parent
9bbbeb805c
commit
a9f1629f82
17 files changed
+874
-21
No files matched your search
@@ -12,21 +12,34 @@ vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn(async () => null) }));
|
||||
|
||||
vi.mock("@/lib/logging/context", () => ({
|
||||
annotate: vi.fn(),
|
||||
getEvent: () => ({ setError: vi.fn(), setAuth: vi.fn() }),
|
||||
getEvent: () => ({
|
||||
setError: vi.fn(),
|
||||
setAuth: vi.fn(),
|
||||
addWarning: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/auth/audit", () => ({ auditLog: vi.fn() }));
|
||||
vi.mock("@/lib/crypto", () => ({ encrypt: (s: string) => `enc:${s}` }));
|
||||
vi.mock("@/lib/integrations/status", () => ({ markReconnected: vi.fn() }));
|
||||
|
||||
const { exchangeMock, getCredsMock, matchMock, verifyMock } = vi.hoisted(
|
||||
() => ({
|
||||
exchangeMock: vi.fn(),
|
||||
getCredsMock: vi.fn(),
|
||||
matchMock: vi.fn(),
|
||||
verifyMock: vi.fn(),
|
||||
}),
|
||||
);
|
||||
const {
|
||||
exchangeMock,
|
||||
getCredsMock,
|
||||
matchMock,
|
||||
verifyMock,
|
||||
bossSend,
|
||||
bossMock,
|
||||
} = vi.hoisted(() => ({
|
||||
exchangeMock: vi.fn(),
|
||||
getCredsMock: vi.fn(),
|
||||
matchMock: vi.fn(),
|
||||
verifyMock: vi.fn(),
|
||||
bossSend: vi.fn(),
|
||||
bossMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: bossMock }));
|
||||
|
||||
vi.mock("@/lib/oura/client", () => ({ exchangeCode: exchangeMock }));
|
||||
vi.mock("@/lib/oura/credentials", () => ({
|
||||
@@ -75,6 +88,8 @@ beforeEach(() => {
|
||||
exchangeMock.mockResolvedValue({ access_token: "at", refresh_token: "rt" });
|
||||
userUpdate.mockResolvedValue({});
|
||||
(getSession as ReturnType<typeof vi.fn>).mockResolvedValue(null);
|
||||
bossSend.mockResolvedValue("job-1");
|
||||
bossMock.mockReturnValue({ send: bossSend });
|
||||
});
|
||||
|
||||
describe("GET /api/oura/callback", () => {
|
||||
@@ -123,3 +138,36 @@ describe("GET /api/oura/callback", () => {
|
||||
expect(res.headers.get("location")).toContain("oura=connected");
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* v1.32.28 — connecting starts a sync straight away. The poll handler has
|
||||
* accepted a single-user payload since it was written; this callback is the
|
||||
* producer that was missing, and without it a fresh connection showed an empty
|
||||
* card for up to an hour.
|
||||
*/
|
||||
describe("GET /api/oura/callback — connect-time first sync", () => {
|
||||
it("enqueues exactly one targeted oura-sync job for the connecting user", async () => {
|
||||
const res = await GET(makeReq({ code: "c", state: "S", cookie: "S" }));
|
||||
|
||||
expect(res.headers.get("location")).toContain("oura=connected");
|
||||
expect(bossSend).toHaveBeenCalledTimes(1);
|
||||
expect(bossSend).toHaveBeenCalledWith("oura-sync", { userId: "u1" });
|
||||
});
|
||||
|
||||
it("still connects when no boss instance is available", async () => {
|
||||
bossMock.mockReturnValue(null);
|
||||
|
||||
const res = await GET(makeReq({ code: "c", state: "S", cookie: "S" }));
|
||||
|
||||
expect(res.headers.get("location")).toContain("oura=connected");
|
||||
expect(bossSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still connects when the enqueue itself rejects", async () => {
|
||||
bossSend.mockRejectedValue(new Error("queue unreachable"));
|
||||
|
||||
const res = await GET(makeReq({ code: "c", state: "S", cookie: "S" }));
|
||||
|
||||
expect(res.headers.get("location")).toContain("oura=connected");
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
verifySignedState,
|
||||
} from "@/lib/oauth/signed-state";
|
||||
import { markReconnected } from "@/lib/integrations/status";
|
||||
import { OURA_SYNC_QUEUE } from "@/lib/jobs/integration-poll-queues";
|
||||
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
/**
|
||||
@@ -82,6 +84,21 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
await auditLog("oura.connect", { userId });
|
||||
await markReconnected(userId, "oura");
|
||||
|
||||
// Start the first sync now instead of leaving the user staring at an empty
|
||||
// card until the next hourly tick. The poll handler has accepted a
|
||||
// single-user payload since it was written; this is the producer that was
|
||||
// missing. Reconnects ride the same path. Best-effort by design: the hourly
|
||||
// cron remains the safety net, so a missing boss instance must never strand
|
||||
// the redirect.
|
||||
const boss = getGlobalBoss();
|
||||
if (boss) {
|
||||
await boss
|
||||
.send(OURA_SYNC_QUEUE, { userId })
|
||||
.catch((err) =>
|
||||
getEvent()?.addWarning(`oura-sync connect enqueue failed: ${err}`),
|
||||
);
|
||||
}
|
||||
|
||||
const response = NextResponse.redirect(
|
||||
new URL(
|
||||
"/settings/integrations?oura=connected",
|
||||
|
||||
Reference in new issue
Block a user