Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

update.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'profile',
  5. docs: {
  6. description: 'Update profile',
  7. notes: 'Update profile responses',
  8. },
  9. }
  10. const responseSchemas = {
  11. responses: Joi.array().items(
  12. Joi.object({
  13. response_id: Joi.number().required(),
  14. profile_id: Joi.number().required(),
  15. response_key_id: Joi.number().required(),
  16. val: Joi.string().required(),
  17. }),
  18. ),
  19. }
  20. const validators = {
  21. /** Validate the header (cookie check) */
  22. // headers: true,
  23. /** Validate the route params (/active/{thing}) */
  24. params: Joi.object({ profile_id: Joi.number() }),
  25. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  26. // query: true,
  27. /** Validate the incoming payload (POST method) */
  28. payload: responseSchemas.responses,
  29. }
  30. module.exports = {
  31. method: 'PATCH',
  32. path: '/{profile_id}/update',
  33. options: {
  34. ...pluginConfig.docs,
  35. tags: ['api'],
  36. /** Protect this route with authentication? */
  37. auth: false,
  38. handler: async function (request) {
  39. const { profileService } = request.services()
  40. const profileId = request.params.profile_id
  41. /** Grab payload info */
  42. const res = request.payload
  43. const updatedResponses =
  44. await profileService.updateResponsesInProfile(profileId, res)
  45. try {
  46. return {
  47. ok: true,
  48. handler: pluginConfig.handlerType,
  49. data: updatedResponses,
  50. }
  51. } catch (err) {
  52. return {
  53. ok: false,
  54. handler: pluginConfig.handlerType,
  55. data: { error: `${err}` },
  56. }
  57. }
  58. },
  59. /** Validate based on validators object */
  60. validate: {
  61. ...validators,
  62. failAction: 'log',
  63. },
  64. /** Validate the server response */
  65. response: {
  66. schema: Joi.object({
  67. ok: Joi.bool(),
  68. handler: Joi.string(),
  69. data: responseSchemas.responses,
  70. }),
  71. },
  72. },
  73. }