From 63e6fde021c522b5812b1dd8e2f3fb805de2750e Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 3 Mar 2022 01:57:49 +0000 Subject: [PATCH] Generalise getTeacher to getUser --- lib/Class.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/Class.js b/lib/Class.js index 076f21a..e464bf2 100644 --- a/lib/Class.js +++ b/lib/Class.js @@ -87,7 +87,7 @@ class Class { this.teacherIds = ids.map(record => record.id); const [ teachers, subject ] = await Promise.all([ - this.getTeachers(), + this.getUsers(this.teacherIds, 'account'), this.getSubject() ]); @@ -102,12 +102,20 @@ class Class { return new (require('./Subject'))(this.subjectId); } - async getTeachers() { - const teacherPromises = this.teacherIds.map(id => { - return new (require('./Account'))(id); + async getUsers(ids, type) { + const types = { + account: 'Account', + student: 'Student' + }; + + if (!(type in types)) + throw new Error('Invalid type'); + + const promises = ids.map(id => { + return new (require(`./${types[type]}`))(id); }); - return Promise.all(teacherPromises); + return Promise.all(promises); } async getTests({ order='desc', range='all' } = {}) {