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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = await profileService.insertSingleResponseForProfile(res)
  44. if (!insertedResponse) {
  45. throw new Error('Response not inserted')
  46. }
  47. return h
  48. .response({
  49. ok: true,
  50. handler: pluginConfig.handlerType,
  51. data: insertedResponse,
  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. }