maintenance/seed-app.js

60 lines
2.8 KiB
JavaScript

#!/usr/bin/env node
// Run from maintenance/ dir: DATABASE_URL=... node seed-app.js
import pg from 'pg'
const { Pool } = pg
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
await pool.query(`
INSERT INTO apps (slug, name, description, base_path, icon, theme_color, category, internal_host, internal_port)
VALUES ('maintenance', 'Maintenance', 'Maintenance log book — faults, recurring tasks, assets and contractors', '/maintenance', 'Wrench', '#b45309', 'Operations', '10.10.10.121', 3080)
ON CONFLICT (slug) DO UPDATE SET
name = EXCLUDED.name,
description = EXCLUDED.description,
base_path = EXCLUDED.base_path,
icon = EXCLUDED.icon,
theme_color = EXCLUDED.theme_color,
category = EXCLUDED.category,
internal_host = EXCLUDED.internal_host,
internal_port = EXCLUDED.internal_port
`)
// Seed 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, (VALUES
('view', 'View Tasks', 'View the maintenance log and history', 1),
('report', 'Report Faults', 'Create tasks, add photos and comments', 2),
('update', 'Update Tasks', 'Change task state, reassign, edit details', 3),
('resolve', 'Resolve Tasks', 'Mark tasks temporary fixed or fixed', 4),
('costs', 'View Costs', 'See and enter repair cost values', 5),
('manage_locations', 'Manage Locations', 'Manage locations, categories and NewBook sync', 6),
('manage_assets', 'Manage Assets', 'Create and edit the asset register', 7),
('manage_contractors', 'Manage Contractors', 'Manage contractors and their documents', 8),
('manage_templates', 'Manage Recurring', 'Create and edit recurring task templates', 9),
('settings', 'Settings', 'Configure maintenance app settings', 10)
) AS c(slug, name, description, sort_order)
WHERE a.slug = 'maintenance'
ON CONFLICT (app_id, slug) DO NOTHING
`)
// Grant view + report to Staff role if they have no maintenance caps yet
await pool.query(`
INSERT INTO role_capabilities (role_id, cap_id)
SELECT r.id, ac.id
FROM roles r
JOIN app_capabilities ac ON ac.slug IN ('view', 'report')
JOIN apps a ON a.id = ac.app_id AND a.slug = 'maintenance'
WHERE r.name = 'Staff'
AND NOT EXISTS (
SELECT 1 FROM role_capabilities rc2
JOIN app_capabilities ac2 ON rc2.cap_id = ac2.id
JOIN apps a2 ON ac2.app_id = a2.id AND a2.slug = 'maintenance'
WHERE rc2.role_id = r.id
)
ON CONFLICT DO NOTHING
`)
console.log('maintenance app seeded.')
await pool.end()