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

list-profiles.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict'
  2. const Joi = require('joi')
  3. const errorSchema = require('../../schemas/errors')
  4. const pluginConfig = {
  5. handlerType: 'user',
  6. docs: {
  7. description: 'profiles',
  8. notes: 'A list of profiles associated with this user',
  9. },
  10. }
  11. const validators = {
  12. /** Validate the header (cookie check) */
  13. // headers: true,
  14. /** Validate the route params (/active/{thing}) */
  15. params: Joi.object({
  16. user_id: Joi.number(),
  17. }),
  18. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  19. // query: true,
  20. /** Validate the incoming payload (POST method) */
  21. // payload: true,
  22. }
  23. const responseSchemas = {
  24. profilesList: Joi.object({
  25. profile_id: Joi.number().integer().greater(0).required(),
  26. user_id: Joi.number().integer().greater(0).required(),
  27. // HELP: not sure if this is right, but attempting to fix
  28. // ValidationError data[0].user_name is not allowed
  29. // the logic being that CompleteProfile has had user_name added
  30. // and getCompleteProfiles utilizes CompleteProfile
  31. // and this route utilizes getCompleteProfiles
  32. user_name: Joi.string(),
  33. user_media: Joi.string(),
  34. responses: Joi.array().items(
  35. Joi.object({
  36. response_key_id: Joi.number().required(),
  37. profile_id: Joi.number().required(),
  38. response_id: Joi.number().required(),
  39. val: Joi.string().required(),
  40. }),
  41. ),
  42. user_type: Joi.string().required(),
  43. }),
  44. error: errorSchema.single
  45. }
  46. module.exports = {
  47. method: 'GET',
  48. path: '/{user_id}/profiles',
  49. options: {
  50. ...pluginConfig.docs,
  51. tags: ['api'],
  52. /** Protect this route with authentication? */
  53. auth: false,
  54. cors: true,
  55. handler: async function (request, h) {
  56. const { userService, profileService } = request.server.services()
  57. const userId = request.params.user_id
  58. const user = await userService.findById(userId)
  59. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  60. const profiles = await profileService.getCompleteProfilesFor(
  61. userId,
  62. type,
  63. )
  64. try {
  65. return {
  66. ok: true,
  67. handler: pluginConfig.handlerType,
  68. data: profiles,
  69. }
  70. } catch (err) {
  71. return {
  72. ok: false,
  73. handler: pluginConfig.handlerType,
  74. data: { error: `${err}` },
  75. }
  76. }
  77. },
  78. /** Validate based on validators object */
  79. validate: {
  80. ...validators,
  81. failAction: 'log',
  82. },
  83. /** Validate the server response */
  84. response: {
  85. status: {
  86. 200: Joi.object({
  87. ok: Joi.bool(),
  88. handler: Joi.string(),
  89. data: Joi.array().items(responseSchemas.profilesList),
  90. }),
  91. 500: Joi.object({
  92. ok: Joi.bool(),
  93. handler: Joi.string(),
  94. data: responseSchemas.error,
  95. }),
  96. },
  97. },
  98. },
  99. }