Ensure room title uniqueness by normalizing case sensitivity

This commit is contained in:
NouhailaAater 2025-02-24 04:09:55 -05:00
parent bf2e6502f3
commit b5547cb100
3 changed files with 235 additions and 226 deletions

View file

@ -26,7 +26,7 @@ export const RoomProvider = ({ children }: { children: React.ReactNode }) => {
setSelectedRoom(defaultRoom); setSelectedRoom(defaultRoom);
localStorage.setItem('selectedRoomId', defaultRoom._id); localStorage.setItem('selectedRoomId', defaultRoom._id);
} else { } else {
const randomRoomName = `Room-${Math.floor(Math.random() * 10000)}`; const randomRoomName = `Room-${Math.floor(Math.random() * 1000000)}`;
await createRoom(randomRoomName); await createRoom(randomRoomName);
} }
}; };

View file

@ -1,8 +1,14 @@
const AppError = require('../middleware/AppError.js'); const AppError = require("../middleware/AppError.js");
const { MISSING_REQUIRED_PARAMETER, ROOM_NOT_FOUND, ROOM_ALREADY_EXISTS, GETTING_ROOM_ERROR, DELETE_ROOM_ERROR, UPDATE_ROOM_ERROR } = require('../constants/errorCodes'); const {
MISSING_REQUIRED_PARAMETER,
ROOM_NOT_FOUND,
ROOM_ALREADY_EXISTS,
GETTING_ROOM_ERROR,
DELETE_ROOM_ERROR,
UPDATE_ROOM_ERROR,
} = require("../constants/errorCodes");
class RoomsController { class RoomsController {
constructor(roomsModel) { constructor(roomsModel) {
this.rooms = roomsModel; this.rooms = roomsModel;
this.getRoomTitle = this.getRoomTitle.bind(this); this.getRoomTitle = this.getRoomTitle.bind(this);
@ -16,21 +22,24 @@ create = async (req, res, next) => {
throw new AppError(MISSING_REQUIRED_PARAMETER); throw new AppError(MISSING_REQUIRED_PARAMETER);
} }
const result = await this.rooms.create(title, req.user.userId); const normalizedTitle = title.toLowerCase();
if (!result) { const roomExists = await this.rooms.roomExists(normalizedTitle);
if (roomExists) {
throw new AppError(ROOM_ALREADY_EXISTS); throw new AppError(ROOM_ALREADY_EXISTS);
} }
return res.status(200).json({ const result = await this.rooms.create(normalizedTitle, req.user.userId);
message: 'Room cree avec succes.',
roomId: result.insertedId
});
return res.status(200).json({
message: "Room cree avec succes.",
roomId: result.insertedId,
});
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
getUserRooms = async (req, res, next) => { getUserRooms = async (req, res, next) => {
try { try {
@ -41,13 +50,12 @@ create = async (req, res, next) => {
} }
return res.status(200).json({ return res.status(200).json({
data: rooms data: rooms,
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
getRoomContent = async (req, res, next) => { getRoomContent = async (req, res, next) => {
try { try {
@ -70,13 +78,12 @@ create = async (req, res, next) => {
} }
return res.status(200).json({ return res.status(200).json({
data: content data: content,
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
delete = async (req, res, next) => { delete = async (req, res, next) => {
try { try {
@ -99,13 +106,12 @@ create = async (req, res, next) => {
} }
return res.status(200).json({ return res.status(200).json({
message: 'Salle supprimé avec succès.' message: "Salle supprimé avec succès.",
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
rename = async (req, res, next) => { rename = async (req, res, next) => {
try { try {
@ -134,13 +140,12 @@ create = async (req, res, next) => {
} }
return res.status(200).json({ return res.status(200).json({
message: 'Salle mis <20> jours avec succ<63>s.' message: "Salle mis <20> jours avec succ<63>s.",
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
getRoomById = async (req, res, next) => { getRoomById = async (req, res, next) => {
try { try {
@ -164,12 +169,12 @@ create = async (req, res, next) => {
} }
return res.status(200).json({ return res.status(200).json({
data: room data: room,
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
getRoomTitle = async (req, res, next) => { getRoomTitle = async (req, res, next) => {
try { try {
const { roomId } = req.params; const { roomId } = req.params;
@ -188,7 +193,7 @@ create = async (req, res, next) => {
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
roomExists = async (req, res, next) => { roomExists = async (req, res, next) => {
try { try {
const { title } = req.body; const { title } = req.body;
@ -197,15 +202,18 @@ create = async (req, res, next) => {
throw new AppError(MISSING_REQUIRED_PARAMETER); throw new AppError(MISSING_REQUIRED_PARAMETER);
} }
const exists = await this.rooms.roomExists(title); const normalizedTitle = title.toLowerCase();
const exists = await this.rooms.roomExists(normalizedTitle);
return res.status(200).json({ return res.status(200).json({
exists: exists message: `La salle avec le titre "${title}" existe déjà.`,
exists: exists,
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
getRoomTitleByUserId = async (req, res, next) => { getRoomTitleByUserId = async (req, res, next) => {
try { try {
const { userId } = req.params; const { userId } = req.params;
@ -220,17 +228,15 @@ create = async (req, res, next) => {
throw new AppError(ROOM_NOT_FOUND); throw new AppError(ROOM_NOT_FOUND);
} }
const roomTitles = rooms.map(room => room.title); const roomTitles = rooms.map((room) => room.title);
return res.status(200).json({ return res.status(200).json({
titles: roomTitles titles: roomTitles,
}); });
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
} };
} }
module.exports = RoomsController; module.exports = RoomsController;

View file

@ -16,7 +16,9 @@ class Rooms {
const roomsCollection = conn.collection('rooms'); const roomsCollection = conn.collection('rooms');
const existingRoom = await roomsCollection.findOne({ title: title, userId: userId }); const normalizedTitle = title.toLowerCase();
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId });
if (existingRoom) { if (existingRoom) {
throw new Error('Room already exists'); throw new Error('Room already exists');
@ -101,7 +103,8 @@ class Rooms {
const conn = this.db.getConnection(); const conn = this.db.getConnection();
const roomsCollection = conn.collection('rooms'); const roomsCollection = conn.collection('rooms');
const existingRoom = await roomsCollection.findOne({ title: title }); const normalizedTitle = title.toLowerCase();
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle });
return existingRoom ? true : false; return existingRoom ? true : false;
} }