group.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. return {
  49. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  50. group
  51. }
  52. },
  53. async delete(obj, args) {
  54. await WIKI.db.Group.destroy({
  55. where: {
  56. id: args.id
  57. },
  58. limit: 1
  59. })
  60. return {
  61. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  62. }
  63. },
  64. unassignUser(obj, args) {
  65. return WIKI.db.Group.findById(args.groupId).then(grp => {
  66. if (!grp) {
  67. throw new gql.GraphQLError('Invalid Group ID')
  68. }
  69. return WIKI.db.User.findById(args.userId).then(usr => {
  70. if (!usr) {
  71. throw new gql.GraphQLError('Invalid User ID')
  72. }
  73. return grp.removeUser(usr)
  74. })
  75. })
  76. },
  77. update(obj, args) {
  78. return WIKI.db.Group.update({
  79. name: args.name
  80. }, {
  81. where: { id: args.id }
  82. })
  83. }
  84. },
  85. Group: {
  86. users(grp) {
  87. return grp.getUsers()
  88. }
  89. }
  90. }