選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

create-profile.js 3.4KB

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. 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. const profile =
  62. await profileService.saveResponsesCreateProfileFor(
  63. userId,
  64. res,
  65. )
  66. return h
  67. .response({
  68. ok: true,
  69. handler: pluginConfig.handlerType,
  70. data: profile,
  71. })
  72. .code(201)
  73. } catch (err) {
  74. return h
  75. .response({
  76. ok: false,
  77. handler: pluginConfig.handlerType,
  78. data: { error: `${err}` },
  79. })
  80. .code(409)
  81. }
  82. },
  83. /** Validate based on validators object */
  84. validate: {
  85. ...validators,
  86. failAction: 'log',
  87. },
  88. /** Validate the server response */
  89. response: {
  90. status: {
  91. 201: Joi.object({
  92. ok: Joi.bool(),
  93. handler: Joi.string(),
  94. data: responseSchemas.response,
  95. }).label('created_profile_res'),
  96. 409: Joi.object({
  97. ok: Joi.bool(),
  98. handler: Joi.string(),
  99. data: responseSchemas.error,
  100. }).label('error_single_res'),
  101. },
  102. },
  103. },
  104. }