You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

  1. 'use strict'
  2. /* global wiki */
  3. const gql = require('graphql')
  4. module.exports = {
  5. Query: {
  6. groups(obj, args, context, info) {
  7. return wiki.db.Group.findAll({ where: args })
  8. }
  9. },
  10. Mutation: {
  11. assignUserToGroup(obj, args) {
  12. return wiki.db.Group.findById(args.groupId).then(grp => {
  13. if (!grp) {
  14. throw new gql.GraphQLError('Invalid Group ID')
  15. }
  16. return wiki.db.User.findById(args.userId).then(usr => {
  17. if (!usr) {
  18. throw new gql.GraphQLError('Invalid User ID')
  19. }
  20. return grp.addUser(usr)
  21. })
  22. })
  23. },
  24. createGroup(obj, args) {
  25. return wiki.db.Group.create(args)
  26. },
  27. deleteGroup(obj, args) {
  28. return wiki.db.Group.destroy({
  29. where: {
  30. id: args.id
  31. },
  32. limit: 1
  33. })
  34. },
  35. removeUserFromGroup(obj, args) {
  36. return wiki.db.Group.findById(args.groupId).then(grp => {
  37. if (!grp) {
  38. throw new gql.GraphQLError('Invalid Group ID')
  39. }
  40. return wiki.db.User.findById(args.userId).then(usr => {
  41. if (!usr) {
  42. throw new gql.GraphQLError('Invalid User ID')
  43. }
  44. return grp.removeUser(usr)
  45. })
  46. })
  47. },
  48. renameGroup(obj, args) {
  49. return wiki.db.Group.update({
  50. name: args.name
  51. }, {
  52. where: { id: args.id }
  53. })
  54. }
  55. },
  56. Group: {
  57. users(grp) {
  58. return grp.getUsers()
  59. }
  60. }
  61. }