mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:19:26 +00:00
Added User.getTests
This commit is contained in:
45
lib/User.js
45
lib/User.js
@@ -5,6 +5,7 @@ const crypto = require('crypto');
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
const PasswordReset = require('./PasswordReset');
|
||||
const Test = require('./Test');
|
||||
|
||||
class User {
|
||||
id;
|
||||
@@ -108,6 +109,50 @@ class User {
|
||||
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) {
|
||||
return await bcrypt.hash(password, 10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user