2018-03-25 02:35:47 +00:00
|
|
|
const graphHelper = require('../../helpers/graph')
|
2017-08-07 01:05:10 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-08-07 01:05:10 +00:00
|
|
|
|
2017-08-14 00:33:06 +00:00
|
|
|
const gql = require('graphql')
|
|
|
|
|
2017-08-07 01:05:10 +00:00
|
|
|
module.exports = {
|
2017-08-09 02:36:29 +00:00
|
|
|
Query: {
|
2018-03-19 03:12:56 +00:00
|
|
|
async groups() { return {} }
|
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
async groups() { return {} }
|
|
|
|
},
|
|
|
|
GroupQuery: {
|
2018-03-25 02:35:47 +00:00
|
|
|
async list(obj, args, context, info) {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.groups.query().select(
|
2018-05-19 20:40:07 +00:00
|
|
|
'groups.*',
|
2018-07-30 02:23:33 +00:00
|
|
|
WIKI.models.groups.relatedQuery('users').count().as('userCount')
|
2018-05-19 20:40:07 +00:00
|
|
|
)
|
2018-03-28 04:02:32 +00:00
|
|
|
},
|
|
|
|
async single(obj, args, context, info) {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.groups.query().findById(args.id)
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-19 03:12:56 +00:00
|
|
|
GroupMutation: {
|
2018-05-12 20:13:04 +00:00
|
|
|
async assignUser(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
const grp = await WIKI.models.groups.query().findById(args.groupId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
const usr = await WIKI.models.users.query().findById(args.userId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
await grp.$relatedQuery('users').relate(usr.id)
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User has been assigned to group.')
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2018-03-25 02:35:47 +00:00
|
|
|
async create(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
const group = await WIKI.models.groups.query().insertAndFetch({
|
2018-03-25 02:35:47 +00:00
|
|
|
name: args.name
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group created successfully.'),
|
|
|
|
group
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2018-03-26 05:11:49 +00:00
|
|
|
async delete(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.groups.query().deleteById(args.id)
|
2018-03-26 05:11:49 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group has been deleted.')
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2018-05-12 20:13:04 +00:00
|
|
|
async unassignUser(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
const grp = await WIKI.models.groups.query().findById(args.groupId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
const usr = await WIKI.models.users.query().findById(args.userId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
|
|
|
|
}
|
2017-08-20 02:51:25 +00:00
|
|
|
},
|
2018-05-12 20:13:04 +00:00
|
|
|
async update(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.groups.query().patch({ name: args.name }).where('id', args.id)
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group has been updated.')
|
|
|
|
}
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
2017-08-07 01:05:10 +00:00
|
|
|
},
|
2017-08-09 02:36:29 +00:00
|
|
|
Group: {
|
2017-08-07 01:05:10 +00:00
|
|
|
users(grp) {
|
2018-05-19 20:40:07 +00:00
|
|
|
return grp.$relatedQuery('users')
|
2017-08-07 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|