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 4.2KB

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