Removing userless mutation and adapting CreateEvent with new format

This commit is contained in:
Nikola Bjelo committed 2018-11-04 16:05:07 +01:00
1 parent 100119a75b
commit 73e1dbcdb8
10 files changed
+67 -501

No files matched your search

-62
View File
@@ -1,62 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLInt,
} from 'graphql';
import {
AuthentificationError,
DatabaseError,
WrongArgumentsError,
} from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventDesignator: { type: new GraphQLNonNull(GraphQLID) },
rank: { type: new GraphQLNonNull(GraphQLInt) },
amount: { type: new GraphQLNonNull(GraphQLInt) },
};
const resolve = (parent, { eventDesignator, rank, amount }, context) => {
const hostId = context.userId;
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
if (event.prizes.some(prize => prize.rank === rank)) {
rej(new WrongArgumentsError('A prize for that rank already exists'));
return;
}
event.prizes.push({
rank,
amount,
});
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
});
};
const mutation = {
addPrize: {
type: EventType,
args,
resolve,
},
};
export default mutation;
-70
View File
@@ -1,70 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLString,
} from 'graphql';
import {
AuthentificationError,
DatabaseError,
WrongArgumentsError,
} from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventDesignator: { type: new GraphQLNonNull(GraphQLID) },
title: { type: new GraphQLNonNull(GraphQLString) },
description: { type: new GraphQLNonNull(GraphQLString) },
};
const resolve = (parent, { eventDesignator, title, description }, context) => {
const hostId = context.userId;
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
if (event.rules.some(rule => rule.title === title)) {
rej(new WrongArgumentsError('A prize for that rank already exists'));
return;
}
const isFree = pos => event.rules.some(rule => rule.pos === pos);
let position = 1;
let shouldGrow = isFree(position);
while (shouldGrow) {
position += 1;
shouldGrow = isFree(position);
}
event.rules.push({
position,
title,
description,
});
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
});
};
const mutation = {
addRule: {
type: EventType,
args,
resolve,
},
};
export default mutation;
@@ -1,56 +0,0 @@
import {
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { AuthentificationError, DatabaseError } from '../../errors';
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;
if (!ownerId) {
throw new AuthentificationError();
}
const newFormula = new Formula({ name, ownerId });
return new Promise((res, reject) => {
Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => {
if (err) {
reject(err);
return;
}
if (!event) {
reject(new DatabaseError('None of the events you own respond to that designator'));
return;
}
newFormula.save((error) => {
if (error) {
reject(error);
return;
}
event.formula = newFormula._id;
event.save((eventError) => {
if (eventError) {
reject(eventError);
return;
}
res(event);
});
});
});
});
};
const mutation = {
createAndSetFormula: {
type: EventType,
args,
resolve,
},
};
export default mutation;
@@ -1,65 +0,0 @@
import {
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { AuthentificationError, DatabaseError } from '../../errors';
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;
if (!ownerId) {
throw new AuthentificationError();
}
const newPlotter = new Plotter({
name, host, repo, moduleName, ownerId,
});
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
newPlotter.save((error) => {
if (error) {
rej(error);
return;
}
event.plotter = newPlotter._id;
event.save((eventError) => {
if (eventError) {
rej(eventError);
return;
}
res(event);
});
});
});
});
};
const mutation = {
createAndSetPlotter: {
type: EventType,
args,
resolve,
},
};
export default mutation;
@@ -0,0 +1,65 @@
import { AuthentificationError } from '../../errors';
import { EventType } from '../types';
import { Event, Formula, Plotter } from '../../models';
import { slugify } from '../../utils/functions';
import { EventInputType } from '../types/input';
export const args = {
event: { type: EventInputType },
};
const resolve = (parent, { event }, context) => {
const hostId = context.userId;
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;
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);
return;
}
res(newEvent);
});
});
};
const mutation = {
createEvent: {
type: EventType,
args,
resolve,
},
};
export default mutation;
-56
View File
@@ -1,56 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLInt,
} from 'graphql';
import { AuthentificationError, DatabaseError } from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventDesignator: { type: new GraphQLNonNull(GraphQLID) },
rank: { type: new GraphQLNonNull(GraphQLInt) },
amount: { type: GraphQLInt },
};
const resolve = (parent, { eventDesignator, rank, amount }, context) => {
const hostId = context.userId;
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
const prizeIndex = event.prizes.findIndex(prize => prize.rank === rank);
if (!prizeIndex) {
rej(new DatabaseError('There is no prize for that index'));
return;
}
event.prizes[prizeIndex].amount = amount || event.prizes[prizeIndex].amount;
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
});
};
const mutation = {
editPrize: {
type: EventType,
args,
resolve,
},
};
export default mutation;
-64
View File
@@ -1,64 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLString,
} from 'graphql';
import { AuthentificationError, DatabaseError, WrongArgumentsError } from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventDesignator: { type: new GraphQLNonNull(GraphQLID) },
title: { type: new GraphQLNonNull(GraphQLString) },
newtitle: { type: GraphQLString },
description: { type: GraphQLString },
};
const resolve = (parent, {
eventDesignator, title, newtitle, description,
}, context) => {
const hostId = context.userId;
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
const ruleIndex = event.rules.findIndex(rule => rule.title === title);
if (!ruleIndex) {
rej(new DatabaseError('There is no rule with that title'));
return;
}
event.rules[ruleIndex].description = description || event.rules[ruleIndex].description;
if (event.rules.some(rule => rule.title === newtitle)) {
rej(new WrongArgumentsError('There is already a rule using your new title'));
return;
}
event.rules[ruleIndex].title = newtitle || event.rules[ruleIndex].title;
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
});
};
const mutation = {
editRule: {
type: EventType,
args,
resolve,
},
};
export default mutation;
-52
View File
@@ -1,52 +0,0 @@
import {
GraphQLNonNull,
GraphQLInt,
GraphQLString,
} from 'graphql';
import { AuthentificationError } from '../../errors';
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;
if (!hostId) {
throw new AuthentificationError();
}
const newEvent = new Event({
hostId,
name,
description,
startDatetime,
finishDatetime,
});
newEvent.designator = `${slugify(newEvent.name)}-${newEvent._id}`;
return new Promise((res, rej) => {
newEvent.save((err) => {
if (err) {
rej(err);
return;
}
res(newEvent);
});
});
};
const mutation = {
hostEvent: {
type: EventType,
args,
resolve,
},
};
export default mutation;
@@ -1,60 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLInt,
} from 'graphql';
import { AuthentificationError, DatabaseError } from '../../errors';
import { isBetween } from '../../utils/functions';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventDesignator: { type: new GraphQLNonNull(GraphQLID) },
fromPosition: { type: new GraphQLNonNull(GraphQLInt) },
toPosition: { type: new GraphQLNonNull(GraphQLInt) },
};
const resolve = (parent, { eventDesignator, fromPosition, toPosition }, context) => {
const hostId = context.userId;
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => {
if (err) {
rej(err);
return;
}
if (!event) {
rej(new DatabaseError('None of the events you own respond to that designator'));
return;
}
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;
}
});
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
});
};
const mutation = {
reorderRules: {
type: EventType,
args,
resolve,
},
};
export default mutation;
+2 -16
View File
@@ -2,36 +2,22 @@ import {
GraphQLObjectType,
} from 'graphql';
import HostEventMutation from './HostEvent';
import CreateEventMutation from './CreateEvent';
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 EditPrizeMutation from './EditPrize';
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(
HostEventMutation,
CreateEventMutation,
EditEventMutation,
RegisterToEventMutation,
EditParticipantMutation,
AddRuleMutation,
EditRuleMutation,
ReorderRulesMutation,
AddPrizeMutation,
EditPrizeMutation,
CreateFormulaMutation,
CreateAndSetFormulaMutation,
CreatePlotterMutation,
CreateAndSetPlotterMutation,
),
});