wikijs-fork/server/graph/resolvers/group.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

const graphHelper = require('../../helpers/graph')
2017-08-07 01:05:10 +00:00
/* global WIKI */
2017-08-07 01:05:10 +00:00
const gql = require('graphql')
2017-08-07 01:05:10 +00:00
module.exports = {
Query: {
async groups() { return {} }
},
Mutation: {
async groups() { return {} }
},
GroupQuery: {
async list(obj, args, context, info) {
return WIKI.models.groups.query().select(
'groups.*',
WIKI.models.groups.relatedQuery('users').count().as('userCount')
)
2018-03-28 04:02:32 +00:00
},
async single(obj, args, context, info) {
return WIKI.models.groups.query().findById(args.id)
}
},
GroupMutation: {
async assignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
await grp.$relatedQuery('users').relate(usr.id)
return {
responseResult: graphHelper.generateSuccess('User has been assigned to group.')
}
},
async create(obj, args) {
const group = await WIKI.models.groups.query().insertAndFetch({
2018-10-14 21:38:39 +00:00
name: args.name,
permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
pageRules: JSON.stringify([]),
2018-10-14 21:38:39 +00:00
isSystem: false
})
return {
responseResult: graphHelper.generateSuccess('Group created successfully.'),
group
}
},
2018-03-26 05:11:49 +00:00
async delete(obj, args) {
await WIKI.models.groups.query().deleteById(args.id)
2018-03-26 05:11:49 +00:00
return {
responseResult: graphHelper.generateSuccess('Group has been deleted.')
}
},
async unassignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
return {
responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
}
},
async update(obj, args) {
await WIKI.models.groups.query().patch({
name: args.name,
permissions: JSON.stringify(args.permissions),
pageRules: JSON.stringify(args.pageRules)
}).where('id', args.id)
return {
responseResult: graphHelper.generateSuccess('Group has been updated.')
}
}
2017-08-07 01:05:10 +00:00
},
Group: {
2017-08-07 01:05:10 +00:00
users(grp) {
return grp.$relatedQuery('users')
2017-08-07 01:05:10 +00:00
}
}
}