fix deprecated ObjectId conversion, remove extraneous refs to db and models

This commit is contained in:
C. Fuhrman 2024-10-02 20:23:15 -04:00
parent b2144345d0
commit f1d50315ba
7 changed files with 23 additions and 28 deletions

View file

@ -68,7 +68,7 @@ describe('Folders', () => {
it('should copy a folder and return the new folder ID', async () => { it('should copy a folder and return the new folder ID', async () => {
const folderId = '60c72b2f9b1d8b3a4c8e4d3b'; const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
const userId = '12345'; const userId = '12345';
const newFolderId = new ObjectId(); const newFolderId = ObjectId.createFromTime();
// Mock some quizzes that are in folder.content // Mock some quizzes that are in folder.content
const sourceFolder = { const sourceFolder = {
title: 'Test Folder', title: 'Test Folder',
@ -88,7 +88,7 @@ describe('Folders', () => {
// expect(db.connect).toHaveBeenCalled(); // expect(db.connect).toHaveBeenCalled();
// expect(db.collection).toHaveBeenCalledWith('folders'); // expect(db.collection).toHaveBeenCalledWith('folders');
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); // expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
// expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ userId })); // expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ userId }));
expect(result).toBe(newFolderId); expect(result).toBe(newFolderId);
}); });
@ -106,7 +106,7 @@ describe('Folders', () => {
// expect(db.connect).toHaveBeenCalled(); // expect(db.connect).toHaveBeenCalled();
// expect(db.collection).toHaveBeenCalledWith('folders'); // expect(db.collection).toHaveBeenCalledWith('folders');
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); // expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
}); });
}); });
@ -115,7 +115,7 @@ describe('Folders', () => {
it('should return a folder with content', async () => { it('should return a folder with content', async () => {
const folderId = '60c72b2f9b1d8b3a4c8e4d3b'; const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
const folder = { const folder = {
_id: new ObjectId(folderId), _id: ObjectId.createFromTime(folderId),
title: 'Test Folder', title: 'Test Folder',
}; };
const content = { const content = {
@ -134,7 +134,7 @@ describe('Folders', () => {
// expect(db.connect).toHaveBeenCalled(); // expect(db.connect).toHaveBeenCalled();
// expect(db.collection).toHaveBeenCalledWith('folders'); // expect(db.collection).toHaveBeenCalledWith('folders');
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); // expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
expect(result).toEqual({ expect(result).toEqual({
...folder, ...folder,
content: content content: content
@ -156,7 +156,7 @@ describe('Folders', () => {
// expect(db.connect).toHaveBeenCalled(); // expect(db.connect).toHaveBeenCalled();
// expect(db.collection).toHaveBeenCalledWith('folders'); // expect(db.collection).toHaveBeenCalledWith('folders');
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); // expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
}); });
}); });
@ -200,7 +200,7 @@ describe('Folders', () => {
it('should return a folder by ID', async () => { it('should return a folder by ID', async () => {
const folderId = '60c72b2f9b1d8b3a4c8e4d3b'; const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
const folder = { const folder = {
_id: new ObjectId(folderId), _id: ObjectId.createFromTime(folderId),
title: 'Test Folder', title: 'Test Folder',
}; };
@ -211,7 +211,7 @@ describe('Folders', () => {
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('folders'); expect(db.collection).toHaveBeenCalledWith('folders');
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
expect(result).toEqual(folder); expect(result).toEqual(folder);
}); });
@ -225,7 +225,7 @@ describe('Folders', () => {
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.collection).toHaveBeenCalledWith('folders'); expect(db.collection).toHaveBeenCalledWith('folders');
expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }); expect(collection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromTime(folderId) });
}); });
}); });
}); });

View file

