選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

list-profiles.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // HELP: not sure if this is right, but attempting to fix
  27. // ValidationError data[0].user_name is not allowed
  28. // the logic being that CompleteProfile has had user_name added
  29. // and getCompleteProfiles utilizes CompleteProfile
  30. // and this route utilizes getCompleteProfiles
  31. user_name: Joi.string(),
  32. user_media: Joi.string(),
  33. responses: Joi.array().items(
  34. Joi.object({
  35. response_key_id: Joi.number().required(),
  36. profile_id: Joi.number().required(),
  37. response_id: Joi.number().required(),
  38. val: Joi.string().required(),
  39. }),
  40. ),
  41. user_type: Joi.string().required(),
  42. }),
  43. error: Joi.object({
  44. error: Joi.string(),
  45. }),
  46. }
  47. module.exports = {
  48. method: 'GET',
  49. path: '/{user_id}/profiles',
  50. options: {
  51. ...pluginConfig.docs,
  52. tags: ['api'],
  53. /** Protect this route with authentication? */
  54. auth: false,
  55. cors: true,
  56. handler: async function (request, h) {
  57. const { userService, profileService } = request.server.services()
  58. const userId = request.params.user_id
  59. const user = await userService.findById(userId)
  60. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  61. const profiles = await profileService.getCompleteProfilesFor(
  62. userId,
  63. type,
  64. )
  65. console.log('get profiles - const profiles / in list-profiles.js', profiles)
  66. try {
  67. return {
  68. ok: true,
  69. handler: pluginConfig.handlerType,
  70. data: profiles,
  71. }
  72. } catch (err) {
  73. return {
  74. ok: false,
  75. handler: pluginConfig.handlerType,
  76. data: { error: `${err}` },
  77. }
  78. }
  79. },
  80. /** Validate based on validators object */
  81. validate: {
  82. ...validators,
  83. failAction: 'log',
  84. },
  85. /** Validate the server response */
  86. response: {
  87. status: {
  88. 200: Joi.object({
  89. ok: Joi.bool(),
  90. handler: Joi.string(),
  91. data: Joi.array().items(responseSchemas.profilesList),
  92. }),
  93. 500: Joi.object({
  94. ok: Joi.bool(),
  95. handler: Joi.string(),
  96. data: responseSchemas.error,
  97. }),
  98. },
  99. },
  100. },
  101. }