diff --git a/server/src/schema/mutations/AddRuleToCompetition.js b/server/src/schema/mutations/AddRuleToCompetition.js new file mode 100644 index 000000000..a165940df --- /dev/null +++ b/server/src/schema/mutations/AddRuleToCompetition.js @@ -0,0 +1,54 @@ +import { + GraphQLNonNull, + GraphQLID, + GraphQLString +} from 'graphql' +import { CompetitionType } from '../types' +import Competition from '../../models/competition' + +const args = { + codeName: { type: new GraphQLNonNull(GraphQLID) }, + title: { type: new GraphQLNonNull(GraphQLString) }, + description: { type: new GraphQLNonNull(GraphQLString) } +} + +const resolve = (parent, args, context) => { + // let authIdOnSession = context.user.sub + return new Promise((resolve, reject) => { + Competition.findOne({ codeName: args.codeName }).exec((err, competition) => { + if (err) reject(err) + else { + if (competition.rules.some(rule => rule.title === args.title)) { + // need to add an error message here TODO + resolve(competition) + } else { + let number = 1 + while (competition.rules.some(rule => rule.number === number)) { + number++ + } + competition.rules.push({ + number, + title: args.title, + description: args.description + }) + competition.save((err) => { + if (err) reject(err) + else { + resolve(competition) + } + }) + } + } + }) + }) +} + +const mutation = { + addRuleToCompetition: { + type: CompetitionType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index 372c2c3a6..ba6d90418 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -4,12 +4,14 @@ import { import HostCompetitionMutation from './HostCompetition' import RegisterToCompetitionMutation from './RegisterToCompetition' +import AddRuleToCompetitionMutation from './AddRuleToCompetition' const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( HostCompetitionMutation, - RegisterToCompetitionMutation + RegisterToCompetitionMutation, + AddRuleToCompetitionMutation ) })