From 2389aff531dc0b0f67bd579d825ee14a6298fe67 Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Sun, 28 Oct 2018 13:12:59 +0100 Subject: [PATCH] Adding a editPrize mutation --- server/src/schema/mutations/EditPrize.js | 55 ++++++++++++++++++++++++ server/src/schema/mutations/index.js | 2 + 2 files changed, 57 insertions(+) create mode 100644 server/src/schema/mutations/EditPrize.js diff --git a/server/src/schema/mutations/EditPrize.js b/server/src/schema/mutations/EditPrize.js new file mode 100644 index 000000000..6d291199c --- /dev/null +++ b/server/src/schema/mutations/EditPrize.js @@ -0,0 +1,55 @@ +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((resolve, reject) => { + Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { + if (err) { + reject(err) + } else if (!event) { + reject(new DatabaseError('None of the events you own respond to that designator')) + } else { + let prizeIndex + event.prizes.find((prize, index) => { + if (prize.rank === rank) { + prizeIndex = index + } + }) + + event.prizes[prizeIndex].amount = amount || event.prizes[prizeIndex].amount + event.save((err) => { + if (err) reject(err) + else { + resolve(event) + } + }) + } + }) + }) +} + +const mutation = { + editPrize: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index 732ffcba8..a49ba5114 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -10,6 +10,7 @@ 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' @@ -26,6 +27,7 @@ const Mutation = new GraphQLObjectType({ EditRuleMutation, ReorderRulesMutation, AddPrizeMutation, + EditPrizeMutation, CreateFormulaMutation, CreateAndSetFormulaMutation, CreatePlotterMutation,