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

profiles.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'profile',
  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_type: Joi.string(),
  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. responses: Joi.array().items(
  28. Joi.object({
  29. response_key_id: Joi.number().required(),
  30. profile_id: Joi.number().required(),
  31. response_id: Joi.number().required(),
  32. val: Joi.string().required(),
  33. }),
  34. ),
  35. user_type: Joi.string().required(),
  36. }),
  37. }
  38. module.exports = {
  39. method: 'GET',
  40. path: '/{user_type}/{user_id}',
  41. options: {
  42. ...pluginConfig.docs,
  43. tags: ['api'],
  44. /** Protect this route with authentication? */
  45. auth: false,
  46. handler: async function (request, h) {
  47. const { profileService } = request.services()
  48. const userId = request.params.user_id
  49. const type = request.params.user_type
  50. const profiles = await profileService.getCompleteProfilesFor(
  51. userId,
  52. type,
  53. )
  54. try {
  55. return {
  56. ok: true,
  57. handler: pluginConfig.handlerType,
  58. data: profiles,
  59. }
  60. } catch (err) {
  61. return {
  62. ok: false,
  63. handler: pluginConfig.handlerType,
  64. data: { error: `${err}` },
  65. }
  66. }
  67. },
  68. /** Validate based on validators object */
  69. validate: {
  70. ...validators,
  71. failAction: 'log',
  72. },
  73. /** Validate the server response */
  74. response: {
  75. schema: Joi.object({
  76. ok: Joi.bool(),
  77. handler: Joi.string(),
  78. data: Joi.array().items(responseSchemas.profilesList),
  79. }),
  80. },
  81. },
  82. }