const REFRESH_MS = 10 * 60_000; const SITE_INTEGRATIONS = 'site-integrations'; /** Hidden group field holding the status — add next to the integration's fields. */ export function buildIntegrationStatusField(name) { return { name, type: 'group', admin: { hidden: true }, fields: [ { name: 'state', type: 'select', options: [ 'ok', 'degraded' ] }, { name: 'code', type: 'text' }, { name: 'message', type: 'text' }, { name: 'since', type: 'date' }, { name: 'checkedAt', type: 'date' } ] }; } /** Read-only banner (UI field) — shows the status group `statusField` on the tab. */ export function buildIntegrationStatusBanner(statusField, label) { return { name: `${statusField}Banner`, type: 'ui', admin: { components: { Field: { path: '@intecion/ipal-kit/client#IntegrationStatusBanner', clientProps: { statusField, label } } } } }; } /** * Persist a status change. Never throws — a status write must not break the * request that triggered it. */ export async function recordIntegrationStatus(payload, field, previous, next) { const now = new Date(); const changed = previous?.state !== next.state || next.state === 'degraded' && previous?.code !== next.code; const stale = next.state === 'degraded' && (!previous?.checkedAt || now.getTime() - new Date(previous.checkedAt).getTime() > REFRESH_MS); // Healthy and already recorded as healthy (or never recorded) → nothing to do. if (!changed && !stale) return; if (next.state === 'ok' && !previous?.state) return; const status = { state: next.state, code: next.code ?? null, message: next.message ?? null, since: changed ? now.toISOString() : previous?.since ?? now.toISOString(), checkedAt: now.toISOString() }; try { await payload.updateGlobal({ slug: SITE_INTEGRATIONS, data: { [field]: status }, depth: 0, context: { ipalIntegrationStatus: true } }); } catch (err) { console.warn(`[ipal] could not record ${field}:`, err instanceof Error ? err.message : err); } } //# sourceMappingURL=status.js.map