1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 19:59:27 +00:00

Added User.getTests

This commit is contained in:
2022-02-25 13:17:07 +00:00
parent f16cab9f8b
commit 98e7b125c4

View File

@@ -5,6 +5,7 @@ const crypto = require('crypto');
const DatabaseConnectionPool = require('./DatabaseConnectionPool'); const DatabaseConnectionPool = require('./DatabaseConnectionPool');
const PasswordReset = require('./PasswordReset'); const PasswordReset = require('./PasswordReset');
const Test = require('./Test');
class User { class User {
id; id;
@@ -108,6 +109,50 @@ class User {
req.session.fullName = `${this.firstName} ${this.lastName}`; req.session.fullName = `${this.firstName} ${this.lastName}`;
} }
async getTests({ order='desc', range='all' } = {}) {
const validOrder = [ 'asc', 'desc' ];
const validRange = [ 'all', 'before', 'after' ];
if (this.type === 'parent')
throw new Error(`Can not fetch tests for ${this.type}`);
if (!validOrder.includes(order))
throw new Error('Invalid order');
if (!validRange.includes(range))
throw new Error('Invalid range');
let sql = `
select
t.*
from
test t
join ${this.type}ClassLink link using(classId)
where
link.${this.type}Id = ?
`;
switch (range) {
case 'before':
sql += `and t.testDate < CURDATE()`;
break;
case 'after':
sql += `and t.testDate > CURDATE()`;
break;
}
sql += `order by t.testDate ${order};`;
const conn = await new DatabaseConnectionPool();
const tests = await conn.runQuery(sql, [ this.id ]);
const testObjects = tests.map(test => {
return new Test(test.testId);
});
return await Promise.all(testObjects);
}
static async hashPassword(password) { static async hashPassword(password) {
return await bcrypt.hash(password, 10); return await bcrypt.hash(password, 10);
} }