Skip to content

Commit 08e7d99

Browse files
authored
N beacons 1 Grp: PR2 (#170)
* Group query added, Create/Join Beacon Mutation upd. * Fix: USER_LOCATION and beacon query. * fix: comments * fix : strict equality. * fix: strict checks. * fix: read access on group iff user is present in it. * comments * fix: == where === fails.
1 parent 85c24e3 commit 08e7d99

File tree

3 files changed

+9252
-13904
lines changed

3 files changed

+9252
-13904
lines changed

graphql/resolvers.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ const resolvers = {
2525
beacon: async (_parent, { id }, { user }) => {
2626
const beacon = await Beacon.findById(id).populate("landmarks leader");
2727
if (!beacon) return new UserInputError("No beacon exists with that id.");
28-
// return beacon iff user in beacon
29-
if (beacon.leader.id === user.id || beacon.followers.includes(user))
28+
// return error iff user not in beacon
29+
if (beacon.leader.id !== user.id && !beacon.followers.includes(user))
3030
return new Error("User should be a part of beacon");
3131
return beacon;
3232
},
33+
group: async (_parent, { id }, { user }) => {
34+
const group = await Group.findById(id).populate("leader members beacons");
35+
if (!group) return new UserInputError("No group exists with that id.");
36+
// return error iff user not in group
37+
if (group.leader.id !== user.id && !group.members.includes(user))
38+
return new Error("User should be a part of the group");
39+
return group;
40+
},
3341
nearbyBeacons: async (_, { location }) => {
3442
// get active beacons
3543
const beacons = await Beacon.find({ expiresAt: { $gte: new Date() } }).populate("leader");
@@ -101,19 +109,25 @@ const resolvers = {
101109
);
102110
},
103111

104-
createBeacon: async (_, { beacon }, { user }) => {
105-
console.log(beacon);
112+
createBeacon: async (_, { beacon, groupID }, { user }) => {
113+
//the group to which this beacon will belong to.
114+
const group = await Group.findById(groupID);
115+
if (!group) return new UserInputError("No group exists with that id.");
116+
if (!group.members.includes(user.id) && group.leader != user.id)
117+
return new Error("User is not a part of the group!");
106118

107119
const beaconDoc = new Beacon({
108120
leader: user.id,
109121
shortcode: nanoid(),
110122
location: beacon.startLocation,
123+
group: group.id,
111124
...beacon,
112125
});
113126
const newBeacon = await beaconDoc.save().then(b => b.populate("leader"));
114127
user.beacons.push(newBeacon.id);
128+
group.beacons.push(newBeacon.id);
115129
await user.save();
116-
130+
await group.save();
117131
return newBeacon;
118132
},
119133

@@ -164,7 +178,22 @@ const resolvers = {
164178

165179
if (!beacon) return new UserInputError("No beacon exists with that shortcode.");
166180
if (beacon.expiresAt < Date.now()) return new Error("Beacon has expired");
167-
if (beacon.followers.includes(user)) return new Error("Already following the beacon");
181+
if (beacon.leader == user.id) return new Error("Already leading the beacon!");
182+
for (let i = 0; i < beacon.followers.length; i++)
183+
if (beacon.followers[i].id === user.id) {
184+
return new Error("Already following the beacon!");
185+
}
186+
const group = await Group.findById(beacon.group);
187+
if (!group) return new UserInputError("No group exists with that id.");
188+
//if the user doesnt belong to the group, add him
189+
if (!group.members.includes(user.id) && group.leader != user.id) {
190+
group.members.push(user.id);
191+
user.groups.push(group.id);
192+
//publish over groupJoined Sub.
193+
pubsub.publish("GROUP_JOINED", { groupJoined: user, groupID: group.id });
194+
195+
await group.save();
196+
}
168197

169198
beacon.followers.push(user);
170199
console.log("user joined beacon: ", user);
@@ -262,7 +291,7 @@ const resolvers = {
262291
},
263292
userLocation: {
264293
subscribe: withFilter(
265-
(_, __, { pubsub }) => pubsub.asyncIterator(["BEACON_LOCATION"]),
294+
(_, __, { pubsub }) => pubsub.asyncIterator(["USER_LOCATION"]),
266295
(payload, variables, { user }) => {
267296
return payload.beaconID === variables.id && payload.userLocation.id !== user.id; // account for self updates
268297
}

graphql/schema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const typeDefs = gql`
3131
followers: [User!]!
3232
route: [Location!]!
3333
landmarks: [Landmark!]!
34+
group: Group!
3435
}
3536
3637
input BeaconInput {
@@ -97,6 +98,7 @@ const typeDefs = gql`
9798
9899
type Query {
99100
beacon(id: ID!): Beacon!
101+
group(id: ID!): Group!
100102
nearbyBeacons(location: LocationInput!): [Beacon!]!
101103
me: User
102104
hello: String
@@ -106,7 +108,7 @@ const typeDefs = gql`
106108
"""
107109
if start time not supplied, default is Date.now
108110
"""
109-
createBeacon(beacon: BeaconInput): Beacon!
111+
createBeacon(beacon: BeaconInput, groupID: String!): Beacon!
110112
createLandmark(landmark: LandmarkInput, beaconID: ID!): Landmark!
111113
register(user: RegistrationInput): User!
112114
"""

0 commit comments

Comments
 (0)