fix duplicate logic

This commit is contained in:
C. Fuhrman 2024-10-03 21:56:56 -04:00
parent ff6d12e972
commit 1b824782b9
3 changed files with 37 additions and 8 deletions

View file

@ -52,6 +52,15 @@ describe('Folders', () => {
expect(result).toBeDefined(); 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 () => { it('should throw an error if the folder already exists', async () => {
const title = 'Existing Folder'; const title = 'Existing Folder';
const userId = '66fc70bea1b9e87655cf17c9'; const userId = '66fc70bea1b9e87655cf17c9';

View file

@ -9,6 +9,13 @@ class Folders {
} }
async create(title, userId) { async create(title, userId) {
console.log("LOG: create", title, userId);
if (!title || !userId) {
throw new Error('Missing required parameter(s)');
}
await this.db.connect() await this.db.connect()
const conn = this.db.getConnection(); const conn = this.db.getConnection();
@ -16,10 +23,7 @@ class Folders {
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId }); const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
console.log(`Folders.create: existingFolder`, existingFolder);
if (existingFolder) { if (existingFolder) {
console.log('Folder already exists, throwing Error');
throw new Error('Folder already exists'); throw new Error('Folder already exists');
} }
@ -51,7 +55,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.createFromHexString(folderId) });
return folder.userId; return folder.userId;
} }
@ -105,18 +109,30 @@ class Folders {
throw new Error(`Folder ${folderId} not found`); throw new Error(`Folder ${folderId} not found`);
} }
const theUserId = userId;
// Use the utility function to generate a unique title // Use the utility function to generate a unique title
const newFolderTitle = await generateUniqueTitle(sourceFolder.title, async (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, userId);
const newFolderId = await this.create(newFolderTitle, sourceFolder.content, userId);
if (!newFolderId) { if (!newFolderId) {
throw new Error('Failed to create duplicate folder'); 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; return newFolderId;
} }

View file

@ -9,6 +9,7 @@ class Quiz {
} }
async create(title, content, folderId, userId) { async create(title, content, folderId, userId) {
console.log(`quizzes: create title: ${title}, folderId: ${folderId}, userId: ${userId}`);
await this.db.connect() await this.db.connect()
const conn = this.db.getConnection(); const conn = this.db.getConnection();
@ -16,7 +17,9 @@ class Quiz {
const existingQuiz = await quizCollection.findOne({ title: title, folderId: folderId, userId: userId }) 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 = { const newQuiz = {
folderId: folderId, folderId: folderId,
@ -28,6 +31,7 @@ class Quiz {
} }
const result = await quizCollection.insertOne(newQuiz); const result = await quizCollection.insertOne(newQuiz);
console.log("quizzes: create insertOne result", result);
return result.insertedId; return result.insertedId;
} }