group.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. async single(obj, args, context, info) {
  30. return WIKI.db.Group.findById(args.id)
  31. }
  32. },
  33. GroupMutation: {
  34. assignUser(obj, args) {
  35. return WIKI.db.Group.findById(args.groupId).then(grp => {
  36. if (!grp) {
  37. throw new gql.GraphQLError('Invalid Group ID')
  38. }
  39. return WIKI.db.User.findById(args.userId).then(usr => {
  40. if (!usr) {
  41. throw new gql.GraphQLError('Invalid User ID')
  42. }
  43. return grp.addUser(usr)
  44. })
  45. })
  46. },
  47. async create(obj, args) {
  48. const group = await WIKI.db.Group.create({
  49. name: args.name
  50. })
  51. return {
  52. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  53. group
  54. }
  55. },
  56. async delete(obj, args) {
  57. await WIKI.db.Group.destroy({
  58. where: {
  59. id: args.id
  60. },
  61. limit: 1
  62. })
  63. return {
  64. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  65. }
  66. },
  67. unassignUser(obj, args) {
  68. return WIKI.db.Group.findById(args.groupId).then(grp => {
  69. if (!grp) {
  70. throw new gql.GraphQLError('Invalid Group ID')
  71. }
  72. return WIKI.db.User.findById(args.userId).then(usr => {
  73. if (!usr) {
  74. throw new gql.GraphQLError('Invalid User ID')
  75. }
  76. return grp.removeUser(usr)
  77. })
  78. })
  79. },
  80. update(obj, args) {
  81. return WIKI.db.Group.update({
  82. name: args.name
  83. }, {
  84. where: { id: args.id }
  85. })
  86. }
  87. },
  88. Group: {
  89. users(grp) {
  90. return grp.getUsers()
  91. }
  92. }
  93. }