/** * Cross Events 3.0 connects itself to the church's Accounts (docs/EVENTS-3.0-PLAN.md ยง9, Hub * 2.5.0): after a heartbeat, when the Hub lists an Accounts for this church and this site isn't * linked to one, it connects (as an admin pressing Connect under Linked products would). Once * only: after it worked (`accounts_auto_connected`), an admin's Disconnect stays as it is. */ import { connectTo } from '../links/connect'; import { linkedProducts } from '../links/store'; import { accountsLink } from './client'; import { getSetting, setSetting, viaAccounts } from './mode'; export const AUTO_KEY = 'accounts_auto_connected'; export async function connectAccountsIfListed(now = new Date()): Promise<'connected' | 'skipped'> { // 3.5.0: a site that signs in through Accounts but has lost its link (before 3.5.0, loading demo // data deleted it) can't sign anyone in, so it reconnects whatever was done before. A site on its // own sign-in keeps an admin's Disconnect. const stranded = (await viaAccounts()) && !(await accountsLink()); if ((await getSetting(AUTO_KEY)) && !stranded) return 'skipped'; if (await accountsLink()) { await setSetting(AUTO_KEY, now.toISOString()); return 'skipped'; } const listed = (await linkedProducts()).find((p) => p.product === 'accounts'); if (!listed) return 'skipped'; await connectTo(listed.installId, { id: null, role: 'system' }, null, now); await setSetting(AUTO_KEY, now.toISOString()); return 'connected'; }