From 959f9668e6a4f75b45aa1f53b60051305f1ccc9f Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Sat, 27 Oct 2018 15:59:45 +0200 Subject: [PATCH] Complete refactoring --- server/src/models/competition.js | 40 ------------- server/src/models/event.js | 46 +++++++++++++++ server/src/models/formula.js | 11 ++++ server/src/models/index.js | 9 +++ server/src/models/plotter.js | 14 +++++ server/src/schema/mutations/AddPrize.js | 35 ++++++----- server/src/schema/mutations/AddRule.js | 32 +++++----- .../schema/mutations/CreateAndSetFormula.js | 46 +++++++++++++++ .../schema/mutations/CreateAndSetPlotter.js | 49 +++++++++++++++ server/src/schema/mutations/CreateFormula.js | 33 +++++++++++ server/src/schema/mutations/CreatePlotter.js | 36 +++++++++++ .../src/schema/mutations/EditCompetition.js | 59 ------------------- server/src/schema/mutations/EditEvent.js | 52 ++++++++++++++++ .../src/schema/mutations/EditParticipant.js | 30 +++++----- server/src/schema/mutations/EditRule.js | 28 ++++----- .../src/schema/mutations/HostCompetition.js | 47 --------------- server/src/schema/mutations/HostEvent.js | 45 ++++++++++++++ .../schema/mutations/RegisterToCompetition.js | 50 ---------------- .../src/schema/mutations/RegisterToEvent.js | 50 ++++++++++++++++ server/src/schema/mutations/ReorderRules.js | 34 +++++------ server/src/schema/mutations/index.js | 22 ++++--- server/src/schema/queries/Competitions.js | 29 --------- .../schema/queries/CompetitionsByDevTeam.js | 35 ----------- .../src/schema/queries/CompetitionsByHost.js | 35 ----------- server/src/schema/queries/Events.js | 35 +++++++++++ server/src/schema/queries/EventsByHost.js | 39 ++++++++++++ server/src/schema/queries/EventsByTeam.js | 39 ++++++++++++ server/src/schema/queries/Formulas.js | 21 +++++++ server/src/schema/queries/Plotters.js | 21 +++++++ server/src/schema/queries/index.js | 16 +++-- .../{OtherPrize.js => AdditionalPrize.js} | 4 +- .../schema/types/{Competition.js => Event.js} | 20 ++++--- server/src/schema/types/Formula.js | 17 ++++++ server/src/schema/types/Participant.js | 6 +- server/src/schema/types/Plotter.js | 8 ++- server/src/schema/types/Prize.js | 12 ++-- server/src/schema/types/Rule.js | 2 +- server/src/schema/types/index.js | 10 ++-- 38 files changed, 704 insertions(+), 413 deletions(-) delete mode 100644 server/src/models/competition.js create mode 100644 server/src/models/event.js create mode 100644 server/src/models/formula.js create mode 100644 server/src/models/index.js create mode 100644 server/src/models/plotter.js create mode 100644 server/src/schema/mutations/CreateAndSetFormula.js create mode 100644 server/src/schema/mutations/CreateAndSetPlotter.js create mode 100644 server/src/schema/mutations/CreateFormula.js create mode 100644 server/src/schema/mutations/CreatePlotter.js delete mode 100644 server/src/schema/mutations/EditCompetition.js create mode 100644 server/src/schema/mutations/EditEvent.js delete mode 100644 server/src/schema/mutations/HostCompetition.js create mode 100644 server/src/schema/mutations/HostEvent.js delete mode 100644 server/src/schema/mutations/RegisterToCompetition.js create mode 100644 server/src/schema/mutations/RegisterToEvent.js delete mode 100644 server/src/schema/queries/Competitions.js delete mode 100644 server/src/schema/queries/CompetitionsByDevTeam.js delete mode 100644 server/src/schema/queries/CompetitionsByHost.js create mode 100644 server/src/schema/queries/Events.js create mode 100644 server/src/schema/queries/EventsByHost.js create mode 100644 server/src/schema/queries/EventsByTeam.js create mode 100644 server/src/schema/queries/Formulas.js create mode 100644 server/src/schema/queries/Plotters.js rename server/src/schema/types/{OtherPrize.js => AdditionalPrize.js} (73%) rename server/src/schema/types/{Competition.js => Event.js} (72%) create mode 100644 server/src/schema/types/Formula.js diff --git a/server/src/models/competition.js b/server/src/models/competition.js deleted file mode 100644 index e7dbc4c0b..000000000 --- a/server/src/models/competition.js +++ /dev/null @@ -1,40 +0,0 @@ -import mongoose, { Schema } from 'mongoose' - -const competitionSchema = new Schema({ - codeName: String, - displayName: String, - host: String, - description: String, - startDatetime: Number, - finishDatetime: Number, - formula: String, - plotter: { - codeName: String, - host: String, - repo: String, - moduleName: String - }, - rules: [{ - number: Number, - title: String, - description: String - }], - prizes: [{ - position: Number, - algoPrize: Number, - otherPrizes: [{ - condition: String, - amount: Number, - asset: String - }] - }], - participants: [{ - devTeam: String, - bot: String, - release: String - }] -}) - -const Competition = mongoose.model('Competition', competitionSchema) - -export default Competition diff --git a/server/src/models/event.js b/server/src/models/event.js new file mode 100644 index 000000000..31d910b5c --- /dev/null +++ b/server/src/models/event.js @@ -0,0 +1,46 @@ +import mongoose, { Schema } from 'mongoose' +import Plotter from './plotter' +import Formula from './formula' + +const eventSchema = new Schema({ + designator: String, + name: String, + hostId: String, + description: String, + startDatetime: Number, + finishDatetime: Number, + formula: { type: Schema.Types.ObjectId, ref: Formula.modelName }, + plotter: { type: Schema.Types.ObjectId, ref: Plotter.modelName }, + rules: [{ + position: Number, + title: String, + description: String + }], + prizes: [{ + rank: Number, + amount: Number, + additional: [{ + condition: String, + amount: Number, + asset: String + }] + }], + participants: [{ + teamId: String, + botId: String, + releaseId: String + }] +}) + +eventSchema.pre('find', function () { + this.populate('formula').populate('plotter') +}) +eventSchema.post('save', function (doc, next) { + doc.populate('formula').populate('plotter').execPopulate().then(function () { + next() + }) +}) + +const Event = mongoose.model('Event', eventSchema) + +export default Event diff --git a/server/src/models/formula.js b/server/src/models/formula.js new file mode 100644 index 000000000..24e7015f4 --- /dev/null +++ b/server/src/models/formula.js @@ -0,0 +1,11 @@ +import mongoose, { Schema } from 'mongoose' + +const formulaSchema = new Schema({ + ownerId: String, + isTemplate: Boolean, + name: String +}) + +const Formula = mongoose.model('Formula', formulaSchema) + +export default Formula diff --git a/server/src/models/index.js b/server/src/models/index.js new file mode 100644 index 000000000..b35809dd1 --- /dev/null +++ b/server/src/models/index.js @@ -0,0 +1,9 @@ +import Plotter from './plotter' +import Event from './event' +import Formula from './formula' + +export { + Event, + Plotter, + Formula +} diff --git a/server/src/models/plotter.js b/server/src/models/plotter.js new file mode 100644 index 000000000..90c3f5d4f --- /dev/null +++ b/server/src/models/plotter.js @@ -0,0 +1,14 @@ +import mongoose, { Schema } from 'mongoose' + +const plotterSchema = new Schema({ + ownerId: String, + isTemplate: Boolean, + name: String, + host: String, + repo: String, + moduleName: String +}) + +const Plotter = mongoose.model('Plotter', plotterSchema) + +export default Plotter diff --git a/server/src/schema/mutations/AddPrize.js b/server/src/schema/mutations/AddPrize.js index 9fdda53e6..0e31a3ba1 100644 --- a/server/src/schema/mutations/AddPrize.js +++ b/server/src/schema/mutations/AddPrize.js @@ -1,36 +1,35 @@ import { GraphQLNonNull, GraphQLID, - GraphQLInt, - GraphQLString + GraphQLInt } from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' +import { EventType } from '../types' +import { Event } from '../../models' const args = { - competitionCodeName: { type: new GraphQLNonNull(GraphQLID) }, - position: { type: new GraphQLNonNull(GraphQLInt) }, - algoPrize: { type: new GraphQLNonNull(GraphQLString) } + eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, + rank: { type: new GraphQLNonNull(GraphQLInt) }, + amount: { type: new GraphQLNonNull(GraphQLInt) } } -const resolve = (parent, { competitionCodeName, position, algoPrize }, context) => { +const resolve = (parent, { eventDesignator, rank, amount }, context) => { return new Promise((resolve, reject) => { - Competition.findOne({ codeName: competitionCodeName, host: context.userId }).exec((err, competition) => { - if (err || !competition) { + Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + if (err || !event) { reject(err) } else { - if (competition.prizes.some(prize => prize.position === position)) { + if (event.prizes.some(prize => prize.rank === rank)) { // need to add an error message here TODO - resolve(competition) + resolve(event) } else { - competition.prizes.push({ - position, - algoPrize + event.prizes.push({ + rank, + amount }) - competition.save((err) => { + event.save((err) => { if (err) reject(err) else { - resolve(competition) + resolve(event) } }) } @@ -41,7 +40,7 @@ const resolve = (parent, { competitionCodeName, position, algoPrize }, context) const mutation = { addPrize: { - type: CompetitionType, + type: EventType, args, resolve } diff --git a/server/src/schema/mutations/AddRule.js b/server/src/schema/mutations/AddRule.js index 2729f463e..606729114 100644 --- a/server/src/schema/mutations/AddRule.js +++ b/server/src/schema/mutations/AddRule.js @@ -3,37 +3,37 @@ import { GraphQLID, GraphQLString } from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' +import { EventType } from '../types' +import { Event } from '../../models' const args = { - competitionCodeName: { type: new GraphQLNonNull(GraphQLID) }, + eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, title: { type: new GraphQLNonNull(GraphQLString) }, description: { type: new GraphQLNonNull(GraphQLString) } } -const resolve = (parent, { competitionCodeName, title, description }, context) => { +const resolve = (parent, { eventDesignator, title, description }, context) => { return new Promise((resolve, reject) => { - Competition.findOne({ codeName: competitionCodeName, host: context.userId }).exec((err, competition) => { - if (err || !competition) { + Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + if (err || !event) { reject(err) } else { - if (competition.rules.some(rule => rule.title === title)) { - resolve(competition) + if (event.rules.some(rule => rule.title === title)) { + resolve(event) } else { - let number = 1 - while (competition.rules.some(rule => rule.number === number)) { - number++ + let position = 1 + while (event.rules.some(rule => rule.position === position)) { + position++ } - competition.rules.push({ - number, + event.rules.push({ + position, title, description }) - competition.save((err) => { + event.save((err) => { if (err) reject(err) else { - resolve(competition) + resolve(event) } }) } @@ -44,7 +44,7 @@ const resolve = (parent, { competitionCodeName, title, description }, context) = const mutation = { addRule: { - type: CompetitionType, + type: EventType, args, resolve } diff --git a/server/src/schema/mutations/CreateAndSetFormula.js b/server/src/schema/mutations/CreateAndSetFormula.js new file mode 100644 index 000000000..1da7c5e5b --- /dev/null +++ b/server/src/schema/mutations/CreateAndSetFormula.js @@ -0,0 +1,46 @@ +import { + GraphQLNonNull, + GraphQLString +} from 'graphql' +import { EventType } from '../types' +import { Event, Formula } from '../../models' + +const args = { + eventDesignator: { type: new GraphQLNonNull(GraphQLString) }, + name: { type: new GraphQLNonNull(GraphQLString) } +} + +const resolve = (parent, { name, eventDesignator }, context) => { + const ownerId = context.userId + let newFormula = new Formula({ name, ownerId }) + return new Promise((resolve, reject) => { + Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { + if (err || !event) { + reject(err) + } else { + newFormula.save((err) => { + if (err) reject(err) + else { + } + }) + event.formula = newFormula._id + event.save((err) => { + if (err) reject(err) + else { + resolve(event) + } + }) + } + }) + }) +} + +const mutation = { + createAndSetFormula: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/CreateAndSetPlotter.js b/server/src/schema/mutations/CreateAndSetPlotter.js new file mode 100644 index 000000000..c8e850d05 --- /dev/null +++ b/server/src/schema/mutations/CreateAndSetPlotter.js @@ -0,0 +1,49 @@ +import { + GraphQLNonNull, + GraphQLString +} from 'graphql' +import { EventType } from '../types' +import { Event, Plotter } from '../../models' + +const args = { + eventDesignator: { type: new GraphQLNonNull(GraphQLString) }, + name: { type: new GraphQLNonNull(GraphQLString) }, + host: { type: new GraphQLNonNull(GraphQLString) }, + repo: { type: new GraphQLNonNull(GraphQLString) }, + moduleName: { type: new GraphQLNonNull(GraphQLString) } +} + +const resolve = (parent, { name, host, repo, moduleName, eventDesignator }, context) => { + const ownerId = context.userId + let newPlotter = new Plotter({ name, host, repo, moduleName, ownerId }) + return new Promise((resolve, reject) => { + Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { + if (err || !event) { + reject(err) + } else { + newPlotter.save((err) => { + if (err) reject(err) + else { + } + }) + event.plotter = newPlotter._id + event.save((err) => { + if (err) reject(err) + else { + resolve(event) + } + }) + } + }) + }) +} + +const mutation = { + createAndSetPlotter: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/CreateFormula.js b/server/src/schema/mutations/CreateFormula.js new file mode 100644 index 000000000..89fd9b611 --- /dev/null +++ b/server/src/schema/mutations/CreateFormula.js @@ -0,0 +1,33 @@ +import { + GraphQLNonNull, + GraphQLString +} from 'graphql' +import { FormulaType } from '../types' +import { Formula } from '../../models' + +const args = { + name: { type: new GraphQLNonNull(GraphQLString) } +} + +const resolve = (parent, { name }, context) => { + const ownerId = context.userId + let newFormula = new Formula({ name, ownerId }) + return new Promise((resolve, reject) => { + newFormula.save((err) => { + if (err) reject(err) + else { + resolve(newFormula) + } + }) + }) +} + +const mutation = { + createFormula: { + type: FormulaType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/CreatePlotter.js b/server/src/schema/mutations/CreatePlotter.js new file mode 100644 index 000000000..30e290b99 --- /dev/null +++ b/server/src/schema/mutations/CreatePlotter.js @@ -0,0 +1,36 @@ +import { + GraphQLNonNull, + GraphQLString +} from 'graphql' +import { PlotterType } from '../types' +import { Plotter } from '../../models' + +const args = { + name: { type: new GraphQLNonNull(GraphQLString) }, + host: { type: new GraphQLNonNull(GraphQLString) }, + repo: { type: new GraphQLNonNull(GraphQLString) }, + moduleName: { type: new GraphQLNonNull(GraphQLString) } +} + +const resolve = (parent, { name, host, repo, moduleName }, context) => { + const ownerId = context.userId + let newPlotter = new Plotter({ name, host, repo, moduleName, ownerId }) + return new Promise((resolve, reject) => { + newPlotter.save((err) => { + if (err) reject(err) + else { + resolve(newPlotter) + } + }) + }) +} + +const mutation = { + createPlotter: { + type: PlotterType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/EditCompetition.js b/server/src/schema/mutations/EditCompetition.js deleted file mode 100644 index 406eab073..000000000 --- a/server/src/schema/mutations/EditCompetition.js +++ /dev/null @@ -1,59 +0,0 @@ -import { - GraphQLNonNull, - GraphQLID, - GraphQLInt, - GraphQLString -} from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' - -const args = { - codeName: { type: new GraphQLNonNull(GraphQLID) }, - displayName: { type: GraphQLString }, - description: { type: GraphQLString }, - startDatetime: { type: GraphQLInt }, - finishDatetime: { type: GraphQLInt }, - formula: { type: GraphQLString }, - plotterCodeName: { type: GraphQLString }, - plotterHost: { type: GraphQLString }, - plotterRepo: { type: GraphQLString }, - plotterModuleName: { type: GraphQLString } -} - -const resolve = (parent, { codeName, displayName, description, startDatetime, finishDatetime, - formula, plotterCodeName, plotterHost, plotterRepo, plotterModuleName }, context) => { - return new Promise((resolve, reject) => { - Competition.findOne({ codeName, host: context.userId }).exec((err, competition) => { - if (err || !competition) { - reject(err) - } else { - competition.displayName = displayName || competition.displayName - competition.description = description || competition.description - competition.startDatetime = startDatetime || competition.startDatetime - competition.finishDatetime = finishDatetime || competition.finishDatetime - competition.formula = formula || competition.formula - competition.plotter.codeName = plotterCodeName || competition.plotter.codeName - competition.plotter.host = plotterHost || competition.plotter.host - competition.plotter.repo = plotterRepo || competition.plotter.repo - competition.plotter.moduleName = plotterModuleName || competition.plotter.moduleName - - competition.save((err) => { - if (err) reject(err) - else { - resolve(competition) - } - }) - } - }) - }) -} - -const mutation = { - editCompetition: { - type: CompetitionType, - args, - resolve - } -} - -export default mutation diff --git a/server/src/schema/mutations/EditEvent.js b/server/src/schema/mutations/EditEvent.js new file mode 100644 index 000000000..1e093b8e5 --- /dev/null +++ b/server/src/schema/mutations/EditEvent.js @@ -0,0 +1,52 @@ +import { + GraphQLNonNull, + GraphQLID, + GraphQLInt, + GraphQLString +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' + +const args = { + designator: { type: new GraphQLNonNull(GraphQLID) }, + name: { type: GraphQLString }, + description: { type: GraphQLString }, + startDatetime: { type: GraphQLInt }, + finishDatetime: { type: GraphQLInt }, + formulaId: { type: GraphQLString }, + plotterId: { type: GraphQLString } +} + +const resolve = (parent, { designator, name, description, startDatetime, finishDatetime, formulaId, plotterId }, context) => { + return new Promise((resolve, reject) => { + Event.findOne({ designator, hostId: context.userId }).exec((err, event) => { + if (err || !event) { + reject(err) + } else { + event.name = name || event.name + event.description = description || event.description + event.startDatetime = startDatetime || event.startDatetime + event.finishDatetime = finishDatetime || event.finishDatetime + event.formula = formulaId || event.formula + event.plotter = plotterId || event.plotter + + event.save((err) => { + if (err) reject(err) + else { + resolve(event) + } + }) + } + }) + }) +} + +const mutation = { + editEvent: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/EditParticipant.js b/server/src/schema/mutations/EditParticipant.js index f89677075..6adac2274 100644 --- a/server/src/schema/mutations/EditParticipant.js +++ b/server/src/schema/mutations/EditParticipant.js @@ -3,35 +3,35 @@ import { GraphQLID, GraphQLString } from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' +import { EventType } from '../types' +import { Event } from '../../models' const args = { - competitionCodeName: { type: new GraphQLNonNull(GraphQLID) }, - devTeam: { type: new GraphQLNonNull(GraphQLString) }, - bot: { type: GraphQLString }, - release: { type: GraphQLString } + eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, + teamId: { type: new GraphQLNonNull(GraphQLString) }, + botId: { type: GraphQLString }, + releaseId: { type: GraphQLString } } -const resolve = (parent, { competitionCodeName, devTeam, bot, release }, context) => { +const resolve = (parent, { eventDesignator, teamId, botId, releaseId }, context) => { return new Promise((resolve, reject) => { - Competition.findOne({ codeName: competitionCodeName }).exec((err, competition) => { + Event.findOne({ designator: eventDesignator }).exec((err, event) => { if (err) reject(err) else { let participantIndex - competition.participants.find((participant, index) => { - if (participant.devTeam === devTeam) { + event.participants.find((participant, index) => { + if (participant.teamId === teamId) { participantIndex = index } }) - competition.participants[participantIndex].bot = bot || competition.participants[participantIndex].bot - competition.participants[participantIndex].release = release || competition.participants[participantIndex].release + event.participants[participantIndex].botId = botId || event.participants[participantIndex].botId + event.participants[participantIndex].releaseId = releaseId || event.participants[participantIndex].releaseId - competition.save((err) => { + event.save((err) => { if (err) reject(err) else { - resolve(competition) + resolve(event) } }) } @@ -41,7 +41,7 @@ const resolve = (parent, { competitionCodeName, devTeam, bot, release }, context const mutation = { editParticipant: { - type: CompetitionType, + type: EventType, args, resolve } diff --git a/server/src/schema/mutations/EditRule.js b/server/src/schema/mutations/EditRule.js index c45da0438..4ec533f94 100644 --- a/server/src/schema/mutations/EditRule.js +++ b/server/src/schema/mutations/EditRule.js @@ -3,41 +3,41 @@ import { GraphQLID, GraphQLString } from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' +import { EventType } from '../types' +import { Event } from '../../models' const args = { - competitionCodeName: { type: new GraphQLNonNull(GraphQLID) }, + eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, title: { type: new GraphQLNonNull(GraphQLString) }, newtitle: { type: GraphQLString }, description: { type: GraphQLString } } -const resolve = (parent, { competitionCodeName, title, newtitle, description }, context) => { +const resolve = (parent, { eventDesignator, title, newtitle, description }, context) => { return new Promise((resolve, reject) => { - Competition.findOne({ codeName: competitionCodeName, host: context.userId }).exec((err, competition) => { - if (err || !competition) { + Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + if (err || !event) { reject(err) } else { let ruleIndex - competition.rules.find((rule, index) => { + event.rules.find((rule, index) => { if (rule.title === title) { ruleIndex = index } }) - competition.rules[ruleIndex].description = description || competition.rules[ruleIndex].description - if (competition.rules.some(rule => rule.title === newtitle)) { + event.rules[ruleIndex].description = description || event.rules[ruleIndex].description + if (event.rules.some(rule => rule.title === newtitle)) { // need to add an error message here TODO - resolve(competition) + resolve(event) } else { - competition.rules[ruleIndex].title = newtitle || competition.rules[ruleIndex].title + event.rules[ruleIndex].title = newtitle || event.rules[ruleIndex].title } - competition.save((err) => { + event.save((err) => { if (err) reject(err) else { - resolve(competition) + resolve(event) } }) } @@ -47,7 +47,7 @@ const resolve = (parent, { competitionCodeName, title, newtitle, description }, const mutation = { editRule: { - type: CompetitionType, + type: EventType, args, resolve } diff --git a/server/src/schema/mutations/HostCompetition.js b/server/src/schema/mutations/HostCompetition.js deleted file mode 100644 index cf763a643..000000000 --- a/server/src/schema/mutations/HostCompetition.js +++ /dev/null @@ -1,47 +0,0 @@ -import { - GraphQLNonNull, - GraphQLInt, - GraphQLString -} from 'graphql' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' -import { slugify } from '../../utils/functions' - -const args = { - displayName: { type: new GraphQLNonNull(GraphQLString) }, - description: { type: new GraphQLNonNull(GraphQLString) }, - startDatetime: { type: new GraphQLNonNull(GraphQLInt) }, - finishDatetime: { type: new GraphQLNonNull(GraphQLInt) }, - formula: { type: new GraphQLNonNull(GraphQLString) } -} - -const resolve = (parent, { displayName, description, startDatetime, finishDatetime, formula }, context) => { - const host = context.userId - let newCompetition = new Competition({ - host, - displayName, - description, - startDatetime, - finishDatetime, - formula - }) - newCompetition.codeName = slugify(newCompetition.displayName) + '-' + newCompetition._id - return new Promise((resolve, reject) => { - newCompetition.save((err) => { - if (err) reject(err) - else { - resolve(newCompetition) - } - }) - }) -} - -const mutation = { - hostCompetition: { - type: CompetitionType, - args, - resolve - } -} - -export default mutation diff --git a/server/src/schema/mutations/HostEvent.js b/server/src/schema/mutations/HostEvent.js new file mode 100644 index 000000000..c2145736b --- /dev/null +++ b/server/src/schema/mutations/HostEvent.js @@ -0,0 +1,45 @@ +import { + GraphQLNonNull, + GraphQLInt, + GraphQLString +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' +import { slugify } from '../../utils/functions' + +const args = { + name: { type: new GraphQLNonNull(GraphQLString) }, + description: { type: new GraphQLNonNull(GraphQLString) }, + startDatetime: { type: new GraphQLNonNull(GraphQLInt) }, + finishDatetime: { type: new GraphQLNonNull(GraphQLInt) } +} + +const resolve = (parent, { name, description, startDatetime, finishDatetime }, context) => { + const hostId = context.userId + let newEvent = new Event({ + hostId, + name, + description, + startDatetime, + finishDatetime + }) + newEvent.designator = slugify(newEvent.name) + '-' + newEvent._id + return new Promise((resolve, reject) => { + newEvent.save((err) => { + if (err) reject(err) + else { + resolve(newEvent) + } + }) + }) +} + +const mutation = { + hostEvent: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/RegisterToCompetition.js b/server/src/schema/mutations/RegisterToCompetition.js deleted file mode 100644 index 932d31018..000000000 --- a/server/src/schema/mutations/RegisterToCompetition.js +++ /dev/null @@ -1,50 +0,0 @@ -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: new GraphQLNonNull(GraphQLString) }, - release: { type: new GraphQLNonNull(GraphQLString) } -} - -const resolve = (parent, { codeName, devTeam, bot, release }, context) => { - return new Promise((resolve, reject) => { - Competition.findOne({ codeName }).exec((err, competition) => { - if (err) reject(err) - else { - if (competition.participants.some(participant => participant.devTeam === args.devTeam)) { - // need to add an error message here TODO - resolve(competition) - } else { - competition.participants.push({ - devTeam, - bot, - release - }) - competition.save((err) => { - if (err) reject(err) - else { - resolve(competition) - } - }) - } - } - }) - }) -} - -const mutation = { - registerToCompetition: { - type: CompetitionType, - args, - resolve - } -} - -export default mutation diff --git a/server/src/schema/mutations/RegisterToEvent.js b/server/src/schema/mutations/RegisterToEvent.js new file mode 100644 index 000000000..1c2e5f1a0 --- /dev/null +++ b/server/src/schema/mutations/RegisterToEvent.js @@ -0,0 +1,50 @@ +import { + GraphQLNonNull, + GraphQLID, + GraphQLString +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' + +const args = { + designator: { type: new GraphQLNonNull(GraphQLID) }, + teamId: { type: new GraphQLNonNull(GraphQLString) }, + botId: { type: GraphQLString }, + releaseId: { type: GraphQLString } +} + +const resolve = (parent, { designator, teamId, botId, releaseId }, context) => { + return new Promise((resolve, reject) => { + Event.findOne({ designator }).exec((err, event) => { + if (err) reject(err) + else { + if (event.participants.some(participant => participant.teamId === teamId)) { + // need to add an error message here TODO + resolve(event) + } else { + event.participants.push({ + teamId, + botId, + releaseId + }) + event.save((err) => { + if (err) reject(err) + else { + resolve(event) + } + }) + } + } + }) + }) +} + +const mutation = { + registerToEvent: { + type: EventType, + args, + resolve + } +} + +export default mutation diff --git a/server/src/schema/mutations/ReorderRules.js b/server/src/schema/mutations/ReorderRules.js index 0d479aa52..7e14e114e 100644 --- a/server/src/schema/mutations/ReorderRules.js +++ b/server/src/schema/mutations/ReorderRules.js @@ -4,34 +4,34 @@ import { GraphQLInt } from 'graphql' import { isBetween } from '../../utils/functions' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' +import { EventType } from '../types' +import { Event } from '../../models' const args = { - competitionCodeName: { type: new GraphQLNonNull(GraphQLID) }, - fromNumber: { type: new GraphQLNonNull(GraphQLInt) }, - toNumber: { type: new GraphQLNonNull(GraphQLInt) } + eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, + fromPosition: { type: new GraphQLNonNull(GraphQLInt) }, + toPosition: { type: new GraphQLNonNull(GraphQLInt) } } -const resolve = (parent, { competitionCodeName, fromNumber, toNumber }, context) => { +const resolve = (parent, { eventDesignator, fromPosition, toPosition }, context) => { return new Promise((resolve, reject) => { - Competition.findOne({ codeName: competitionCodeName, host: context.userId }).exec((err, competition) => { - if (err || !competition) { + Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + if (err || !event) { reject(err) } else { - const delta = fromNumber > toNumber ? 1 : -1 - competition.rules.forEach((part) => { - if (part.number === fromNumber) { - part.number = toNumber - } else if (isBetween(part.number, fromNumber, toNumber)) { - part.number += delta + const delta = fromPosition > toPosition ? 1 : -1 + event.rules.forEach((part) => { + if (part.position === fromPosition) { + part.position = toPosition + } else if (isBetween(part.position, fromPosition, toPosition)) { + part.position += delta } }) - competition.save((err) => { + event.save((err) => { if (err) reject(err) else { - resolve(competition) + resolve(event) } }) } @@ -41,7 +41,7 @@ const resolve = (parent, { competitionCodeName, fromNumber, toNumber }, context) const mutation = { reorderRules: { - type: CompetitionType, + type: EventType, args, resolve } diff --git a/server/src/schema/mutations/index.js b/server/src/schema/mutations/index.js index a760dee03..732ffcba8 100644 --- a/server/src/schema/mutations/index.js +++ b/server/src/schema/mutations/index.js @@ -2,26 +2,34 @@ import { GraphQLObjectType } from 'graphql' -import HostCompetitionMutation from './HostCompetition' -import EditCompetitionMutation from './EditCompetition' -import RegisterToCompetitionMutation from './RegisterToCompetition' +import HostEventMutation from './HostEvent' +import EditEventMutation from './EditEvent' +import RegisterToEventMutation from './RegisterToEvent' import EditParticipantMutation from './EditParticipant' import AddRuleMutation from './AddRule' import EditRuleMutation from './EditRule' import ReorderRulesMutation from './ReorderRules' import AddPrizeMutation from './AddPrize' +import CreateFormulaMutation from './CreateFormula' +import CreateAndSetFormulaMutation from './CreateAndSetFormula' +import CreatePlotterMutation from './CreatePlotter' +import CreateAndSetPlotterMutation from './CreateAndSetPlotter' const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( - HostCompetitionMutation, - EditCompetitionMutation, - RegisterToCompetitionMutation, + HostEventMutation, + EditEventMutation, + RegisterToEventMutation, EditParticipantMutation, AddRuleMutation, EditRuleMutation, ReorderRulesMutation, - AddPrizeMutation + AddPrizeMutation, + CreateFormulaMutation, + CreateAndSetFormulaMutation, + CreatePlotterMutation, + CreateAndSetPlotterMutation ) }) diff --git a/server/src/schema/queries/Competitions.js b/server/src/schema/queries/Competitions.js deleted file mode 100644 index 1b933000a..000000000 --- a/server/src/schema/queries/Competitions.js +++ /dev/null @@ -1,29 +0,0 @@ -import { - GraphQLBoolean, - GraphQLList -} from 'graphql' -import { epoch } from '../../utils/functions' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' - -const args = { - includeOld: { type: GraphQLBoolean } -} - -const resolve = (parent, { includeOld }, context) => { - return Competition.find( - Object.assign( - {}, - includeOld ? {} : { finishDatetime: { $gt: epoch() } } - )) -} - -const query = { - competitions: { - type: new GraphQLList(CompetitionType), - args, - resolve - } -} - -export default query diff --git a/server/src/schema/queries/CompetitionsByDevTeam.js b/server/src/schema/queries/CompetitionsByDevTeam.js deleted file mode 100644 index cb03ec215..000000000 --- a/server/src/schema/queries/CompetitionsByDevTeam.js +++ /dev/null @@ -1,35 +0,0 @@ -import { - GraphQLBoolean, - GraphQLList, - GraphQLString, - GraphQLNonNull -} from 'graphql' -import { epoch } from '../../utils/functions' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' - -const args = { - devTeam: { type: new GraphQLNonNull(GraphQLString) }, - includeOld: { type: GraphQLBoolean } -} - -const resolve = (parent, { devTeam, includeOld }, context) => { - return Competition.find( - Object.assign( - { - participants: { $elemMatch: { devTeam } } - }, - includeOld ? {} : { finishDatetime: { $gt: epoch() } } - ) - ) -} - -const query = { - competitionsByDevTeam: { - type: new GraphQLList(CompetitionType), - args, - resolve - } -} - -export default query diff --git a/server/src/schema/queries/CompetitionsByHost.js b/server/src/schema/queries/CompetitionsByHost.js deleted file mode 100644 index 70e36fac5..000000000 --- a/server/src/schema/queries/CompetitionsByHost.js +++ /dev/null @@ -1,35 +0,0 @@ -import { - GraphQLBoolean, - GraphQLList, - GraphQLString, - GraphQLNonNull -} from 'graphql' -import { epoch } from '../../utils/functions' -import { CompetitionType } from '../types' -import Competition from '../../models/competition' - -const args = { - host: { type: new GraphQLNonNull(GraphQLString) }, - includeOld: { type: GraphQLBoolean } -} - -const resolve = (parent, { host, includeOld }, context) => { - return Competition.find( - Object.assign( - { - host - }, - includeOld ? {} : { finishDatetime: { $gt: epoch() } } - ) - ) -} - -const query = { - competitionsByHost: { - type: new GraphQLList(CompetitionType), - args, - resolve - } -} - -export default query diff --git a/server/src/schema/queries/Events.js b/server/src/schema/queries/Events.js new file mode 100644 index 000000000..636988ac4 --- /dev/null +++ b/server/src/schema/queries/Events.js @@ -0,0 +1,35 @@ +import { + GraphQLInt, + GraphQLList +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' + +const args = { + minDate: { type: GraphQLInt }, + maxDate: { type: GraphQLInt } +} + +const resolve = (parent, { minDate, maxDate }, context) => { + return Event.find( + Object.assign( + minDate || maxDate + ? { startDatetime: + Object.assign( + minDate ? { $gte: minDate } : {}, + maxDate ? { $lte: maxDate } : {} + ) } + : {} + ) + ) +} + +const query = { + events: { + type: new GraphQLList(EventType), + args, + resolve + } +} + +export default query diff --git a/server/src/schema/queries/EventsByHost.js b/server/src/schema/queries/EventsByHost.js new file mode 100644 index 000000000..4b2cf35b3 --- /dev/null +++ b/server/src/schema/queries/EventsByHost.js @@ -0,0 +1,39 @@ +import { + GraphQLInt, + GraphQLList, + GraphQLString, + GraphQLNonNull +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' + +const args = { + hostId: { type: new GraphQLNonNull(GraphQLString) }, + minDate: { type: GraphQLInt }, + maxDate: { type: GraphQLInt } +} + +const resolve = (parent, { hostId, minDate, maxDate }, context) => { + return Event.find( + Object.assign( + { hostId }, + minDate || maxDate + ? { startDatetime: + Object.assign( + minDate ? { $gte: minDate } : {}, + maxDate ? { $lte: maxDate } : {} + ) } + : {} + ) + ) +} + +const query = { + eventsByHost: { + type: new GraphQLList(EventType), + args, + resolve + } +} + +export default query diff --git a/server/src/schema/queries/EventsByTeam.js b/server/src/schema/queries/EventsByTeam.js new file mode 100644 index 000000000..8c28391d7 --- /dev/null +++ b/server/src/schema/queries/EventsByTeam.js @@ -0,0 +1,39 @@ +import { + GraphQLInt, + GraphQLList, + GraphQLString, + GraphQLNonNull +} from 'graphql' +import { EventType } from '../types' +import { Event } from '../../models' + +const args = { + teamId: { type: new GraphQLNonNull(GraphQLString) }, + minDate: { type: GraphQLInt }, + maxDate: { type: GraphQLInt } +} + +const resolve = (parent, { teamId, minDate, maxDate }, context) => { + return Event.find( + Object.assign( + { participants: { $elemMatch: { teamId } } }, + minDate || maxDate + ? { startDatetime: + Object.assign( + minDate ? { $gte: minDate } : {}, + maxDate ? { $lte: maxDate } : {} + ) } + : {} + ) + ) +} + +const query = { + eventsByTeam: { + type: new GraphQLList(EventType), + args, + resolve + } +} + +export default query diff --git a/server/src/schema/queries/Formulas.js b/server/src/schema/queries/Formulas.js new file mode 100644 index 000000000..158fcf853 --- /dev/null +++ b/server/src/schema/queries/Formulas.js @@ -0,0 +1,21 @@ +import { + GraphQLList +} from 'graphql' +import { FormulaType } from '../types' +import { Formula } from '../../models' + +const args = {} + +const resolve = (parent, args, context) => { + return Formula.find() +} + +const query = { + formulas: { + type: new GraphQLList(FormulaType), + args, + resolve + } +} + +export default query diff --git a/server/src/schema/queries/Plotters.js b/server/src/schema/queries/Plotters.js new file mode 100644 index 000000000..9bdc46296 --- /dev/null +++ b/server/src/schema/queries/Plotters.js @@ -0,0 +1,21 @@ +import { + GraphQLList +} from 'graphql' +import { PlotterType } from '../types' +import { Plotter } from '../../models' + +const args = {} + +const resolve = (parent, args, context) => { + return Plotter.find() +} + +const query = { + plotters: { + type: new GraphQLList(PlotterType), + args, + resolve + } +} + +export default query diff --git a/server/src/schema/queries/index.js b/server/src/schema/queries/index.js index 58a55bec3..71aef39df 100644 --- a/server/src/schema/queries/index.js +++ b/server/src/schema/queries/index.js @@ -2,16 +2,20 @@ import { GraphQLObjectType } from 'graphql' -import CompetitionsQuery from './Competitions' -import CompetitionsByHostQuery from './CompetitionsByHost' -import CompetitionsByDevTeamQuery from './CompetitionsByDevTeam' +import EventsQuery from './Events' +import EventsByHostQuery from './EventsByHost' +import EventsByTeamQuery from './EventsByTeam' +import FormulasQuery from './Formulas' +import PlottersQuery from './Plotters' const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: Object.assign( - CompetitionsQuery, - CompetitionsByHostQuery, - CompetitionsByDevTeamQuery + EventsQuery, + EventsByHostQuery, + EventsByTeamQuery, + FormulasQuery, + PlottersQuery ) }) diff --git a/server/src/schema/types/OtherPrize.js b/server/src/schema/types/AdditionalPrize.js similarity index 73% rename from server/src/schema/types/OtherPrize.js rename to server/src/schema/types/AdditionalPrize.js index 6fb186283..f78c24d13 100644 --- a/server/src/schema/types/OtherPrize.js +++ b/server/src/schema/types/AdditionalPrize.js @@ -4,7 +4,7 @@ import { GraphQLInt } from 'graphql' -const OtherPrizeType = new GraphQLObjectType({ +const AdditionalPrizeType = new GraphQLObjectType({ name: 'OtherPrize', fields: () => ({ condition: { type: GraphQLString }, @@ -13,4 +13,4 @@ const OtherPrizeType = new GraphQLObjectType({ }) }) -export default OtherPrizeType +export default AdditionalPrizeType diff --git a/server/src/schema/types/Competition.js b/server/src/schema/types/Event.js similarity index 72% rename from server/src/schema/types/Competition.js rename to server/src/schema/types/Event.js index 009cfe702..1bb76b3a6 100644 --- a/server/src/schema/types/Competition.js +++ b/server/src/schema/types/Event.js @@ -6,22 +6,28 @@ import { GraphQLID } from 'graphql' import { + FormulaType, PlotterType, RuleType, ParticipantType, PrizeType } from './index' -const CompetitionType = new GraphQLObjectType({ - name: 'Competition', +const EventType = new GraphQLObjectType({ + name: 'Event', fields: () => ({ - codeName: { type: GraphQLID }, - displayName: { type: GraphQLString }, - host: { type: GraphQLString }, + designator: { type: GraphQLID }, + name: { type: GraphQLString }, + hostId: { type: GraphQLString }, description: { type: GraphQLString }, startDatetime: { type: GraphQLInt }, finishDatetime: { type: GraphQLInt }, - formula: { type: GraphQLString }, + formula: { + type: FormulaType, + resolve (parent, args) { + return parent.formula + } + }, plotter: { type: PlotterType, resolve (parent, args) { @@ -49,4 +55,4 @@ const CompetitionType = new GraphQLObjectType({ }) }) -export default CompetitionType +export default EventType diff --git a/server/src/schema/types/Formula.js b/server/src/schema/types/Formula.js new file mode 100644 index 000000000..b081cae14 --- /dev/null +++ b/server/src/schema/types/Formula.js @@ -0,0 +1,17 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLBoolean +} from 'graphql' + +const FormulaType = new GraphQLObjectType({ + name: 'Formula', + fields: () => ({ + id: { type: GraphQLString }, + ownerId: { type: GraphQLString }, + isTemplate: { type: GraphQLBoolean }, + name: { type: GraphQLString } + }) +}) + +export default FormulaType diff --git a/server/src/schema/types/Participant.js b/server/src/schema/types/Participant.js index 13f27da4f..038c30a07 100644 --- a/server/src/schema/types/Participant.js +++ b/server/src/schema/types/Participant.js @@ -6,9 +6,9 @@ import { const ParticipantType = new GraphQLObjectType({ name: 'Participant', fields: () => ({ - devTeam: { type: GraphQLString }, - bot: { type: GraphQLString }, - release: { type: GraphQLString } + teamId: { type: GraphQLString }, + botId: { type: GraphQLString }, + releaseId: { type: GraphQLString } }) }) diff --git a/server/src/schema/types/Plotter.js b/server/src/schema/types/Plotter.js index ee2b3de67..610870eb6 100644 --- a/server/src/schema/types/Plotter.js +++ b/server/src/schema/types/Plotter.js @@ -1,12 +1,16 @@ import { GraphQLObjectType, - GraphQLString + GraphQLString, + GraphQLBoolean } from 'graphql' const PlotterType = new GraphQLObjectType({ name: 'Plotter', fields: () => ({ - codeName: { type: GraphQLString }, + id: { type: GraphQLString }, + ownerId: { type: GraphQLString }, + isTemplate: { type: GraphQLBoolean }, + name: { type: GraphQLString }, host: { type: GraphQLString }, repo: { type: GraphQLString }, moduleName: { type: GraphQLString } diff --git a/server/src/schema/types/Prize.js b/server/src/schema/types/Prize.js index 5588afd9a..aa0cabc38 100644 --- a/server/src/schema/types/Prize.js +++ b/server/src/schema/types/Prize.js @@ -3,17 +3,17 @@ import { GraphQLList, GraphQLInt } from 'graphql' -import { OtherPrizeType } from './index' +import { AdditionalPrizeType } from './index' const PrizeType = new GraphQLObjectType({ name: 'Prize', fields: () => ({ - position: { type: GraphQLInt }, - algoPrize: { type: GraphQLInt }, - otherPrizes: { - type: new GraphQLList(OtherPrizeType), + rank: { type: GraphQLInt }, + amount: { type: GraphQLInt }, + additional: { + type: new GraphQLList(AdditionalPrizeType), resolve (parent, args) { - return parent.otherPrizes + return parent.additional } } }) diff --git a/server/src/schema/types/Rule.js b/server/src/schema/types/Rule.js index f8a6b98ef..b9a5d59ca 100644 --- a/server/src/schema/types/Rule.js +++ b/server/src/schema/types/Rule.js @@ -7,7 +7,7 @@ import { const RuleType = new GraphQLObjectType({ name: 'Rule', fields: () => ({ - number: { type: GraphQLInt }, + position: { type: GraphQLInt }, title: { type: GraphQLString }, description: { type: GraphQLString } }) diff --git a/server/src/schema/types/index.js b/server/src/schema/types/index.js index 6431a687c..be64a7f25 100644 --- a/server/src/schema/types/index.js +++ b/server/src/schema/types/index.js @@ -1,15 +1,17 @@ import PlotterType from './Plotter' import RuleType from './Rule' import ParticipantType from './Participant' -import OtherPrizeType from './OtherPrize' +import AdditionalPrizeType from './AdditionalPrize' import PrizeType from './Prize' -import CompetitionType from './Competition' +import FormulaType from './Formula' +import EventType from './Event' export { PlotterType, RuleType, ParticipantType, - OtherPrizeType, + AdditionalPrizeType, PrizeType, - CompetitionType + FormulaType, + EventType }