diff --git a/UC/src/db_managers/department_database.ts b/UC/src/db_managers/department_database.ts
index fb3474c..31f5f9f 100644
--- a/UC/src/db_managers/department_database.ts
+++ b/UC/src/db_managers/department_database.ts
@@ -15,7 +15,7 @@ export class DepartmentDatabase {
}
// Create the table if it doesn't exist
- private createTableIfNotExists() {
+ public createTableIfNotExists() {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS departments (
id TEXT PRIMARY KEY,
@@ -25,7 +25,6 @@ export class DepartmentDatabase {
this.db.exec(createTableQuery);
const ceoDepartment = this.db.prepare('SELECT COUNT(*) as count FROM departments WHERE name = ?').get('CEO').count;
- const defaultDepartment = this.db.prepare('SELECT COUNT(*) as count FROM departments WHERE name = ?').get('Worker').count;
// Create CEO department if it doesn't exist
if (ceoDepartment === 0) {
@@ -35,13 +34,6 @@ export class DepartmentDatabase {
console.log('CEO department already exists.');
}
- if (defaultDepartment === 0) {
- this.createDepartment('Worker');
- console.log('Created Worker department.');
- } else {
- console.log('Worker department already exists.');
- }
-
console.log('Departments table checked/created.');
}
@@ -92,25 +84,15 @@ export class DepartmentDatabase {
return stmt.all();
}
- public cleanTable(): { success: boolean; message: string } {
+ public dropTable(): { success: boolean; message: string } {
try {
- // Fetch the CEO department ID
- const ceoDepartmentRow = this.db.prepare(`SELECT id FROM departments WHERE LOWER(name) = 'ceo'`).get();
- if (!ceoDepartmentRow) {
- console.error('CEO department not found in the database.');
- return { success: false, message: 'Failed to find CEO department.' };
- }
-
- const ceoDepartmentId = ceoDepartmentRow.id;
-
- // Delete all departments except the CEO department
- const deleteDepartmentsStmt = this.db.prepare(`DELETE FROM departments WHERE id != ?`);
- deleteDepartmentsStmt.run(ceoDepartmentId);
-
- return { success: true, message: 'All non-CEO departments have been deleted.' };
+ this.db.exec('DROP TABLE IF EXISTS departments;');
+ console.log('Departments table dropped successfully.');
+ return { success: true, message: 'Departments table dropped successfully.' };
} catch (error) {
- console.error('Error while cleaning the departments table:', error);
- return { success: false, message: 'Failed to clean departments table.' };
+ console.error('Error while dropping the departments table:', error);
+ return { success: false, message: 'Failed to drop departments table.' };
}
}
+
}
diff --git a/UC/src/db_managers/key_database.ts b/UC/src/db_managers/key_database.ts
index ceeeba8..bee1516 100644
--- a/UC/src/db_managers/key_database.ts
+++ b/UC/src/db_managers/key_database.ts
@@ -14,19 +14,16 @@ export class KeyDatabase {
private db: any;
constructor() {
- // Get the singleton instance of the database
this.db = SQLiteDatabase.getInstance();
-
- // Create the keys table if it doesn't exist
this.createTableIfNotExists();
}
// Create the table if it doesn't exist
- private createTableIfNotExists() {
+ public createTableIfNotExists() {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS user_keys (
id TEXT PRIMARY KEY,
- userId TEXT UNIQUE NOT NULL, -- Ensure one key per user
+ userId TEXT UNIQUE NOT NULL,
key TEXT NOT NULL,
iv TEXT NOT NULL,
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
@@ -134,31 +131,15 @@ export class KeyDatabase {
return stmt.all();
}
- // Clean the table but keep CEO keys
- public cleanTable(): { success: boolean; message: string } {
+ public dropTable(): { success: boolean; message: string } {
try {
- const departments = departmentDatabase.getAllDepartments();
- const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
-
- if (!ceoDepartment) {
- console.error('CEO department not found in the database.');
- return { success: false, message: 'Failed to find CEO department.' };
- }
-
- // Fetch all users in the CEO department
- const ceoUsers = userDatabase.findByDepartmentId(ceoDepartment.id);
- const ceoUserIds = ceoUsers.map(user => user.id);
-
- // Delete all keys except for the CEO users
- const stmt = this.db.prepare(`
- DELETE FROM user_keys WHERE userId NOT IN (${ceoUserIds.map(() => '?').join(', ')})
- `);
- stmt.run(...ceoUserIds);
-
- return { success: true, message: 'All non-CEO keys have been deleted.' };
+ this.db.exec('DROP TABLE IF EXISTS user_keys;');
+ console.log('user_keys table dropped successfully.');
+ return { success: true, message: 'user_keys table dropped successfully.' };
} catch (error) {
- console.error('Error while cleaning the keys table:', error);
- return { success: false, message: 'Failed to clean keys table.' };
+ console.error('Error while dropping the user_keys table:', error);
+ return { success: false, message: 'Failed to drop user_keys table.' };
}
}
+
}
diff --git a/UC/src/db_managers/user_database.ts b/UC/src/db_managers/user_database.ts
index 7e9299a..6bdfa85 100644
--- a/UC/src/db_managers/user_database.ts
+++ b/UC/src/db_managers/user_database.ts
@@ -28,7 +28,7 @@ export class UserDatabase {
}
// Create the table if it doesn't exist
- private createTableIfNotExists() {
+ public createTableIfNotExists() {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
@@ -209,28 +209,14 @@ export class UserDatabase {
return pbkdf2Sync(password, salt, 1000, 64, 'sha256').toString('hex');
}
- public cleanTable(): { success: boolean; message: string } {
+ public dropTable(): { success: boolean; message: string } {
try {
- // Fetch departments
- const departments = departmentDatabase.getAllDepartments();
-
- const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
-
- if (!ceoDepartment) {
- console.error('Admin or CEO department not found in the database.');
- return { success: false, message: 'Failed to find Admin or CEO department.' };
- }
-
- // Delete users from the table except for those in the Admin and CEO departments
- const stmt = this.db.prepare(`
- DELETE FROM users WHERE departmentId NOT IN (?, ?)
- `);
- stmt.run(ceoDepartment.id);
-
- return { success: true, message: 'All users except Admin and CEO have been deleted.' };
+ this.db.exec('DROP TABLE IF EXISTS users;');
+ console.log('Users table dropped successfully.');
+ return { success: true, message: 'Users table dropped successfully.' };
} catch (error) {
- console.error('Error while cleaning the users table:', error);
- return { success: false, message: 'Failed to clean users table.' };
+ console.error('Error while dropping the users table:', error);
+ return { success: false, message: 'Failed to drop users table.' };
}
}
}
diff --git a/UC/src/network/operations_custom/ceo_operations.ts b/UC/src/network/operations_custom/ceo_operations.ts
index 1f46c20..f50729b 100644
--- a/UC/src/network/operations_custom/ceo_operations.ts
+++ b/UC/src/network/operations_custom/ceo_operations.ts
@@ -11,10 +11,13 @@ export class CeoOperations implements OperationPlugin {
public static async handleResetDatabase(): Promise
{
console.log('Resetting the database...');
- departmentDatabase.cleanTable();
- userDatabase.cleanTable();
- keyDatabase.cleanTable();
- departmentDatabase.createDepartment('Worker');
+ keyDatabase.dropTable();
+ userDatabase.dropTable();
+ departmentDatabase.dropTable();
+
+ departmentDatabase.createTableIfNotExists();
+ userDatabase.createTableIfNotExists();
+ keyDatabase.createTableIfNotExists();
return {
operationCode: operationCodes.OK,