mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-03-31 19:45:33 +00:00
Rewrite User constructor to search all tables if no type given
This commit is contained in:
26
lib/User.js
26
lib/User.js
@@ -15,6 +15,16 @@ class User {
|
||||
type = null;
|
||||
|
||||
constructor(type, userId) {
|
||||
type = type ?? false;
|
||||
|
||||
let types = [];
|
||||
if (type)
|
||||
types.push(type);
|
||||
else
|
||||
types = [ 'account', 'student', 'parent' ];
|
||||
|
||||
return (async () => {
|
||||
for (const type of types) {
|
||||
const sql = `
|
||||
select
|
||||
email,
|
||||
@@ -28,16 +38,22 @@ class User {
|
||||
${type}Id = ?;
|
||||
`;
|
||||
|
||||
this.type = type;
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const record = await conn.runQuery(sql, [ userId ]);
|
||||
const res =
|
||||
await conn.runQuery(sql, [ userId ]);
|
||||
|
||||
for (const [ k, v ] of Object.entries(record[0]))
|
||||
if (!res)
|
||||
continue;
|
||||
|
||||
const record = res[0];
|
||||
|
||||
for (const [ k, v ] of Object.entries(record))
|
||||
this[k] = v;
|
||||
|
||||
this.type = type;
|
||||
|
||||
return this;
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user