@@ -25,11 +25,19 @@ const resolvers = {
25
25
beacon : async ( _parent , { id } , { user } ) => {
26
26
const beacon = await Beacon . findById ( id ) . populate ( "landmarks leader" ) ;
27
27
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 ) )
30
30
return new Error ( "User should be a part of beacon" ) ;
31
31
return beacon ;
32
32
} ,
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
+ } ,
33
41
nearbyBeacons : async ( _ , { location } ) => {
34
42
// get active beacons
35
43
const beacons = await Beacon . find ( { expiresAt : { $gte : new Date ( ) } } ) . populate ( "leader" ) ;
@@ -101,19 +109,25 @@ const resolvers = {
101
109
) ;
102
110
} ,
103
111
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!" ) ;
106
118
107
119
const beaconDoc = new Beacon ( {
108
120
leader : user . id ,
109
121
shortcode : nanoid ( ) ,
110
122
location : beacon . startLocation ,
123
+ group : group . id ,
111
124
...beacon ,
112
125
} ) ;
113
126
const newBeacon = await beaconDoc . save ( ) . then ( b => b . populate ( "leader" ) ) ;
114
127
user . beacons . push ( newBeacon . id ) ;
128
+ group . beacons . push ( newBeacon . id ) ;
115
129
await user . save ( ) ;
116
-
130
+ await group . save ( ) ;
117
131
return newBeacon ;
118
132
} ,
119
133
@@ -164,7 +178,22 @@ const resolvers = {
164
178
165
179
if ( ! beacon ) return new UserInputError ( "No beacon exists with that shortcode." ) ;
166
180
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
+ }
168
197
169
198
beacon . followers . push ( user ) ;
170
199
console . log ( "user joined beacon: " , user ) ;
@@ -262,7 +291,7 @@ const resolvers = {
262
291
} ,
263
292
userLocation : {
264
293
subscribe : withFilter (
265
- ( _ , __ , { pubsub } ) => pubsub . asyncIterator ( [ "BEACON_LOCATION " ] ) ,
294
+ ( _ , __ , { pubsub } ) => pubsub . asyncIterator ( [ "USER_LOCATION " ] ) ,
266
295
( payload , variables , { user } ) => {
267
296
return payload . beaconID === variables . id && payload . userLocation . id !== user . id ; // account for self updates
268
297
}
0 commit comments