Files
ipal-kit/dist/modules/integrations/status.js
T
2026-10-15 15:27:43 +02:00

92 lines
2.8 KiB
JavaScript

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: {
clientProps: {
label,
statusField
},
path: '@intecion/ipal-kit/client#IntegrationStatusBanner'
}
}
}
};
}
/**
* 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 = {
checkedAt: now.toISOString(),
code: next.code ?? null,
message: next.message ?? null,
since: changed ? now.toISOString() : previous?.since ?? now.toISOString(),
state: next.state
};
try {
await payload.updateGlobal({
slug: SITE_INTEGRATIONS,
context: {
ipalIntegrationStatus: true
},
data: {
[field]: status
},
depth: 0
});
} catch (err) {
console.warn(`[ipal] could not record ${field}:`, err instanceof Error ? err.message : err);
}
}
//# sourceMappingURL=status.js.map