1
0
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:
2022-02-14 00:19:28 +00:00
parent ef7f3ddb28
commit 1b2dfe5681

View File

@@ -15,6 +15,16 @@ class User {
type = null; type = null;
constructor(type, userId) { 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 = ` const sql = `
select select
email, email,
@@ -28,16 +38,22 @@ class User {
${type}Id = ?; ${type}Id = ?;
`; `;
this.type = type;
return (async () => {
const conn = await new DatabaseConnectionPool(); 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[k] = v;
this.type = type;
return this; return this;
}
})(); })();
} }