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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  11. const validators = {
  12. params: Joi.object({ grouping_id: Joi.number() }),
  13. query: Joi.object({ profile_id: Joi.number(), tag_id: Joi.number() }),
  14. }
  15. const responseSchemas = {
  16. response: Joi.object({
  17. tags: Joi.array().items(),
  18. }),
  19. error: errorSchema.single,
  20. }
  21. module.exports = {
  22. method: 'POST',
  23. path: '/{grouping_id}/reveal',
  24. options: {
  25. ...pluginConfig.docs,
  26. tags: ['api'],
  27. auth: false,
  28. cors: true,
  29. handler: async function (request, h) {
  30. const { membershipService, profileService } =
  31. request.server.services()
  32. const grouping_id = request.params.grouping_id
  33. const { profile_id, tag_id } = request.query
  34. try {
  35. const tags = await profileService.revealProfileInfo({
  36. profile_id,
  37. grouping_id,
  38. tag_id,
  39. is_deleted: false,
  40. })
  41. // Notify both profiles that information has been revealed
  42. const memberships = await membershipService.findMemberships([
  43. grouping_id,
  44. ])
  45. const idsInGroup = memberships.map(
  46. membership => membership.profile_id,
  47. )
  48. // Grab User Info from Users Table and prepare revealed info for
  49. // notification based off of tag_id
  50. const completeProfile = await profileService.getProfilesFor([profile_id], 'participant')
  51. let revealInfo
  52. let tag_description
  53. if (tag_id == 7) {
  54. revealInfo = completeProfile[0].user_name
  55. tag_description = 'name'
  56. } else if (tag_id == 8) {
  57. revealInfo = completeProfile[0].user_email
  58. tag_description = 'email'
  59. }
  60. idsInGroup.forEach(profile_id => {
  61. request.server.methods.notify(
  62. `${profile_id}.stonk`,
  63. {
  64. name: 'REVEALED_INFO',
  65. revealed_info: revealInfo,
  66. grouping_id: grouping_id,
  67. tag: tag_id,
  68. description: tag_description,
  69. type: 'info',
  70. },
  71. h,
  72. )
  73. })
  74. return h
  75. .response({
  76. ok: true,
  77. handler: pluginConfig.handlerType,
  78. data: { tags },
  79. })
  80. .code(200)
  81. } catch (err) {
  82. return h
  83. .response({
  84. ok: false,
  85. handler: pluginConfig.handlerType,
  86. data: { error: `${err}` },
  87. })
  88. .code(409)
  89. }
  90. },
  91. /** Validate based on validators object */
  92. validate: {
  93. ...validators,
  94. failAction: 'log',
  95. },
  96. /** Validate the server response */
  97. response: {
  98. status: {
  99. 200: apiSchema.single
  100. .append({
  101. data: responseSchemas.response,
  102. })
  103. .label('reveal_res'),
  104. 409: apiSchema.single
  105. .append({
  106. data: responseSchemas.error,
  107. })
  108. .label('error_single_res'),
  109. },
  110. },
  111. },
  112. }