您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

list-profiles.js 2.9KB

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. cors: true,
  49. handler: async function (request, h) {
  50. const { userService, profileService } = request.server.services()
  51. const userId = request.params.user_id
  52. const user = await userService.findById(userId)
  53. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  54. const profiles = await profileService.getCompleteProfilesFor(
  55. userId,
  56. type,
  57. )
  58. try {
  59. return {
  60. ok: true,
  61. handler: pluginConfig.handlerType,
  62. data: profiles,
  63. }
  64. } catch (err) {
  65. return {
  66. ok: false,
  67. handler: pluginConfig.handlerType,
  68. data: { error: `${err}` },
  69. }
  70. }
  71. },
  72. /** Validate based on validators object */
  73. validate: {
  74. ...validators,
  75. failAction: 'log',
  76. },
  77. /** Validate the server response */
  78. response: {
  79. status: {
  80. 200: Joi.object({
  81. ok: Joi.bool(),
  82. handler: Joi.string(),
  83. data: Joi.array().items(responseSchemas.profilesList),
  84. }),
  85. 500: Joi.object({
  86. ok: Joi.bool(),
  87. handler: Joi.string(),
  88. data: responseSchemas.error,
  89. }),
  90. },
  91. },
  92. },
  93. }