Setting mutations

This commit is contained in:
Unknown committed 2019-03-16 12:43:04 +01:00
1 parent d6657a4a5d
commit 60cf41c2f6
6 files changed
+74 -161

No files matched your search

-39
View File
@@ -1,39 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
} from 'graphql';
import { AuthentificationError } from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
import EventStateEnum from '../types/enum/EventState';
const args = {
eventId: { type: new GraphQLNonNull(GraphQLID) },
state: { type: EventStateEnum },
};
const resolve = (parent, { eventId: _id, state }, { userId: hostId }) => {
if (!hostId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
Event.findOneAndUpdate({ _id, hostId }, { state }, { new: true }, (err, modifiedEvent) => {
if (err) {
rej(err);
return;
}
res(modifiedEvent);
});
});
};
const mutation = {
changeEventState: {
type: EventType,
args,
resolve,
},
};
export default mutation;
-37
View File
@@ -1,37 +0,0 @@
import { AuthentificationError } from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
import { EventInputType } from '../types/input';
export const args = {
event: { type: EventInputType },
};
const resolve = (parent, { event }, { userId: hostId }) => {
if (!hostId) {
throw new AuthentificationError();
}
event.hostId = hostId;
const newEvent = new Event(event);
return new Promise((res, rej) => {
newEvent.save((err) => {
if (err) {
rej(err);
return;
}
res(newEvent);
});
});
};
const mutation = {
createEvent: {
type: EventType,
args,
resolve,
},
};
export default mutation;
+31
View File
@@ -0,0 +1,31 @@
import { StrategyType } from '../types';
import { Strategy } from '../../models';
import { StrategyInputType } from '../types/input';
export const args = {
strategy: { type: StrategyInputType },
};
const resolve = (parent, { strategy }) => {
const newStrategy = new Strategy(strategy);
return new Promise((res, rej) => {
newStrategy.save((err) => {
if (err) {
rej(err);
return;
}
res(newStrategy);
});
});
};
const mutation = {
createStrategy: {
type: StrategyType,
args,
resolve,
},
};
export default mutation;
-81
View File
@@ -1,81 +0,0 @@
import {
GraphQLNonNull,
GraphQLID,
} from 'graphql';
import axios from 'axios';
import {
AuthentificationError,
WrongArgumentsError,
ServiceUnavailableError,
} from '../../errors';
import { EventType } from '../types';
import { Event } from '../../models';
const args = {
eventId: { type: new GraphQLNonNull(GraphQLID) },
};
const resolve = (parent,
{ eventId: _id, participant: { teamId, botId, releaseId } },
{ userId, authorization }) => {
if (!userId) {
throw new AuthentificationError();
}
return new Promise((res, rej) => {
axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
{
teams_TeamsByRole{
id
}
}
`,
},
headers: { authorization },
}).then(
(result) => {
if (!result.data.data.teamsByRole.some(team => team.id === teamId)) {
rej(new WrongArgumentsError('You are not eligible to edit this team'));
return;
}
Event.findOne({ _id }).exec((err, event) => {
if (err) {
rej(err);
return;
}
const participantIndex = event.participant
.findIndex(participant => participant.teamId === teamId);
event.participants[participantIndex].botId = botId
|| event.participants[participantIndex].botId;
event.participants[participantIndex].releaseId = releaseId
|| event.participants[participantIndex].releaseId;
event.save((error) => {
if (error) {
rej(error);
return;
}
res(event);
});
});
},
(error) => {
rej(new ServiceUnavailableError(error));
},
);
});
};
const mutation = {
editParticipant: {
type: EventType,
args,
resolve,
},
};
export default mutation;
+39
View File
@@ -0,0 +1,39 @@
import {
GraphQLNonNull,
GraphQLID,
} from 'graphql';
import { StrategyType } from '../types';
import { Strategy } from '../../models';
import { StrategyInputType } from '../types/input';
const args = {
id: { type: new GraphQLNonNull(GraphQLID) },
newStrategy: { type: new GraphQLNonNull(StrategyInputType) },
};
const resolve = (parent, { id: _id, newStrategy }) => new Promise((res, rej) => {
Strategy.findOne({ _id }).exec((err, strategy) => {
if (err) {
rej(err);
return;
}
strategy = newStrategy;
strategy.save((error) => {
if (error) {
rej(error);
return;
}
res(strategy);
});
});
});
const mutation = {
editParticipant: {
type: StrategyType,
args,
resolve,
},
};
export default mutation;
+4 -4
View File
@@ -2,14 +2,14 @@ import {
GraphQLObjectType,
} from 'graphql';
import CreateEventMutation from './CreateEvent';
import ChangeEventStateMutation from './ChangeEventState';
import CreateStrategyMutation from './CreateStrategy';
import EditStrategyMutation from './EditStrategy';
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: Object.assign(
CreateEventMutation,
ChangeEventStateMutation,
CreateStrategyMutation,
EditStrategyMutation,
),
});