You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

active.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const groupings = await membershipService.findGroupingsByProfileId(
  61. profileId,
  62. membershipType,
  63. )
  64. if (!groupings.length) {
  65. return {
  66. ok: true,
  67. handler: pluginConfig.handlerType,
  68. data: [],
  69. }
  70. }
  71. const groupingIds = groupings.map(grouping => grouping.grouping_id)
  72. const memberships =
  73. await membershipService.findMemberships(groupingIds)
  74. let profileIds = memberships
  75. .filter(membership => membership.profile_id !== profileId)
  76. .map(membership => membership.profile_id)
  77. profileIds =
  78. !profileIds.length || profileIds[0] === undefined
  79. ? []
  80. : profileIds
  81. /** Assemble complete profiles to reference and pass */
  82. const completedProfiles = !profileIds.length
  83. ? []
  84. : await profileService.getProfilesFor(profileIds, 'participant')
  85. /**
  86. * Heavily process the result by storing just a profile_id
  87. * and attach complete profiles
  88. * !: This still assumes only ONE other profile
  89. * TODO: should be refactored to many other profiles
  90. */
  91. const reformattedGroupings =
  92. groupings.length && completedProfiles.length
  93. ? groupings.map(grouping => {
  94. const otherPid = grouping.profiles.find(
  95. p => p.profile_id != profileId,
  96. ).profile_id
  97. grouping.profile = completedProfiles.find(
  98. p => otherPid == p.profile_id,
  99. )
  100. grouping.is_paired = _activeGroupingIds(
  101. memberships,
  102. ).includes(grouping.grouping_id)
  103. delete grouping.profiles
  104. return grouping
  105. })
  106. : []
  107. /** Grabs revealTags */
  108. const revealTags =
  109. profileIds.length && groupingIds.length
  110. ? await profileService.getTagsFor(
  111. profileIds,
  112. groupingIds,
  113. 'reveal',
  114. )
  115. : undefined
  116. console.log('revealTags :=>', revealTags)
  117. /** If the revealTags exist, the completedProfile's hidden info is
  118. * removed and replaced with the completedProfile's user information
  119. * Otherwise the completedProfiles remain unchanged
  120. */
  121. const user = completedProfiles.length
  122. ? await userService.findById(
  123. completedProfiles.map(p => p.user_id),
  124. )
  125. : undefined
  126. console.log('user :=>', user)
  127. // TODO: Refactor this. Is it safe to always use completedProfiles[0]?
  128. if (revealTags && user) {
  129. revealTags.forEach(t => {
  130. console.log('t :=>', t)
  131. if (!t.tag.tag_description) return
  132. completedProfiles[0][t.tag.tag_description] =
  133. user[t.tag.tag_description]
  134. })
  135. }
  136. try {
  137. return {
  138. ok: true,
  139. handler: pluginConfig.handlerType,
  140. data: reformattedGroupings,
  141. }
  142. } catch (err) {
  143. return {
  144. ok: false,
  145. handler: pluginConfig.handlerType,
  146. data: { error: `${err}` },
  147. }
  148. }
  149. },
  150. /** Validate based on validators object */
  151. validate: {
  152. ...validators,
  153. failAction: 'log',
  154. },
  155. /** Validate the server response */
  156. response: {
  157. schema: apiSchema.single
  158. .append({
  159. data: responseSchemas.list,
  160. })
  161. .label('grouping_list_res'),
  162. },
  163. },
  164. }