Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (idsInGroup.length > 2)
  49. return console.error('ERROR: idsInGroup cannot have more than 2 entries: ', idsInGroup)
  50. // Grab User Info from Users Table
  51. const completeProfile = await profileService.getProfilesFor([profile_id], 'participant')
  52. // Grab the TagAssociation that matches the revealed profile
  53. // TODO: Check if there are multiple matching tags(?)(there shouldn't be)
  54. const returnedTag = () => {
  55. let matchingTag
  56. tags.forEach(tagAssoc => {
  57. if (tagAssoc.grouping_id === grouping_id &&
  58. tagAssoc.profile_id === profile_id &&
  59. tagAssoc.tag_id === tag_id) {
  60. matchingTag = tagAssoc
  61. }
  62. })
  63. if (matchingTag)
  64. return matchingTag
  65. return console.error('ERROR: No matching tagAssociation')
  66. }
  67. const tag_description = returnedTag().tag.tag_description
  68. // TODO: Refactor completeProfile[0]... code smell
  69. const revealInfo = completeProfile[0][tag_description]
  70. idsInGroup.forEach(profile_id => {
  71. request.server.methods.notify(
  72. `${profile_id}.stonk`,
  73. {
  74. name: 'REVEALED_INFO',
  75. revealed_info: revealInfo,
  76. grouping_id: grouping_id,
  77. tag: tag_id,
  78. description: tag_description,
  79. type: 'info',
  80. },
  81. h,
  82. )
  83. })
  84. return h
  85. .response({
  86. ok: true,
  87. handler: pluginConfig.handlerType,
  88. data: { tags },
  89. })
  90. .code(200)
  91. } catch (err) {
  92. return h
  93. .response({
  94. ok: false,
  95. handler: pluginConfig.handlerType,
  96. data: { error: `${err}` },
  97. })
  98. .code(409)
  99. }
  100. },
  101. /** Validate based on validators object */
  102. validate: {
  103. ...validators,
  104. failAction: 'log',
  105. },
  106. /** Validate the server response */
  107. response: {
  108. status: {
  109. 200: apiSchema.single
  110. .append({
  111. data: responseSchemas.response,
  112. })
  113. .label('reveal_res'),
  114. 409: apiSchema.single
  115. .append({
  116. data: responseSchemas.error,
  117. })
  118. .label('error_single_res'),
  119. },
  120. },
  121. },
  122. }