Skip to content

Commit

Permalink
fixing find condition for mongodb query
Browse files Browse the repository at this point in the history
  • Loading branch information
mbogo-mit committed Dec 8, 2023
1 parent 2b5f2f8 commit b048e88
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/components/DashboardChannels/DocumentsChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}) => {
Expand Down
15 changes: 13 additions & 2 deletions src/pages/api/annotation/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand Down Expand Up @@ -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) };
}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
30 changes: 22 additions & 8 deletions src/pages/api/document/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
{
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/pages/api/document/slug/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 27 additions & 10 deletions src/pages/api/group/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
{
Expand All @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions src/pages/api/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ 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, {
projection,
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');
Expand Down
13 changes: 8 additions & 5 deletions src/pages/api/ideaspace/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions src/pages/api/outline/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b048e88

Please sign in to comment.