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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const surveyResponseSchema = require('../../schemas/responses')
  6. const params = require('../../schemas/params')
  7. const pluginConfig = {
  8. handlerType: 'profile',
  9. docs: {
  10. description: 'Insert responses',
  11. notes: 'Insert new responses',
  12. },
  13. }
  14. const responseSchemas = {
  15. response: surveyResponseSchema.single,
  16. error: errorSchema.single,
  17. }
  18. const validators = {
  19. /** Validate the header (cookie check) */
  20. // headers: true,
  21. /** Validate the route params (/active/{thing}) */
  22. params: params.profileId,
  23. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  24. // query: true,
  25. /** Validate the incoming payload (POST method) */
  26. payload: responseSchemas.responses,
  27. }
  28. module.exports = {
  29. method: 'POST',
  30. path: '/{profile_id}/insert/{response_key_id?}',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { profileService } = request.services()
  39. /** Grab payload info */
  40. const res = request.payload
  41. try {
  42. const insertedResponse = await profileService.insertSingleResponseForProfile(res)
  43. if (!insertedResponse) {
  44. throw new Error('Response not inserted')
  45. }
  46. return h
  47. .response({
  48. ok: true,
  49. handler: pluginConfig.handlerType,
  50. data: insertedResponse,
  51. })
  52. .code(200)
  53. } catch (err) {
  54. return h
  55. .response({
  56. ok: false,
  57. handler: pluginConfig.handlerType,
  58. data: { error: `${err}` },
  59. })
  60. .code(409)
  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. 200: apiSchema.single
  72. .append({
  73. data: responseSchemas.response,
  74. })
  75. .label('response_list_res'),
  76. 409: apiSchema.single
  77. .append({
  78. data: responseSchemas.error,
  79. })
  80. .label('error_single_res'),
  81. },
  82. },
  83. },
  84. }