From 766ed5e8ef34d14dac29dcd16ba70470d819eb8c Mon Sep 17 00:00:00 2001 From: Barry Low Date: Fri, 15 Feb 2019 09:02:16 -0800 Subject: [PATCH] API: Add queries teamsByIds and teamAuthorization --- api/src/graphql/resolvers/Query/Team.js | 22 ++++++++++++++++++++-- api/src/graphql/resolvers/Query/index.js | 4 +++- api/src/graphql/schema.graphql | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/api/src/graphql/resolvers/Query/Team.js b/api/src/graphql/resolvers/Query/Team.js index 2149dc71c..9c9683148 100644 --- a/api/src/graphql/resolvers/Query/Team.js +++ b/api/src/graphql/resolvers/Query/Team.js @@ -5,14 +5,19 @@ import TEAMS_FRAGMENT from '../../fragments/TeamsFragment' export const teams = async (parent, args, ctx, info) => { logger.info('teams') - console.log('teams') return ctx.db.query.teamsConnection({}, TEAMS_CONNECTIONS_FRAGMENT) } +export const teamsByIds = async (parent, { teamIds }, ctx, info) => { + logger.info('teamsByIds') + logger.info(teamIds) + return ctx.db.query.teams({where: { id_in: teamIds } }, TEAMS_FRAGMENT) +} + export const teamById = async (parent, { teamId }, ctx, info) => { logger.info('teamById') logger.info(teamId) - return ctx.db.query.team({ where: { id: teamId } }, info) + return ctx.db.query.team({ where: { id: teamId } }, TEAMS_FRAGMENT) } export const teamByName = async (parent, { name }, ctx, info) => { @@ -29,6 +34,19 @@ export const teamWithRole = async (parent, { teamId, role }, ctx, info) => { return ctx.db.query.teamsConnection({where: {AND: [{id: teamId},{members_some:{role: role}}]}, first:1}, info) } +export const teamAuthorization = async (parent, { teamId, role }, ctx, info) => { + const authId = ctx.request.headers.userid + if (!authId) { + throw new AuthenticationError() + return + } + if(role == undefined || role == null){ + return ctx.db.query.teamsConnection({where:{ AND:{id:teamId,members_some:{member:{authId:authId}, role_in:['OWNER','ADMIN']}}}, first:1}, info) + } else { + return ctx.db.query.teamsConnection({where:{ AND:{id:teamId,members_some:{member:{authId:authId}, role:role}}}, first:1}, info) + } +} + export const teamsByOwner = async (parent, args, ctx, info) => { logger.info('teamsByOwner') const authId = ctx.request.headers.userid diff --git a/api/src/graphql/resolvers/Query/index.js b/api/src/graphql/resolvers/Query/index.js index c88d132c1..d33889b88 100644 --- a/api/src/graphql/resolvers/Query/index.js +++ b/api/src/graphql/resolvers/Query/index.js @@ -1,7 +1,7 @@ import { logger } from '../../../logger' import { member, owner } from './Member' -import { teams, teamById, teamByName, teamBySlug, teamWithRole, teamsByOwner, teamsByRole } from './Team' +import { teams, teamById, teamsByIds, teamByName, teamBySlug, teamWithRole, teamAuthorization, teamsByOwner, teamsByRole } from './Team' import{ fbByTeamMember } from './TeamMember' export const resolvers = { @@ -9,9 +9,11 @@ export const resolvers = { member: (parent, arg, ctx, info) => member(parent, arg, ctx, info), teams: (parent, arg, ctx, info) => teams(parent, arg, ctx, info), teamById: (parent, arg, ctx, info) => teamById(parent, arg, ctx, info), + teamsByIds: (parent, arg, ctx, info) => teamsByIds(parent, arg, ctx, info), teamByName: (parent, arg, ctx, info) => teamByName(parent, arg, ctx, info), teamBySlug: (parent, arg, ctx, info) => teamBySlug(parent, arg, ctx, info), teamWithRole: (parent, arg, ctx, info) => teamWithRole(parent, arg, ctx, info), + teamAuthorization: (parent, arg, ctx, info) => teamAuthorization(parent, arg, ctx, info), teamsByOwner: (parent, arg, ctx, info) => teamsByOwner(parent, arg, ctx, info), teamsByRole: (parent, arg, ctx, info) => teamsByRole(parent, arg, ctx, info), owner: (parent, arg, ctx, info) => owner(parent, arg, ctx, info), diff --git a/api/src/graphql/schema.graphql b/api/src/graphql/schema.graphql index b187cef99..e43cd51ed 100644 --- a/api/src/graphql/schema.graphql +++ b/api/src/graphql/schema.graphql @@ -11,7 +11,9 @@ type Query { teamByName(name: String!): Team teamBySlug(slug: String!): Team teamById(teamId: String!): Team + teamsByIds(teamIds: [ID!]): [Team] teamWithRole(teamId: String!, role: String!): Team + teamAuthorization(teamId: String!, role: String): Team teamsByOwner: [Team!]! @isAuthenticated teamsByRole: [Team!]! @isAuthenticated fbByTeamMember: Team @isAuthenticated