From 73bf0f699c93ff98399a16040b8efd49351435d8 Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 15:52:33 +0000 Subject: [PATCH] Added User.getClasses --- lib/User.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/User.js b/lib/User.js index eff812e..cc8e208 100644 --- a/lib/User.js +++ b/lib/User.js @@ -4,6 +4,8 @@ const bcrypt = require('bcrypt'); const crypto = require('crypto'); const DatabaseConnectionPool = require('./DatabaseConnectionPool'); + +const Class = require('./Class'); const PasswordReset = require('./PasswordReset'); const Test = require('./Test'); @@ -157,6 +159,32 @@ class User { return await Promise.all(testObjects); } + async getClasses() { + if (this.type === 'parent') + throw new Error(`Can not fetch class for ${this.type}`); + + let sql = ` + select + c.classId as classId + from + class c + join ${this.type}ClassLink link using(classId) + where + link.${this.type}Id = ? + order by + c.name; + `; + + const conn = await new DatabaseConnectionPool(); + const classes = await conn.runQuery(sql, [ this.id ]); + + const classObjects = classes.map(c => { + return new Class(c.classId); + }); + + return await Promise.all(classObjects); + } + static async hashPassword(password) { return await bcrypt.hash(password, 10); }