Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

list-profiles.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. tags: Joi.array().items(),
  35. responses: Joi.array().items(
  36. Joi.object({
  37. response_key_id: Joi.number().required(),
  38. profile_id: Joi.number().required(),
  39. response_id: Joi.number().required(),
  40. val: Joi.string().required(),
  41. }),
  42. ),
  43. user_type: Joi.string().required(),
  44. }),
  45. error: errorSchema.single
  46. }
  47. module.exports = {
  48. method: 'GET',
  49. path: '/{user_id}/profiles',
  50. options: {
  51. ...pluginConfig.docs,
  52. tags: ['api'],
  53. /** Protect this route with authentication? */
  54. auth: false,
  55. cors: true,
  56. handler: async function (request, h) {
  57. const { userService, profileService } = request.server.services()
  58. const userId = request.params.user_id
  59. const user = await userService.findById(userId)
  60. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  61. const profiles = await profileService.getCompleteProfilesFor(
  62. userId,
  63. type,
  64. )
  65. try {
  66. return {
  67. ok: true,
  68. handler: pluginConfig.handlerType,
  69. data: profiles,
  70. }
  71. } catch (err) {
  72. return {
  73. ok: false,
  74. handler: pluginConfig.handlerType,
  75. data: { error: `${err}` },
  76. }
  77. }
  78. },
  79. /** Validate based on validators object */
  80. validate: {
  81. ...validators,
  82. failAction: 'log',
  83. },
  84. /** Validate the server response */
  85. response: {
  86. status: {
  87. 200: Joi.object({
  88. ok: Joi.bool(),
  89. handler: Joi.string(),
  90. data: Joi.array().items(responseSchemas.profilesList),
  91. }),
  92. 500: Joi.object({
  93. ok: Joi.bool(),
  94. handler: Joi.string(),
  95. data: responseSchemas.error,
  96. }),
  97. },
  98. },
  99. },
  100. }