From 73e1dbcdb8886779bcc9ec13814d3b688c4e8a01 Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Sun, 4 Nov 2018 16:05:07 +0100 Subject: [PATCH] Removing userless mutation and adapting CreateEvent with new format --- server/src/schema/mutations/AddPrize.js | 62 ---------------- server/src/schema/mutations/AddRule.js | 70 ------------------- .../schema/mutations/CreateAndSetFormula.js | 56 --------------- .../schema/mutations/CreateAndSetPlotter.js | 65 ----------------- server/src/schema/mutations/CreateEvent.js | 65 +++++++++++++++++ server/src/schema/mutations/EditPrize.js | 56 --------------- server/src/schema/mutations/EditRule.js | 64 ----------------- server/src/schema/mutations/HostEvent.js | 52 -------------- server/src/schema/mutations/ReorderRules.js | 60 ---------------- server/src/schema/mutations/index.js | 18 +---- 10 files changed, 67 insertions(+), 501 deletions(-) delete mode 100644 server/src/schema/mutations/AddPrize.js delete mode 100644 server/src/schema/mutations/AddRule.js delete mode 100644 server/src/schema/mutations/CreateAndSetFormula.js delete mode 100644 server/src/schema/mutations/CreateAndSetPlotter.js create mode 100644 server/src/schema/mutations/CreateEvent.js delete mode 100644 server/src/schema/mutations/EditPrize.js delete mode 100644 server/src/schema/mutations/EditRule.js delete mode 100644 server/src/schema/mutations/HostEvent.js delete mode 100644 server/src/schema/mutations/ReorderRules.js diff --git a/server/src/schema/mutations/AddPrize.js b/server/src/schema/mutations/AddPrize.js deleted file mode 100644 index ff22e69ab..000000000 --- a/server/src/schema/mutations/AddPrize.js +++ /dev/null @@ -1,62 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLInt, -} from 'graphql'; -import { - AuthentificationError, - DatabaseError, - WrongArgumentsError, -} from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - rank: { type: new GraphQLNonNull(GraphQLInt) }, - amount: { type: new GraphQLNonNull(GraphQLInt) }, -}; - -const resolve = (parent, { eventDesignator, rank, amount }, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - if (event.prizes.some(prize => prize.rank === rank)) { - rej(new WrongArgumentsError('A prize for that rank already exists')); - return; - } - event.prizes.push({ - rank, - amount, - }); - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }); -}; - -const mutation = { - addPrize: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/AddRule.js b/server/src/schema/mutations/AddRule.js deleted file mode 100644 index c7b43cdb5..000000000 --- a/server/src/schema/mutations/AddRule.js +++ /dev/null @@ -1,70 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLString, -} from 'graphql'; -import { - AuthentificationError, - DatabaseError, - WrongArgumentsError, -} from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - title: { type: new GraphQLNonNull(GraphQLString) }, - description: { type: new GraphQLNonNull(GraphQLString) }, -}; - -const resolve = (parent, { eventDesignator, title, description }, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - if (event.rules.some(rule => rule.title === title)) { - rej(new WrongArgumentsError('A prize for that rank already exists')); - return; - } - const isFree = pos => event.rules.some(rule => rule.pos === pos); - let position = 1; - let shouldGrow = isFree(position); - while (shouldGrow) { - position += 1; - shouldGrow = isFree(position); - } - event.rules.push({ - position, - title, - description, - }); - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }); -}; - -const mutation = { - addRule: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/CreateAndSetFormula.js b/server/src/schema/mutations/CreateAndSetFormula.js deleted file mode 100644 index d1d45970f..000000000 --- a/server/src/schema/mutations/CreateAndSetFormula.js +++ /dev/null @@ -1,56 +0,0 @@ -import { - GraphQLNonNull, - GraphQLString, -} from 'graphql'; -import { AuthentificationError, DatabaseError } from '../../errors'; -import { EventType } from '../types'; -import { Event, Formula } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLString) }, - name: { type: new GraphQLNonNull(GraphQLString) }, -}; - -const resolve = (parent, { name, eventDesignator }, context) => { - const ownerId = context.userId; - if (!ownerId) { - throw new AuthentificationError(); - } - const newFormula = new Formula({ name, ownerId }); - return new Promise((res, reject) => { - Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { - if (err) { - reject(err); - return; - } - if (!event) { - reject(new DatabaseError('None of the events you own respond to that designator')); - return; - } - newFormula.save((error) => { - if (error) { - reject(error); - return; - } - event.formula = newFormula._id; - event.save((eventError) => { - if (eventError) { - reject(eventError); - return; - } - res(event); - }); - }); - }); - }); -}; - -const mutation = { - createAndSetFormula: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/CreateAndSetPlotter.js b/server/src/schema/mutations/CreateAndSetPlotter.js deleted file mode 100644 index 11cbfe846..000000000 --- a/server/src/schema/mutations/CreateAndSetPlotter.js +++ /dev/null @@ -1,65 +0,0 @@ -import { - GraphQLNonNull, - GraphQLString, -} from 'graphql'; -import { AuthentificationError, DatabaseError } from '../../errors'; -import { EventType } from '../types'; -import { Event, Plotter } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLString) }, - name: { type: new GraphQLNonNull(GraphQLString) }, - host: { type: new GraphQLNonNull(GraphQLString) }, - repo: { type: new GraphQLNonNull(GraphQLString) }, - moduleName: { type: new GraphQLNonNull(GraphQLString) }, -}; - -const resolve = (parent, { - name, host, repo, moduleName, eventDesignator, -}, context) => { - const ownerId = context.userId; - if (!ownerId) { - throw new AuthentificationError(); - } - const newPlotter = new Plotter({ - name, host, repo, moduleName, ownerId, - }); - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - - newPlotter.save((error) => { - if (error) { - rej(error); - return; - } - - event.plotter = newPlotter._id; - event.save((eventError) => { - if (eventError) { - rej(eventError); - return; - } - res(event); - }); - }); - }); - }); -}; - -const mutation = { - createAndSetPlotter: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/CreateEvent.js b/server/src/schema/mutations/CreateEvent.js new file mode 100644 index 000000000..abb920a3f --- /dev/null +++ b/server/src/schema/mutations/CreateEvent.js @@ -0,0 +1,65 @@ +import { AuthentificationError } from '../../errors'; +import { EventType } from '../types'; +import { Event, Formula, Plotter } from '../../models'; +import { slugify } from '../../utils/functions'; +import { EventInputType } from '../types/input'; + +export const args = { + event: { type: EventInputType }, +}; + +const resolve = (parent, { event }, context) => { + const hostId = context.userId; + if (!hostId) { + throw new AuthentificationError(); + } + + const eventSqueleton = { ...event }; + delete eventSqueleton.plotter; + delete eventSqueleton.formula; + eventSqueleton.hostId = hostId; + + const newEvent = new Event(eventSqueleton); + newEvent.designator = `${slugify(newEvent.name)}-${newEvent._id}`; + const { formula, plotter } = event; + + return new Promise((res, rej) => { + if (formula) { + formula.ownerId = hostId; + const newFormula = new Formula(formula); + newFormula.save((err) => { + if (err) { + rej(err); + } + }); + newEvent.formulaId = newFormula._id; + } + if (plotter) { + plotter.ownerId = hostId; + const newPlotter = new Plotter(plotter); + newPlotter.save((err) => { + if (err) { + rej(err); + } + }); + newEvent.plotterId = newPlotter._id; + } + newEvent.save((err) => { + if (err) { + rej(err); + return; + } + res(newEvent); + }); + }); +}; + +const mutation = { + createEvent: { + type: EventType, + args, + resolve, + }, +}; + +export default mutation; diff --git a/server/src/schema/mutations/EditPrize.js b/server/src/schema/mutations/EditPrize.js deleted file mode 100644 index 279733d85..000000000 --- a/server/src/schema/mutations/EditPrize.js +++ /dev/null @@ -1,56 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLInt, -} from 'graphql'; -import { AuthentificationError, DatabaseError } from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - rank: { type: new GraphQLNonNull(GraphQLInt) }, - amount: { type: GraphQLInt }, -}; - -const resolve = (parent, { eventDesignator, rank, amount }, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - const prizeIndex = event.prizes.findIndex(prize => prize.rank === rank); - if (!prizeIndex) { - rej(new DatabaseError('There is no prize for that index')); - return; - } - event.prizes[prizeIndex].amount = amount || event.prizes[prizeIndex].amount; - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }); -}; - -const mutation = { - editPrize: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/EditRule.js b/server/src/schema/mutations/EditRule.js deleted file mode 100644 index c931151ce..000000000 --- a/server/src/schema/mutations/EditRule.js +++ /dev/null @@ -1,64 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLString, -} from 'graphql'; -import { AuthentificationError, DatabaseError, WrongArgumentsError } from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - title: { type: new GraphQLNonNull(GraphQLString) }, - newtitle: { type: GraphQLString }, - description: { type: GraphQLString }, -}; - -const resolve = (parent, { - eventDesignator, title, newtitle, description, -}, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - const ruleIndex = event.rules.findIndex(rule => rule.title === title); - if (!ruleIndex) { - rej(new DatabaseError('There is no rule with that title')); - return; - } - event.rules[ruleIndex].description = description || event.rules[ruleIndex].description; - if (event.rules.some(rule => rule.title === newtitle)) { - rej(new WrongArgumentsError('There is already a rule using your new title')); - return; - } - event.rules[ruleIndex].title = newtitle || event.rules[ruleIndex].title; - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }); -}; - -const mutation = { - editRule: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/HostEvent.js b/server/src/schema/mutations/HostEvent.js deleted file mode 100644 index 13fd6b494..000000000 --- a/server/src/schema/mutations/HostEvent.js +++ /dev/null @@ -1,52 +0,0 @@ -import { - GraphQLNonNull, - GraphQLInt, - GraphQLString, -} from 'graphql'; -import { AuthentificationError } from '../../errors'; -import { EventType } from '../types'; -import { Event } from '../../models'; -import { slugify } from '../../utils/functions'; - -const args = { - name: { type: new GraphQLNonNull(GraphQLString) }, - description: { type: new GraphQLNonNull(GraphQLString) }, - startDatetime: { type: new GraphQLNonNull(GraphQLInt) }, - finishDatetime: { type: new GraphQLNonNull(GraphQLInt) }, -}; - -const resolve = (parent, { - name, description, startDatetime, finishDatetime, -}, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - const newEvent = new Event({ - hostId, - name, - description, - startDatetime, - finishDatetime, - }); - newEvent.designator = `${slugify(newEvent.name)}-${newEvent._id}`; - return new Promise((res, rej) => { - newEvent.save((err) => { - if (err) { - rej(err); - return; - } - res(newEvent); - }); - }); -}; - -const mutation = { - hostEvent: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/ReorderRules.js b/server/src/schema/mutations/ReorderRules.js deleted file mode 100644 index 8f16546aa..000000000 --- a/server/src/schema/mutations/ReorderRules.js +++ /dev/null @@ -1,60 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLInt, -} from 'graphql'; -import { AuthentificationError, DatabaseError } from '../../errors'; -import { isBetween } from '../../utils/functions'; -import { EventType } from '../types'; -import { Event } from '../../models'; - -const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - fromPosition: { type: new GraphQLNonNull(GraphQLInt) }, - toPosition: { type: new GraphQLNonNull(GraphQLInt) }, -}; - -const resolve = (parent, { eventDesignator, fromPosition, toPosition }, context) => { - const hostId = context.userId; - if (!hostId) { - throw new AuthentificationError(); - } - return new Promise((res, rej) => { - Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { - if (err) { - rej(err); - return; - } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - const delta = fromPosition > toPosition ? 1 : -1; - event.rules.forEach((part) => { - if (part.position === fromPosition) { - part.position = toPosition; - } else if (isBetween(part.position, fromPosition, toPosition)) { - part.position += delta; - } - }); - - event.save((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); - }); - }); -}; - -const mutation = { - reorderRules: { - type: EventType, - args, - resolve, - }, -}; - -export default mutation; diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index ffefe7dcf..1f621f13e 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -2,36 +2,22 @@ import { GraphQLObjectType, } from 'graphql'; -import HostEventMutation from './HostEvent'; +import CreateEventMutation from './CreateEvent'; import EditEventMutation from './EditEvent'; import RegisterToEventMutation from './RegisterToEvent'; import EditParticipantMutation from './EditParticipant'; -import AddRuleMutation from './AddRule'; -import EditRuleMutation from './EditRule'; -import ReorderRulesMutation from './ReorderRules'; -import AddPrizeMutation from './AddPrize'; -import EditPrizeMutation from './EditPrize'; import CreateFormulaMutation from './CreateFormula'; -import CreateAndSetFormulaMutation from './CreateAndSetFormula'; import CreatePlotterMutation from './CreatePlotter'; -import CreateAndSetPlotterMutation from './CreateAndSetPlotter'; const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( - HostEventMutation, + CreateEventMutation, EditEventMutation, RegisterToEventMutation, EditParticipantMutation, - AddRuleMutation, - EditRuleMutation, - ReorderRulesMutation, - AddPrizeMutation, - EditPrizeMutation, CreateFormulaMutation, - CreateAndSetFormulaMutation, CreatePlotterMutation, - CreateAndSetPlotterMutation, ), });