-- Users table CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, password_hash VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, role VARCHAR(50) DEFAULT 'user', is_active BOOLEAN DEFAULT true, created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ); -- User permissions table CREATE TABLE IF NOT EXISTS user_permissions ( id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, -- Section access (which pages can be viewed) can_view_dashboard BOOLEAN DEFAULT true, can_view_components BOOLEAN DEFAULT true, can_view_products BOOLEAN DEFAULT true, can_view_production BOOLEAN DEFAULT true, can_view_suppliers BOOLEAN DEFAULT true, can_view_customers BOOLEAN DEFAULT true, can_view_stock_history BOOLEAN DEFAULT true, can_view_stock_take BOOLEAN DEFAULT true, can_view_logistics BOOLEAN DEFAULT true, can_view_settings BOOLEAN DEFAULT false, -- Financial visibility can_view_financial BOOLEAN DEFAULT false, -- Actions can_create BOOLEAN DEFAULT false, can_edit BOOLEAN DEFAULT false, can_delete BOOLEAN DEFAULT false, -- Download/export can_download BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ); -- Sessions table for auth CREATE TABLE IF NOT EXISTS sessions ( id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, token VARCHAR(255) UNIQUE NOT NULL, expires_at TIMESTAMP NOT NULL, created_at TIMESTAMP DEFAULT NOW() ); -- Create default admin user (password: admin123) -- The hash is bcrypt for 'admin123' INSERT INTO users (email, password_hash, name, role) VALUES ('admin@linse.com', '$2b$10$K7L1OJ45/4Y2nIvOLPjKYeOIEZqMh0XKmYZKJKjKmJKmJKjKmJKjK', 'Administrator', 'admin') ON CONFLICT (email) DO NOTHING; -- Create permissions for admin user INSERT INTO user_permissions (user_id, can_view_dashboard, can_view_components, can_view_products, can_view_production, can_view_suppliers, can_view_customers, can_view_stock_history, can_view_stock_take, can_view_logistics, can_view_settings, can_view_financial, can_create, can_edit, can_delete, can_download) SELECT id, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true FROM users WHERE email = 'admin@linse.com' ON CONFLICT DO NOTHING;