From 189a8f1d8441d8f4371d1089b504d2666c664cc1 Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Mon, 5 Nov 2018 15:27:04 +0100 Subject: [PATCH] Master refactoring, remove designator field --- server/src/db.js | 1 + server/src/models/event.js | 9 ++-- server/src/schema/mutations/CreateEvent.js | 36 ++-------------- server/src/schema/mutations/CreateFormula.js | 15 ++++--- server/src/schema/mutations/CreatePlotter.js | 22 ++++------ server/src/schema/mutations/EditEvent.js | 41 ++++--------------- .../src/schema/mutations/EditParticipant.js | 25 +++++------ .../src/schema/mutations/RegisterToEvent.js | 23 ++++------- server/src/schema/queries/Event.js | 8 ++-- server/src/schema/types/Event.js | 2 +- server/src/schema/types/input/Event.js | 4 -- 11 files changed, 56 insertions(+), 130 deletions(-) diff --git a/server/src/db.js b/server/src/db.js index fb87523ca..4ed47a0c2 100644 --- a/server/src/db.js +++ b/server/src/db.js @@ -3,6 +3,7 @@ import mongoose from 'mongoose'; mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true }); mongoose.set('useCreateIndex', true); +mongoose.set('useFindAndModify', false); mongoose.Promise = global.Promise; const db = mongoose.connection; diff --git a/server/src/models/event.js b/server/src/models/event.js index 7f1b430b4..87d495ccd 100644 --- a/server/src/models/event.js +++ b/server/src/models/event.js @@ -9,10 +9,6 @@ import { const { ObjectId } = Schema.Types; const eventSchema = new Schema({ - designator: { - type: String, - required: true, - }, status: { type: String, enum: EventStatusEnum, @@ -97,6 +93,11 @@ eventSchema.post('save', (doc, next) => { next(); }); }); +eventSchema.post('findOneAndUpdate', (doc, next) => { + doc.populate('formulaId').populate('plotterId').execPopulate().then(() => { + next(); + }); +}); const Event = mongoose.model('Event', eventSchema); diff --git a/server/src/schema/mutations/CreateEvent.js b/server/src/schema/mutations/CreateEvent.js index abb920a3f..2704f53e7 100644 --- a/server/src/schema/mutations/CreateEvent.js +++ b/server/src/schema/mutations/CreateEvent.js @@ -1,49 +1,21 @@ import { AuthentificationError } from '../../errors'; import { EventType } from '../types'; -import { Event, Formula, Plotter } from '../../models'; -import { slugify } from '../../utils/functions'; +import { Event } from '../../models'; import { EventInputType } from '../types/input'; export const args = { event: { type: EventInputType }, }; -const resolve = (parent, { event }, context) => { - const hostId = context.userId; +const resolve = (parent, { event }, { userId: hostId }) => { if (!hostId) { throw new AuthentificationError(); } - const eventSqueleton = { ...event }; - delete eventSqueleton.plotter; - delete eventSqueleton.formula; - eventSqueleton.hostId = hostId; - - const newEvent = new Event(eventSqueleton); - newEvent.designator = `${slugify(newEvent.name)}-${newEvent._id}`; - const { formula, plotter } = event; + event.hostId = hostId; + const newEvent = new Event(event); return new Promise((res, rej) => { - if (formula) { - formula.ownerId = hostId; - const newFormula = new Formula(formula); - newFormula.save((err) => { - if (err) { - rej(err); - } - }); - newEvent.formulaId = newFormula._id; - } - if (plotter) { - plotter.ownerId = hostId; - const newPlotter = new Plotter(plotter); - newPlotter.save((err) => { - if (err) { - rej(err); - } - }); - newEvent.plotterId = newPlotter._id; - } newEvent.save((err) => { if (err) { rej(err); diff --git a/server/src/schema/mutations/CreateFormula.js b/server/src/schema/mutations/CreateFormula.js index 4ff97b8c1..198c1fd0e 100644 --- a/server/src/schema/mutations/CreateFormula.js +++ b/server/src/schema/mutations/CreateFormula.js @@ -1,21 +1,20 @@ -import { - GraphQLNonNull, - GraphQLString, -} from 'graphql'; import { AuthentificationError } from '../../errors'; import { FormulaType } from '../types'; import { Formula } from '../../models'; +import { FormulaInputType } from '../types/input'; const args = { - name: { type: new GraphQLNonNull(GraphQLString) }, + formula: { type: FormulaInputType }, }; -const resolve = (parent, { name }, context) => { - const ownerId = context.userId; +const resolve = (parent, { formula }, { userId: ownerId }) => { if (!ownerId) { throw new AuthentificationError(); } - const newFormula = new Formula({ name, ownerId }); + + formula.ownerId = ownerId; + const newFormula = new Formula(formula); + return new Promise((res, rej) => { newFormula.save((err) => { if (err) { diff --git a/server/src/schema/mutations/CreatePlotter.js b/server/src/schema/mutations/CreatePlotter.js index a4fcdf26a..6cf060fbf 100644 --- a/server/src/schema/mutations/CreatePlotter.js +++ b/server/src/schema/mutations/CreatePlotter.js @@ -1,28 +1,20 @@ -import { - GraphQLNonNull, - GraphQLString, -} from 'graphql'; import { AuthentificationError } from '../../errors'; import { PlotterType } from '../types'; import { Plotter } from '../../models'; +import { PlotterInputType } from '../types/input'; const args = { - name: { type: new GraphQLNonNull(GraphQLString) }, - host: { type: new GraphQLNonNull(GraphQLString) }, - repo: { type: new GraphQLNonNull(GraphQLString) }, - moduleName: { type: new GraphQLNonNull(GraphQLString) }, + plotter: { type: PlotterInputType }, }; -const resolve = (parent, { - name, host, repo, moduleName, -}, context) => { - const ownerId = context.userId; +const resolve = (parent, { plotter }, { userId: ownerId }) => { if (!ownerId) { throw new AuthentificationError(); } - const newPlotter = new Plotter({ - name, host, repo, moduleName, ownerId, - }); + + plotter.ownerId = ownerId; + const newPlotter = new Plotter(plotter); + return new Promise((res, reject) => { newPlotter.save((err) => { if (err) { diff --git a/server/src/schema/mutations/EditEvent.js b/server/src/schema/mutations/EditEvent.js index e60cf6cbd..093376207 100644 --- a/server/src/schema/mutations/EditEvent.js +++ b/server/src/schema/mutations/EditEvent.js @@ -1,54 +1,29 @@ import { GraphQLNonNull, GraphQLID, - GraphQLInt, - GraphQLString, } from 'graphql'; -import { AuthentificationError, DatabaseError } from '../../errors'; +import { AuthentificationError } from '../../errors'; import { EventType } from '../types'; import { Event } from '../../models'; +import { EventInputType } from '../types/input'; 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 }, + eventId: { type: new GraphQLNonNull(GraphQLID) }, + event: { type: EventInputType }, }; -const resolve = (parent, { - designator, name, description, startDatetime, finishDatetime, formulaId, plotterId, -}, context) => { - const hostId = context.userId; +const resolve = (parent, { eventId: _id, event }, { userId: hostId }) => { if (!hostId) { throw new AuthentificationError(); } + return new Promise((res, rej) => { - Event.findOne({ designator, hostId }).exec((err, event) => { + Event.findOneAndUpdate({ _id, hostId }, event, { new: true }, (err, modifiedEvent) => { if (err) { rej(err); return; } - if (!event) { - rej(new DatabaseError('None of the events you own respond to that designator')); - return; - } - 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((error) => { - if (error) { - rej(error); - return; - } - res(event); - }); + res(modifiedEvent); }); }); }; diff --git a/server/src/schema/mutations/EditParticipant.js b/server/src/schema/mutations/EditParticipant.js index fde3145da..a241f34b4 100644 --- a/server/src/schema/mutations/EditParticipant.js +++ b/server/src/schema/mutations/EditParticipant.js @@ -1,7 +1,6 @@ import { GraphQLNonNull, GraphQLID, - GraphQLString, } from 'graphql'; import axios from 'axios'; import { @@ -11,19 +10,17 @@ import { } from '../../errors'; import { EventType } from '../types'; import { Event } from '../../models'; +import { ParticipantInputType } from '../types/input'; const args = { - eventDesignator: { type: new GraphQLNonNull(GraphQLID) }, - teamId: { type: new GraphQLNonNull(GraphQLString) }, - botId: { type: GraphQLString }, - releaseId: { type: GraphQLString }, + eventId: { type: new GraphQLNonNull(GraphQLID) }, + participant: { type: ParticipantInputType }, }; -const resolve = (parent, { - eventDesignator, teamId, botId, releaseId, -}, context) => { - const userid = context.userId; - if (!userid) { +const resolve = (parent, + { eventId: _id, participant: { teamId, botId, releaseId } }, + { userId, authorization }) => { + if (!userId) { throw new AuthentificationError(); } return new Promise((res, rej) => { @@ -39,16 +36,14 @@ const resolve = (parent, { } `, }, - headers: { - authorization: context.authorization, - }, + headers: { authorization }, }).then( (result) => { if (!result.data.data.teamsByRole.some(team => team.id === teamId)) { - rej(new WrongArgumentsError('You are not eligible to register this team')); + rej(new WrongArgumentsError('You are not eligible to edit this team')); return; } - Event.findOne({ designator: eventDesignator }).exec((err, event) => { + Event.findOne({ _id }).exec((err, event) => { if (err) { rej(err); return; diff --git a/server/src/schema/mutations/RegisterToEvent.js b/server/src/schema/mutations/RegisterToEvent.js index a3443c4de..445e26628 100644 --- a/server/src/schema/mutations/RegisterToEvent.js +++ b/server/src/schema/mutations/RegisterToEvent.js @@ -2,7 +2,6 @@ import 'dotenv/config'; import { GraphQLNonNull, GraphQLID, - GraphQLString, } from 'graphql'; import axios from 'axios'; import { @@ -12,19 +11,17 @@ import { } from '../../errors'; import { EventType } from '../types'; import { Event } from '../../models'; +import { ParticipantInputType } from '../types/input'; const args = { - designator: { type: new GraphQLNonNull(GraphQLID) }, - teamId: { type: new GraphQLNonNull(GraphQLString) }, - botId: { type: GraphQLString }, - releaseId: { type: GraphQLString }, + eventId: { type: new GraphQLNonNull(GraphQLID) }, + participant: { type: ParticipantInputType }, }; -const resolve = (parent, { - designator, teamId, botId, releaseId, -}, context) => { - const userid = context.userId; - if (!userid) { +const resolve = (parent, + { eventId: _id, participant: { teamId, botId, releaseId } }, + { userId, authorization }) => { + if (!userId) { throw new AuthentificationError(); } return new Promise((res, rej) => { @@ -40,16 +37,14 @@ const resolve = (parent, { } `, }, - headers: { - authorization: context.authorization, - }, + headers: { authorization }, }).then( (result) => { if (!result.data.data.teamsByRole.some(team => team.id === teamId)) { rej(new WrongArgumentsError('You are not eligible to register this team')); return; } - Event.findOne({ designator }).exec((err, event) => { + Event.findOne({ _id }).exec((err, event) => { if (err) { rej(err); return; diff --git a/server/src/schema/queries/Event.js b/server/src/schema/queries/Event.js index c35d48f47..590aac259 100644 --- a/server/src/schema/queries/Event.js +++ b/server/src/schema/queries/Event.js @@ -7,17 +7,17 @@ import { EventType } from '../types'; import { Event } from '../../models'; const args = { - designator: { type: new GraphQLNonNull(GraphQLID) }, + eventId: { type: new GraphQLNonNull(GraphQLID) }, }; -const resolve = (parent, { designator }) => new Promise((res, rej) => { - Event.findOne({ designator }).exec((err, event) => { +const resolve = (parent, { eventId: _id }) => new Promise((res, rej) => { + Event.findOne({ _id }).exec((err, event) => { if (err) { rej(err); return; } if (!event) { - rej(new DatabaseError('None of the events respond to that designator')); + rej(new DatabaseError('None of the events respond to that id')); return; } res(event); diff --git a/server/src/schema/types/Event.js b/server/src/schema/types/Event.js index d54ecf377..4cc843906 100644 --- a/server/src/schema/types/Event.js +++ b/server/src/schema/types/Event.js @@ -17,7 +17,7 @@ const EventType = new GraphQLObjectType({ name: 'Event', description: 'Everything you need to know about an event', fields: () => ({ - designator: { type: GraphQLID }, + id: { type: GraphQLID }, name: { type: GraphQLString }, hostId: { type: GraphQLString }, description: { type: GraphQLString }, diff --git a/server/src/schema/types/input/Event.js b/server/src/schema/types/input/Event.js index fba283123..23fd7c2c7 100644 --- a/server/src/schema/types/input/Event.js +++ b/server/src/schema/types/input/Event.js @@ -4,8 +4,6 @@ import { GraphQLInputObjectType, GraphQLList, } from 'graphql'; -import InputFormulaType from './Formula'; -import InputPlotterType from './Plotter'; import InputPrizeType from './Prize'; import InputParticipantType from './Participant'; import InputRuleType from './Rule'; @@ -19,9 +17,7 @@ const Type = new GraphQLInputObjectType({ startDatetime: { type: GraphQLInt }, finishDatetime: { type: GraphQLInt }, formulaId: { type: GraphQLString }, - formula: { type: InputFormulaType }, plotterId: { type: GraphQLString }, - plotter: { type: InputPlotterType }, prizes: { type: new GraphQLList(InputPrizeType) }, participants: { type: new GraphQLList(InputParticipantType) }, rules: { type: new GraphQLList(InputRuleType) },