Creating a "addPrizeToCompetition" mutation
This commit is contained in:
1 parent
3e16e089f0
commit
d5d8c2fbe3
2 files changed
+53
-1
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
GraphQLNonNull,
|
||||
GraphQLID,
|
||||
GraphQLInt,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { CompetitionType } from '../types'
|
||||
import Competition from '../../models/competition'
|
||||
|
||||
const args = {
|
||||
codeName: { type: new GraphQLNonNull(GraphQLID) },
|
||||
position: { type: new GraphQLNonNull(GraphQLInt) },
|
||||
algoPrize: { type: new GraphQLNonNull(GraphQLString) }
|
||||
}
|
||||
|
||||
const resolve = (parent, { codeName, position, algoPrize }, context) => {
|
||||
// let authIdOnSession = context.user.sub
|
||||
return new Promise((resolve, reject) => {
|
||||
Competition.findOne({ codeName }).exec((err, competition) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
if (competition.prizes.some(prize => prize.position === position)) {
|
||||
// need to add an error message here TODO
|
||||
resolve(competition)
|
||||
} else {
|
||||
competition.prizes.push({
|
||||
position,
|
||||
algoPrize
|
||||
})
|
||||
competition.save((err) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
resolve(competition)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const mutation = {
|
||||
addPrizeToCompetition: {
|
||||
type: CompetitionType,
|
||||
args,
|
||||
resolve
|
||||
}
|
||||
}
|
||||
|
||||
export default mutation
|
||||
@@ -8,6 +8,7 @@ import RegisterToCompetitionMutation from './RegisterToCompetition'
|
||||
import EditParticipantOfCompetitionMutation from './EditParticipantOfCompetition'
|
||||
import AddRuleToCompetitionMutation from './AddRuleToCompetition'
|
||||
import EditRuleOfCompetitionMutation from './EditRuleOfCompetition'
|
||||
import AddPrizeToCompetitionMutation from './AddPrizeToCompetition'
|
||||
|
||||
const Mutation = new GraphQLObjectType({
|
||||
name: 'Mutation',
|
||||
@@ -17,7 +18,8 @@ const Mutation = new GraphQLObjectType({
|
||||
RegisterToCompetitionMutation,
|
||||
EditParticipantOfCompetitionMutation,
|
||||
AddRuleToCompetitionMutation,
|
||||
EditRuleOfCompetitionMutation
|
||||
EditRuleOfCompetitionMutation,
|
||||
AddPrizeToCompetitionMutation
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user