From f0e865803e76c3e03283decffd486c039fd5c57a Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 13:03:26 +0000 Subject: [PATCH] User constructor should return the correct child class is type is unspecified --- lib/User.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/User.js b/lib/User.js index dc26362..e099547 100644 --- a/lib/User.js +++ b/lib/User.js @@ -19,10 +19,13 @@ class User { type = type ?? false; let types = []; - if (type) + let knownType = false; + if (type) { types.push(type); - else + knownType = true; + } else { types = [ 'account', 'student', 'parent' ]; + } return (async () => { for (const type of types) { @@ -43,7 +46,7 @@ class User { const res = await conn.runQuery(sql, [ userId ]); - if (!res) + if (!res.length) continue; const record = res[0]; @@ -53,7 +56,14 @@ class User { 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); } })(); }