mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
AppErreur lancée par les contrôleurs, Erreur lancée par les modèles.
This commit is contained in:
parent
3855eca6c6
commit
0d56fa246d
2 changed files with 161 additions and 156 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
const AppError = require("../middleware/AppError");
|
|
||||||
|
|
||||||
jest.mock("../middleware/AppError", () => {
|
jest.mock("../middleware/AppError", () => {
|
||||||
const actualAppError = jest.requireActual("../middleware/AppError");
|
const actualAppError = jest.requireActual("../middleware/AppError");
|
||||||
|
|
||||||
|
|
@ -47,7 +45,7 @@ describe("Rooms", () => {
|
||||||
|
|
||||||
it("should throw error when userId is missing", async () => {
|
it("should throw error when userId is missing", async () => {
|
||||||
await expect(rooms.create("test", undefined)).rejects.toThrowError(
|
await expect(rooms.create("test", undefined)).rejects.toThrowError(
|
||||||
new AppError("Missing required parameter(s)", 400)
|
new Error("Missing required parameter(s)", 400)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -59,7 +57,7 @@ describe("Rooms", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(rooms.create("existing room", "12345")).rejects.toThrowError(
|
await expect(rooms.create("existing room", "12345")).rejects.toThrowError(
|
||||||
new AppError("Une salle avec ce nom existe déjà", 409)
|
new Error("Room already exists", 409)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -211,9 +209,9 @@ describe("Rooms", () => {
|
||||||
const result = await rooms.roomExists(title);
|
const result = await rooms.roomExists(title);
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
expect(db.connect).toHaveBeenCalled();
|
||||||
expect(db.collection).toHaveBeenCalledWith("rooms");
|
expect(db.collection).toHaveBeenCalledWith('rooms');
|
||||||
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
expect(collection.findOne).toHaveBeenCalledWith({ title });
|
||||||
expect(result).toBe(false);
|
expect(result).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -243,7 +241,7 @@ describe("Rooms", () => {
|
||||||
collection.findOne.mockResolvedValue(null);
|
collection.findOne.mockResolvedValue(null);
|
||||||
|
|
||||||
await expect(rooms.getRoomById(roomId)).rejects.toThrowError(
|
await expect(rooms.getRoomById(roomId)).rejects.toThrowError(
|
||||||
new AppError(`Room ${roomId} not found`, 404)
|
new Error(`Room ${roomId} not found`, 404)
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(db.connect).toHaveBeenCalled();
|
expect(db.connect).toHaveBeenCalled();
|
||||||
|
|
|
||||||
|
|
@ -1,48 +1,44 @@
|
||||||
const AppError = require("../middleware/AppError");
|
|
||||||
|
|
||||||
const ObjectId = require("mongodb").ObjectId;
|
const ObjectId = require("mongodb").ObjectId;
|
||||||
|
|
||||||
class Rooms {
|
class Rooms
|
||||||
constructor(db) {
|
{
|
||||||
|
constructor(db)
|
||||||
|
{
|
||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(title, userId) {
|
async create(title, userId)
|
||||||
try {
|
{
|
||||||
if (!title || !userId) {
|
if (!title || !userId) {
|
||||||
throw new AppError("Missing required parameter(s)", 400);
|
throw new Error('Missing required parameter(s)', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
const roomsCollection = conn.collection("rooms");
|
const roomsCollection = conn.collection('rooms');
|
||||||
const normalizedTitle = title.toLowerCase();
|
const normalizedTitle = title.toLowerCase();
|
||||||
|
|
||||||
const existingRoom = await roomsCollection.findOne({
|
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId });
|
||||||
title: normalizedTitle,
|
|
||||||
userId: userId,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (existingRoom) {
|
if (existingRoom) {
|
||||||
throw new AppError("Une salle avec ce nom existe déjà", 409);
|
throw new Error('Room already exists', 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newRoom = {
|
const newRoom = {
|
||||||
userId: userId,
|
userId: userId,
|
||||||
title: title,
|
title: title,
|
||||||
created_at: new Date(),
|
created_at: new Date()
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await roomsCollection.insertOne(newRoom);
|
const result = await roomsCollection.insertOne(newRoom);
|
||||||
|
|
||||||
return result.insertedId;
|
return result.insertedId;
|
||||||
} catch (error) {
|
|
||||||
console.error("Error in create function:", error);
|
|
||||||
throw new AppError(error.message || "Internal Server Error", 500);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserRooms(userId) {
|
|
||||||
|
async getUserRooms(userId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
@ -53,7 +49,8 @@ class Rooms {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOwner(roomId) {
|
async getOwner(roomId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
@ -66,11 +63,13 @@ class Rooms {
|
||||||
return room.userId;
|
return room.userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getContent(roomId) {
|
async getContent(roomId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
const roomsCollection = conn.collection("rooms");
|
const roomsCollection = conn.collection("rooms");
|
||||||
if (!ObjectId.isValid(roomId)) {
|
if (!ObjectId.isValid(roomId))
|
||||||
|
{
|
||||||
return null; // Évite d'envoyer une requête invalide
|
return null; // Évite d'envoyer une requête invalide
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,7 +78,8 @@ class Rooms {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(roomId) {
|
async delete(roomId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
@ -94,7 +94,8 @@ class Rooms {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async rename(roomId, userId, newTitle) {
|
async rename(roomId, userId, newTitle)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
@ -118,21 +119,25 @@ class Rooms {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async roomExists(title) {
|
async roomExists(title)
|
||||||
// Ajouter userId en paramètre
|
{ // Ajouter userId en paramètre
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
const existingRoom = await conn.collection("rooms").findOne({
|
const existingRoom = await conn.collection('rooms').findOne({
|
||||||
title: title.toLowerCase(),
|
title: title.toLowerCase()
|
||||||
});
|
});
|
||||||
return !!existingRoom;
|
return existingRoom ? true : false;
|
||||||
} catch (_error) {
|
} catch (error)
|
||||||
throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs
|
{
|
||||||
|
throw new Error(`Database error (${error})`, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoomById(roomId) {
|
|
||||||
|
async getRoomById(roomId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
@ -142,12 +147,13 @@ class Rooms {
|
||||||
_id: ObjectId.createFromHexString(roomId),
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!room) throw new AppError(`Room ${roomId} not found`, 404);
|
if (!room) throw new Error(`Room ${roomId} not found`, 404);
|
||||||
|
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoomWithContent(roomId) {
|
async getRoomWithContent(roomId)
|
||||||
|
{
|
||||||
const room = await this.getRoomById(roomId);
|
const room = await this.getRoomById(roomId);
|
||||||
|
|
||||||
const content = await this.getContent(roomId);
|
const content = await this.getContent(roomId);
|
||||||
|
|
@ -157,7 +163,8 @@ class Rooms {
|
||||||
content: content,
|
content: content,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
async getRoomTitleByUserId(userId) {
|
async getRoomTitleByUserId(userId)
|
||||||
|
{
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue