Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

create-profile.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. const Joi = require('joi')
  3. const errorSchema = require('../../schemas/errors')
  4. const pluginConfig = {
  5. handlerType: 'user',
  6. docs: {
  7. description: 'Create profile for user',
  8. notes: 'Create a profile associated with this user',
  9. },
  10. }
  11. const validators = {
  12. /** Validate the header (cookie check) */
  13. // headers: true,
  14. /** Validate the route params (/active/{thing}) */
  15. params: Joi.object({ user_id: Joi.number() }),
  16. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  17. // query: true,
  18. /** Validate the incoming payload (POST method) */
  19. payload: Joi.array().items(
  20. Joi.object({
  21. response_key_id: Joi.number().required(),
  22. val: Joi.string().required(),
  23. }),
  24. ),
  25. }
  26. const responseSchemas = {
  27. response: Joi.object({
  28. profile_id: Joi.number(),
  29. user_id: Joi.number(),
  30. // HELP: not sure if this is right, but attempting to fix
  31. // ValidationError data[0].user_name is not allowed
  32. // the logic being that CompleteProfile has had user_name added
  33. // and getCompleteProfiles utilizes CompleteProfile
  34. // and this route utilizes getCompleteProfiles
  35. user_name: Joi.string(),
  36. }),
  37. error: errorSchema.single
  38. }
  39. module.exports = {
  40. method: 'POST',
  41. path: '/{user_id}/profile',
  42. options: {
  43. ...pluginConfig.docs,
  44. tags: ['api'],
  45. /** Protect this route with authentication? */
  46. auth: false,
  47. handler: async function (request, h) {
  48. const { userService, profileService } = request.server.services()
  49. const userId = request.params.user_id
  50. const user = await userService.findById(userId)
  51. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  52. const profiles = await profileService.getCompleteProfilesFor(
  53. userId,
  54. type,
  55. )
  56. try {
  57. if (type === 'seeker' && profiles.length > 0) {
  58. throw new RangeError(
  59. 'Job seekers may only have ONE profile',
  60. )
  61. }
  62. /** Grab payload info */
  63. const res = request.payload
  64. const profile =
  65. await profileService.saveResponsesCreateProfileFor(
  66. userId,
  67. res,
  68. )
  69. return h
  70. .response({
  71. ok: true,
  72. handler: pluginConfig.handlerType,
  73. data: profile,
  74. })
  75. .code(201)
  76. } catch (err) {
  77. return h
  78. .response({
  79. ok: false,
  80. handler: pluginConfig.handlerType,
  81. data: { error: `${err}` },
  82. })
  83. .code(409)
  84. }
  85. },
  86. /** Validate based on validators object */
  87. validate: {
  88. ...validators,
  89. failAction: 'log',
  90. },
  91. /** Validate the server response */
  92. response: {
  93. status: {
  94. 201: Joi.object({
  95. ok: Joi.bool(),
  96. handler: Joi.string(),
  97. data: responseSchemas.response,
  98. }),
  99. 409: Joi.object({
  100. ok: Joi.bool(),
  101. handler: Joi.string(),
  102. data: responseSchemas.error,
  103. }),
  104. },
  105. },
  106. },
  107. }