Adding a editPrize mutation
This commit is contained in:
1 parent
d3f0a610c1
commit
2389aff531
2 files changed
+57
No files matched your search
@@ -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
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user