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ů.

insert.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. module.exports = {
  19. method: 'POST',
  20. path: '/{profile_id}/insert/{response_key_id?}',
  21. options: {
  22. ...pluginConfig.docs,
  23. tags: ['api'],
  24. /** Protect this route with authentication? */
  25. auth: false,
  26. cors: true,
  27. handler: async function (request, h) {
  28. const { profileService } = request.services()
  29. /** Grab payload info */
  30. const res = request.payload
  31. try {
  32. // TODO: Currently passwords are stored in plain text, big no no...
  33. const insertedResponse =
  34. await profileService.insertSingleResponseForProfile(res)
  35. if (!insertedResponse) {
  36. throw new Error('Response not inserted')
  37. }
  38. return h
  39. .response({
  40. ok: true,
  41. handler: pluginConfig.handlerType,
  42. data: insertedResponse,
  43. })
  44. .code(200)
  45. } catch (err) {
  46. return h
  47. .response({
  48. ok: false,
  49. handler: pluginConfig.handlerType,
  50. data: { error: `${err}` },
  51. })
  52. .code(409)
  53. }
  54. },
  55. /** Validate based on validators object */
  56. validate: {
  57. failAction: 'log',
  58. },
  59. /** Validate the server response */
  60. response: {
  61. status: {
  62. 200: apiSchema.single
  63. .append({
  64. data: responseSchemas.response,
  65. })
  66. .label('response_list_res'),
  67. 409: apiSchema.single
  68. .append({
  69. data: responseSchemas.error,
  70. })
  71. .label('error_single_res'),
  72. },
  73. },
  74. },
  75. }