57 lines
2.4 KiB
JavaScript
57 lines
2.4 KiB
JavaScript
#!/usr/bin/env node
|
|
// Run from room-planner/ 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 ('room-planner', 'Day Planner', 'Daily housekeeping room view with NewBook task management', '/room-planner', 'BedDouble', '#2d6a4f', 'Housekeeping', '10.10.10.120', 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 Planner', 'Access the room planner view', 1),
|
|
('guest_details', 'Guest Details', 'View guest names and personal information', 2),
|
|
('rate_details', 'Rate Details', 'View pricing and rate plan information', 3),
|
|
('view_all_notes','View All Notes', 'View all booking note types', 4),
|
|
('complete_tasks','Complete Tasks', 'Mark NewBook tasks as complete', 5),
|
|
('update_status', 'Update Room Status', 'Mark rooms clean, dirty or inspected', 6),
|
|
('settings', 'Settings', 'Configure room planner settings', 7)
|
|
) AS c(slug, name, description, sort_order)
|
|
WHERE a.slug = 'room-planner'
|
|
ON CONFLICT (app_id, slug) DO NOTHING
|
|
`)
|
|
|
|
// Grant all caps to Staff role (except settings) if they have none 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 != 'settings'
|
|
JOIN apps a ON a.id = ac.app_id AND a.slug = 'room-planner'
|
|
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 = 'room-planner'
|
|
WHERE rc2.role_id = r.id
|
|
)
|
|
ON CONFLICT DO NOTHING
|
|
`)
|
|
|
|
console.log('room-planner app seeded.')
|
|
await pool.end()
|