Checking for eligibility in the team

This commit is contained in:
Nikola Bjelo committed 2018-10-28 17:10:46 +01:00
1 parent 13ad9b4e39
commit 7476850135
7 files changed
+154 -70

No files matched your search

+2
View File
@@ -8,3 +8,5 @@ AUTH0_AUDIENCE=auth0-audience
AUTH0_ISSUER=auth0-issuer
SENTRY_DNS=https://some-val@sentry.io/some-id
TEAMS_ENDPOINT=https://teams-api.advancedalgos.net/graphql/
+38 -30
View File
@@ -168,6 +168,15 @@
"integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==",
"dev": true
},
"axios": {
"version": "0.18.0",
"resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
}
},
"babel-cli": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz",
@@ -1738,6 +1747,24 @@
"integrity": "sha512-lAq4tLbm3sidmdCN8G3ExaxH7cUCtP5mgDvrYowsx84dcYkJJ4I28N7gkxA6+YlSXzaGLJYIDEi9WGfXzMiXdw==",
"dev": true
},
"follow-redirects": {
"version": "1.5.9",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.9.tgz",
"integrity": "sha512-Bh65EZI/RU8nx0wbYF9shkFZlqLP+6WT/5FnA3cE/djNSuKNHJEinGGZgu/cQEkeeb2GdFOgenAmn8qaqYke2w==",
"requires": {
"debug": "=3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@@ -1811,8 +1838,7 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"aproba": {
"version": "1.2.0",
@@ -1833,14 +1859,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@@ -1855,20 +1879,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@@ -1985,8 +2006,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@@ -1998,7 +2018,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@@ -2013,7 +2032,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@@ -2021,14 +2039,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
@@ -2047,7 +2063,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@@ -2128,8 +2143,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@@ -2141,7 +2155,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@@ -2227,8 +2240,7 @@
"safe-buffer": {
"version": "5.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
@@ -2264,7 +2276,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@@ -2284,7 +2295,6 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@@ -2328,14 +2338,12 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"yallist": {
"version": "3.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
}
}
},
+1
View File
@@ -22,6 +22,7 @@
},
"dependencies": {
"@sentry/node": "^4.1.1",
"axios": "^0.18.0",
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"express": "^4.16.3",
+5
View File
@@ -12,3 +12,8 @@ export class WrongArgumentsError extends Error {
code = 400
message = 'Wrong arguments : ' + this.message
}
export class ServiceUnavailableError extends Error {
code = 503
message = 'At least one service is unresponding ' + this.message
}
+4 -1
View File
@@ -39,7 +39,10 @@ db.once('open', () => { console.log('Connected to the DB.') })
app.use('/graphql', graphqlHTTP(req => {
return {
schema: schema,
context: { userId: req.headers.userid }
context: {
userId: req.headers.userid,
authorization: req.headers.authorization
}
}
}))
app.get('/playground', expressPlayground({ endpoint: '/graphql' }))
+50 -18
View File
@@ -3,7 +3,12 @@ import {
GraphQLID,
GraphQLString
} from 'graphql'
import { AuthentificationError } from '../../errors'
import axios from 'axios'
import {
AuthentificationError,
WrongArgumentsError,
ServiceUnavailableError
} from '../../errors'
import { EventType } from '../types'
import { Event } from '../../models'
@@ -15,32 +20,59 @@ const args = {
}
const resolve = (parent, { eventDesignator, teamId, botId, releaseId }, context) => {
const hostId = context.userId
if (!hostId) {
const userid = context.userId
if (!userid) {
throw new AuthentificationError()
}
return new Promise((resolve, reject) => {
Event.findOne({ designator: eventDesignator }).exec((err, event) => {
if (err) reject(err)
else {
let participantIndex
event.participants.find((participant, index) => {
if (participant.teamId === teamId) {
participantIndex = index
axios({
url: process.env.TEAMS_ENDPOINT,
method: 'post',
data: {
query: `
{
teamsByRole{
id
}
}
})
event.participants[participantIndex].botId = botId || event.participants[participantIndex].botId
event.participants[participantIndex].releaseId = releaseId || event.participants[participantIndex].releaseId
event.save((err) => {
`
},
headers: {
userid,
authorization: context.authorization
}
}).then(
(result) => {
if (!result.data.data.teamsByRole.some(team => team.id === teamId)) {
reject(new WrongArgumentsError('You are not eligible to register this team'))
return
}
Event.findOne({ designator: eventDesignator }).exec((err, event) => {
if (err) reject(err)
else {
resolve(event)
let participantIndex
event.participants.find((participant, index) => {
if (participant.teamId === teamId) {
participantIndex = index
}
})
event.participants[participantIndex].botId = botId || event.participants[participantIndex].botId
event.participants[participantIndex].releaseId = releaseId || event.participants[participantIndex].releaseId
event.save((err) => {
if (err) reject(err)
else {
resolve(event)
}
})
}
})
},
(error) => {
reject(new ServiceUnavailableError(error))
}
})
)
})
}
+54 -21
View File
@@ -1,9 +1,15 @@
import 'dotenv/config'
import {
GraphQLNonNull,
GraphQLID,
GraphQLString
} from 'graphql'
import { AuthentificationError, WrongArgumentsError } from '../../errors'
import axios from 'axios'
import {
AuthentificationError,
WrongArgumentsError,
ServiceUnavailableError
} from '../../errors'
import { EventType } from '../types'
import { Event } from '../../models'
@@ -15,31 +21,58 @@ const args = {
}
const resolve = (parent, { designator, teamId, botId, releaseId }, context) => {
const hostId = context.userId
if (!hostId) {
const userid = context.userId
if (!userid) {
throw new AuthentificationError()
}
return new Promise((resolve, reject) => {
Event.findOne({ designator }).exec((err, event) => {
if (err) reject(err)
else {
if (event.participants.some(participant => participant.teamId === teamId)) {
reject(new WrongArgumentsError('You are already registred'))
} else {
event.participants.push({
teamId,
botId,
releaseId
})
event.save((err) => {
if (err) reject(err)
else {
resolve(event)
axios({
url: process.env.TEAMS_ENDPOINT,
method: 'post',
data: {
query: `
{
teamsByRole{
id
}
})
}
}
`
},
headers: {
userid,
authorization: context.authorization
}
})
}).then(
(result) => {
if (!result.data.data.teamsByRole.some(team => team.id === teamId)) {
reject(new WrongArgumentsError('You are not eligible to register this team'))
return
}
Event.findOne({ designator }).exec((err, event) => {
if (err) reject(err)
else {
if (event.participants.some(participant => participant.teamId === teamId)) {
reject(new WrongArgumentsError('You are already registred'))
} else {
event.participants.push({
teamId,
botId,
releaseId
})
event.save((err) => {
if (err) reject(err)
else {
resolve(event)
}
})
}
}
})
},
(error) => {
reject(new ServiceUnavailableError(error))
}
)
})
}