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) {
|
|
|
|
return WIKI.db.Group.findAll({
|
|
|
|
attributes: {
|
|
|
|
include: [[WIKI.db.inst.fn('COUNT', WIKI.db.inst.col('users.id')), 'userCount']]
|
|
|
|
},
|
|
|
|
include: [{
|
|
|
|
model: WIKI.db.User,
|
|
|
|
attributes: [],
|
|
|
|
through: {
|
|
|
|
attributes: []
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
raw: true,
|
|
|
|
// TODO: Figure out how to exclude these extra fields...
|
|
|
|
group: ['group.id', 'users->userGroups.createdAt', 'users->userGroups.updatedAt', 'users->userGroups.version', 'users->userGroups.userId', 'users->userGroups.groupId']
|
|
|
|
})
|
2018-03-28 04:02:32 +00:00
|
|
|
},
|
|
|
|
async single(obj, args, context, info) {
|
|
|
|
return WIKI.db.Group.findById(args.id)
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-19 03:12:56 +00:00
|
|
|
GroupMutation: {
|
|
|
|
assignUser(obj, args) {
|
2018-03-05 20:49:36 +00:00
|
|
|
return WIKI.db.Group.findById(args.groupId).then(grp => {
|
2017-08-14 00:33:06 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2018-03-05 20:49:36 +00:00
|
|
|
return WIKI.db.User.findById(args.userId).then(usr => {
|
2017-08-14 00:33:06 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
|
|
|
return grp.addUser(usr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2018-03-25 02:35:47 +00:00
|
|
|
async create(obj, args) {
|
|
|
|
const group = await WIKI.db.Group.create({
|
|
|
|
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) {
|
|
|
|
await WIKI.db.Group.destroy({
|
2017-08-14 00:33:06 +00:00
|
|
|
where: {
|
|
|
|
id: args.id
|
|
|
|
},
|
|
|
|
limit: 1
|
|
|
|
})
|
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-03-19 03:12:56 +00:00
|
|
|
unassignUser(obj, args) {
|
2018-03-05 20:49:36 +00:00
|
|
|
return WIKI.db.Group.findById(args.groupId).then(grp => {
|
2017-08-14 00:33:06 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2018-03-05 20:49:36 +00:00
|
|
|
return WIKI.db.User.findById(args.userId).then(usr => {
|
2017-08-14 00:33:06 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
|
|
|
return grp.removeUser(usr)
|
|
|
|
})
|
|
|
|
})
|
2017-08-20 02:51:25 +00:00
|
|
|
},
|
2018-03-19 03:12:56 +00:00
|
|
|
update(obj, args) {
|
2018-03-05 20:49:36 +00:00
|
|
|
return WIKI.db.Group.update({
|
2017-08-20 02:51:25 +00:00
|
|
|
name: args.name
|
|
|
|
}, {
|
|
|
|
where: { id: args.id }
|
|
|
|
})
|
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) {
|
|
|
|
return grp.getUsers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|