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.

83 lines
2.4 KiB

  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.models.groups.query().select(
  14. 'groups.*',
  15. WIKI.models.groups.relatedQuery('users').count().as('userCount')
  16. )
  17. },
  18. async single(obj, args, context, info) {
  19. return WIKI.models.groups.query().findById(args.id)
  20. }
  21. },
  22. GroupMutation: {
  23. async assignUser(obj, args) {
  24. const grp = await WIKI.models.groups.query().findById(args.groupId)
  25. if (!grp) {
  26. throw new gql.GraphQLError('Invalid Group ID')
  27. }
  28. const usr = await WIKI.models.users.query().findById(args.userId)
  29. if (!usr) {
  30. throw new gql.GraphQLError('Invalid User ID')
  31. }
  32. await grp.$relatedQuery('users').relate(usr.id)
  33. return {
  34. responseResult: graphHelper.generateSuccess('User has been assigned to group.')
  35. }
  36. },
  37. async create(obj, args) {
  38. const group = await WIKI.models.groups.query().insertAndFetch({
  39. name: args.name,
  40. permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
  41. isSystem: false
  42. })
  43. return {
  44. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  45. group
  46. }
  47. },
  48. async delete(obj, args) {
  49. await WIKI.models.groups.query().deleteById(args.id)
  50. return {
  51. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  52. }
  53. },
  54. async unassignUser(obj, args) {
  55. const grp = await WIKI.models.groups.query().findById(args.groupId)
  56. if (!grp) {
  57. throw new gql.GraphQLError('Invalid Group ID')
  58. }
  59. const usr = await WIKI.models.users.query().findById(args.userId)
  60. if (!usr) {
  61. throw new gql.GraphQLError('Invalid User ID')
  62. }
  63. await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
  64. return {
  65. responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
  66. }
  67. },
  68. async update(obj, args) {
  69. await WIKI.models.groups.query().patch({ name: args.name }).where('id', args.id)
  70. return {
  71. responseResult: graphHelper.generateSuccess('Group has been updated.')
  72. }
  73. }
  74. },
  75. Group: {
  76. users(grp) {
  77. return grp.$relatedQuery('users')
  78. }
  79. }
  80. }