Skip to content

Commit

Permalink
beacons leader fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
RunTerror committed Feb 8, 2024
1 parent 24d89cb commit 736e699
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const Group = require("../models/group.js");
const Landmark = require("../models/landmark.js");
const { User } = require("../models/user.js");
const { MongoServerError } = require("mongodb");
const user = require("../models/user.js");

const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// even if we generate 10 IDs per hour,
Expand Down Expand Up @@ -37,12 +36,20 @@ const resolvers = {
return beacon;
},
group: async (_parent, { id }, { user }) => {
const group = await Group.findById(id).populate("leader members beacons");
if (!group) return new UserInputError("No group exists with that id.");
// return error iff user not in group
if (group.leader.id !== user.id && !group.members.includes(user))
return new Error("User should be a part of the group");
return group;
const group = await Group.findById(id).populate('leader members').populate({
path: 'beacons',
populate: {
path: 'leader',
},
});

if (!group) return new UserInputError("No group exists with that id.");
// Check if the user is part of the group
if (group.leader.id !== user.id && !group.members.includes(user))
throw new Error("User should be a part of the group");

console.log(`group: ${group}`);
return group;
},
nearbyBeacons: async (_, { location }) => {
// get active beacons
Expand Down

0 comments on commit 736e699

Please sign in to comment.