Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

create-profile.js 3.2KB

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