Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: showAdminName parameter added and populating leader of beacon #378

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions graphql/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { AuthenticationError, UserInputError, withFilter } = require("apollo-server-express");
const { AuthenticationError, UserInputError, withFilter, PubSub } = require("apollo-server-express");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const { customAlphabet } = require("nanoid");
Expand All @@ -19,7 +19,12 @@ const resolvers = {
Query: {
hello: () => "Hello world!",
me: async (_parent, _args, { user }) => {
await user.populate("groups beacons.leader beacons.landmarks");
const result = await user.populate({
path: "groups",
populate: {
path: "leader members",
},
});
return user;
},
beacon: async (_parent, { id }, { user }) => {
Expand All @@ -36,11 +41,21 @@ const resolvers = {
return beacon;
},
group: async (_parent, { id }, { user }) => {
const group = await Group.findById(id).populate("leader members beacons");
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.");
// 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");

// Check if the user is part of the group
if (group.leader.id !== user.id && !group.members.some(member => member.id === user.id))
throw new Error("User should be a part of the group");

return group;
},
nearbyBeacons: async (_, { location }) => {
Expand Down Expand Up @@ -73,10 +88,37 @@ const resolvers = {
password: await bcrypt.hash(credentials.password, 10),
}),
});
console.log(newUser);
const userObj = await newUser.save();
return userObj;
},

oAuth: async (_parent, { userInput }) => {
const { name, email } = userInput;
let user = await User.findOne({ email });

if (!user) {
const newUser = new User({ name, email });
user = await newUser.save();
}

const anon = false;
const tokenPayload = {
"https://beacon.ccextractor.org": {
anon,
...(email && { email }),
},
};

const token = jwt.sign(tokenPayload, process.env.JWT_SECRET, {
algorithm: "HS256",
subject: user._id.toString(),
expiresIn: "7d",
});

return token;
},

login: async (_parent, { id, credentials }) => {
if (!id && !credentials) return new UserInputError("One of ID and credentials required");

Expand All @@ -93,11 +135,10 @@ const resolvers = {
let anon = true;

if (credentials) {
const valid = email === user.email && (await bcrypt.compare(password, user.password));
const valid = email === user.email && bcrypt.compare(password, user.password);
if (!valid) return new AuthenticationError("credentials don't match");
anon = false;
}

return jwt.sign(
{
"https://beacon.ccextractor.org": {
Expand Down Expand Up @@ -129,6 +170,7 @@ const resolvers = {
...beacon,
});
const newBeacon = await beaconDoc.save().then(b => b.populate("leader"));
console.log(newBeacon);
user.beacons.push(newBeacon.id);
group.beacons.push(newBeacon.id);
await user.save();
Expand Down
9 changes: 9 additions & 0 deletions graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const typeDefs = gql`
leader: [ID!]!
"""
leader: User!
showAdminName: Boolean!
followers: [User!]!
route: [Location!]!
landmarks: [Landmark!]!
Expand All @@ -39,6 +40,7 @@ const typeDefs = gql`
"""
default is Date.now
"""
showAdminName: Boolean
startsAt: Float
expiresAt: Float!
startLocation: LocationInput!
Expand Down Expand Up @@ -104,6 +106,11 @@ const typeDefs = gql`
hello: String
}

input oAuthInput {
email: String
name: String
}

type Mutation {
"""
if start time not supplied, default is Date.now
Expand All @@ -115,6 +122,7 @@ const typeDefs = gql`
one of ID or credentials required (ID for anon)
"""
login(id: ID, credentials: AuthPayload): String
oAuth(userInput: oAuthInput): String
joinBeacon(shortcode: String!): Beacon!
updateBeaconLocation(id: ID!, location: LocationInput!): Beacon!
updateUserLocation(id: ID!, location: LocationInput!): User!
Expand All @@ -129,6 +137,7 @@ const typeDefs = gql`
userLocation(id: ID!): User
beaconJoined(id: ID!): User
groupJoined(groupID: ID!): User
groupUpdate(groupId: ID!): ID!
}

schema {
Expand Down
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { permissions } from "./permissions/index.js";
import pubsub from "./pubsub.js";

const server = new ApolloServer({
schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }), permissions),
schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }),permissions),
// schema: makeExecutableSchema({ typeDefs, resolvers }), // to temp disable shield on dev
context: async ({ req, connection }) => {
// initialize context even if it comes from subscription connection
Expand Down
1 change: 1 addition & 0 deletions models/beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const beaconSchema = new Schema(
startsAt: { type: Date, default: Date.now },
expiresAt: { type: Date, required: true },
leader: { type: Schema.Types.ObjectId, required: true, ref: "User" },
showAdminName: { type: Boolean, default: false },
location: LocationSchema,
followers: [UserSchema],
route: [LocationSchema],
Expand Down
Loading