From dd3c259887dabfbcfbd1fd1b6d9f865f88881763 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 19 Feb 2019 01:14:03 +0100 Subject: [PATCH] adding participatingAsId and canParticipateAsId fields --- src/schema/types/Event.js | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) 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) {