mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 17:59:25 +00:00
Lint cleanup with new rules
This commit is contained in:
61
lib/User.js
61
lib/User.js
@@ -11,13 +11,21 @@ const Test = require('./Test');
|
||||
|
||||
class User {
|
||||
id;
|
||||
|
||||
firstName;
|
||||
|
||||
otherNames;
|
||||
|
||||
lastName;
|
||||
|
||||
shortName;
|
||||
|
||||
fullName;
|
||||
|
||||
email;
|
||||
|
||||
#password;
|
||||
|
||||
type = null;
|
||||
|
||||
constructor(type, userId) {
|
||||
@@ -33,6 +41,9 @@ class User {
|
||||
}
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const queryPromises = [];
|
||||
|
||||
for (const type of types) {
|
||||
const sql = `
|
||||
select
|
||||
@@ -48,19 +59,27 @@ class User {
|
||||
${type}Id = ?;
|
||||
`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const res =
|
||||
await conn.runQuery(sql, [ userId ]);
|
||||
queryPromises.push(conn.runQuery(sql, [
|
||||
userId
|
||||
]));
|
||||
}
|
||||
|
||||
conn.close();
|
||||
const typeResults = await Promise.all(queryPromises);
|
||||
conn.close();
|
||||
|
||||
if (!res.length)
|
||||
for (const [ i, result ] of typeResults.entries()) {
|
||||
if (!result.length)
|
||||
continue;
|
||||
|
||||
const record = res[0];
|
||||
const record = result[0];
|
||||
const type = types[i];
|
||||
|
||||
for (const [ k, v ] of Object.entries(record))
|
||||
this[k] = v;
|
||||
for (const [ k, v ] of Object.entries(record)) {
|
||||
if (k === 'password')
|
||||
this.#password = v;
|
||||
else
|
||||
this[k] = v;
|
||||
}
|
||||
|
||||
this.type = type;
|
||||
this.shortName = this.getShortName();
|
||||
@@ -70,8 +89,8 @@ class User {
|
||||
return this;
|
||||
|
||||
const className =
|
||||
`${type.substring(0, 1).toUpperCase()}`
|
||||
+ `${type.substring(1)}`;
|
||||
`${type.substring(0, 1).toUpperCase()}`+
|
||||
`${type.substring(1)}`;
|
||||
|
||||
return new (require(`./${className}`))(this.id);
|
||||
}
|
||||
@@ -94,7 +113,7 @@ class User {
|
||||
}
|
||||
|
||||
async verifyPassword(password) {
|
||||
return await bcrypt.compare(password, this.password);
|
||||
return await bcrypt.compare(password, this.#password);
|
||||
}
|
||||
|
||||
async changePassword(password) {
|
||||
@@ -173,7 +192,7 @@ class User {
|
||||
if (this.type === 'parent')
|
||||
throw new Error(`Can not fetch class for ${this.type}`);
|
||||
|
||||
let sql = `
|
||||
const sql = `
|
||||
select
|
||||
c.classId as classId
|
||||
from
|
||||
@@ -243,6 +262,8 @@ class User {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const types = [ 'account', 'student', 'parent' ];
|
||||
|
||||
const idPromises = [];
|
||||
|
||||
for (const type of types) {
|
||||
const sql = `
|
||||
select
|
||||
@@ -253,18 +274,24 @@ class User {
|
||||
email = ?;
|
||||
`;
|
||||
|
||||
const id = (await conn.runQuery(sql, [
|
||||
idPromises.push(conn.runQuery(sql, [
|
||||
email
|
||||
]))?.[0]?.['id'];
|
||||
]));
|
||||
}
|
||||
|
||||
const ids = await Promise.all(idPromises);
|
||||
|
||||
for (const [ i, records ] of ids.entries()) {
|
||||
const type = types[i];
|
||||
const id = records?.[0]?.id;
|
||||
|
||||
if (typeof id !== 'undefined') {
|
||||
const className =
|
||||
`${type.substring(0, 1).toUpperCase()}`
|
||||
+ `${type.substring(1)}`;
|
||||
`${type.substring(0, 1).toUpperCase()}`+
|
||||
`${type.substring(1)}`;
|
||||
return new (require(`./${className}`))(id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user