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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. const _activeGroupingIds = allMemberships => {
  30. const active = {}
  31. allMemberships.forEach(membership => {
  32. if (!membership.is_active) return
  33. if (!active[membership.grouping_id]) {
  34. active[membership.grouping_id] = []
  35. }
  36. active[membership.grouping_id].push(membership)
  37. })
  38. const ids = []
  39. Object.values(active).forEach(profileListInGrouping => {
  40. if (profileListInGrouping.length == 2) {
  41. ids.push(profileListInGrouping[0].grouping_id)
  42. }
  43. })
  44. return ids
  45. }
  46. module.exports = {
  47. method: 'GET',
  48. path: '/{profile_id}',
  49. options: {
  50. ...pluginConfig.docs,
  51. tags: ['api'],
  52. /** Protect this route with authentication? */
  53. auth: false,
  54. cors: true,
  55. handler: async function (request, h) {
  56. const { membershipService, profileService, userService } =
  57. request.server.services()
  58. const membershipType = request.query.type
  59. const profileId = request.params.profile_id
  60. let groupings = await membershipService.findGroupingsByProfileId(
  61. profileId,
  62. membershipType,
  63. )
  64. let memberships = await membershipService.findMemberships(
  65. groupings.map(grouping => grouping.grouping_id),
  66. )
  67. /**
  68. * Heavily process the result by storing just a profile_id
  69. * and attach complete profiles
  70. */
  71. let pIds = groupings.reduce((ids, grouping) => {
  72. grouping.profiles.forEach(p => {
  73. if (p.profile_id == profileId) return
  74. ids.push(p.profile_id)
  75. grouping.profile = p.profile_id
  76. })
  77. delete grouping.profiles
  78. return ids
  79. }, [])
  80. /** Assemble complete profiles to reference and pass */
  81. const completedProfiles = await profileService.getProfilesFor(
  82. pIds,
  83. 'participant',
  84. false,
  85. )
  86. // NOTE: Doesn't rely on profile reveal field to get reveal tags
  87. const getGroupingRevealTags = async () => {
  88. const groupingRevealTags = []
  89. for (const grouping of groupings) {
  90. for (const profile of completedProfiles) {
  91. const revealTags = await profileService.getTagsFor(profile.profile_id, grouping.grouping_id, 'reveal')
  92. if (revealTags.length) {
  93. groupingRevealTags.push(revealTags)
  94. }
  95. }
  96. }
  97. return groupingRevealTags
  98. }
  99. const revealTags = await getGroupingRevealTags()
  100. // TODO: Refactor, triple for of loop...
  101. const getRevealInfo = async () => {
  102. const profilesWithRevealed = []
  103. for (const profile of completedProfiles) {
  104. const userInfo = await userService.findById(profile.user_id)
  105. if (userInfo && revealTags.length) {
  106. for (const tags of revealTags) {
  107. for (const tag of tags) {
  108. if (tag.tag.tag_description) {
  109. profile[tag.tag.tag_description] = userInfo[tag.tag.tag_description]
  110. }
  111. }
  112. }
  113. profilesWithRevealed.push(profile)
  114. }
  115. }
  116. if (profilesWithRevealed.length)
  117. return profilesWithRevealed
  118. else
  119. return completedProfiles
  120. }
  121. const completedProfilesWithRevealedInfo = await getRevealInfo()
  122. const reformattedGroupings = groupings.map(g => {
  123. completedProfilesWithRevealedInfo.forEach(p => {
  124. g.profile = g.profile == p.profile_id ? p : g.profile
  125. })
  126. g.is_paired = _activeGroupingIds(memberships).includes(
  127. g.grouping_id,
  128. )
  129. return g
  130. })
  131. try {
  132. return {
  133. ok: true,
  134. handler: pluginConfig.handlerType,
  135. data: reformattedGroupings,
  136. }
  137. } catch (err) {
  138. return {
  139. ok: false,
  140. handler: pluginConfig.handlerType,
  141. data: { error: `${err}` },
  142. }
  143. }
  144. },
  145. /** Validate based on validators object */
  146. validate: {
  147. ...validators,
  148. failAction: 'log',
  149. },
  150. /** Validate the server response */
  151. response: {
  152. schema: apiSchema.single
  153. .append({
  154. data: responseSchemas.list,
  155. })
  156. .label('grouping_list_res'),
  157. },
  158. },
  159. }