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, ), });