API-19: switching from for...in to for...of #9

Merged
Paddy merged 1 commits from API-19 into master 2021-08-27 14:24:17 +00:00
2 changed files with 42 additions and 54 deletions

View File

@ -71,19 +71,17 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
let user: UserData = {} as UserData; let user: UserData = {} as UserData;
for (let row in rows) { for (let row of rows) {
if (row !== 'meta') {
user = { user = {
username: rows[row].username, username: row.username,
email: rows[row].email, email: row.email,
firstName: rows[row].first_name, firstName: row.first_name,
lastName: rows[row].last_name, lastName: row.last_name,
lastLogin: rows[row].last_login, lastLogin: row.last_login,
emailIsVerified: rows[row].email_is_verified, emailIsVerified: row.email_is_verified,
isPremiumUser: rows[row].is_premium_user isPremiumUser: row.is_premium_user
}; };
} }
}
return user; return user;
} catch (err) { } catch (err) {
@ -114,16 +112,14 @@ export const getSessionData = async (useDev: boolean, userId: string): Promise<S
let sessions: SessionData[] = []; let sessions: SessionData[] = [];
for (let row in rows) { for (let row of rows) {
if (row !== 'meta') {
sessions.push({ sessions.push({
sessionId: rows[row].session_id, sessionId: row.session_id,
type: rows[row].type, type: row.type,
lastLogin: rows[row].last_login, lastLogin: row.last_login,
lastIp: rows[row].last_ip lastIp: row.last_ip
}); });
} }
}
return sessions; return sessions;
} catch (err) { } catch (err) {
@ -154,17 +150,15 @@ export const getFriendshipData = async (useDev: boolean, userId: string): Promis
let friends: Friendship[] = []; let friends: Friendship[] = [];
for (let row in rows) { for (let row of rows) {
if (row !== 'meta') {
friends.push({ friends.push({
friendshipId: rows[row].friendship_id, friendshipId: row.friendship_id,
friendId: rows[row].friend_id, friendId: row.friend_id,
friendFirstName: rows[row].friend_first_name, friendFirstName: row.friend_first_name,
friendLastName: rows[row].friend_last_name, friendLastName: row.friend_last_name,
friendUsername: rows[row].friend_username friendUsername: row.friend_username
}); });
} }
}
return friends; return friends;
} catch (err) { } catch (err) {

View File

@ -57,11 +57,9 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
let usernames: string[] = []; let usernames: string[] = [];
let emails: string[] = []; let emails: string[] = [];
for (let row in rows) { for (let row of rows) {
if (row !== 'meta') { usernames.push(row.username);
usernames.push(rows[row].username); emails.push(row.email);
emails.push(rows[row].email);
}
} }
return { return {
@ -109,9 +107,9 @@ export const registerUser = async (useDev: boolean, username: string, email: str
// Get user id of the created user // Get user id of the created user
let userId: number = -1; let userId: number = -1;
for (const row in userIdRes) { for (const row of userIdRes) {
if (row !== 'meta' && userIdRes[row].user_id != null) { if (row.user_id != null) {
userId = userIdRes[row].user_id; userId = row.user_id;
} }
} }
@ -129,9 +127,9 @@ export const registerUser = async (useDev: boolean, username: string, email: str
// Get session id of the created session // Get session id of the created session
let sessionId: number = -1; let sessionId: number = -1;
for (const row in sessionIdRes) { for (const row of sessionIdRes) {
if (row !== 'meta' && sessionIdRes[row].session_id != null) { if (row.session_id != null) {
sessionId = sessionIdRes[row].session_id; sessionId = row.session_id;
} }
} }
@ -179,11 +177,9 @@ export const loginUser = async (useDev: boolean, username: string, email: string
let passwordHash: string = ''; let passwordHash: string = '';
let userId: string = ''; let userId: string = '';
for (let row in query_result) { for (let row of query_result) {
if (row !== 'meta') { passwordHash = row.password_hash;
passwordHash = query_result[row].password_hash; userId = row.user_id;
userId = query_result[row].user_id;
}
} }
// Wrong password // Wrong password
@ -212,9 +208,9 @@ export const loginUser = async (useDev: boolean, username: string, email: string
// Get session id of the created session // Get session id of the created session
let sessionId: number = -1; let sessionId: number = -1;
for (const row in sessionIdRes) { for (const row of sessionIdRes) {
if (row !== 'meta' && sessionIdRes[row].session_id != null) { if (row.session_id != null) {
sessionId = sessionIdRes[row].session_id; sessionId = row.session_id;
} }
} }
@ -311,10 +307,8 @@ export const checkSession = async (useDev: boolean, userId: string, sessionId: s
let rows = await conn.query('SELECT session_key_hash FROM sessions WHERE user_id = ? AND session_id = ?', [userId, sessionId]); let rows = await conn.query('SELECT session_key_hash FROM sessions WHERE user_id = ? AND session_id = ?', [userId, sessionId]);
let savedHash = ''; let savedHash = '';
for (let row in rows) { for (let row of rows) {
if (row !== 'meta') { savedHash = row.session_key_hash;
savedHash = rows[row].session_key_hash;
}
} }
return bcrypt.compareSync(sessionKey, savedHash); return bcrypt.compareSync(sessionKey, savedHash);