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 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. cors: true,
  36. handler: async function (request, h) {
  37. const { userService, profileService } = request.server.services()
  38. const userId = request.params.user_id
  39. const user = await userService.findById(userId)
  40. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  41. const profiles = await profileService.getCompleteProfilesFor(
  42. userId,
  43. type,
  44. )
  45. try {
  46. return {
  47. ok: true,
  48. handler: pluginConfig.handlerType,
  49. data: profiles,
  50. }
  51. } catch (err) {
  52. return {
  53. ok: false,
  54. handler: pluginConfig.handlerType,
  55. data: { error: `${err}` },
  56. }
  57. }
  58. },
  59. /** Validate based on validators object */
  60. validate: {
  61. ...validators,
  62. failAction: 'log',
  63. },
  64. /** Validate the server response */
  65. response: {
  66. status: {
  67. 200: Joi.object({
  68. ok: Joi.bool(),
  69. handler: Joi.string(),
  70. data: responseSchemas.profilesList,
  71. }).label('list_profiles_res'),
  72. 500: Joi.object({
  73. ok: Joi.bool(),
  74. handler: Joi.string(),
  75. data: responseSchemas.error,
  76. }).label('error_single_res'),
  77. },
  78. },
  79. },
  80. }