group.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const graphHelper = require('../../helpers/graph')
  2. /* global WIKI */
  3. const gql = require('graphql')
  4. module.exports = {
  5. Query: {
  6. async groups() { return {} }
  7. },
  8. Mutation: {
  9. async groups() { return {} }
  10. },
  11. GroupQuery: {
  12. async list(obj, args, context, info) {
  13. return WIKI.db.Group.findAll({
  14. attributes: {
  15. include: [[WIKI.db.inst.fn('COUNT', WIKI.db.inst.col('users.id')), 'userCount']]
  16. },
  17. include: [{
  18. model: WIKI.db.User,
  19. attributes: [],
  20. through: {
  21. attributes: []
  22. }
  23. }],
  24. raw: true,
  25. // TODO: Figure out how to exclude these extra fields...
  26. group: ['group.id', 'users->userGroups.createdAt', 'users->userGroups.updatedAt', 'users->userGroups.version', 'users->userGroups.userId', 'users->userGroups.groupId']
  27. })
  28. }
  29. },
  30. GroupMutation: {
  31. assignUser(obj, args) {
  32. return WIKI.db.Group.findById(args.groupId).then(grp => {
  33. if (!grp) {
  34. throw new gql.GraphQLError('Invalid Group ID')
  35. }
  36. return WIKI.db.User.findById(args.userId).then(usr => {
  37. if (!usr) {
  38. throw new gql.GraphQLError('Invalid User ID')
  39. }
  40. return grp.addUser(usr)
  41. })
  42. })
  43. },
  44. async create(obj, args) {
  45. const group = await WIKI.db.Group.create({
  46. name: args.name
  47. })
  48. console.info(group)
  49. return {
  50. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  51. group
  52. }
  53. },
  54. delete(obj, args) {
  55. return WIKI.db.Group.destroy({
  56. where: {
  57. id: args.id
  58. },
  59. limit: 1
  60. })
  61. },
  62. unassignUser(obj, args) {
  63. return WIKI.db.Group.findById(args.groupId).then(grp => {
  64. if (!grp) {
  65. throw new gql.GraphQLError('Invalid Group ID')
  66. }
  67. return WIKI.db.User.findById(args.userId).then(usr => {
  68. if (!usr) {
  69. throw new gql.GraphQLError('Invalid User ID')
  70. }
  71. return grp.removeUser(usr)
  72. })
  73. })
  74. },
  75. update(obj, args) {
  76. return WIKI.db.Group.update({
  77. name: args.name
  78. }, {
  79. where: { id: args.id }
  80. })
  81. }
  82. },
  83. Group: {
  84. users(grp) {
  85. return grp.getUsers()
  86. }
  87. }
  88. }