Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

active.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // NOTE: Current implementation works okay, it only reveals the targetID's
  68. // hidden info on the page currently, it does NOT reveal OUR OWN information
  69. // to ourselves when revealed
  70. /**
  71. * Heavily process the result by storing just a profile_id
  72. * and attach complete profiles
  73. */
  74. // NOTE: only returns the pId for the target
  75. let pIds = groupings.reduce((ids, grouping) => {
  76. grouping.profiles.forEach(p => {
  77. // you'd think to comment out here, but it still only returns CompleteProfile of target
  78. // pIds would be both currentProfile id and target id though
  79. if (p.profile_id == profileId) return
  80. ids.push(p.profile_id)
  81. grouping.profile = p.profile_id
  82. })
  83. delete grouping.profiles
  84. return ids
  85. }, [])
  86. console.log('pIds :=>', pIds) // only returns single array of target id
  87. /** Assemble complete profiles to reference and pass */
  88. const completedProfiles = await profileService.getProfilesFor(
  89. pIds,
  90. 'participant',
  91. false,
  92. )
  93. // NOTE: Doesn't rely on profile reveal field to get reveal tags
  94. const getGroupingRevealTags = async () => {
  95. const groupingRevealTags = []
  96. for (const grouping of groupings) {
  97. for (const profile of completedProfiles) {
  98. const revealTags = await profileService.getTagsFor(profile.profile_id, grouping.grouping_id, 'reveal')
  99. if (revealTags.length) {
  100. groupingRevealTags.push(revealTags)
  101. }
  102. }
  103. }
  104. return groupingRevealTags
  105. }
  106. const revealTags = await getGroupingRevealTags()
  107. // TODO: Refactor, triple for of loop...
  108. const getRevealInfo = async () => {
  109. const profilesWithRevealed = []
  110. for (const profile of completedProfiles) {
  111. const userInfo = await userService.findById(profile.user_id)
  112. if (userInfo && revealTags.length) {
  113. for (const tags of revealTags) {
  114. for (const tag of tags) {
  115. if (tag.tag.tag_description) {
  116. profile[tag.tag.tag_description] = userInfo[tag.tag.tag_description]
  117. }
  118. }
  119. }
  120. profilesWithRevealed.push(profile)
  121. }
  122. }
  123. if (profilesWithRevealed.length)
  124. return profilesWithRevealed
  125. else
  126. return completedProfiles
  127. }
  128. const completedProfilesWithRevealedInfo = await getRevealInfo()
  129. // ISSUE: renders target ID but not currentProfileID in ChatView.vue
  130. const reformattedGroupings = groupings.map(g => {
  131. completedProfilesWithRevealedInfo.forEach(p => {
  132. g.profile = g.profile == p.profile_id ? p : g.profile
  133. })
  134. g.is_paired = _activeGroupingIds(memberships).includes(
  135. g.grouping_id,
  136. )
  137. return g
  138. })
  139. console.log('reformattedGroupings :=>', reformattedGroupings)
  140. try {
  141. return {
  142. ok: true,
  143. handler: pluginConfig.handlerType,
  144. data: reformattedGroupings,
  145. }
  146. } catch (err) {
  147. return {
  148. ok: false,
  149. handler: pluginConfig.handlerType,
  150. data: { error: `${err}` },
  151. }
  152. }
  153. },
  154. /** Validate based on validators object */
  155. validate: {
  156. ...validators,
  157. failAction: 'log',
  158. },
  159. /** Validate the server response */
  160. response: {
  161. schema: apiSchema.single
  162. .append({
  163. data: responseSchemas.list,
  164. })
  165. .label('grouping_list_res'),
  166. },
  167. },
  168. }