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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const groupingSchema = require('../../schemas/groupings')
  6. const params = require('../../schemas/params')
  7. const pluginConfig = {
  8. handlerType: 'grouping',
  9. docs: {
  10. description: 'active memberships',
  11. notes: 'A list of groupings with active membership',
  12. },
  13. }
  14. const validators = {
  15. /** Validate the header (cookie check) */
  16. // headers: true,
  17. /** Validate the route params (/active/{thing}) */
  18. params: params.profileId,
  19. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  20. query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
  21. /** Validate the incoming payload (POST method) */
  22. // payload: true,
  23. }
  24. const responseSchemas = {
  25. single: groupingSchema.single,
  26. list: groupingSchema.listWithProfiles,
  27. error: errorSchema.single,
  28. }
  29. module.exports = {
  30. method: 'GET',
  31. path: '/{profile_id}',
  32. options: {
  33. ...pluginConfig.docs,
  34. tags: ['api'],
  35. /** Protect this route with authentication? */
  36. auth: false,
  37. cors: true,
  38. handler: async function (request, h) {
  39. const { membershipService, profileService } =
  40. request.server.services()
  41. const membershipType = request.query.type
  42. const profileId = request.params.profile_id
  43. let groupings = await membershipService.findGroupingsByProfileId(
  44. profileId,
  45. membershipType,
  46. )
  47. // console.log('groupings :>> ', groupings)
  48. /**
  49. * Heavily process the result by storing just a profile_id
  50. * and attach complete profiles
  51. */
  52. let pIds = groupings.reduce((ids, grouping) => {
  53. grouping.profiles.forEach(p => {
  54. if (p.profile_id == profileId) return
  55. ids.push(p.profile_id)
  56. grouping.profile = p.profile_id
  57. })
  58. delete grouping.profiles
  59. return ids
  60. }, [])
  61. /** Assemble complete profiles to reference and pass */
  62. const completedProfiles = await profileService.getProfilesFor(
  63. pIds,
  64. 'participant',
  65. false,
  66. )
  67. const reformattedGroupings = groupings.map(g => {
  68. completedProfiles.forEach(p => {
  69. g.profile = g.profile == p.profile_id ? p : g.profile
  70. })
  71. return g
  72. })
  73. try {
  74. return {
  75. ok: true,
  76. handler: pluginConfig.handlerType,
  77. data: reformattedGroupings,
  78. }
  79. } catch (err) {
  80. return {
  81. ok: false,
  82. handler: pluginConfig.handlerType,
  83. data: { error: `${err}` },
  84. }
  85. }
  86. },
  87. /** Validate based on validators object */
  88. validate: {
  89. ...validators,
  90. failAction: 'log',
  91. },
  92. /** Validate the server response */
  93. response: {
  94. schema: apiSchema.single
  95. .append({
  96. data: responseSchemas.list,
  97. })
  98. .label('grouping_list_res'),
  99. },
  100. },
  101. }