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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. opts: {
  14. tags: ['api'],
  15. auth: { strategy: 'default_jwt' },
  16. cors: true,
  17. },
  18. }
  19. const validators = {
  20. /** Validate the header (cookie check) */
  21. // headers: true,
  22. /** Validate the route params (/active/{thing}) */
  23. params: params.profileId,
  24. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  25. query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
  26. /** Validate the incoming payload (POST method) */
  27. // payload: true,
  28. }
  29. const responseSchemas = {
  30. single: groupingSchema.single,
  31. list: groupingSchema.listWithProfiles,
  32. error: errorSchema.single,
  33. }
  34. const _activeGroupingIds = allMemberships => {
  35. const active = {}
  36. allMemberships.forEach(membership => {
  37. if (!membership.is_active) return
  38. if (!active[membership.grouping_id]) {
  39. active[membership.grouping_id] = []
  40. }
  41. active[membership.grouping_id].push(membership)
  42. })
  43. const ids = []
  44. Object.values(active).forEach(profileListInGrouping => {
  45. if (profileListInGrouping.length == 2) {
  46. ids.push(profileListInGrouping[0].grouping_id)
  47. }
  48. })
  49. return ids
  50. }
  51. module.exports = {
  52. method: 'GET',
  53. path: '/{profile_id}',
  54. options: {
  55. ...pluginConfig.docs,
  56. ...pluginConfig.opts,
  57. handler: async function (request, h) {
  58. const { membershipService, profileService } =
  59. request.server.services()
  60. const membershipType = request.query.type
  61. const profileId = request.params.profile_id
  62. let groupings = await membershipService.findGroupingsByProfileId(
  63. profileId,
  64. membershipType,
  65. )
  66. let memberships = await membershipService.findMemberships(
  67. groupings.map(grouping => grouping.grouping_id),
  68. )
  69. /**
  70. * Heavily process the result by storing just a profile_id
  71. * and attach complete profiles
  72. */
  73. let pIds = groupings.reduce((ids, grouping) => {
  74. grouping.profiles.forEach(p => {
  75. if (p.profile_id == profileId) return
  76. ids.push(p.profile_id)
  77. grouping.profile = p.profile_id
  78. })
  79. delete grouping.profiles
  80. return ids
  81. }, [])
  82. /** Assemble complete profiles to reference and pass */
  83. const completedProfiles = await profileService.getProfilesFor(
  84. pIds,
  85. 'participant',
  86. false,
  87. )
  88. const reformattedGroupings = groupings.map(g => {
  89. completedProfiles.forEach(p => {
  90. g.profile = g.profile == p.profile_id ? p : g.profile
  91. })
  92. g.is_paired = _activeGroupingIds(memberships).includes(
  93. g.grouping_id,
  94. )
  95. return g
  96. })
  97. try {
  98. return {
  99. ok: true,
  100. handler: pluginConfig.handlerType,
  101. data: reformattedGroupings,
  102. }
  103. } catch (err) {
  104. return {
  105. ok: false,
  106. handler: pluginConfig.handlerType,
  107. data: { error: `${err}` },
  108. }
  109. }
  110. },
  111. /** Validate based on validators object */
  112. validate: {
  113. ...validators,
  114. failAction: 'log',
  115. },
  116. /** Validate the server response */
  117. response: {
  118. schema: apiSchema.single
  119. .append({
  120. data: responseSchemas.list,
  121. })
  122. .label('grouping_list_res'),
  123. },
  124. },
  125. }