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.

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