// AK-P16a · webhook-subscription renewal decision (PURE) — M25 Block E. // // Google Calendar push channels expire (~1 week). A worker cron renews channels // that will lapse soon, BEFORE they go silent. This module holds only the pure // decision (no DB/network) so the cron stays thin and the timing is unit-tested. // The renewal threshold (default 24h) gives ample margin against the cron cadence. export const DEFAULT_RENEWAL_THRESHOLD_MS = 24 * 60 * 60 * 1000; // 24 hours export interface WebhookRenewalInput { /** Current provider channel expiry (null = no active subscription). */ readonly expiresAt: Date | null; readonly now: Date; readonly thresholdMs?: number; } export interface WebhookRenewalDecision { readonly shouldRenew: boolean; /** Stable reason code (also a Klartext i18n suffix). */ readonly reason: "no_subscription" | "expiring_soon" | "still_valid"; } /** * Decide whether a connection's push channel needs renewal now. Renew when there * is no subscription yet, or it expires within `thresholdMs`. */ export function decideWebhookRenewal(input: WebhookRenewalInput): WebhookRenewalDecision { if (input.expiresAt === null) { return { shouldRenew: true, reason: "no_subscription" }; } const threshold = input.thresholdMs ?? DEFAULT_RENEWAL_THRESHOLD_MS; const msLeft = input.expiresAt.getTime() - input.now.getTime(); if (msLeft <= threshold) { return { shouldRenew: true, reason: "expiring_soon" }; } return { shouldRenew: false, reason: "still_valid" }; }