Phase 4
This commit is contained in:
@@ -2,18 +2,31 @@ const Database = require('better-sqlite3');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const DB_PATH = process.env.DB_PATH || '/data/cpas.db';
|
||||
const SCHEMA_PATH = path.join(__dirname, 'schema.sql');
|
||||
const dbPath = process.env.DB_PATH || path.join(__dirname, '..', 'data', 'cpas.db');
|
||||
const dir = path.dirname(dbPath);
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
|
||||
const dbDir = path.dirname(DB_PATH);
|
||||
if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true });
|
||||
|
||||
const db = new Database(DB_PATH);
|
||||
const db = new Database(dbPath);
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('foreign_keys = ON');
|
||||
|
||||
const schema = fs.readFileSync(SCHEMA_PATH, 'utf8');
|
||||
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
||||
db.exec(schema);
|
||||
|
||||
console.log(`[DB] Connected: ${DB_PATH}`);
|
||||
// Migrate: add negated columns if upgrading from Phase 1-3
|
||||
const cols = db.prepare("PRAGMA table_info(violations)").all().map(c => c.name);
|
||||
if (!cols.includes('negated')) db.exec("ALTER TABLE violations ADD COLUMN negated INTEGER NOT NULL DEFAULT 0");
|
||||
if (!cols.includes('negated_at')) db.exec("ALTER TABLE violations ADD COLUMN negated_at DATETIME");
|
||||
|
||||
// Ensure resolutions table exists on upgrade
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS violation_resolutions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
violation_id INTEGER NOT NULL REFERENCES violations(id) ON DELETE CASCADE,
|
||||
resolution_type TEXT NOT NULL,
|
||||
details TEXT,
|
||||
resolved_by TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)`);
|
||||
|
||||
console.log('[DB] Connected:', dbPath);
|
||||
module.exports = db;
|
||||
|
||||
@@ -1,36 +1,45 @@
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
department TEXT,
|
||||
supervisor TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS violations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
employee_id INTEGER NOT NULL REFERENCES employees(id),
|
||||
violation_type TEXT NOT NULL,
|
||||
violation_name TEXT NOT NULL,
|
||||
category TEXT NOT NULL DEFAULT 'General',
|
||||
points INTEGER NOT NULL,
|
||||
incident_date TEXT NOT NULL,
|
||||
incident_time TEXT,
|
||||
location TEXT,
|
||||
details TEXT,
|
||||
submitted_by TEXT,
|
||||
witness_name TEXT,
|
||||
negated INTEGER NOT NULL DEFAULT 0,
|
||||
negated_at DATETIME,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS violation_resolutions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
employee_id INTEGER NOT NULL REFERENCES employees(id),
|
||||
violation_type TEXT NOT NULL,
|
||||
violation_name TEXT NOT NULL,
|
||||
category TEXT NOT NULL,
|
||||
points INTEGER NOT NULL,
|
||||
incident_date DATE NOT NULL,
|
||||
incident_time TEXT,
|
||||
location TEXT,
|
||||
violation_id INTEGER NOT NULL REFERENCES violations(id) ON DELETE CASCADE,
|
||||
resolution_type TEXT NOT NULL,
|
||||
details TEXT,
|
||||
submitted_by TEXT,
|
||||
witness_name TEXT,
|
||||
resolved_by TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Active score: only non-negated violations in rolling 90 days
|
||||
CREATE VIEW IF NOT EXISTS active_cpas_scores AS
|
||||
SELECT
|
||||
e.id AS employee_id,
|
||||
e.name AS employee_name,
|
||||
e.department,
|
||||
COALESCE(SUM(v.points), 0) AS active_points,
|
||||
COUNT(v.id) AS violation_count
|
||||
FROM employees e
|
||||
LEFT JOIN violations v
|
||||
ON v.employee_id = e.id
|
||||
AND v.incident_date >= DATE('now', '-90 days')
|
||||
GROUP BY e.id;
|
||||
employee_id,
|
||||
SUM(points) AS active_points,
|
||||
COUNT(*) AS violation_count
|
||||
FROM violations
|
||||
WHERE negated = 0
|
||||
AND incident_date >= DATE('now', '-90 days')
|
||||
GROUP BY employee_id;
|
||||
|
||||
Reference in New Issue
Block a user