Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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