Files
Superalgos/api/src/graphql/resolvers/Query/Team.js
T
2018-10-30 21:34:00 -07:00

52 lines
1.7 KiB
JavaScript

import { logger } 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, { id }, ctx, info) => {
return ctx.db.query.team({ where: { id } }, info)
}
export const teamByName = async (parent, { name }, ctx, info) => {
return ctx.db.query.team({ where: { name: name } }, `{ name }`)
}
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 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 = res.graphQLErrors.map((error) => {
return error.message
})
return errors
}
logger.info('teamsByRole teamAdmin: ', teamAdmin)
return teamAdmin
}