From dd0ac436be2373b50cd8c301254fc3d9cfbd0c4c Mon Sep 17 00:00:00 2001 From: Laurell Seville Date: Fri, 26 Jun 2020 08:26:29 -0700 Subject: [PATCH] added chain deletes to delete event --- nodemon.json | 4 +- .../admin_mutations/admin-remove-event.js | 35 ++++----- resolvers/event_mutations/createEvent.js | 72 ++++++++++--------- schema.graphql | 4 +- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/nodemon.json b/nodemon.json index 7aac7ac54e..f10d0791c4 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,7 +1,7 @@ { "env" : { - "MONGO_USER":"Laurell", - "MONGO_PASSWORD":"ZcLvZYZshDa7Jt7j", + "MONGO_USER":"devtest876", + "MONGO_PASSWORD":"5zsHsI2TiuikxMud", "MONGO_DB":"tenten" } } \ No newline at end of file diff --git a/resolvers/admin_mutations/admin-remove-event.js b/resolvers/admin_mutations/admin-remove-event.js index 0c1d55ad0c..937af97494 100644 --- a/resolvers/admin_mutations/admin-remove-event.js +++ b/resolvers/admin_mutations/admin-remove-event.js @@ -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 @@ -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(); diff --git a/resolvers/event_mutations/createEvent.js b/resolvers/event_mutations/createEvent.js index fb0ff37bca..27741c0f0f 100644 --- a/resolvers/event_mutations/createEvent.js +++ b/resolvers/event_mutations/createEvent.js @@ -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; diff --git a/schema.graphql b/schema.graphql index 3bb6d21cab..428b45f32a 100644 --- a/schema.graphql +++ b/schema.graphql @@ -28,7 +28,7 @@ const typeDefs = gql` creator: User! registrants: [User] admins(adminId: ID): [User] - organization: Organization! + organization: Organization } type EventProject { @@ -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!