From b048e88b954fdb009e8dbee518844667a5cca751 Mon Sep 17 00:00:00 2001 From: Joshua Mbogo <55801923+mbogo-mit@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:35:55 -0500 Subject: [PATCH 1/2] fixing find condition for mongodb query --- .../DashboardChannels/DocumentsChannel.js | 3 ++ src/pages/api/annotation/[id].js | 15 +++++++- src/pages/api/annotations.js | 6 ++- src/pages/api/document/[id].js | 30 +++++++++++---- src/pages/api/document/slug/[slug].js | 8 +++- src/pages/api/group/[id].js | 37 ++++++++++++++----- src/pages/api/groups.js | 13 +++++-- src/pages/api/ideaspace/[id].js | 13 ++++--- src/pages/api/outline/[id].js | 11 ++++-- 9 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/components/DashboardChannels/DocumentsChannel.js b/src/components/DashboardChannels/DocumentsChannel.js index 39804270..07141574 100644 --- a/src/components/DashboardChannels/DocumentsChannel.js +++ b/src/components/DashboardChannels/DocumentsChannel.js @@ -195,8 +195,10 @@ export default function DocumentsChannel({ }) .then(async (data) => { const { docs, count } = data; + console.log('docs: ', docs); await addGroupNamesToDocuments(docs) .then((docsWithGroupNames) => { + console.log('docsWithGroupNames: ', docsWithGroupNames) const d = { countByPermissions: { [documentPermissions]: count, @@ -280,6 +282,7 @@ export default function DocumentsChannel({ ? [] : (queriedDocuments || documents[selectedGroupId][documentPermissions]).sort(sortDocuments); + documentTiles = rawDocumentTiles.map(({ _id, title, groups, contributors, updatedAt, slug, owner, state, }) => { diff --git a/src/pages/api/annotation/[id].js b/src/pages/api/annotation/[id].js index 2e6802a4..780b5d52 100644 --- a/src/pages/api/annotation/[id].js +++ b/src/pages/api/annotation/[id].js @@ -48,7 +48,12 @@ const handler = async (req, res) => { .collection('users') .findOne({ _id: ObjectID(token.sub) }); const { role } = userObj; - let findCondition = { _id: ObjectID(req.query.id), 'creator.id': token.sub }; + let findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + { 'creator.id': token.sub }, + ], + }; if (role === 'admin') { findCondition = { _id: ObjectID(req.query.id) }; } @@ -79,7 +84,13 @@ const handler = async (req, res) => { .collection('users') .findOne({ _id: ObjectID(token.sub) }); const { role } = userObj; - let findCondition = { _id: ObjectID(req.query.id), 'creator.id': token.sub }; + let findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + { 'creator.id': token.sub }, + ], + }; + if (role === 'admin') { findCondition = { _id: ObjectID(req.query.id) }; } diff --git a/src/pages/api/annotations.js b/src/pages/api/annotations.js index 426a3987..0064218a 100644 --- a/src/pages/api/annotations.js +++ b/src/pages/api/annotations.js @@ -248,8 +248,10 @@ const handler = async (req, res) => { .findOne({ _id: ObjectID(token.sub) }); const { role } = userObj; let findCondition = { - 'target.document.slug': documentToUpdate.slug, - 'target.document.owner.id': token.sub, + $and: [ + { 'target.document.slug': documentToUpdate.slug }, + { 'target.document.owner.id': token.sub }, + ], }; if (role === 'admin') { findCondition = { 'target.document.slug': documentToUpdate.slug }; diff --git a/src/pages/api/document/[id].js b/src/pages/api/document/[id].js index 5476f905..89c6742c 100644 --- a/src/pages/api/document/[id].js +++ b/src/pages/api/document/[id].js @@ -16,8 +16,12 @@ const handler = async (req, res) => { const userGroups = (userObj.groups && userObj.groups.length > 0) ? userObj.groups.map((group) => group.id) : []; - const findCondition = { _id: ObjectID(req.query.id) }; - if (userObj.role !== 'admin') findCondition.$or = [{ groups: { $in: userGroups } }, { owner: token.sub }]; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + ], + }; + if (userObj.role !== 'admin') findCondition.$and.push({ $or: [{ groups: { $in: userGroups } }, { owner: token.sub }] }); const doc = await db .collection('documents') .find(findCondition) @@ -191,14 +195,20 @@ const handler = async (req, res) => { const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); - const findCondition = { _id: ObjectID(req.query.id) }; - if (userObj.role !== 'admin') findCondition.owner = token.sub; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) } + ], + }; + if (userObj.role !== 'admin') findCondition.$and.push({ owner: token.sub }); const doc = await db .collection('documents') .findOneAndUpdate( { - ...findCondition, - ...groupById, + $and: [ + findCondition, + groupById, + ], }, updateMethods, { @@ -214,8 +224,12 @@ const handler = async (req, res) => { const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); - const findCondition = { _id: ObjectID(req.query.id) }; - if (userObj.role !== 'admin') findCondition.owner = token.sub; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + ], + }; + if (userObj.role !== 'admin') findCondition.$and.push({ owner: token.sub }); const doc = await db .collection('documents') .findOneAndDelete(findCondition); diff --git a/src/pages/api/document/slug/[slug].js b/src/pages/api/document/slug/[slug].js index 74e9f53e..f60b5974 100644 --- a/src/pages/api/document/slug/[slug].js +++ b/src/pages/api/document/slug/[slug].js @@ -16,8 +16,12 @@ const handler = async (req, res) => { const userGroups = (userObj.groups && userObj.groups.length > 0) ? userObj.groups.map((group) => group.id) : []; - const findCondition = { slug: req.query.slug }; - if (userObj.role !== 'admin') findCondition.$or = [{ groups: { $in: userGroups } }, { owner: token.sub }]; + const findCondition = { + $and: [ + { slug: req.query.slug }, + ], + }; + if (userObj.role !== 'admin') findCondition.$and.push({ $or: [{ groups: { $in: userGroups } }, { owner: token.sub }] }); const doc = await db .collection('documents') .find(findCondition) diff --git a/src/pages/api/group/[id].js b/src/pages/api/group/[id].js index 99cb51f6..6e08b799 100644 --- a/src/pages/api/group/[id].js +++ b/src/pages/api/group/[id].js @@ -13,8 +13,15 @@ const handler = async (req, res) => { const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); - const findCondition = { _id: ObjectID(req.query.id) }; - if (userObj.role !== 'admin') findCondition['members.id'] = token.sub; + + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) } + ], + }; + + if (userObj.role !== 'admin') findCondition.$and.push({ 'members.id': token.sub }); + const doc = await db .collection('groups') .find(findCondition) @@ -97,25 +104,31 @@ const handler = async (req, res) => { const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); - const findCondition = { _id: ObjectID(req.query.id) }; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + ], + }; if (userObj.role !== 'admin') { if (req.body.inviteToken) { const tokenObj = await db .collection('inviteTokens') .findOne({ token: req.body.inviteToken }); if (tokenObj.group !== req.query.id) { - findCondition['members.id'] = token.sub; + findCondition.$and.push({ 'members.id': token.sub }); } } else { - findCondition['members.id'] = token.sub; + findCondition.$and.push({ 'members.id': token.sub }); } } const doc = await db .collection('groups') .findOneAndUpdate( - { - ...findCondition, - ...memberById, + { + $and: [ + findCondition, + memberById, + ], }, updateMethods, { @@ -131,8 +144,12 @@ const handler = async (req, res) => { const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); - const findCondition = { _id: ObjectID(req.query.id) }; - if (userObj.role !== 'admin') findCondition.members = { $elemMatch: { id: token.sub, role: 'owner' } }; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) } + ], + }; + if (userObj.role !== 'admin') findCondition.$and.push({ members: { $elemMatch: { id: token.sub, role: 'owner' } } }); const doc = await db .collection('groups') .findOneAndDelete(findCondition); diff --git a/src/pages/api/groups.js b/src/pages/api/groups.js index 132d780b..bce5a8d4 100644 --- a/src/pages/api/groups.js +++ b/src/pages/api/groups.js @@ -12,13 +12,18 @@ const handler = async (req, res) => { const { groupIds } = req.body; if (groupIds) { const { db } = await connectToDatabase(); + const userObj = await db .collection('users') .findOne({ _id: ObjectID(token.sub) }); const groupObjectIds = groupIds.map((id) => ObjectID(id)); - const findCondition = { _id: { $in: groupObjectIds } }; - if (userObj.role !== 'admin') findCondition['members.id'] = token.sub; - const projection = { _id: 1, name: 1 }; + const findCondition = { + $and: [ + { _id: { $in: groupObjectIds } }, + ] + }; + if (userObj.role !== 'admin') findCondition.$and.push({ 'members.id': token.sub }); + const projection = { _id: 1, name: 1, members: 1 }; const arr = await db .collection('groups') .find(findCondition, { @@ -26,6 +31,8 @@ const handler = async (req, res) => { sort: [['_id', -1]], }) .toArray(); + + res.status(200).json({ groups: arr }); } else res.status(400).end('Bad request'); } else res.status(403).end('Invalid or expired token'); diff --git a/src/pages/api/ideaspace/[id].js b/src/pages/api/ideaspace/[id].js index fcd08f04..f50e4ef4 100644 --- a/src/pages/api/ideaspace/[id].js +++ b/src/pages/api/ideaspace/[id].js @@ -34,16 +34,19 @@ const handler = async (req, res) => { updateObj.name = name; } - console.log('updateObj', updateObj) + // console.log('updateObj', updateObj) const { db } = await connectToDatabase(); - const findCondition = { _id: ObjectID(req.query.id), owner: token.sub }; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + { owner: token.sub }, + ], + }; const doc = await db .collection('ideaspaces') .findOneAndUpdate( - { - ...findCondition, - }, + findCondition, { $set: updateObj }, { returnOriginal: false, diff --git a/src/pages/api/outline/[id].js b/src/pages/api/outline/[id].js index 8dc41276..8cb39f3e 100644 --- a/src/pages/api/outline/[id].js +++ b/src/pages/api/outline/[id].js @@ -35,13 +35,16 @@ const handler = async (req, res) => { } const { db } = await connectToDatabase(); - const findCondition = { _id: ObjectID(req.query.id), owner: token.sub }; + const findCondition = { + $and: [ + { _id: ObjectID(req.query.id) }, + { owner: token.sub }, + ], + }; const doc = await db .collection('outlines') .findOneAndUpdate( - { - ...findCondition, - }, + findCondition, { $set: updateObj }, { returnOriginal: false, From e549b2b0e9a2cd0114febcf148c1ecc086994b6f Mon Sep 17 00:00:00 2001 From: Joshua Mbogo <55801923+mbogo-mit@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:49:57 -0500 Subject: [PATCH 2/2] removing console.logs and fixing Document Tile code --- src/components/DashboardChannels/DocumentTile.js | 5 +++-- src/components/DashboardChannels/DocumentsChannel.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/DashboardChannels/DocumentTile.js b/src/components/DashboardChannels/DocumentTile.js index 0d2f41c9..339f7161 100644 --- a/src/components/DashboardChannels/DocumentTile.js +++ b/src/components/DashboardChannels/DocumentTile.js @@ -44,10 +44,11 @@ export default function DocumentTile({ const g = groups.slice().filter((g_item) => g_item); if (g.length > 0) { - const indexOfSelectedGroup = groups.findIndex((grp) => grp?._id === selectedGroupId); + const indexOfSelectedGroup = g.findIndex((grp) => grp?._id === selectedGroupId); const selectedGroup = g.splice(indexOfSelectedGroup, 1)[0]; + tileBadges = [ - , + , ]; } diff --git a/src/components/DashboardChannels/DocumentsChannel.js b/src/components/DashboardChannels/DocumentsChannel.js index 07141574..08b0f4da 100644 --- a/src/components/DashboardChannels/DocumentsChannel.js +++ b/src/components/DashboardChannels/DocumentsChannel.js @@ -195,10 +195,10 @@ export default function DocumentsChannel({ }) .then(async (data) => { const { docs, count } = data; - console.log('docs: ', docs); + await addGroupNamesToDocuments(docs) .then((docsWithGroupNames) => { - console.log('docsWithGroupNames: ', docsWithGroupNames) + const d = { countByPermissions: { [documentPermissions]: count,