@ -21,7 +21,7 @@ describe('Users', () => {
getConnection: jest.fn().mockReturnThis(), // Add getConnection method getConnection: jest.fn().mockReturnThis(), // Add getConnection method
collection: jest.fn().mockReturnThis(), collection: jest.fn().mockReturnThis(),
findOne: jest.fn(), findOne: jest.fn(),
insertOne: jest.fn().mockResolvedValue({ insertedId: new ObjectId() }), // Mock insertOne to return an ObjectId insertOne: jest.fn().mockResolvedValue({ insertedId: ObjectId.createFromTime() }), // Mock insertOne to return an ObjectId
updateOne: jest.fn(), updateOne: jest.fn(),
deleteOne: jest.fn(), deleteOne: jest.fn(),
}; };
@ -31,7 +31,7 @@ describe('Users', () => {
it('should register a new user', async () => { it('should register a new user', async () => {
db.collection().findOne.mockResolvedValue(null); // No user found db.collection().findOne.mockResolvedValue(null); // No user found
db.collection().insertOne.mockResolvedValue({ insertedId: new ObjectId() }); db.collection().insertOne.mockResolvedValue({ insertedId: ObjectId.createFromTime() });
bcrypt.hash.mockResolvedValue('hashedPassword'); bcrypt.hash.mockResolvedValue('hashedPassword');
Folders.create.mockResolvedValue(true); Folders.create.mockResolvedValue(true);

View file

@ -1,7 +1,4 @@
//controller //controller
const model = require('../models/folders.js');
const db = require('../config/db.js');
const AppError = require('../middleware/AppError.js'); const AppError = require('../middleware/AppError.js');
const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, FOLDER_NOT_FOUND, FOLDER_ALREADY_EXISTS, GETTING_FOLDER_ERROR, DELETE_FOLDER_ERROR, UPDATE_FOLDER_ERROR, MOVING_FOLDER_ERROR, DUPLICATE_FOLDER_ERROR, COPY_FOLDER_ERROR } = require('../constants/errorCodes'); const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, FOLDER_NOT_FOUND, FOLDER_ALREADY_EXISTS, GETTING_FOLDER_ERROR, DELETE_FOLDER_ERROR, UPDATE_FOLDER_ERROR, MOVING_FOLDER_ERROR, DUPLICATE_FOLDER_ERROR, COPY_FOLDER_ERROR } = require('../constants/errorCodes');
@ -9,8 +6,6 @@ const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, FOLDER_NOT_FOUND, FOLDER_AL
class FoldersController { class FoldersController {
constructor(foldersModel) { constructor(foldersModel) {
console.log("FoldersController constructor: db", db)
this.db = db;
this.folders = foldersModel; this.folders = foldersModel;
// this.quizzes = quizModel; // this.quizzes = quizModel;
console.log("FoldersController constructor: folders", this.folders); console.log("FoldersController constructor: folders", this.folders);

View file

@ -181,7 +181,7 @@ class QuizController {
// try { // try {
// //Trouver le quiz a dupliquer // //Trouver le quiz a dupliquer
// const conn = db.getConnection(); // const conn = db.getConnection();
// const quiztoduplicate = await conn.collection('quiz').findOne({ _id: new ObjectId(quizId) }); // const quiztoduplicate = await conn.collection('quiz').findOne({ _id: ObjectId.createFromTime(quizId) });
// if (!quiztoduplicate) { // if (!quiztoduplicate) {
// throw new Error("Quiz non trouvé"); // throw new Error("Quiz non trouvé");
// } // }
@ -189,7 +189,7 @@ class QuizController {
// //Suppression du id du quiz pour ne pas le répliquer // //Suppression du id du quiz pour ne pas le répliquer
// delete quiztoduplicate._id; // delete quiztoduplicate._id;
// //Ajout du duplicata // //Ajout du duplicata
// await conn.collection('quiz').insertOne({ ...quiztoduplicate, userId: new ObjectId(newUserId) }); // await conn.collection('quiz').insertOne({ ...quiztoduplicate, userId: ObjectId.createFromTime(newUserId) });
// res.json(Response.ok("Dossier dupliqué avec succès pour un autre utilisateur")); // res.json(Response.ok("Dossier dupliqué avec succès pour un autre utilisateur"));
// } catch (error) { // } catch (error) {

View file

@ -45,7 +45,7 @@ class Folders {
const foldersCollection = conn.collection('folders'); const foldersCollection = conn.collection('folders');
const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); const folder = await foldersCollection.findOne({ _id: ObjectId.createFromTime(folderId) });
return folder.userId; return folder.userId;
} }
@ -67,7 +67,7 @@ class Folders {
const foldersCollection = conn.collection('folders'); const foldersCollection = conn.collection('folders');
const folderResult = await foldersCollection.deleteOne({ _id: new ObjectId(folderId) }); const folderResult = await foldersCollection.deleteOne({ _id: ObjectId.createFromTime(folderId) });
if (folderResult.deletedCount != 1) return false; if (folderResult.deletedCount != 1) return false;
await this.quizModel.deleteQuizzesByFolderId(folderId); await this.quizModel.deleteQuizzesByFolderId(folderId);
@ -81,7 +81,7 @@ class Folders {
const foldersCollection = conn.collection('folders'); const foldersCollection = conn.collection('folders');
const result = await foldersCollection.updateOne({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } }) const result = await foldersCollection.updateOne({ _id: ObjectId.createFromTime(folderId) }, { $set: { title: newTitle } })
if (result.modifiedCount != 1) return false; if (result.modifiedCount != 1) return false;
@ -151,7 +151,7 @@ class Folders {
const foldersCollection = conn.collection('folders'); const foldersCollection = conn.collection('folders');
const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); const folder = await foldersCollection.findOne({ _id: ObjectId.createFromTime(folderId) });
if (!folder) return new Error(`Folder ${folderId} not found`); if (!folder) return new Error(`Folder ${folderId} not found`);

View file

@ -32,7 +32,7 @@ class Images {
const imagesCollection = conn.collection('images'); const imagesCollection = conn.collection('images');
const result = await imagesCollection.findOne({ _id: new ObjectId(id) }); const result = await imagesCollection.findOne({ _id: ObjectId.createFromTime(id) });
if (!result) return null; if (!result) return null;

View file

@ -37,7 +37,7 @@ class Quiz {
const quizCollection = conn.collection('files'); const quizCollection = conn.collection('files');
const quiz = await quizCollection.findOne({ _id: new ObjectId(quizId) }); const quiz = await quizCollection.findOne({ _id: ObjectId.createFromTime(quizId) });
return quiz.userId; return quiz.userId;
} }
@ -48,7 +48,7 @@ class Quiz {
const quizCollection = conn.collection('files'); const quizCollection = conn.collection('files');
const quiz = await quizCollection.findOne({ _id: new ObjectId(quizId) }); const quiz = await quizCollection.findOne({ _id: ObjectId.createFromTime(quizId) });
return quiz; return quiz;
} }
@ -59,7 +59,7 @@ class Quiz {
const quizCollection = conn.collection('files'); const quizCollection = conn.collection('files');
const result = await quizCollection.deleteOne({ _id: new ObjectId(quizId) }); const result = await quizCollection.deleteOne({ _id: ObjectId.createFromTime(quizId) });
if (result.deletedCount != 1) return false; if (result.deletedCount != 1) return false;
@ -81,7 +81,7 @@ class Quiz {
const quizCollection = conn.collection('files'); const quizCollection = conn.collection('files');
const result = await quizCollection.updateOne({ _id: new ObjectId(quizId) }, { $set: { title: newTitle, content: newContent } }); const result = await quizCollection.updateOne({ _id: ObjectId.createFromTime(quizId) }, { $set: { title: newTitle, content: newContent } });
//Ne fonctionne pas si rien n'est chngé dans le quiz //Ne fonctionne pas si rien n'est chngé dans le quiz
//if (result.modifiedCount != 1) return false; //if (result.modifiedCount != 1) return false;
@ -94,7 +94,7 @@ class Quiz {
const quizCollection = conn.collection('files'); const quizCollection = conn.collection('files');
const result = await quizCollection.updateOne({ _id: new ObjectId(quizId) }, { $set: { folderId: newFolderId } }); const result = await quizCollection.updateOne({ _id: ObjectId.createFromTime(quizId) }, { $set: { folderId: newFolderId } });
if (result.modifiedCount != 1) return false; if (result.modifiedCount != 1) return false;