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.

reveal.js 3.3KB

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