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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const Joi = require('joi')
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const pluginConfig = {
  5. handlerType: 'reveal',
  6. docs: {
  7. description: 'reveal',
  8. notes: 'Reveal profile information to a grouping by membership',
  9. },
  10. opts: {
  11. tags: ['api'],
  12. auth: { strategy: 'default_jwt' },
  13. cors: true,
  14. },
  15. }
  16. const validators = {
  17. params: Joi.object({ grouping_id: Joi.number() }),
  18. query: Joi.object({ profile_id: Joi.number(), tag_id: Joi.number() }),
  19. }
  20. const responseSchemas = {
  21. response: Joi.object({
  22. tags: Joi.array().items(),
  23. }),
  24. error: errorSchema.single,
  25. }
  26. module.exports = {
  27. method: 'POST',
  28. path: '/{grouping_id}/reveal',
  29. options: {
  30. ...pluginConfig.docs,
  31. ...pluginConfig.opts,
  32. handler: async function (request, h) {
  33. const { membershipService, profileService } =
  34. request.server.services()
  35. const grouping_id = request.params.grouping_id
  36. const { profile_id, tag_id } = request.query
  37. try {
  38. const tags = await profileService.revealProfileInfo({
  39. profile_id,
  40. grouping_id,
  41. tag_id,
  42. is_deleted: false,
  43. })
  44. // Notify both profiles that information has been revealed
  45. const memberships = await membershipService.findMemberships([
  46. grouping_id,
  47. ])
  48. const idsInGroup = memberships.map(
  49. membership => membership.profile_id,
  50. )
  51. idsInGroup.forEach(profile_id => {
  52. request.server.methods.notify(
  53. `${profile_id}.stonk`,
  54. {
  55. name: 'REVEALED INFO',
  56. tag: tag_id,
  57. type: 'info',
  58. },
  59. h,
  60. )
  61. })
  62. return h
  63. .response({
  64. ok: true,
  65. handler: pluginConfig.handlerType,
  66. data: { tags },
  67. })
  68. .code(200)
  69. } catch (err) {
  70. return h
  71. .response({
  72. ok: false,
  73. handler: pluginConfig.handlerType,
  74. data: { error: `${err}` },
  75. })
  76. .code(409)
  77. }
  78. },
  79. /** Validate based on validators object */
  80. validate: {
  81. ...validators,
  82. failAction: 'log',
  83. },
  84. /** Validate the server response */
  85. response: {
  86. status: {
  87. 200: apiSchema.single
  88. .append({
  89. data: responseSchemas.response,
  90. })
  91. .label('reveal_res'),
  92. 409: apiSchema.single
  93. .append({
  94. data: responseSchemas.error,
  95. })
  96. .label('error_single_res'),
  97. },
  98. },
  99. },
  100. }