mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
fix duplicate logic
This commit is contained in:
parent
ff6d12e972
commit
1b824782b9
3 changed files with 37 additions and 8 deletions
|
|
@ -52,6 +52,15 @@ describe('Folders', () => {
|
|||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
// throw an error if userId is undefined
|
||||
it('should throw an error if userId is undefined', async () => {
|
||||
const title = 'Test Folder';
|
||||
|
||||
await expect(folders.create(title, undefined)).rejects.toThrow('Missing required parameter(s)');
|
||||
|
||||
expect(db.connect).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw an error if the folder already exists', async () => {
|
||||
const title = 'Existing Folder';
|
||||
const userId = '66fc70bea1b9e87655cf17c9';
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@ class Folders {
|
|||
}
|
||||
|
||||
async create(title, userId) {
|
||||
|
||||
console.log("LOG: create", title, userId);
|
||||
|
||||
if (!title || !userId) {
|
||||
throw new Error('Missing required parameter(s)');
|
||||
}
|
||||
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
|
|
@ -16,10 +23,7 @@ class Folders {
|
|||
|
||||
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
||||
|
||||
console.log(`Folders.create: existingFolder`, existingFolder);
|
||||
|
||||
if (existingFolder) {
|
||||
console.log('Folder already exists, throwing Error');
|
||||
throw new Error('Folder already exists');
|
||||
}
|
||||
|
||||
|
|
@ -51,7 +55,7 @@ class Folders {
|
|||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) });
|
||||
const folder = await foldersCollection.findOne({ _id: ObjectId.createFromHexString(folderId) });
|
||||
|
||||
return folder.userId;
|
||||
}
|
||||
|
|
@ -105,18 +109,30 @@ class Folders {
|
|||
throw new Error(`Folder ${folderId} not found`);
|
||||
}
|
||||
|
||||
const theUserId = userId;
|
||||
// Use the utility function to generate a unique title
|
||||
const newFolderTitle = await generateUniqueTitle(sourceFolder.title, async (title) => {
|
||||
return await foldersCollection.findOne({ title: title, userId: userId });
|
||||
console.log(`generateUniqueTitle(${title}): userId`, theUserId);
|
||||
return await foldersCollection.findOne({ title: title, userId: theUserId });
|
||||
});
|
||||
|
||||
console.log(`duplicate: userId`, userId);
|
||||
const newFolderId = await this.create(newFolderTitle, sourceFolder.content, userId);
|
||||
const newFolderId = await this.create(newFolderTitle, userId);
|
||||
|
||||
if (!newFolderId) {
|
||||
throw new Error('Failed to create duplicate folder');
|
||||
}
|
||||
|
||||
// copy the quizzes from source folder to destination folder
|
||||
const content = await this.getContent(folderId);
|
||||
console.log("folders.duplicate: found content", content);
|
||||
for (const quiz of content) {
|
||||
console.log("folders.duplicate: creating quiz (copy)", quiz);
|
||||
const result = await this.quizModel.create(quiz.title, quiz.content, newFolderId.toString(), userId);
|
||||
if (!result) {
|
||||
throw new Error('Failed to create duplicate quiz');
|
||||
}
|
||||
}
|
||||
|
||||
return newFolderId;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ class Quiz {
|
|||
}
|
||||
|
||||
async create(title, content, folderId, userId) {
|
||||
console.log(`quizzes: create title: ${title}, folderId: ${folderId}, userId: ${userId}`);
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
|
|
@ -16,7 +17,9 @@ class Quiz {
|
|||
|
||||
const existingQuiz = await quizCollection.findOne({ title: title, folderId: folderId, userId: userId })
|
||||
|
||||
if (existingQuiz) return null;
|
||||
if (existingQuiz) {
|
||||
throw new Error(`Quiz already exists with title: ${title}, folderId: ${folderId}, userId: ${userId}`);
|
||||
}
|
||||
|
||||
const newQuiz = {
|
||||
folderId: folderId,
|
||||
|
|
@ -28,6 +31,7 @@ class Quiz {
|
|||
}
|
||||
|
||||
const result = await quizCollection.insertOne(newQuiz);
|
||||
console.log("quizzes: create insertOne result", result);
|
||||
|
||||
return result.insertedId;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue