Planning Center: compare a People list with the site's people and apply what an admin picks
Migration 085 adds people.pco_id and Site settings' list. Data -> Planning Center fetches the list, suggests matches (linked, same email, same name), shows each field that would change (Planning Center wins), and applies the chosen links, updates and new unpublished people in one transaction. Needs PCO_APP_ID and PCO_SECRET. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a10ff6d75f
commit
e8aab4d7ec
12 changed files with 607 additions and 7 deletions
79
server/src/routes/pco.js
Normal file
79
server/src/routes/pco.js
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
PLANNING CENTER ROUTES: Data → Planning Center, mounted under
|
||||
/api/admin, admins and up (planningCenter.js)
|
||||
|
||||
GET /pco whether it's set up, and the list's id
|
||||
POST /pco/compare the list, each member with its match and what
|
||||
would change; nothing is written
|
||||
POST /pco/apply { decisions: [{ pco, action, person_id, update }] }
|
||||
what the admin chose, in one transaction
|
||||
|
||||
The comparison is fetched again for apply's members rather than
|
||||
trusting the browser's copy of Planning Center's values: only the
|
||||
choices (which person, link or create, update or not) come from it.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Hono } from "hono";
|
||||
|
||||
import { requireRole } from "../auth.js";
|
||||
import { tx } from "../db.js";
|
||||
import { PcoError, apply, compare, listPeople, pcoReady } from "../planningCenter.js";
|
||||
import { NO_STORE } from "../shape.js";
|
||||
|
||||
const pco = new Hono();
|
||||
|
||||
pco.use("/pco", requireRole("admin"));
|
||||
pco.use("/pco/*", requireRole("admin"));
|
||||
|
||||
const listId = (db) => db.prepare(`SELECT pco_list_id FROM site_settings WHERE id = 'site'`).get()?.pco_list_id ?? null;
|
||||
|
||||
pco.get("/pco", (c) => c.json({ ready: pcoReady(), list_id: listId(c.get("db")) }, 200, NO_STORE));
|
||||
|
||||
pco.post("/pco/compare", async (c) => {
|
||||
const db = c.get("db");
|
||||
try {
|
||||
const members = await listPeople(listId(db));
|
||||
return c.json({ rows: compare(db, members) }, 200, NO_STORE);
|
||||
} catch (err) {
|
||||
if (err instanceof PcoError) return c.json({ error: err.message }, 502, NO_STORE);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
pco.post("/pco/apply", async (c) => {
|
||||
const db = c.get("db");
|
||||
let body;
|
||||
try {
|
||||
body = (await c.req.json()) ?? {};
|
||||
} catch {
|
||||
return c.json({ error: "Expected a JSON body." }, 400);
|
||||
}
|
||||
const chosen = Array.isArray(body.decisions) ? body.decisions : [];
|
||||
|
||||
let members;
|
||||
try {
|
||||
members = await listPeople(listId(db));
|
||||
} catch (err) {
|
||||
if (err instanceof PcoError) return c.json({ error: err.message }, 502, NO_STORE);
|
||||
throw err;
|
||||
}
|
||||
const byId = new Map(members.map((m) => [m.id, m]));
|
||||
const known = new Set(db.prepare(`SELECT id FROM people`).all().map((r) => r.id));
|
||||
|
||||
const decisions = chosen
|
||||
.map((d) => ({
|
||||
pco: byId.get(String(d?.pco_id ?? "")),
|
||||
action: d?.action === "create" ? "create" : d?.action === "link" ? "link" : null,
|
||||
person_id: d?.person_id && known.has(String(d.person_id)) ? String(d.person_id) : null,
|
||||
update: Boolean(d?.update),
|
||||
}))
|
||||
.filter((d) => d.pco && d.action && (d.action === "create" || d.person_id));
|
||||
|
||||
const counts = tx(db, () => apply(db, decisions));
|
||||
console.log(
|
||||
`pco sync by ${c.get("user").email}: ${counts.linked} linked, ${counts.updated} updated, ${counts.created} created`,
|
||||
);
|
||||
return c.json({ counts }, 200, NO_STORE);
|
||||
});
|
||||
|
||||
export default pco;
|
||||
Loading…
Add table
Add a link
Reference in a new issue