From 7347d472bd9b58a725ec0a65712992d715047a90 Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Tue, 6 Nov 2018 06:38:56 +0100 Subject: [PATCH] Adding a new mutation for updating event status --- .../src/schema/mutations/ChangeEventStatus.js | 39 +++++++++++++++++++ server/src/schema/mutations/index.js | 2 + 2 files changed, 41 insertions(+) create mode 100644 server/src/schema/mutations/ChangeEventStatus.js diff --git a/server/src/schema/mutations/ChangeEventStatus.js b/server/src/schema/mutations/ChangeEventStatus.js new file mode 100644 index 000000000..8e95007b0 --- /dev/null +++ b/server/src/schema/mutations/ChangeEventStatus.js @@ -0,0 +1,39 @@ +import { + GraphQLNonNull, + GraphQLID, +} from 'graphql'; +import { AuthentificationError } from '../../errors'; +import { EventType } from '../types'; +import { Event } from '../../models'; +import EventStatusEnum from '../types/enum/EventStatus'; + +const args = { + eventId: { type: new GraphQLNonNull(GraphQLID) }, + status: { type: EventStatusEnum }, +}; + +const resolve = (parent, { eventId: _id, status }, { userId: hostId }) => { + if (!hostId) { + throw new AuthentificationError(); + } + + return new Promise((res, rej) => { + Event.findOneAndUpdate({ _id, hostId }, { status }, { new: true }, (err, modifiedEvent) => { + if (err) { + rej(err); + return; + } + res(modifiedEvent); + }); + }); +}; + +const mutation = { + changeEventStatus: { + type: EventType, + args, + resolve, + }, +}; + +export default mutation; diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index 1f621f13e..5aa9b2354 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -8,6 +8,7 @@ import RegisterToEventMutation from './RegisterToEvent'; import EditParticipantMutation from './EditParticipant'; import CreateFormulaMutation from './CreateFormula'; import CreatePlotterMutation from './CreatePlotter'; +import ChangeEventStatusMutation from './ChangeEventStatus'; const Mutation = new GraphQLObjectType({ name: 'Mutation', @@ -18,6 +19,7 @@ const Mutation = new GraphQLObjectType({ EditParticipantMutation, CreateFormulaMutation, CreatePlotterMutation, + ChangeEventStatusMutation, ), });