// AK-P16a · Google Calendar integration adapter (catalog presence + health). // // The Apps-Engine catalog needs a registered adapter per app key (drift-free, // registry.test.ts). The HEAVY per-USER bidirectional sync does NOT run through // the generic AppSyncRun path — it lives in the calendar module // (apps/web/app/modules/calendar) keyed by CalendarConnection, because calendar // connections are per-user with leak-protection, not per-tenant DataConnections. // // So this adapter is intentionally thin: it satisfies the contract for catalog // reachability + lets the platform-admin health surface probe a token, and // implements OAuth refresh by delegating to googleCalendarApi. `sync()` reports a // Klartext summary that the per-user engine owns the actual event sync. import { registerIntegrationAdapter, type AdapterResult, type AdapterSyncOutcome, type IntegrationAdapter, type AdapterTokenRefresh } from "@hivegrid/foundation"; import { GoogleCalendarError, listCalendars, refreshAccessToken, resolveGoogleOAuthConfig } from "../google/googleCalendarApi.js"; /** Decrypted credential shape stored (envelope-encrypted) per CalendarConnection. */ export interface GoogleCalendarCreds { accessToken: string; refreshToken: string; expiresAt?: string | null; } function healthFromError(error: unknown): AdapterResult { if (error instanceof GoogleCalendarError) { switch (error.code) { case "auth_token_expired": return { ok: false, health: "broken", messageKey: "calendar.health.tokenExpired" }; case "auth_scope_missing": return { ok: false, health: "broken", messageKey: "calendar.health.scopeMissing" }; case "rate_limit_exceeded": return { ok: true, health: "degraded", messageKey: "calendar.health.rateLimited" }; case "provider_outage": return { ok: false, health: "degraded", messageKey: "calendar.health.providerOutage" }; default: return { ok: false, health: "broken", messageKey: "calendar.health.unknown" }; } } return { ok: false, health: "broken", messageKey: "calendar.health.unknown" }; } async function probe(creds: GoogleCalendarCreds): Promise { if (!creds.accessToken) { return { ok: false, health: "broken", messageKey: "calendar.health.tokenExpired" }; } try { await listCalendars(creds.accessToken); return { ok: true, health: "active", messageKey: "calendar.health.active" }; } catch (error) { return healthFromError(error); } } export const googleCalendarAdapter: IntegrationAdapter = registerIntegrationAdapter({ key: "google-calendar", connectionType: "oauth2", validateConnection: probe, healthCheck: probe, refreshToken: async (creds: GoogleCalendarCreds): Promise> => { const config = resolveGoogleOAuthConfig(); if (!config) { throw new GoogleCalendarError("not_configured"); } const next = await refreshAccessToken(config, creds.refreshToken); return { creds: { accessToken: next.accessToken, refreshToken: next.refreshToken ?? creds.refreshToken, expiresAt: next.expiresAt.toISOString() }, expiresAt: next.expiresAt, }; }, // The per-user calendar engine (webhook + cron driven) owns event sync; the // generic engine sync is a no-op that reports this in Klartext. sync: (): Promise => Promise.resolve({ items: [], summaryKey: "calendar.sync.summary.perUserEngine" }), });