Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

create-profile.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'user',
  5. docs: {
  6. description: 'Create profile for user',
  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. // HELP: not sure if this is right, but attempting to fix
  30. // ValidationError data[0].user_name is not allowed
  31. // the logic being that CompleteProfile has had user_name added
  32. // and getCompleteProfiles utilizes CompleteProfile
  33. // and this route utilizes getCompleteProfiles
  34. user_name: Joi.string(),
  35. }),
  36. error: Joi.object({
  37. error: Joi.string(),
  38. }),
  39. }
  40. module.exports = {
  41. method: 'POST',
  42. path: '/{user_id}/profile',
  43. options: {
  44. ...pluginConfig.docs,
  45. tags: ['api'],
  46. /** Protect this route with authentication? */
  47. auth: false,
  48. handler: async function (request, h) {
  49. const { userService, profileService } = request.server.services()
  50. const userId = request.params.user_id
  51. const user = await userService.findById(userId)
  52. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  53. const profiles = await profileService.getCompleteProfilesFor(
  54. userId,
  55. type,
  56. )
  57. try {
  58. if (type === 'seeker' && profiles.length > 0) {
  59. throw new RangeError(
  60. 'Job seekers may only have ONE profile',
  61. )
  62. }
  63. /** Grab payload info */
  64. const res = request.payload
  65. const profile =
  66. await profileService.saveResponsesCreateProfileFor(
  67. userId,
  68. res,
  69. )
  70. return h
  71. .response({
  72. ok: true,
  73. handler: pluginConfig.handlerType,
  74. data: profile,
  75. })
  76. .code(201)
  77. } catch (err) {
  78. return h
  79. .response({
  80. ok: false,
  81. handler: pluginConfig.handlerType,
  82. data: { error: `${err}` },
  83. })
  84. .code(409)
  85. }
  86. },
  87. /** Validate based on validators object */
  88. validate: {
  89. ...validators,
  90. failAction: 'log',
  91. },
  92. /** Validate the server response */
  93. response: {
  94. status: {
  95. 201: Joi.object({
  96. ok: Joi.bool(),
  97. handler: Joi.string(),
  98. data: responseSchemas.response,
  99. }),
  100. 409: Joi.object({
  101. ok: Joi.bool(),
  102. handler: Joi.string(),
  103. data: responseSchemas.error,
  104. }),
  105. },
  106. },
  107. },
  108. }