Master refactoring, remove designator field

This commit is contained in:
Nikola Bjelo committed 2018-11-05 15:27:09 +01:00
1 parent 91b1a99598
commit 189a8f1d84
11 files changed
+56 -130

No files matched your search

+1
View File
@@ -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;
+5 -4
View File
@@ -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);
+4 -32
View File
@@ -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);
+7 -8
View File
@@ -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) {
+7 -15
View File
@@ -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) {
+8 -33
View File
@@ -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);
});
});
};
+10 -15
View File
@@ -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;
+9 -14
View File
@@ -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;
+4 -4
View File
@@ -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);
+1 -1
View File
@@ -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 },
-4
View File
@@ -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) },