Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. error: Joi.object({
  20. error: Joi.string(),
  21. }),
  22. }
  23. const validators = {
  24. /** Validate the header (cookie check) */
  25. // headers: true,
  26. /** Validate the route params (/active/{thing}) */
  27. params: Joi.object({ profile_id: Joi.number() }),
  28. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  29. // query: true,
  30. /** Validate the incoming payload (POST method) */
  31. payload: responseSchemas.responses,
  32. }
  33. module.exports = {
  34. method: 'PATCH',
  35. path: '/{profile_id}/update/{response_id?}',
  36. options: {
  37. ...pluginConfig.docs,
  38. tags: ['api'],
  39. /** Protect this route with authentication? */
  40. auth: false,
  41. handler: async function (request) {
  42. const { profileService } = request.services()
  43. const profileId = request.params.profile_id
  44. const responseId = request.params.response_id ? request.params.response_id : null
  45. /** Grab payload info */
  46. const res = request.payload
  47. const updatedResponses =
  48. await profileService.updateResponsesInProfile(profileId, res)
  49. try {
  50. return {
  51. ok: true,
  52. handler: pluginConfig.handlerType,
  53. data: updatedResponses,
  54. }
  55. } catch (err) {
  56. return {
  57. ok: false,
  58. handler: pluginConfig.handlerType,
  59. data: { error: `${err}` },
  60. }
  61. }
  62. },
  63. /** Validate based on validators object */
  64. validate: {
  65. ...validators,
  66. failAction: 'log',
  67. },
  68. /** Validate the server response */
  69. response: {
  70. status: {
  71. 201: Joi.object({
  72. ok: Joi.bool(),
  73. handler: Joi.string(),
  74. data: responseSchemas.responses,
  75. }),
  76. 500: Joi.object({
  77. ok: Joi.bool(),
  78. handler: Joi.string(),
  79. data: responseSchemas.error,
  80. }),
  81. },
  82. },
  83. },
  84. }