Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

active.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. module.exports = {
  30. method: 'GET',
  31. path: '/{profile_id}',
  32. options: {
  33. ...pluginConfig.docs,
  34. tags: ['api'],
  35. /** Protect this route with authentication? */
  36. auth: false,
  37. cors: true,
  38. handler: async function (request, h) {
  39. const { membershipService, profileService } =
  40. request.server.services()
  41. const membershipType = request.query.type
  42. const profileId = request.params.profile_id
  43. let groupings = await membershipService.findGroupingsByProfileId(
  44. profileId,
  45. membershipType,
  46. )
  47. /**
  48. * Heavily process the result by storing just a profile_id
  49. * and attach complete profiles
  50. */
  51. let pIds = groupings.reduce((ids, grouping) => {
  52. grouping.profiles.forEach(p => {
  53. if (p.profile_id == profileId) return
  54. ids.push(p.profile_id)
  55. grouping.profile = p.profile_id
  56. })
  57. delete grouping.profiles
  58. return ids
  59. }, [])
  60. /** Assemble complete profiles to reference and pass */
  61. const completedProfiles = await profileService.getProfilesFor(
  62. pIds,
  63. 'participant',
  64. false,
  65. )
  66. const reformattedGroupings = groupings.map(g => {
  67. completedProfiles.forEach(p => {
  68. g.profile = g.profile == p.profile_id ? p : g.profile
  69. })
  70. return g
  71. })
  72. try {
  73. return {
  74. ok: true,
  75. handler: pluginConfig.handlerType,
  76. data: reformattedGroupings,
  77. }
  78. } catch (err) {
  79. return {
  80. ok: false,
  81. handler: pluginConfig.handlerType,
  82. data: { error: `${err}` },
  83. }
  84. }
  85. },
  86. /** Validate based on validators object */
  87. validate: {
  88. ...validators,
  89. failAction: 'log',
  90. },
  91. /** Validate the server response */
  92. response: {
  93. schema: apiSchema.single
  94. .append({
  95. data: responseSchemas.list,
  96. })
  97. .label('grouping_list_res'),
  98. },
  99. },
  100. }