Files
Superalgos/api/src/graphql/resolvers/Query/Team.js
T
2018-11-07 08:55:56 -08:00

58 lines
1.9 KiB
JavaScript

import { logger, AuthenticationError, DatabaseError } from '../../../logger'
import TEAMS_CONNECTIONS_FRAGMENT from '../../fragments/TeamsConnectionsFragment'
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 teamById = async (parent, { teamId }, ctx, info) => {
logger.info('teamById')
logger.info(teamId)
return ctx.db.query.team({ where: { id: teamId } }, info)
}
export const teamByName = async (parent, { name }, ctx, info) => {
return ctx.db.query.team({ where: { name: name } }, TEAMS_FRAGMENT)
}
export const teamBySlug= async (parent, { slug }, ctx, info) => {
return ctx.db.query.team({ where: { slug: slug } }, TEAMS_FRAGMENT)
}
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 teamsByOwner = async (parent, args, ctx, info) => {
logger.info('teamsByOwner')
const authId = ctx.request.headers.userid
if (!authId) {
throw new AuthenticationError()
return
}
return ctx.db.query.teams({where: { owner: authId }, orderBy:'updatedAt_DESC'}, TEAMS_FRAGMENT)
}
export const teamsByRole = async (parent, args, ctx, info) => {
const authId = ctx.request.headers.userid
if (!authId) {
throw new AuthenticationError()
}
let teamAdmin
try {
teamAdmin= await ctx.db.query.teams({where: {members_some: {AND: [{member: {authId: authId}, role_not:'MEMBER'}]}}}, TEAMS_FRAGMENT)
} catch (err) {
logger.debug('teamsByRole error: ')
logger.debug(err)
const errors = err.graphQLErrors.map((error) => {
throw new DatabaseError(error.message)
})
}
logger.info('teamsByRole teamAdmin: ', teamAdmin)
return teamAdmin
}