17 lines
1.0 KiB
TypeScript
17 lines
1.0 KiB
TypeScript
import { z } from "zod"
|
|
import type { ChangeEvidence } from "./action-types.js"
|
|
|
|
/** Internal value calculation only; evidence comes from action.prepare, never
|
|
* caller JSON. Generic plans may outlive their preparation access credential.
|
|
* A missing result refuses a time-bound plan before storage. */
|
|
export function preparedPlanExpiry(operationId: string, section: string, evidence: Pick<ChangeEvidence, "validUntil">): Date | null {
|
|
const now = Date.now(), ordinaryExpiry = now + (section === "network" ? 300000 : 900000)
|
|
if (operationId !== "servers.poll" && operationId !== "routing.community.switch") return new Date(ordinaryExpiry)
|
|
const validUntil = evidence.validUntil
|
|
if (operationId === "routing.community.switch" && validUntil === undefined) return new Date(ordinaryExpiry)
|
|
const validity = z.string().datetime({ offset: true }).safeParse(validUntil)
|
|
const bound = validity.success ? Date.parse(validity.data) : NaN
|
|
if (!Number.isFinite(bound) || bound <= now) return null
|
|
return new Date(Math.min(ordinaryExpiry, bound))
|
|
}
|