Skip to content

Commit

Permalink
added chain deletes to delete event
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurell876 committed Jun 26, 2020
1 parent 13bc93b commit dd0ac43
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"env" : {
"MONGO_USER":"Laurell",
"MONGO_PASSWORD":"ZcLvZYZshDa7Jt7j",
"MONGO_USER":"devtest876",
"MONGO_PASSWORD":"5zsHsI2TiuikxMud",
"MONGO_DB":"tenten"
}
}
35 changes: 18 additions & 17 deletions resolvers/admin_mutations/admin-remove-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ const Event = require("../../models/Event");
module.exports = async (parent, args, context, info) => {
authCheck(context);
try {
//find event
let event = await Event.findOne({ _id: args.eventId });
if (!event) throw new Error("Event does not exist");

//ensure organization exists
let org = await Organization.findOne({ _id: args.organizationId });
let org = await Organization.findOne({ _id: event.organization });
if (!org) throw new Error("Organization not found");

//ensure user is an admin
Expand All @@ -21,22 +25,19 @@ module.exports = async (parent, args, context, info) => {
throw new Error("User does not exist");
}

//find event
let event = await Event.findOne({ _id: args.eventId });
if (!event) throw new Error("Event does not exist");

//remove event from organization
// org.overwrite({
// ...org._doc,
// events: org._doc.events.filter((event) => event != args.eventId),
// });
// await org.save();

// //remove post from user
// user.overwrite({
// ...user._doc,
// events: user._doc.events.filter((event) => event != args.eventId),
// });
//remove event from user
user.overwrite({
...user._doc,
eventAdmin: user._doc.eventAdmin.filter(
(eventAdmin) => eventAdmin != event.id
),
createdEvents: user._doc.createdEvents.filter(
(createdEvent) => createdEvent != event.id
),
registeredEvents: user._doc.registeredEvents.filter(
(registeredEvent) => registeredEvent != event.id
),
});

await user.save();

Expand Down
72 changes: 37 additions & 35 deletions resolvers/event_mutations/createEvent.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
const User = require("../../models/User");
const Event = require("../../models/Event");
const Organization = require("../../models/Organization");

const createEvent = async (parent, args, context, info) => {
//authentication check
if (!context.isAuth) throw new Error("User is not authenticated");
//authentication check
if (!context.isAuth) throw new Error("User is not authenticated");

try {
//gets user in token - to be used later on
let userFound = await User.findOne({ _id: context.userId });
if (!userFound) {
throw new Error("User does not exist");
}
try {
//gets user in token - to be used later on
let userFound = await User.findOne({ _id: context.userId });
if (!userFound) {
throw new Error("User does not exist");
}

let newEvent = new Event({
...args.data,
creator: context.userId,
registrants: [context.userId],
admins: [context.userId],
organization: args.data.organizationId
});
await newEvent.save();
//ensure organization exists
let org = await Organization.findOne({ _id: args.data.organizationId });
if (!org) throw new Error("Organization not found");


//add event to the user record
await User.updateOne(
{ _id: userFound.id },
{
// $set: {
// createdEvents: [...userFound.createdEvents, newEvent],
// eventAdmin: [...userFound.eventAdmin, newEvent],
// },
$push: {
eventAdmin: newEvent,
let newEvent = new Event({
...args.data,
creator: context.userId,
registrants: [context.userId],
admins: [context.userId],
organization: args.data.organizationId,
});
await newEvent.save();

//add event to the user record
await User.updateOne(
{ _id: userFound.id },
{
$push: {
eventAdmin: newEvent,
createdEvents: newEvent,
registeredEvents: newEvent,
},
}
);
},
}
);

return {
...newEvent._doc,
};
} catch (e) {
throw e;
}
return {
...newEvent._doc,
};
} catch (e) {
throw e;
}
};

module.exports = createEvent;
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const typeDefs = gql`
creator: User!
registrants: [User]
admins(adminId: ID): [User]
organization: Organization!
organization: Organization
}

type EventProject {
Expand Down Expand Up @@ -191,7 +191,7 @@ const typeDefs = gql`
removeMember (data: UserAndOrganizationInput!) : User!

adminRemovePost(organizationId: ID!, postId:ID!):Post!
adminRemoveEvent(organizationId: ID!,eventId: ID!): Event!
adminRemoveEvent(eventId: ID!): Event!
adminRemoveGroupChat(organizationId:ID!, groupChatId:ID!):Message!

createPost(data: PostInput!): Post!
Expand Down

0 comments on commit dd0ac43

Please sign in to comment.