mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Rename model classes (singular not plural)
This commit is contained in:
parent
7abe517c8f
commit
e28fa40b69
5 changed files with 305 additions and 308 deletions
166
server/models/folder.ts
Normal file
166
server/models/folder.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
import db from '../config/db';
|
||||||
|
import { ObjectId } from 'mongodb';
|
||||||
|
import { Quiz } from './quiz';
|
||||||
|
import { User } from './user';
|
||||||
|
|
||||||
|
export class Folder {
|
||||||
|
private created_at: Date;
|
||||||
|
|
||||||
|
constructor (public title: string, public user: User) {
|
||||||
|
this.title = title;
|
||||||
|
this.user = user;
|
||||||
|
this.created_at = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
// async create(title: string, userId: string): Promise<string> {
|
||||||
|
// // await db.connect();
|
||||||
|
// // const conn = db.getConnection();
|
||||||
|
|
||||||
|
// // const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// // const existingFolder = await foldersCollection.findOne({ title, userId });
|
||||||
|
|
||||||
|
// // if (existingFolder) throw new Error('Folder already exists');
|
||||||
|
|
||||||
|
// // const newFolder = {
|
||||||
|
// // userId,
|
||||||
|
// // title,
|
||||||
|
// // created_at: new Date(),
|
||||||
|
// // };
|
||||||
|
|
||||||
|
// // const result = await foldersCollection.insertOne(newFolder);
|
||||||
|
|
||||||
|
// // return result.insertedId;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getUserFolders(userId: string): Promise<any[]> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// const result = await foldersCollection.find({ userId }).toArray();
|
||||||
|
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getOwner(folderId: string): Promise<string> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) });
|
||||||
|
|
||||||
|
// return folder.userId;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getContent(folderId: string): Promise<any[]> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const filesCollection = conn.collection('files');
|
||||||
|
|
||||||
|
// const result = await filesCollection.find({ folderId }).toArray();
|
||||||
|
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async delete(folderId: string): Promise<boolean> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// const folderResult = await foldersCollection.deleteOne({ _id: new ObjectId(folderId) });
|
||||||
|
|
||||||
|
// if (folderResult.deletedCount !== 1) return false;
|
||||||
|
// await Quiz.deleteQuizzesByFolderId(folderId);
|
||||||
|
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async rename(folderId: string, newTitle: string): Promise<boolean> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// const result = await foldersCollection.updateOne({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } });
|
||||||
|
|
||||||
|
// if (result.modifiedCount !== 1) return false;
|
||||||
|
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async duplicate(folderId: string, userId: string): Promise<string> {
|
||||||
|
// const sourceFolder = await this.getFolderWithContent(folderId);
|
||||||
|
|
||||||
|
// let newFolderTitle = `${sourceFolder.title}-copie`;
|
||||||
|
// let counter = 1;
|
||||||
|
|
||||||
|
// while (await this.folderExists(newFolderTitle, userId)) {
|
||||||
|
// newFolderTitle = `${sourceFolder.title}-copie(${counter})`;
|
||||||
|
// counter++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const newFolderId = await this.create(newFolderTitle, userId);
|
||||||
|
|
||||||
|
// if (!newFolderId) {
|
||||||
|
// throw new Error('Failed to create a duplicate folder.');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (const quiz of sourceFolder.content) {
|
||||||
|
// const { title, content } = quiz;
|
||||||
|
// await Quiz.create(title, content, newFolderId.toString(), userId);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return newFolderId;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async folderExists(title: string, userId: string): Promise<boolean> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
// const existingFolder = await foldersCollection.findOne({ title, userId });
|
||||||
|
|
||||||
|
// return existingFolder !== null;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async copy(folderId: string, userId: string): Promise<string> {
|
||||||
|
// const sourceFolder = await this.getFolderWithContent(folderId);
|
||||||
|
// const newFolderId = await this.create(sourceFolder.title, userId);
|
||||||
|
|
||||||
|
// if (!newFolderId) {
|
||||||
|
// throw new Error('Failed to create a new folder.');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (const quiz of sourceFolder.content) {
|
||||||
|
// await this.createQuiz(quiz.title, quiz.content, newFolderId, userId);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return newFolderId;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getFolderById(folderId: string): Promise<any> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const foldersCollection = conn.collection('folders');
|
||||||
|
|
||||||
|
// const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) });
|
||||||
|
|
||||||
|
// return folder;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getFolderWithContent(folderId: string): Promise<any> {
|
||||||
|
// const folder = await this.getFolderById(folderId);
|
||||||
|
// const content = await this.getContent(folderId);
|
||||||
|
|
||||||
|
// return {
|
||||||
|
// ...folder,
|
||||||
|
// content,
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
@ -1,174 +0,0 @@
|
||||||
//model
|
|
||||||
const db = require('../config/db.js')
|
|
||||||
const { ObjectId } = require('mongodb');
|
|
||||||
const Quiz = require('./quiz.js');
|
|
||||||
|
|
||||||
class Folders {
|
|
||||||
|
|
||||||
async create(title, userId) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
|
||||||
|
|
||||||
if (existingFolder) return new Error('Folder already exists');
|
|
||||||
|
|
||||||
const newFolder = {
|
|
||||||
userId: userId,
|
|
||||||
title: title,
|
|
||||||
created_at: new Date()
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await foldersCollection.insertOne(newFolder);
|
|
||||||
|
|
||||||
return result.insertedId;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getUserFolders(userId) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const result = await foldersCollection.find({ userId: userId }).toArray();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getOwner(folderId) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) });
|
|
||||||
|
|
||||||
return folder.userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getContent(folderId) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const filesCollection = conn.collection('files');
|
|
||||||
|
|
||||||
const result = await filesCollection.find({ folderId: folderId }).toArray();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(folderId) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const folderResult = await foldersCollection.deleteOne({ _id: new ObjectId(folderId) });
|
|
||||||
|
|
||||||
if (folderResult.deletedCount != 1) return false;
|
|
||||||
await Quiz.deleteQuizzesByFolderId(folderId);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
async rename(folderId, newTitle) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const result = await foldersCollection.updateOne({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } })
|
|
||||||
|
|
||||||
if (result.modifiedCount != 1) return false;
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
async duplicate(folderId, userId) {
|
|
||||||
|
|
||||||
const sourceFolder = await this.getFolderWithContent(folderId);
|
|
||||||
|
|
||||||
// Check if the new title already exists
|
|
||||||
let newFolderTitle = sourceFolder.title + "-copie";
|
|
||||||
let counter = 1;
|
|
||||||
|
|
||||||
while (await this.folderExists(newFolderTitle, userId)) {
|
|
||||||
newFolderTitle = `${sourceFolder.title}-copie(${counter})`;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const newFolderId = await this.create(newFolderTitle, userId);
|
|
||||||
|
|
||||||
if (!newFolderId) {
|
|
||||||
throw new Error('Failed to create a duplicate folder.');
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const quiz of sourceFolder.content) {
|
|
||||||
const { title, content } = quiz;
|
|
||||||
//console.log(title);
|
|
||||||
//console.log(content);
|
|
||||||
await Quiz.create(title, content, newFolderId.toString(), userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return newFolderId;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async folderExists(title, userId) {
|
|
||||||
await db.connect();
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
|
||||||
|
|
||||||
return existingFolder !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async copy(folderId, userId) {
|
|
||||||
|
|
||||||
|
|
||||||
const sourceFolder = await this.getFolderWithContent(folderId);
|
|
||||||
const newFolderId = await this.create(sourceFolder.title, userId);
|
|
||||||
if (!newFolderId) {
|
|
||||||
throw new Error('Failed to create a new folder.');
|
|
||||||
}
|
|
||||||
for (const quiz of sourceFolder.content) {
|
|
||||||
await this.createQuiz(quiz.title, quiz.content, newFolderId, userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return newFolderId;
|
|
||||||
|
|
||||||
}
|
|
||||||
async getFolderById(folderId) {
|
|
||||||
await db.connect();
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const foldersCollection = conn.collection('folders');
|
|
||||||
|
|
||||||
const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) });
|
|
||||||
|
|
||||||
return folder;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async getFolderWithContent(folderId) {
|
|
||||||
|
|
||||||
|
|
||||||
const folder = await this.getFolderById(folderId);
|
|
||||||
|
|
||||||
const content = await this.getContent(folderId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...folder,
|
|
||||||
content: content
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new Folders;
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
const db = require('../config/db.js')
|
import db from '../config/db';
|
||||||
const { ObjectId } = require('mongodb');
|
import { ObjectId } from 'mongodb';
|
||||||
|
|
||||||
class Images {
|
export class Image {
|
||||||
|
async upload(file: Express.Multer.File, userId: string): Promise<string> {
|
||||||
async upload(file, userId) {
|
await db.connect();
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
const conn = db.getConnection();
|
||||||
|
|
||||||
const imagesCollection = conn.collection('images');
|
const imagesCollection = conn.collection('images');
|
||||||
|
|
@ -14,16 +13,16 @@ class Images {
|
||||||
file_name: file.originalname,
|
file_name: file.originalname,
|
||||||
file_content: file.buffer.toString('base64'),
|
file_content: file.buffer.toString('base64'),
|
||||||
mime_type: file.mimetype,
|
mime_type: file.mimetype,
|
||||||
created_at: new Date()
|
created_at: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await imagesCollection.insertOne(newImage);
|
const result = await imagesCollection.insertOne(newImage);
|
||||||
|
|
||||||
return result.insertedId;
|
return result.insertedId.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(id) {
|
async get(id: string): Promise<{ file_name: string; file_content: Buffer; mime_type: string } | null> {
|
||||||
await db.connect()
|
await db.connect();
|
||||||
const conn = db.getConnection();
|
const conn = db.getConnection();
|
||||||
|
|
||||||
const imagesCollection = conn.collection('images');
|
const imagesCollection = conn.collection('images');
|
||||||
|
|
@ -35,10 +34,9 @@ class Images {
|
||||||
return {
|
return {
|
||||||
file_name: result.file_name,
|
file_name: result.file_name,
|
||||||
file_content: Buffer.from(result.file_content, 'base64'),
|
file_content: Buffer.from(result.file_content, 'base64'),
|
||||||
mime_type: result.mime_type
|
mime_type: result.mime_type,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new Images;
|
export default new Image();
|
||||||
128
server/models/user.ts
Normal file
128
server/models/user.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
import db from '../config/db';
|
||||||
|
import bcrypt from 'bcrypt';
|
||||||
|
import AppError from '../middleware/AppError';
|
||||||
|
import { USER_ALREADY_EXISTS } from '../constants/errorCodes';
|
||||||
|
import { Folder } from './folder';
|
||||||
|
import { Image } from './image';
|
||||||
|
import { Quiz } from './quiz';
|
||||||
|
|
||||||
|
export class User {
|
||||||
|
constructor(public email: string, public hashedPassword: string, public created_at?: Date, public _id?: string, public folders?: Array<Folder>, public images?: Array<Image>, public quizzes?: Array<Quiz>) {
|
||||||
|
this.email = email;
|
||||||
|
this.hashedPassword = hashedPassword;
|
||||||
|
this.created_at = created_at || new Date();
|
||||||
|
this.folders = folders || new Array<Folder>();
|
||||||
|
this.images = images || new Array<Image>();
|
||||||
|
this.quizzes = quizzes || new Array<Quiz>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// async hashPassword(password: string): Promise<string> {
|
||||||
|
// return await bcrypt.hash(password, 10);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// generatePassword(): string {
|
||||||
|
// return Math.random().toString(36).slice(-8);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async verify(password: string, hash: string): Promise<boolean> {
|
||||||
|
// return await bcrypt.compare(password, hash);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async register(email: string, password: string): Promise<void> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const userCollection = conn.collection('users');
|
||||||
|
|
||||||
|
// const existingUser = await userCollection.findOne({ email });
|
||||||
|
|
||||||
|
// if (existingUser) {
|
||||||
|
// throw new AppError(USER_ALREADY_EXISTS);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const newUser = {
|
||||||
|
// email,
|
||||||
|
// password: await this.hashPassword(password),
|
||||||
|
// created_at: new Date(),
|
||||||
|
// };
|
||||||
|
|
||||||
|
// await userCollection.insertOne(newUser);
|
||||||
|
|
||||||
|
// const folderTitle = 'Dossier par Défaut';
|
||||||
|
// const userId = newUser._id.toString();
|
||||||
|
// await Folders.create(folderTitle, userId);
|
||||||
|
|
||||||
|
// // TODO: verif if inserted properly...
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async login(email: string, password: string): Promise<any> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const userCollection = conn.collection('users');
|
||||||
|
|
||||||
|
// const user = await userCollection.findOne({ email });
|
||||||
|
|
||||||
|
// if (!user) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const passwordMatch = await this.verify(password, user.password);
|
||||||
|
|
||||||
|
// if (!passwordMatch) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return user;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async resetPassword(email: string): Promise<string> {
|
||||||
|
// const newPassword = this.generatePassword();
|
||||||
|
|
||||||
|
// return await this.changePassword(email, newPassword);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async changePassword(email: string, newPassword: string): Promise<string | null> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const userCollection = conn.collection('users');
|
||||||
|
|
||||||
|
// const hashedPassword = await this.hashPassword(newPassword);
|
||||||
|
|
||||||
|
// const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } });
|
||||||
|
|
||||||
|
// if (result.modifiedCount !== 1) return null;
|
||||||
|
|
||||||
|
// return newPassword;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async delete(email: string): Promise<boolean> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const userCollection = conn.collection('users');
|
||||||
|
|
||||||
|
// const result = await userCollection.deleteOne({ email });
|
||||||
|
|
||||||
|
// if (result.deletedCount !== 1) return false;
|
||||||
|
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async getId(email: string): Promise<string | false> {
|
||||||
|
// await db.connect();
|
||||||
|
// const conn = db.getConnection();
|
||||||
|
|
||||||
|
// const userCollection = conn.collection('users');
|
||||||
|
|
||||||
|
// const user = await userCollection.findOne({ email });
|
||||||
|
|
||||||
|
// if (!user) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return user._id;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,121 +0,0 @@
|
||||||
//user
|
|
||||||
const db = require('../config/db.js');
|
|
||||||
const bcrypt = require('bcrypt');
|
|
||||||
const AppError = require('../middleware/AppError.js');
|
|
||||||
const { USER_ALREADY_EXISTS } = require('../constants/errorCodes');
|
|
||||||
const Folders = require('../models/folders.js');
|
|
||||||
|
|
||||||
class Users {
|
|
||||||
|
|
||||||
async hashPassword(password) {
|
|
||||||
return await bcrypt.hash(password, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
generatePassword() {
|
|
||||||
return Math.random().toString(36).slice(-8);
|
|
||||||
}
|
|
||||||
|
|
||||||
async verify(password, hash) {
|
|
||||||
return await bcrypt.compare(password, hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
async register(email, password) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const userCollection = conn.collection('users');
|
|
||||||
|
|
||||||
const existingUser = await userCollection.findOne({ email: email });
|
|
||||||
|
|
||||||
if (existingUser) {
|
|
||||||
throw new AppError(USER_ALREADY_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
const newUser = {
|
|
||||||
email: email,
|
|
||||||
password: await this.hashPassword(password),
|
|
||||||
created_at: new Date()
|
|
||||||
};
|
|
||||||
|
|
||||||
await userCollection.insertOne(newUser);
|
|
||||||
|
|
||||||
const folderTitle = 'Dossier par Défaut';
|
|
||||||
const userId = newUser._id.toString();
|
|
||||||
await Folders.create(folderTitle, userId);
|
|
||||||
|
|
||||||
// TODO: verif if inserted properly...
|
|
||||||
}
|
|
||||||
|
|
||||||
async login(email, password) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const userCollection = conn.collection('users');
|
|
||||||
|
|
||||||
const user = await userCollection.findOne({ email: email });
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const passwordMatch = await this.verify(password, user.password);
|
|
||||||
|
|
||||||
if (!passwordMatch) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
async resetPassword(email) {
|
|
||||||
const newPassword = this.generatePassword();
|
|
||||||
|
|
||||||
return await this.changePassword(email, newPassword);
|
|
||||||
}
|
|
||||||
|
|
||||||
async changePassword(email, newPassword) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const userCollection = conn.collection('users');
|
|
||||||
|
|
||||||
const hashedPassword = await this.hashPassword(newPassword);
|
|
||||||
|
|
||||||
const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } });
|
|
||||||
|
|
||||||
if (result.modifiedCount != 1) return null;
|
|
||||||
|
|
||||||
return newPassword
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(email) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const userCollection = conn.collection('users');
|
|
||||||
|
|
||||||
const result = await userCollection.deleteOne({ email });
|
|
||||||
|
|
||||||
if (result.deletedCount != 1) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getId(email) {
|
|
||||||
await db.connect()
|
|
||||||
const conn = db.getConnection();
|
|
||||||
|
|
||||||
const userCollection = conn.collection('users');
|
|
||||||
|
|
||||||
const user = await userCollection.findOne({ email: email });
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return user._id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new Users;
|
|
||||||
Loading…
Reference in a new issue