Seed hk-planner capabilities: planner + settings

Staff role gets planner by default; settings is admin/explicit-grant only.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-02 14:29:58 +00:00
parent c7f7079b99
commit b5d494a691

View file

@ -180,6 +180,39 @@ export async function initDb() {
ON CONFLICT DO NOTHING
`)
// Seed hk-planner capabilities
await pool.query(`
INSERT INTO app_capabilities (app_id, slug, name, description, sort_order)
SELECT a.id, c.slug, c.name, c.description, c.sort_order
FROM apps a
CROSS JOIN (VALUES
('planner', 'Use planner', 'View occupancy, adjust pickup, staff rota, time requirements', 1),
('settings', 'Category settings', 'Reorder and hide Newbook room categories', 2)
) AS c(slug, name, description, sort_order)
WHERE a.slug = 'hk-planner'
ON CONFLICT (app_id, slug) DO UPDATE SET
name = EXCLUDED.name,
description = EXCLUDED.description,
sort_order = EXCLUDED.sort_order
`)
// Grant Staff role the planner cap by default (settings is admin-only)
await pool.query(`
INSERT INTO role_capabilities (role_id, capability_id)
SELECT r.id, ac.id
FROM roles r
JOIN app_capabilities ac ON ac.app_id = (SELECT id FROM apps WHERE slug = 'hk-planner')
JOIN apps a ON a.id = ac.app_id
WHERE r.slug = 'staff'
AND ac.slug IN ('planner')
AND NOT EXISTS (
SELECT 1 FROM role_capabilities rc
JOIN app_capabilities ac2 ON ac2.id = rc.capability_id
WHERE rc.role_id = r.id AND ac2.app_id = a.id
)
ON CONFLICT DO NOTHING
`)
// Seed first admin user if table is empty
const { rows } = await pool.query('SELECT COUNT(*) FROM users')
if (parseInt(rows[0].count) === 0) {