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.

list-profiles.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict'
  2. const Joi = require('joi')
  3. const errorSchema = require('../../schemas/errors')
  4. const profileSchema = require('../../schemas/profiles')
  5. const params = require('../../schemas/params')
  6. const pluginConfig = {
  7. handlerType: 'user',
  8. docs: {
  9. description: 'profiles',
  10. notes: 'A list of profiles associated with this user',
  11. },
  12. }
  13. const validators = {
  14. /** Validate the header (cookie check) */
  15. // headers: true,
  16. /** Validate the route params (/active/{thing}) */
  17. params: params.userId,
  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: profileSchema.list,
  25. error: errorSchema.single,
  26. }
  27. module.exports = {
  28. method: 'GET',
  29. path: '/{user_id}/profiles',
  30. options: {
  31. ...pluginConfig.docs,
  32. tags: ['api'],
  33. /** Protect this route with authentication? */
  34. // auth: false,
  35. auth: 'default_jwt',
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { userService, profileService } = request.server.services()
  39. const userId = request.params.user_id
  40. const user = await userService.findById(userId)
  41. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  42. const profiles = await profileService.getCompleteProfilesFor(
  43. userId,
  44. type,
  45. )
  46. try {
  47. return {
  48. ok: true,
  49. handler: pluginConfig.handlerType,
  50. data: profiles,
  51. }
  52. } catch (err) {
  53. return {
  54. ok: false,
  55. handler: pluginConfig.handlerType,
  56. data: { error: `${err}` },
  57. }
  58. }
  59. },
  60. /** Validate based on validators object */
  61. validate: {
  62. ...validators,
  63. failAction: 'log',
  64. },
  65. /** Validate the server response */
  66. response: {
  67. status: {
  68. 200: Joi.object({
  69. ok: Joi.bool(),
  70. handler: Joi.string(),
  71. data: responseSchemas.profilesList,
  72. }).label('list_profiles_res'),
  73. 500: Joi.object({
  74. ok: Joi.bool(),
  75. handler: Joi.string(),
  76. data: responseSchemas.error,
  77. }).label('error_single_res'),
  78. },
  79. },
  80. },
  81. }