mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:39:26 +00:00
Rewrite User constructor to search all tables if no type given
This commit is contained in:
52
lib/User.js
52
lib/User.js
@@ -15,29 +15,45 @@ class User {
|
||||
type = null;
|
||||
|
||||
constructor(type, userId) {
|
||||
const sql = `
|
||||
select
|
||||
email,
|
||||
firstName,
|
||||
otherNames,
|
||||
lastName,
|
||||
password,
|
||||
accountId as id
|
||||
from ${type}
|
||||
where
|
||||
${type}Id = ?;
|
||||
`;
|
||||
type = type ?? false;
|
||||
|
||||
this.type = type;
|
||||
let types = [];
|
||||
if (type)
|
||||
types.push(type);
|
||||
else
|
||||
types = [ 'account', 'student', 'parent' ];
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const record = await conn.runQuery(sql, [ userId ]);
|
||||
for (const type of types) {
|
||||
const sql = `
|
||||
select
|
||||
email,
|
||||
firstName,
|
||||
otherNames,
|
||||
lastName,
|
||||
password,
|
||||
accountId as id
|
||||
from ${type}
|
||||
where
|
||||
${type}Id = ?;
|
||||
`;
|
||||
|
||||
for (const [ k, v ] of Object.entries(record[0]))
|
||||
this[k] = v;
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const res =
|
||||
await conn.runQuery(sql, [ userId ]);
|
||||
|
||||
return this;
|
||||
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