Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

active.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. request.server.methods.notify(
  73. `${profileId}.stonk`,
  74. {
  75. name: 'MSHRM',
  76. price: (500 + Math.floor(Math.random() * 100)).toString(),
  77. order: null,
  78. type: 'info',
  79. },
  80. h,
  81. )
  82. try {
  83. return {
  84. ok: true,
  85. handler: pluginConfig.handlerType,
  86. data: reformattedGroupings,
  87. }
  88. } catch (err) {
  89. return {
  90. ok: false,
  91. handler: pluginConfig.handlerType,
  92. data: { error: `${err}` },
  93. }
  94. }
  95. },
  96. /** Validate based on validators object */
  97. validate: {
  98. ...validators,
  99. failAction: 'log',
  100. },
  101. /** Validate the server response */
  102. response: {
  103. schema: apiSchema.single
  104. .append({
  105. data: responseSchemas.list,
  106. })
  107. .label('grouping_list_res'),
  108. },
  109. },
  110. }