diff --git a/src/schema/types/Event.js b/src/schema/types/Event.js index 419c178ae..d20c85fe6 100644 --- a/src/schema/types/Event.js +++ b/src/schema/types/Event.js @@ -5,6 +5,9 @@ import { GraphQLList, GraphQLID, } from 'graphql'; +import axios from 'axios'; +import { ServiceUnavailableError } from '../../errors'; +import { Event } from '../../models'; import { FormulaType, PlotterType, @@ -46,6 +49,92 @@ const EventType = new GraphQLObjectType({ return parent.participants; }, }, + participatingAsId: { + type: new GraphQLList(GraphQLString), + resolve({ id: _id }, arg, { authorization }) { + if (!authorization) { + return ({}); + } + return new Promise((res, rej) => { + axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + { + teams_TeamsByRole{ + id + } + } + `, + }, + headers: { authorization }, + }).then( + (result) => { + const ret = []; + Event.findOne({ _id }).exec((err, event) => { + if (err) { + rej(err); + return; + } + result.data.data.teams_TeamsByRole.forEach((team) => { + if (event.participants.some(participant => participant.participantId === team.id)) { + ret.push(team.id); + } + }); + res(ret); + }); + }, + (error) => { + rej(new ServiceUnavailableError(error)); + }, + ); + }); + }, + }, + canParticipateAsId: { + type: new GraphQLList(GraphQLString), + resolve({ id: _id }, arg, { authorization }) { + if (!authorization) { + return ({}); + } + return new Promise((res, rej) => { + axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + { + teams_TeamsByRole{ + id + } + } + `, + }, + headers: { authorization }, + }).then( + (result) => { + const ret = []; + Event.findOne({ _id }).exec((err, event) => { + if (err) { + rej(err); + return; + } + result.data.data.teams_TeamsByRole.forEach((team) => { + if (!(event.participants.some(participant => participant.participantId === team.id))) { + ret.push(team.id); + } + }); + res(ret); + }); + }, + (error) => { + rej(new ServiceUnavailableError(error)); + }, + ); + }); + }, + }, invitations: { type: new GraphQLList(InvitationType), resolve(parent) {