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-profile.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  12. const validators = {
  13. /** Validate the header (cookie check) */
  14. // headers: true,
  15. /** Validate the route params (/active/{thing}) */
  16. params: params.userId,
  17. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  18. // query: true,
  19. /** Validate the incoming payload (POST method) */
  20. payload: Joi.array().items(
  21. Joi.object({
  22. response_key_id: Joi.number().required(),
  23. val: Joi.string().required(),
  24. }),
  25. ),
  26. }
  27. const responseSchemas = {
  28. response: Joi.object({
  29. profile_id: Joi.number(),
  30. user_id: Joi.number(),
  31. user_name: Joi.string(),
  32. }).label('created_profile'),
  33. error: errorSchema.single,
  34. }
  35. module.exports = {
  36. method: 'POST',
  37. path: '/{user_id}/profile',
  38. options: {
  39. ...pluginConfig.docs,
  40. tags: ['api'],
  41. /** Protect this route with authentication? */
  42. auth: false,
  43. cors: true,
  44. handler: async function (request, h) {
  45. const { userService, profileService } = request.server.services()
  46. const userId = request.params.user_id
  47. const user = await userService.findById(userId)
  48. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  49. const profiles = await profileService.getCompleteProfilesFor(
  50. userId,
  51. type,
  52. )
  53. try {
  54. if (type === 'seeker' && profiles.length > 0) {
  55. throw new RangeError(
  56. 'Job seekers may only have ONE profile',
  57. )
  58. }
  59. /** Grab payload info */
  60. const res = request.payload
  61. /** Don't log password in response table */
  62. const resWithoutPass = res.filter(r => {
  63. return r.response_key_id !== 9
  64. })
  65. const profile =
  66. await profileService.saveResponsesCreateProfileFor(
  67. userId,
  68. resWithoutPass,
  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. }).label('created_profile_res'),
  100. 409: Joi.object({
  101. ok: Joi.bool(),
  102. handler: Joi.string(),
  103. data: responseSchemas.error,
  104. }).label('error_single_res'),
  105. },
  106. },
  107. },
  108. }