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.

profiles.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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({ user_id: Joi.number() }),
  15. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  16. // query: true,
  17. /** Validate the incoming payload (POST method) */
  18. // payload: true,
  19. }
  20. const responseSchemas = {
  21. profilesList: Joi.object({
  22. profile_id: Joi.number().required(),
  23. user_id: Joi.number().required(),
  24. responses: Joi.array().items(
  25. Joi.object({
  26. response_id: Joi.number().required(),
  27. profile_id: Joi.number().required(),
  28. response_key_id: Joi.number().required(),
  29. val: Joi.string().required(),
  30. }),
  31. ),
  32. response_keys: Joi.array(),
  33. }),
  34. }
  35. module.exports = {
  36. method: 'GET',
  37. path: '/{user_id}',
  38. options: {
  39. ...pluginConfig.docs,
  40. tags: ['api'],
  41. /** Protect this route with authentication? */
  42. auth: false,
  43. handler: async function (request, h) {
  44. const { profileService } = request.services()
  45. const userId = request.params.user_id
  46. const profiles = await profileService.getCompleteProfilesFor(userId)
  47. try {
  48. return {
  49. ok: true,
  50. handler: pluginConfig.handlerType,
  51. data: profiles,
  52. }
  53. } catch (err) {
  54. return {
  55. ok: false,
  56. handler: pluginConfig.handlerType,
  57. data: { error: `${err}` },
  58. }
  59. }
  60. },
  61. /** Validate based on validators object */
  62. validate: {
  63. ...validators,
  64. failAction: 'log',
  65. },
  66. /** Validate the server response */
  67. response: {
  68. schema: Joi.object({
  69. ok: Joi.bool(),
  70. handler: Joi.string(),
  71. data: Joi.array().items(responseSchemas.profilesList),
  72. }),
  73. },
  74. },
  75. }