mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:19: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;
|
type = null;
|
||||||
|
|
||||||
constructor(type, userId) {
|
constructor(type, userId) {
|
||||||
const sql = `
|
type = type ?? false;
|
||||||
select
|
|
||||||
email,
|
|
||||||
firstName,
|
|
||||||
otherNames,
|
|
||||||
lastName,
|
|
||||||
password,
|
|
||||||
accountId as id
|
|
||||||
from ${type}
|
|
||||||
where
|
|
||||||
${type}Id = ?;
|
|
||||||
`;
|
|
||||||
|
|
||||||
this.type = type;
|
let types = [];
|
||||||
|
if (type)
|
||||||
|
types.push(type);
|
||||||
|
else
|
||||||
|
types = [ 'account', 'student', 'parent' ];
|
||||||
|
|
||||||
return (async () => {
|
return (async () => {
|
||||||
const conn = await new DatabaseConnectionPool();
|
for (const type of types) {
|
||||||
const record = await conn.runQuery(sql, [ userId ]);
|
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]))
|
const conn = await new DatabaseConnectionPool();
|
||||||
this[k] = v;
|
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