Files
ipal-kit/dist/globals/SiteIntegrations/index.js
T
2026-10-15 15:53:32 +02:00

83 lines
3.2 KiB
JavaScript

import { isAdmin } from '../../modules/access/index.js';
import { analyticsFields } from './fields/analytics.js';
import { smtpFields } from './fields/smtp.js';
import { turnstileFields } from './fields/turnstile.js';
/**
* Builds the SiteIntegrations global.
*
* Holds third-party service credentials. Access is enforced at the global
* level — the whole global requires an authenticated user — so secrets stay
* out of anonymous API responses while remaining editable in the admin panel
* and readable via the server-side Local API. (Field-level read:false was
* avoided because it also hides fields from the admin UI, making them
* impossible to enter.)
*
* Unnamed tabs keep data flat (siteIntegrations.ga4MeasurementId).
*/ export function buildSiteIntegrations({ additionalFields } = {}) {
return {
slug: 'site-integrations',
access: {
// Admin-only — secrets live here. Anonymous and non-admin users get
// nothing through the API; admins read/edit in the panel and via Local API.
read: ({ req: { user } })=>isAdmin(user),
update: ({ req: { user } })=>isAdmin(user)
},
admin: {
group: 'Settings'
},
fields: [
{
type: 'tabs',
tabs: [
{
fields: analyticsFields,
label: 'Analytics'
},
{
fields: turnstileFields,
label: 'Turnstile'
},
{
fields: smtpFields,
label: 'SMTP'
},
...additionalFields?.length ? [
{
fields: additionalFields,
label: 'Custom'
}
] : []
]
}
],
hooks: {
beforeChange: [
// Keys edited in the panel → clear that integration's old status, so a stale
// "not working" banner doesn't linger until the next form submission.
({ context, data, originalDoc })=>{
if (context?.ipalIntegrationStatus || !data) {
return data;
}
const changed = (k)=>k in data && data[k] !== originalDoc?.[k];
const cleared = {
checkedAt: null,
code: null,
message: null,
since: null,
state: null
};
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {
data.turnstileStatus = cleared;
}
if (Object.keys(data).some((k)=>(k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
data.emailStatus = cleared;
}
return data;
}
]
},
label: 'Site Integrations'
};
}
//# sourceMappingURL=index.js.map