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

create-profile.js 3.4KB

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