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.

insert.js 2.8KB

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 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. // TODO: Currently passwords are stored in plain text, big no no...
  43. const insertedResponse =
  44. await profileService.insertSingleResponseForProfile(res)
  45. if (!insertedResponse) {
  46. throw new Error('Response not inserted')
  47. }
  48. return h
  49. .response({
  50. ok: true,
  51. handler: pluginConfig.handlerType,
  52. data: insertedResponse,
  53. })
  54. .code(200)
  55. } catch (err) {
  56. return h
  57. .response({
  58. ok: false,
  59. handler: pluginConfig.handlerType,
  60. data: { error: `${err}` },
  61. })
  62. .code(409)
  63. }
  64. },
  65. /** Validate based on validators object */
  66. validate: {
  67. ...validators,
  68. failAction: 'log',
  69. },
  70. /** Validate the server response */
  71. response: {
  72. status: {
  73. 200: apiSchema.single
  74. .append({
  75. data: responseSchemas.response,
  76. })
  77. .label('response_list_res'),
  78. 409: apiSchema.single
  79. .append({
  80. data: responseSchemas.error,
  81. })
  82. .label('error_single_res'),
  83. },
  84. },
  85. },
  86. }