Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 PassThrough = require('stream').PassThrough
  8. const pluginConfig = {
  9. handlerType: 'grouping',
  10. docs: {
  11. description: 'active memberships',
  12. notes: 'A list of groupings with active membership',
  13. },
  14. }
  15. const validators = {
  16. /** Validate the header (cookie check) */
  17. // headers: true,
  18. /** Validate the route params (/active/{thing}) */
  19. params: params.profileId,
  20. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  21. query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
  22. /** Validate the incoming payload (POST method) */
  23. // payload: true,
  24. }
  25. const responseSchemas = {
  26. single: groupingSchema.single,
  27. list: groupingSchema.listWithProfiles,
  28. error: errorSchema.single,
  29. }
  30. const dispatch = (streamName, streams, h) => {
  31. const stream = streams[streamName]
  32. if (!stream || !streams) return
  33. const msg = {
  34. name: 'MSHRM',
  35. price: (500 + Math.floor(Math.random() * 100)).toString(),
  36. order: null,
  37. type: 'info',
  38. }
  39. stream.write(msg)
  40. h.event(stream, h, { event: streamName })
  41. }
  42. module.exports = {
  43. method: 'GET',
  44. path: '/{profile_id}',
  45. options: {
  46. ...pluginConfig.docs,
  47. tags: ['api'],
  48. /** Protect this route with authentication? */
  49. auth: false,
  50. cors: true,
  51. handler: async function (request, h) {
  52. const { membershipService, profileService } =
  53. request.server.services()
  54. const membershipType = request.query.type
  55. const profileId = request.params.profile_id
  56. let groupings = await membershipService.findGroupingsByProfileId(
  57. profileId,
  58. membershipType,
  59. )
  60. /**
  61. * Heavily process the result by storing just a profile_id
  62. * and attach complete profiles
  63. */
  64. let pIds = groupings.reduce((ids, grouping) => {
  65. grouping.profiles.forEach(p => {
  66. if (p.profile_id == profileId) return
  67. ids.push(p.profile_id)
  68. grouping.profile = p.profile_id
  69. })
  70. delete grouping.profiles
  71. return ids
  72. }, [])
  73. /** Assemble complete profiles to reference and pass */
  74. const completedProfiles = await profileService.getProfilesFor(
  75. pIds,
  76. 'participant',
  77. false,
  78. )
  79. const reformattedGroupings = groupings.map(g => {
  80. completedProfiles.forEach(p => {
  81. g.profile = g.profile == p.profile_id ? p : g.profile
  82. })
  83. return g
  84. })
  85. const allStreams =
  86. request.server.plugins['notification-plugin']['streams']
  87. dispatch(`${profileId}.stonk`, allStreams, h)
  88. try {
  89. return {
  90. ok: true,
  91. handler: pluginConfig.handlerType,
  92. data: reformattedGroupings,
  93. }
  94. } catch (err) {
  95. return {
  96. ok: false,
  97. handler: pluginConfig.handlerType,
  98. data: { error: `${err}` },
  99. }
  100. }
  101. },
  102. /** Validate based on validators object */
  103. validate: {
  104. ...validators,
  105. failAction: 'log',
  106. },
  107. /** Validate the server response */
  108. response: {
  109. schema: apiSchema.single
  110. .append({
  111. data: responseSchemas.list,
  112. })
  113. .label('grouping_list_res'),
  114. },
  115. },
  116. }