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.

63 lines
1.4 KiB

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