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.5KB

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