Skip to content

Commit 2e1886a

Browse files
committed
Added time checking function for expired user_roles and added time assigned column to user_roles table
1 parent 2873c3d commit 2e1886a

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

lib/database/database.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DBuser {
2727
load_db_login() {
2828
// Get document, or throw exception on error
2929
try {
30-
const doc = yaml.load(fs.readFileSync("../../config/database.yml"));
30+
const doc = yaml.load(fs.readFileSync("./config/database.yml"));
3131
return doc;
3232
} catch (e) {
3333
console.log(e);
@@ -90,14 +90,14 @@ class DBuser {
9090
const client = await this.pool.connect();
9191
try {
9292
if ((await this.check_table("users")) == false) {
93-
// console.log("Running create_table")
93+
console.log("Running creating user table");
9494
await client.query("BEGIN");
9595
const query = `CREATE TABLE users (
9696
userid INTEGER PRIMARY KEY,
9797
joindate DATE NOT NULL,
9898
leavedate DATE,
9999
userleft BOOLEAN
100-
);`;
100+
)`;
101101
await client.query(query);
102102
await client.query("COMMIT");
103103
}
@@ -112,23 +112,28 @@ class DBuser {
112112

113113
// Creates the user roles table
114114
async create_table_user_roles() {
115-
const client = await this.pool.connect();
115+
const client = await this.pool.connect();
116116
try {
117-
if ((await this.check_table("user_roles")) == false) {
118-
// console.log("Running create_table")
119-
await client.query("BEGIN");
117+
// if ((await this.check_table("user_roles")) == false) {
118+
119+
await client.query("BEGIN");
120120
const query = `CREATE TABLE user_roles (
121121
rid INTEGER PRIMARY KEY,
122-
userid INTEGER NOT NULL,
122+
userid BIGINT NOT NULL,
123123
role varchar(64),
124-
FOREIGN KEY (userid)
125-
REFERENCES users (userid)
126-
);`;
124+
time_assigned TIMESTAMP
125+
)`;
127126
await client.query(query);
128127
await client.query("COMMIT");
129-
}
128+
// } else {
129+
// console.log("Deleting create_user_roles_table");
130+
// await client.query("BEGIN");
131+
// const query = `DROP TABLE user_roles`;
132+
// await client.query(query);
133+
// await client.query("COMMIT");
134+
// }
130135
} catch (ex) {
131-
console.log(`Something wrong happend ${ex}`);
136+
console.log(`Something wrong happende:${ex}`);
132137
} finally {
133138
await client.query("ROLLBACK");
134139
client.release();
@@ -170,12 +175,12 @@ class DBuser {
170175
// console.log("Running create_table")
171176
await client.query("BEGIN");
172177
const query = `CREATE TABLE user_permissions (
173-
pid INTEGER PRIMARY KEY,
174-
userid INTEGER NOT NULL,
175-
permission varchar(64),
176-
FOREIGN KEY (userid)
177-
REFERENCES users (userid)
178-
);`;
178+
pid INTEGER PRIMARY KEY,
179+
userid INTEGER NOT NULL,
180+
permission varchar(64),
181+
FOREIGN KEY (userid)
182+
REFERENCES users (userid)
183+
);`;
179184
await client.query(query);
180185
await client.query("COMMIT");
181186
}
@@ -250,33 +255,31 @@ class DBuser {
250255
const client = await this.pool.connect();
251256
try {
252257
await client.query("BEGIN");
253-
254258
let query = "SELECT max(rid) from user_roles";
255259
let result = await client.query(query);
256260

257261
const count = result.rows[0]["max"] + 1;
258-
query = "INSERT INTO user_roles (RID, USERID, ROLE) VALUES ($1,$2,$3)";
262+
query = "INSERT INTO user_roles (RID, USERID, ROLE, TIME_ASSIGNED) VALUES ($1,$2,$3,NOW())";
259263
const values = [count, userid, role];
260264
result = await client.query(query, values);
261265

262266
await client.query("COMMIT");
263267
} catch (ex) {
264-
console.log(`Something wrong happend ${ex}`);
268+
console.log(`Something wrong happend when trying to add user role to table ${ex}`);
265269
} finally {
266270
await client.query("ROLLBACK");
267271
client.release();
268272
// console.log("Client released successfully.")
269273
}
270-
}
274+
}
271275

272276
// Removing a user role
273277
async remove_user_role(userid, role) {
274278
const client = await this.pool.connect();
275279
try {
276280
await client.query("BEGIN");
277281
const values = [userid, role];
278-
const query = `DELETE FROM user_roles
279-
where userid = $1 and role = $2`;
282+
const query = `DELETE FROM user_roles where userid = $1 and role = $2`;
280283
await client.query(query, values);
281284
await client.query("COMMIT");
282285
} catch (ex) {
@@ -456,6 +459,31 @@ class DBuser {
456459
// console.log("Client released successfully.")
457460
}
458461
}
462+
463+
async checkTimeAssigned() {
464+
const client = await this.pool.connect();
465+
try {
466+
// Query to select rows where time_assigned is older than 1 hour
467+
const query = `
468+
SELECT * FROM user_roles WHERE time_assigned < NOW() - interval '1 year'
469+
`;
470+
471+
const result = await client.query(query);
472+
473+
const old_roles = result.rows.map(row => ({
474+
role_name: row.role,
475+
userid: row.userid
476+
}));
477+
478+
return old_roles;
479+
} catch (error) {
480+
console.error(error);
481+
return old_roles;
482+
} finally {
483+
client.release();
484+
}
485+
}
486+
459487
}
460488

461489
module.exports = {

0 commit comments

Comments
 (0)