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.

116 lines
3.1 KiB

  1. const graphHelper = require('../../helpers/graph')
  2. /* global WIKI */
  3. module.exports = {
  4. Query: {
  5. async users() { return {} }
  6. },
  7. Mutation: {
  8. async users() { return {} }
  9. },
  10. UserQuery: {
  11. async list(obj, args, context, info) {
  12. return WIKI.models.users.query()
  13. .select('id', 'email', 'name', 'providerKey', 'isSystem', 'createdAt')
  14. },
  15. async search(obj, args, context, info) {
  16. return WIKI.models.users.query()
  17. .where('email', 'like', `%${args.query}%`)
  18. .orWhere('name', 'like', `%${args.query}%`)
  19. .limit(10)
  20. .select('id', 'email', 'name', 'providerKey', 'createdAt')
  21. },
  22. async single(obj, args, context, info) {
  23. let usr = await WIKI.models.users.query().findById(args.id)
  24. usr.password = ''
  25. usr.tfaSecret = ''
  26. return usr
  27. }
  28. },
  29. UserMutation: {
  30. async create (obj, args) {
  31. try {
  32. await WIKI.models.users.createNewUser(args)
  33. return {
  34. responseResult: graphHelper.generateSuccess('User created successfully')
  35. }
  36. } catch (err) {
  37. return graphHelper.generateError(err)
  38. }
  39. },
  40. async delete (obj, args) {
  41. try {
  42. if (args.id <= 2) {
  43. throw new WIKI.Error.UserDeleteProtected()
  44. }
  45. await WIKI.models.users.query().deleteById(args.id)
  46. return {
  47. responseResult: graphHelper.generateSuccess('User deleted successfully')
  48. }
  49. } catch (err) {
  50. if (err.message.indexOf('foreign') >= 0) {
  51. return graphHelper.generateError(new WIKI.Error.UserDeleteForeignConstraint())
  52. } else {
  53. return graphHelper.generateError(err)
  54. }
  55. }
  56. },
  57. async update (obj, args) {
  58. try {
  59. await WIKI.models.users.updateUser(args)
  60. return {
  61. responseResult: graphHelper.generateSuccess('User created successfully')
  62. }
  63. } catch (err) {
  64. return graphHelper.generateError(err)
  65. }
  66. },
  67. async verify (obj, args) {
  68. try {
  69. await WIKI.models.users.query().patch({ isVerified: true }).findById(args.id)
  70. return {
  71. responseResult: graphHelper.generateSuccess('User verified successfully')
  72. }
  73. } catch (err) {
  74. return graphHelper.generateError(err)
  75. }
  76. },
  77. async activate (obj, args) {
  78. try {
  79. await WIKI.models.users.query().patch({ isActive: true }).findById(args.id)
  80. return {
  81. responseResult: graphHelper.generateSuccess('User activated successfully')
  82. }
  83. } catch (err) {
  84. return graphHelper.generateError(err)
  85. }
  86. },
  87. async deactivate (obj, args) {
  88. try {
  89. if (args.id <= 2) {
  90. throw new Error('Cannot deactivate system accounts.')
  91. }
  92. await WIKI.models.users.query().patch({ isActive: false }).findById(args.id)
  93. return {
  94. responseResult: graphHelper.generateSuccess('User deactivated successfully')
  95. }
  96. } catch (err) {
  97. return graphHelper.generateError(err)
  98. }
  99. },
  100. resetPassword (obj, args) {
  101. return false
  102. }
  103. },
  104. User: {
  105. groups(usr) {
  106. return usr.$relatedQuery('groups')
  107. }
  108. }
  109. }