Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

list-profiles.js 2.8KB

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