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

User constructor should return the correct child class is type is unspecified

This commit is contained in:
2022-02-25 13:03:26 +00:00
parent 6c843731d1
commit f0e865803e

View File

@@ -19,10 +19,13 @@ class User {
type = type ?? false; type = type ?? false;
let types = []; let types = [];
if (type) let knownType = false;
if (type) {
types.push(type); types.push(type);
else knownType = true;
} else {
types = [ 'account', 'student', 'parent' ]; types = [ 'account', 'student', 'parent' ];
}
return (async () => { return (async () => {
for (const type of types) { for (const type of types) {
@@ -43,7 +46,7 @@ class User {
const res = const res =
await conn.runQuery(sql, [ userId ]); await conn.runQuery(sql, [ userId ]);
if (!res) if (!res.length)
continue; continue;
const record = res[0]; const record = res[0];
@@ -53,7 +56,14 @@ class User {
this.type = type; this.type = type;
return this; if (knownType)
return this;
const className =
`${type.substring(0, 1).toUpperCase()}`
+ `${type.substring(1)}`;
return new (require(`./${className}`))(this.id);
} }
})(); })();
} }