From 60cf41c2f6ab9bfcb5fdf4134aef9761d59d2889 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 16 Mar 2019 12:43:04 +0100 Subject: [PATCH] Setting mutations --- src/schema/mutations/ChangeEventState.js | 39 ------------ src/schema/mutations/CreateEvent.js | 37 ----------- src/schema/mutations/CreateStrategy.js | 31 +++++++++ src/schema/mutations/EditParticipant.js | 81 ------------------------ src/schema/mutations/EditStrategy.js | 39 ++++++++++++ src/schema/mutations/index.js | 8 +-- 6 files changed, 74 insertions(+), 161 deletions(-) delete mode 100644 src/schema/mutations/ChangeEventState.js delete mode 100644 src/schema/mutations/CreateEvent.js create mode 100644 src/schema/mutations/CreateStrategy.js delete mode 100644 src/schema/mutations/EditParticipant.js create mode 100644 src/schema/mutations/EditStrategy.js diff --git a/src/schema/mutations/ChangeEventState.js b/src/schema/mutations/ChangeEventState.js deleted file mode 100644 index ada9367b8..000000000 --- a/src/schema/mutations/ChangeEventState.js +++ /dev/null @@ -1,39 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, -} from 'graphql'; -import { AuthentificationError } from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; -import EventStateEnum from '../types/enum/EventState'; - -const args = { - eventId: { type: new GraphQLNonNull(GraphQLID) }, - state: { type: EventStateEnum }, -}; - -const resolve = (parent, { eventId: _id, state }, { userId: hostId }) => { - if (!hostId) { - throw new AuthentificationError(); - } - - return new Promise((res, rej) => { - Event.findOneAndUpdate({ _id, hostId }, { state }, { new: true }, (err, modifiedEvent) => { - if (err) { - rej(err); - return; - } - res(modifiedEvent); - }); - }); -}; - -const mutation = { - changeEventState: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/src/schema/mutations/CreateEvent.js b/src/schema/mutations/CreateEvent.js deleted file mode 100644 index 2704f53e7..000000000 --- a/src/schema/mutations/CreateEvent.js +++ /dev/null @@ -1,37 +0,0 @@ -import { AuthentificationError } from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; -import { EventInputType } from '../types/input'; - -export const args = { - event: { type: EventInputType }, -}; - -const resolve = (parent, { event }, { userId: hostId }) => { - if (!hostId) { - throw new AuthentificationError(); - } - - event.hostId = hostId; - const newEvent = new Event(event); - - return new Promise((res, rej) => { - newEvent.save((err) => { - if (err) { - rej(err); - return; - } - res(newEvent); - }); - }); -}; - -const mutation = { - createEvent: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/src/schema/mutations/CreateStrategy.js b/src/schema/mutations/CreateStrategy.js new file mode 100644 index 000000000..7a0bfe720 --- /dev/null +++ b/src/schema/mutations/CreateStrategy.js @@ -0,0 +1,31 @@ +import { StrategyType } from '../types'; +import { Strategy } from '../../models'; +import { StrategyInputType } from '../types/input'; + +export const args = { + strategy: { type: StrategyInputType }, +}; + +const resolve = (parent, { strategy }) => { + const newStrategy = new Strategy(strategy); + + return new Promise((res, rej) => { + newStrategy.save((err) => { + if (err) { + rej(err); + return; + } + res(newStrategy); + }); + }); +}; + +const mutation = { + createStrategy: { + type: StrategyType, + args, + resolve, + }, +}; + +export default mutation; diff --git a/src/schema/mutations/EditParticipant.js b/src/schema/mutations/EditParticipant.js deleted file mode 100644 index 20d465a27..000000000 --- a/src/schema/mutations/EditParticipant.js +++ /dev/null @@ -1,81 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, -} from 'graphql'; -import axios from 'axios'; -import { - AuthentificationError, - WrongArgumentsError, - ServiceUnavailableError, -} from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventId: { type: new GraphQLNonNull(GraphQLID) }, -}; - -const resolve = (parent, - { eventId: _id, participant: { teamId, botId, releaseId } }, - { userId, authorization }) => { - if (!userId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - axios({ - url: process.env.GATEWAY_ENDPOINT, - method: 'post', - data: { - query: ` - { - teams_TeamsByRole{ - id - } - } - `, - }, - headers: { authorization }, - }).then( - (result) => { - if (!result.data.data.teamsByRole.some(team => team.id === teamId)) { - rej(new WrongArgumentsError('You are not eligible to edit this team')); - return; - } - Event.findOne({ _id }).exec((err, event) => { - if (err) { - rej(err); - return; - } - const participantIndex = event.participant - .findIndex(participant => participant.teamId === teamId); - - event.participants[participantIndex].botId = botId - || event.participants[participantIndex].botId; - event.participants[participantIndex].releaseId = releaseId - || event.participants[participantIndex].releaseId; - - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }, - (error) => { - rej(new ServiceUnavailableError(error)); - }, - ); - }); -}; - -const mutation = { - editParticipant: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/src/schema/mutations/EditStrategy.js b/src/schema/mutations/EditStrategy.js new file mode 100644 index 000000000..a522b1441 --- /dev/null +++ b/src/schema/mutations/EditStrategy.js @@ -0,0 +1,39 @@ +import { + GraphQLNonNull, + GraphQLID, +} from 'graphql'; +import { StrategyType } from '../types'; +import { Strategy } from '../../models'; +import { StrategyInputType } from '../types/input'; + +const args = { + id: { type: new GraphQLNonNull(GraphQLID) }, + newStrategy: { type: new GraphQLNonNull(StrategyInputType) }, +}; + +const resolve = (parent, { id: _id, newStrategy }) => new Promise((res, rej) => { + Strategy.findOne({ _id }).exec((err, strategy) => { + if (err) { + rej(err); + return; + } + strategy = newStrategy; + strategy.save((error) => { + if (error) { + rej(error); + return; + } + res(strategy); + }); + }); +}); + +const mutation = { + editParticipant: { + type: StrategyType, + args, + resolve, + }, +}; + +export default mutation; diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js index 5c2a45fd5..b5b401e8a 100644 --- a/src/schema/mutations/index.js +++ b/src/schema/mutations/index.js @@ -2,14 +2,14 @@ import { GraphQLObjectType, } from 'graphql'; -import CreateEventMutation from './CreateEvent'; -import ChangeEventStateMutation from './ChangeEventState'; +import CreateStrategyMutation from './CreateStrategy'; +import EditStrategyMutation from './EditStrategy'; const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( - CreateEventMutation, - ChangeEventStateMutation, + CreateStrategyMutation, + EditStrategyMutation, ), });