You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

create-profile.js 3.3KB

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