From db12ec20e41b37f40e507dc2b1253745548f58e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Ili=C4=87?= Date: Mon, 16 Sep 2024 02:11:13 +0200 Subject: [PATCH] drafter fix --- routers/session-router.js | 2 +- tests/admin.spec.js | 18 ++++++ utils/adminPipeline.js | 119 ++++++++++++++------------------------ utils/chatPipeline.js | 19 ++---- utils/database.js | 3 +- utils/friendsPipeline.js | 25 +++----- utils/historyPipeline.js | 62 +++++++------------- utils/loginPipeline.js | 13 ++--- utils/santaPipeline.js | 7 +-- 9 files changed, 105 insertions(+), 163 deletions(-) diff --git a/routers/session-router.js b/routers/session-router.js index 7ccbb57..61e51ff 100644 --- a/routers/session-router.js +++ b/routers/session-router.js @@ -41,7 +41,7 @@ sessionRouter.post('/api/register', async (req, res) => { return res.send({ error: 'Email already used' }); } const result = await createNewUser(req.body); - if (result.insertedId) { + if (result !== null && result.insertedId) { log.info(`User registered: ${req.body.email}`); const temp = { _id: req.body._id, diff --git a/tests/admin.spec.js b/tests/admin.spec.js index 3537598..7a7151d 100644 --- a/tests/admin.spec.js +++ b/tests/admin.spec.js @@ -6,6 +6,8 @@ import { createGroup, inviteUserToGroup, addForbiddenPair, + draftSantaPairs, + revealSantaPairs, } from './helpers/admin.js'; import { createNewGroup, createDraftedGroup } from './helpers/setup.js'; @@ -136,6 +138,22 @@ test.describe('admin tests', () => { 'Forbidden pair already exists.' ); }); + + test('forbidden pairs should not draft each other', async ({ page }) => { + const groupData = await createNewGroup(page.request); + const forbiddenPair = { + forbiddenUser1Id: groupData.users.user1.id, + forbiddenUser2Id: groupData.users.user2.id, + }; + await addForbiddenPair(page.request, forbiddenPair); + await draftSantaPairs(page.request); + await revealSantaPairs(page.request); + await page.goto('/history'); + await page.getByText('N/A').click(); + await expect( + page.getByRole('row', { name: groupData.users.user1.name }).first() + ).not.toHaveText(groupData.users.user2.name); + }); }); test.describe('admin access tests', () => { diff --git a/utils/adminPipeline.js b/utils/adminPipeline.js index 5a8dd2f..37c4be6 100644 --- a/utils/adminPipeline.js +++ b/utils/adminPipeline.js @@ -14,13 +14,10 @@ export async function getUsers(groupId) { }; try { - return await client - .db(process.env.database) - .collection('users') - .find(query, options) - .toArray(); + return await client.collection('users').find(query, options).toArray(); } catch (err) { log.error('getUsers: ' + err); + await client.close(); return null; } } @@ -48,13 +45,10 @@ export async function getUsersAndRoles(groupId) { ]; try { - return await client - .db(process.env.database) - .collection('users') - .aggregate(pipeline) - .toArray(); + return await client.collection('users').aggregate(pipeline).toArray(); } catch (err) { log.error('getUsersAndRoles: ' + err); + await client.close(); return null; } } @@ -64,12 +58,10 @@ export async function checkIfUserExists(email) { const query = { email }; try { - return await client - .db(process.env.database) - .collection('users') - .findOne(query); + return await client.collection('users').findOne(query); } catch (err) { log.error('checkIfUserExists: ' + err); + await client.close(); return null; } } @@ -84,10 +76,7 @@ export async function addUserToGroup(groupId, email, role) { }; try { - const result = await client - .db(process.env.database) - .collection('users') - .updateOne(filter, update); + const result = await client.collection('users').updateOne(filter, update); if (result.acknowledged !== true || result.modifiedCount !== 1) { log.error('ERROR addUserToGroup: failed to add user to the group'); return null; @@ -95,6 +84,7 @@ export async function addUserToGroup(groupId, email, role) { return true; } catch (err) { log.error('addUserToGroup: ' + err); + await client.close(); return null; } } @@ -109,10 +99,7 @@ export async function removeUserFromGroup(userId, groupId) { }; try { - const result = await client - .db(process.env.database) - .collection('users') - .updateOne(filter, update); + const result = await client.collection('users').updateOne(filter, update); if (result.acknowledged !== true || result.modifiedCount !== 1) { log.error( 'removeUserFromGroup: failed to remove the user from the group' @@ -122,6 +109,7 @@ export async function removeUserFromGroup(userId, groupId) { return true; } catch (err) { log.error('removeUserFromGroup: ' + err); + await client.close(); return null; } } @@ -136,12 +124,10 @@ export async function addNewUser(groupId, email, password) { address: { street: '', city: '', postalCode: '', state: '' }, }; try { - return await client - .db(process.env.database) - .collection('users') - .insertOne(user); + return await client.collection('users').insertOne(user); } catch (err) { log.error('addNewUser: ' + err); + await client.close(); return null; } } @@ -149,12 +135,10 @@ export async function addNewUser(groupId, email, password) { export async function createNewUser(user) { const client = await getClient(); try { - return await client - .db(process.env.database) - .collection('users') - .insertOne(user); + return await client.collection('users').insertOne(user); } catch (err) { log.error('createNewUser: ' + err); + await client.close(); return null; } } @@ -171,10 +155,7 @@ export async function updateUsersRoles(groupId, usersRoles) { const update = { $set: { 'groups.$.role': userData.role }, }; - const result = await client - .db(process.env.database) - .collection('users') - .updateOne(filter, update); + const result = await client.collection('users').updateOne(filter, update); if (result.modifiedCount !== 0) { modifiedCount += result.modifiedCount; } @@ -182,6 +163,7 @@ export async function updateUsersRoles(groupId, usersRoles) { return modifiedCount; } catch (err) { log.error('updateUsersRoles: ' + err); + await client.close(); return null; } } @@ -191,12 +173,10 @@ export async function getGroup(groupId) { const query = { _id: groupId }; try { - return await client - .db(process.env.database) - .collection('groups') - .findOne(query); + return await client.collection('groups').findOne(query); } catch (err) { log.error('getGroup: ' + err); + await client.close(); return null; } } @@ -211,10 +191,7 @@ export async function createGroup(groupName) { }; try { - const result = await client - .db(process.env.database) - .collection('groups') - .insertOne(group); + const result = await client.collection('groups').insertOne(group); if (result.acknowledged !== true) { log.error('ERROR createGroup: failed to create new group'); @@ -224,6 +201,7 @@ export async function createGroup(groupName) { return group; } catch (err) { log.error('createGroup: ' + err); + await client.close(); return null; } } @@ -236,12 +214,10 @@ export async function updateGroup(groupId, groupData) { }; try { - return await client - .db(process.env.database) - .collection('groups') - .updateOne(filter, update); + return await client.collection('groups').updateOne(filter, update); } catch (err) { log.error('updateGroup: ' + err); + await client.close(); return null; } } @@ -251,12 +227,10 @@ export async function deleteForbiddenPair(_id) { const filter = { _id: _id }; try { - return await client - .db(process.env.database) - .collection('forbiddenPairs') - .deleteOne(filter); + return await client.collection('forbiddenPairs').deleteOne(filter); } catch (err) { log.error('deleteForbiddenPair: ' + err); + await client.close(); return null; } } @@ -299,12 +273,12 @@ export async function getForbiddenPairs(groupId) { try { return await client - .db(process.env.database) .collection('forbiddenPairs') .aggregate(pipeline) .toArray(); } catch (err) { log.error('getForbiddenPairs: ' + err); + await client.close(); return null; } } @@ -326,35 +300,30 @@ export async function createForbiddenPair(groupId, forbiddenPair) { ), }; - return await client - .db(process.env.database) - .collection('forbiddenPairs') - .insertOne(document); + return await client.collection('forbiddenPairs').insertOne(document); } catch (err) { log.error('createForbiddenPair: ' + err); + await client.close(); return null; } } async function findExistingPair(client, groupId, forbiddenPair) { - return await client - .db(process.env.database) - .collection('forbiddenPairs') - .findOne({ - groupId: groupId, - $or: [ - { - userId: ObjectId.createFromHexString(forbiddenPair.forbiddenUser1Id), - forbiddenPairId: ObjectId.createFromHexString( - forbiddenPair.forbiddenUser2Id - ), - }, - { - userId: ObjectId.createFromHexString(forbiddenPair.forbiddenUser2Id), - forbiddenPairId: ObjectId.createFromHexString( - forbiddenPair.forbiddenUser1Id - ), - }, - ], - }); + return await client.collection('forbiddenPairs').findOne({ + groupId: groupId, + $or: [ + { + userId: ObjectId.createFromHexString(forbiddenPair.forbiddenUser1Id), + forbiddenPairId: ObjectId.createFromHexString( + forbiddenPair.forbiddenUser2Id + ), + }, + { + userId: ObjectId.createFromHexString(forbiddenPair.forbiddenUser2Id), + forbiddenPairId: ObjectId.createFromHexString( + forbiddenPair.forbiddenUser1Id + ), + }, + ], + }); } diff --git a/utils/chatPipeline.js b/utils/chatPipeline.js index f604076..574ff19 100644 --- a/utils/chatPipeline.js +++ b/utils/chatPipeline.js @@ -39,13 +39,10 @@ export async function getChat(groupId) { ]; try { - return await client - .db(process.env.database) - .collection('chat') - .aggregate(pipeline) - .toArray(); + return await client.collection('chat').aggregate(pipeline).toArray(); } catch (err) { log.error('getChat: ' + err); + await client.close(); return null; } } @@ -55,12 +52,10 @@ export async function deleteChatMessage(_id) { const filter = { _id: ObjectId.createFromHexString(_id) }; try { - return await client - .db(process.env.database) - .collection('chat') - .deleteOne(filter); + return await client.collection('chat').deleteOne(filter); } catch (err) { log.error('deleteChatMessage: ' + err); + await client.close(); return null; } } @@ -74,12 +69,10 @@ export async function sendMessage(message, userId, groupId) { timestamp: new Date(), }; try { - return await client - .db(process.env.database) - .collection('chat') - .insertOne(document); + return await client.collection('chat').insertOne(document); } catch (err) { log.error('sendMessage: ' + err); + await client.close(); return null; } } diff --git a/utils/database.js b/utils/database.js index 2894afe..3a0f870 100644 --- a/utils/database.js +++ b/utils/database.js @@ -6,7 +6,8 @@ const database = (function () { function createClient() { const { MongoClient } = mongodb; - return new MongoClient(process.env.mongodbUri); + const client = new MongoClient(process.env.mongodbUri); + return client.db(process.env.database); } return { diff --git a/utils/friendsPipeline.js b/utils/friendsPipeline.js index 4b00f6c..8be02e2 100644 --- a/utils/friendsPipeline.js +++ b/utils/friendsPipeline.js @@ -10,13 +10,10 @@ export async function getFriends(groupId) { const options = { projection: { password: 0, chat: 0 } }; try { - return await client - .db(process.env.database) - .collection('users') - .find(query, options) - .toArray(); + return await client.collection('users').find(query, options).toArray(); } catch (err) { log.error('getFriends: ' + err); + await client.close(); return null; } } @@ -31,12 +28,10 @@ export async function getProfile(_id) { const options = { projection: { password: 0, chat: 0 } }; try { - return await client - .db(process.env.database) - .collection('users') - .findOne(query, options); + return await client.collection('users').findOne(query, options); } catch (err) { log.error('getProfile: ' + err); + await client.close(); return null; } } @@ -53,12 +48,10 @@ export async function updateProfile(_id, friend) { }; try { - return await client - .db(process.env.database) - .collection('users') - .updateOne(filter, update); + return await client.collection('users').updateOne(filter, update); } catch (err) { log.error('updateProfile: ' + err); + await client.close(); return null; } } @@ -73,12 +66,10 @@ export async function updateProfileImage(_id) { }; try { - return await client - .db(process.env.database) - .collection('users') - .updateOne(filter, update); + return await client.collection('users').updateOne(filter, update); } catch (err) { log.error('updateProfileImage: ' + err); + await client.close(); return null; } } diff --git a/utils/historyPipeline.js b/utils/historyPipeline.js index b40a0ba..763c2fb 100644 --- a/utils/historyPipeline.js +++ b/utils/historyPipeline.js @@ -25,13 +25,10 @@ export async function getYearsByGroup(groupId) { const client = await getClient(); try { - return await client - .db(process.env.database) - .collection('history') - .aggregate(pipeline) - .toArray(); + return await client.collection('history').aggregate(pipeline).toArray(); } catch (err) { log.error('getYearsByGroup: ' + err); + await client.close(); return null; } } @@ -108,13 +105,10 @@ export async function getGiftsByYear(groupId, yearId) { const client = await getClient(); try { - return await client - .db(process.env.database) - .collection('history') - .aggregate(pipeline) - .toArray(); + return await client.collection('history').aggregate(pipeline).toArray(); } catch (err) { log.error('getClient: ' + err); + await client.close(); return null; } } @@ -142,12 +136,10 @@ export async function addDraftsForNextYear(groupId, santaPairs) { const client = await getClient(); try { - return await client - .db(process.env.database) - .collection('history') - .insertOne(document); + return await client.collection('history').insertOne(document); } catch (err) { log.error('addDraftsForNextYear: ' + err); + await client.close(); return null; } } @@ -160,14 +152,12 @@ export async function isNextYearDrafted(groupId) { }; try { - const result = await client - .db(process.env.database) - .collection('history') - .findOne(query); + const result = await client.collection('history').findOne(query); return !result; } catch (err) { log.error('isNextYearDrafted: ' + err); + await client.close(); return null; } } @@ -183,14 +173,12 @@ export async function isLastYearRevealed(groupId) { }; try { - const result = await client - .db(process.env.database) - .collection('history') - .findOne(query, options); + const result = await client.collection('history').findOne(query, options); return result?.revealed; } catch (err) { log.error('isLastYearRevealed: ' + err); + await client.close(); return null; } } @@ -204,12 +192,10 @@ export async function setLastYearRevealed(groupId, year) { const update = { $set: { revealed: true } }; try { - return await client - .db(process.env.database) - .collection('history') - .updateOne(filter, update); + return await client.collection('history').updateOne(filter, update); } catch (err) { log.error('setLastYearRevealed: ' + err); + await client.close(); return null; } } @@ -224,12 +210,10 @@ export async function updateLocationImage(yearId) { }; try { - return await client - .db(process.env.database) - .collection('history') - .updateOne(filter, update); + return await client.collection('history').updateOne(filter, update); } catch (err) { log.error('updateLocationImage: ' + err); + await client.close(); return null; } } @@ -247,12 +231,10 @@ export async function updateGiftImage(yearId, giftId) { }; try { - return await client - .db(process.env.database) - .collection('history') - .updateOne(filter, update); + return await client.collection('history').updateOne(filter, update); } catch (err) { log.error('updateGiftImage: ' + err); + await client.close(); return null; } } @@ -267,12 +249,10 @@ export async function updateGiftDescription(giftId, description) { }; try { - return await client - .db(process.env.database) - .collection('history') - .updateOne(filter, update); + return await client.collection('history').updateOne(filter, update); } catch (err) { log.error('updateGiftDescription: ' + err); + await client.close(); return null; } } @@ -287,12 +267,10 @@ export async function updateYearDescription(yearId, description) { }; try { - return await client - .db(process.env.database) - .collection('history') - .updateOne(filter, update); + return await client.collection('history').updateOne(filter, update); } catch (err) { log.error('updateYearDescription: ' + err); + await client.close(); return null; } } diff --git a/utils/loginPipeline.js b/utils/loginPipeline.js index c30c75a..d2de975 100644 --- a/utils/loginPipeline.js +++ b/utils/loginPipeline.js @@ -26,12 +26,10 @@ export async function checkEmail(email) { const options = { projection: { _id: 0 } }; try { - return await client - .db(process.env.database) - .collection('users') - .findOne(query, options); + return await client.collection('users').findOne(query, options); } catch (err) { log.error('checkEmail: ' + err); + await client.close(); return null; } } @@ -104,13 +102,10 @@ async function getUser($match) { ]; try { - return await client - .db(process.env.database) - .collection('users') - .aggregate(pipeline) - .toArray(); + return await client.collection('users').aggregate(pipeline).toArray(); } catch (err) { log.error('getUser: ' + err); + await client.close(); return null; } } diff --git a/utils/santaPipeline.js b/utils/santaPipeline.js index ba71c9b..0e2aa26 100644 --- a/utils/santaPipeline.js +++ b/utils/santaPipeline.js @@ -51,13 +51,10 @@ export async function getSanta(_id, groupId) { const client = await getClient(); try { - return await client - .db(process.env.database) - .collection('history') - .aggregate(pipeline) - .toArray(); + return await client.collection('history').aggregate(pipeline).toArray(); } catch (err) { log.error('getSanta: ' + err); + await client.close(); return null; } }