Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drafter fix #108

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion routers/session-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/admin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
createGroup,
inviteUserToGroup,
addForbiddenPair,
draftSantaPairs,
revealSantaPairs,
} from './helpers/admin.js';
import { createNewGroup, createDraftedGroup } from './helpers/setup.js';

Expand Down Expand Up @@ -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', () => {
Expand Down
119 changes: 44 additions & 75 deletions utils/adminPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -84,17 +76,15 @@ 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;
}
return true;
} catch (err) {
log.error('addUserToGroup: ' + err);
await client.close();
return null;
}
}
Expand All @@ -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'
Expand All @@ -122,6 +109,7 @@ export async function removeUserFromGroup(userId, groupId) {
return true;
} catch (err) {
log.error('removeUserFromGroup: ' + err);
await client.close();
return null;
}
}
Expand All @@ -136,25 +124,21 @@ 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;
}
}

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;
}
}
Expand All @@ -171,17 +155,15 @@ 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;
}
}
return modifiedCount;
} catch (err) {
log.error('updateUsersRoles: ' + err);
await client.close();
return null;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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');
Expand All @@ -224,6 +201,7 @@ export async function createGroup(groupName) {
return group;
} catch (err) {
log.error('createGroup: ' + err);
await client.close();
return null;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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
),
},
],
});
}
19 changes: 6 additions & 13 deletions utils/chatPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading