From 6e5932581c6ccbc0eec460615368f87b4149b2d9 Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Fri, 19 Oct 2018 18:42:32 +0200 Subject: [PATCH] Creating a "editParticipantOfCompetition" mutation --- .../mutations/EditParticipantOfCompetition.js | 51 +++++++++++++++++++ server/src/schema/mutations/index.js | 4 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 server/src/schema/mutations/EditParticipantOfCompetition.js diff --git a/server/src/schema/mutations/EditParticipantOfCompetition.js b/server/src/schema/mutations/EditParticipantOfCompetition.js new file mode 100644 index 000000000..0cb37d749 --- /dev/null +++ b/server/src/schema/mutations/EditParticipantOfCompetition.js @@ -0,0 +1,51 @@ +import { + GraphQLNonNull, + GraphQLID, + GraphQLString +} from 'graphql' +import { CompetitionType } from '../types' +import Competition from '../../models/competition' + +const args = { + codeName: { type: new GraphQLNonNull(GraphQLID) }, + devTeam: { type: new GraphQLNonNull(GraphQLString) }, + bot: { type: GraphQLString }, + release: { type: GraphQLString } +} + +const resolve = (parent, { codeName, devTeam, bot, release }, context) => { + // let authIdOnSession = context.user.sub + return new Promise((resolve, reject) => { + Competition.findOne({ codeName }).exec((err, competition) => { + if (err) reject(err) + else { + let participantIndex + competition.participants.find((participant, index) => { + if (participant.devTeam === devTeam) { + participantIndex = index + } + }) + + competition.participants[participantIndex].bot = bot || competition.participants[participantIndex].bot + competition.participants[participantIndex].release = release || competition.participants[participantIndex].release + + competition.save((err) => { + if (err) reject(err) + else { + resolve(competition) + } + }) + } + }) + }) +} + +const mutation = { + editParticipantOfCompetition: { + type: CompetitionType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index 4ae05f2ce..475977133 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -7,6 +7,7 @@ import RegisterToCompetitionMutation from './RegisterToCompetition' import AddRuleToCompetitionMutation from './AddRuleToCompetition' import EditCompetitionMutation from './EditCompetition' import EditRuleOfCompetitionMutation from './EditRuleOfCompetition' +import EditParticipantOfCompetitionMutation from './EditParticipantOfCompetition' const Mutation = new GraphQLObjectType({ name: 'Mutation', @@ -15,7 +16,8 @@ const Mutation = new GraphQLObjectType({ RegisterToCompetitionMutation, AddRuleToCompetitionMutation, EditCompetitionMutation, - EditRuleOfCompetitionMutation + EditRuleOfCompetitionMutation, + EditParticipantOfCompetitionMutation ) })