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

create.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'profile',
  5. docs: {
  6. description: 'Create profile',
  7. notes: 'Create a profile associated with this user',
  8. },
  9. }
  10. const validators = {
  11. /** Validate the header (cookie check) */
  12. // headers: true,
  13. /** Validate the route params (/active/{thing}) */
  14. params: Joi.object({ user_id: Joi.number() }),
  15. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  16. // query: true,
  17. /** Validate the incoming payload (POST method) */
  18. payload: Joi.array().items(
  19. Joi.object({
  20. response_key_id: Joi.number().required(),
  21. val: Joi.string().required(),
  22. }),
  23. ),
  24. }
  25. const responseSchemas = {
  26. response: Joi.object({
  27. profile_id: Joi.number(),
  28. user_id: Joi.number(),
  29. }),
  30. }
  31. module.exports = {
  32. method: 'POST',
  33. path: '/{user_id}/create',
  34. options: {
  35. ...pluginConfig.docs,
  36. tags: ['api'],
  37. /** Protect this route with authentication? */
  38. auth: false,
  39. handler: async function (request) {
  40. const { profileService } = request.services()
  41. const userId = request.params.user_id
  42. /** Grab payload info */
  43. const res = request.payload
  44. const profile = await profileService.saveResponsesCreateProfileFor(
  45. userId,
  46. res,
  47. )
  48. try {
  49. return {
  50. ok: true,
  51. handler: pluginConfig.handlerType,
  52. data: profile,
  53. }
  54. } catch (err) {
  55. return {
  56. ok: false,
  57. handler: pluginConfig.handlerType,
  58. data: { error: `${err}` },
  59. }
  60. }
  61. },
  62. /** Validate based on validators object */
  63. validate: {
  64. ...validators,
  65. failAction: 'log',
  66. },
  67. /** Validate the server response */
  68. response: {
  69. schema: Joi.object({
  70. ok: Joi.bool(),
  71. handler: Joi.string(),
  72. data: responseSchemas.response,
  73. }),
  74. },
  75. },
  76. }