Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

list-profiles.js 2.5KB

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