fix from copilot

This commit is contained in:
C. Fuhrman 2025-03-10 14:21:25 -04:00
parent ba055070a3
commit 8a8c58304e

View file

@ -25,6 +25,7 @@ class Users {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const existingUser = await userCollection.findOne({ email: userInfos.email }); const existingUser = await userCollection.findOne({ email: userInfos.email });
@ -51,13 +52,17 @@ class Users {
// TODO: verif if inserted properly... // TODO: verif if inserted properly...
return user; return user;
} finally {
await this.db.close();
}
} }
async login(email, password) { async login(email, password) {
console.log(`models/users: login: email: ${email}, password: ${password}`); console.log(`models/users: login: email: ${email}, password: ${password}`);
try {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ email: email }); const user = await userCollection.findOne({ email: email });
@ -80,6 +85,8 @@ class Users {
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
} finally {
await this.db.close();
} }
} }
@ -93,6 +100,7 @@ class Users {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const hashedPassword = await this.hashPassword(newPassword); const hashedPassword = await this.hashPassword(newPassword);
@ -105,12 +113,16 @@ class Users {
if (result.modifiedCount != 1) return null; if (result.modifiedCount != 1) return null;
return newPassword; return newPassword;
} finally {
await this.db.close();
}
} }
async delete(email) { async delete(email) {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const result = await userCollection.deleteOne({ email }); const result = await userCollection.deleteOne({ email });
@ -118,12 +130,16 @@ class Users {
if (result.deletedCount != 1) return false; if (result.deletedCount != 1) return false;
return true; return true;
} finally {
await this.db.close();
}
} }
async getId(email) { async getId(email) {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ email: email }); const user = await userCollection.findOne({ email: email });
@ -133,12 +149,16 @@ class Users {
} }
return user._id; return user._id;
} finally {
await this.db.close();
}
} }
async getById(id) { async getById(id) {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: id }); const user = await userCollection.findOne({ _id: id });
@ -148,12 +168,16 @@ class Users {
} }
return user; return user;
} finally {
await this.db.close();
}
} }
async editUser(userInfo) { async editUser(userInfo) {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
try {
const userCollection = conn.collection("users"); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: userInfo.id }); const user = await userCollection.findOne({ _id: userInfo.id });
@ -175,6 +199,9 @@ class Users {
} }
return false; return false;
} finally {
await this.db.close();
}
} }
} }