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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 pluginConfig = {
  7. handlerType: 'grouping',
  8. docs: {
  9. description: 'active memberships',
  10. notes: 'A list of groupings with active membership',
  11. },
  12. }
  13. const validators = {
  14. /** Validate the header (cookie check) */
  15. // headers: true,
  16. /** Validate the route params (/active/{thing}) */
  17. params: Joi.object({ profile_id: Joi.number() }),
  18. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  19. query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
  20. /** Validate the incoming payload (POST method) */
  21. // payload: true,
  22. }
  23. const responseSchemas = {
  24. single: groupingSchema.single,
  25. list: groupingSchema.listWithProfiles,
  26. error: errorSchema.single
  27. }
  28. module.exports = {
  29. method: 'GET',
  30. path: '/{profile_id}',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { membershipService, profileService } = request.server.services()
  39. const membershipType = request.query.type
  40. const profileId = request.params.profile_id
  41. let groupings = await membershipService.findGroupingsByProfileId(
  42. profileId,
  43. membershipType
  44. )
  45. /**
  46. * Heavily process the result by storing just a profile_id
  47. * and attach complete profiles
  48. */
  49. let pIds = groupings.reduce((ids, grouping) => {
  50. grouping.profiles.forEach(p => {
  51. if(p.profile_id == profileId) return
  52. ids.push(p.profile_id)
  53. grouping.profile = p.profile_id
  54. })
  55. delete grouping.profiles
  56. return ids
  57. }, [])
  58. /** Assemble complete profiles to reference and pass */
  59. const completedProfiles = await profileService.getProfilesFor(pIds, 'participant', false)
  60. const reformattedGroupings = groupings.map(g => {
  61. completedProfiles.forEach(p => {
  62. g.profile = g.profile == p.profile_id ? p : g.profile
  63. })
  64. return g
  65. })
  66. try {
  67. return {
  68. ok: true,
  69. handler: pluginConfig.handlerType,
  70. data: reformattedGroupings
  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. schema: apiSchema.single.append({
  88. data: responseSchemas.list
  89. })
  90. },
  91. },
  92. }