hooks.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const graphHelper = require('../../helpers/graph')
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async hooks () {
  7. return WIKI.models.hooks.query().orderBy('name')
  8. },
  9. async hookById (obj, args) {
  10. return WIKI.models.hooks.query().findById(args.id)
  11. }
  12. },
  13. Mutation: {
  14. /**
  15. * CREATE HOOK
  16. */
  17. async createHook (obj, args) {
  18. try {
  19. // -> Validate inputs
  20. if (!args.name || args.name.length < 1) {
  21. throw new WIKI.Error.Custom('HookCreateInvalidName', 'Invalid Hook Name')
  22. }
  23. if (!args.events || args.events.length < 1) {
  24. throw new WIKI.Error.Custom('HookCreateInvalidEvents', 'Invalid Hook Events')
  25. }
  26. if (!args.url || args.url.length < 8 || !args.url.startsWith('http')) {
  27. throw new WIKI.Error.Custom('HookCreateInvalidURL', 'Invalid Hook URL')
  28. }
  29. // -> Create hook
  30. const newHook = await WIKI.models.hooks.createHook(args)
  31. WIKI.logger.debug(`New Hook ${newHook.id} created successfully.`)
  32. return {
  33. operation: graphHelper.generateSuccess('Hook created successfully'),
  34. hook: newHook
  35. }
  36. } catch (err) {
  37. return graphHelper.generateError(err)
  38. }
  39. },
  40. /**
  41. * UPDATE HOOK
  42. */
  43. async updateHook (obj, args) {
  44. try {
  45. // -> Load hook
  46. const hook = await WIKI.models.hooks.query().findById(args.id)
  47. if (!hook) {
  48. throw new WIKI.Error.Custom('HookInvalidId', 'Invalid Hook ID')
  49. }
  50. // -> Check for bad input
  51. if (_.has(args.patch, 'name') && args.patch.name.length < 1) {
  52. throw new WIKI.Error.Custom('HookCreateInvalidName', 'Invalid Hook Name')
  53. }
  54. if (_.has(args.patch, 'events') && args.patch.events.length < 1) {
  55. throw new WIKI.Error.Custom('HookCreateInvalidEvents', 'Invalid Hook Events')
  56. }
  57. if (_.has(args.patch, 'url') && (_.trim(args.patch.url).length < 8 || !args.patch.url.startsWith('http'))) {
  58. throw new WIKI.Error.Custom('HookInvalidURL', 'URL is invalid.')
  59. }
  60. // -> Update hook
  61. await WIKI.models.hooks.query().findById(args.id).patch(args.patch)
  62. WIKI.logger.debug(`Hook ${args.id} updated successfully.`)
  63. return {
  64. operation: graphHelper.generateSuccess('Hook updated successfully')
  65. }
  66. } catch (err) {
  67. return graphHelper.generateError(err)
  68. }
  69. },
  70. /**
  71. * DELETE HOOK
  72. */
  73. async deleteHook (obj, args) {
  74. try {
  75. await WIKI.models.hooks.deleteHook(args.id)
  76. WIKI.logger.debug(`Hook ${args.id} deleted successfully.`)
  77. return {
  78. operation: graphHelper.generateSuccess('Hook deleted successfully')
  79. }
  80. } catch (err) {
  81. return graphHelper.generateError(err)
  82. }
  83. }
  84. }
  85. }