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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 profile',
  11. notes: 'Insert new profile responses',
  12. },
  13. }
  14. const responseSchemas = {
  15. response: surveyResponseSchema.list,
  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_id?}',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. handler: async function (request, h) {
  37. const { profileService } = request.services()
  38. const profileId = request.params.profile_id
  39. /** Grab payload info */
  40. const res = request.payload
  41. console.log('res :=>', res)
  42. try {
  43. const allResponses = profileService.saveResponseForProfile(profileId, res)
  44. if (!allResponses) {
  45. throw new RangeError('Response not inserted')
  46. }
  47. return h
  48. .response({
  49. ok: true,
  50. handler: pluginConfig.handlerType,
  51. data: allResponses,
  52. })
  53. .code(200)
  54. } catch (err) {
  55. return h
  56. .response({
  57. ok: false,
  58. handler: pluginConfig.handlerType,
  59. data: { error: `${err}` },
  60. })
  61. .code(409)
  62. }
  63. },
  64. /** Validate based on validators object */
  65. validate: {
  66. ...validators,
  67. failAction: 'log',
  68. },
  69. /** Validate the server response */
  70. response: {
  71. status: {
  72. 200: apiSchema.single
  73. .append({
  74. data: responseSchemas.response,
  75. })
  76. .label('response_list_res'),
  77. 409: apiSchema.single
  78. .append({
  79. data: responseSchemas.error,
  80. })
  81. .label('error_single_res'),
  82. },
  83. },
  84. },
  85. }