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.

join.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const Joi = require('joi')
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const groupingSchema = require('../../schemas/groupings')
  5. const params = require('../../schemas/params')
  6. const pluginConfig = {
  7. handlerType: 'grouping',
  8. docs: {
  9. description: 'join',
  10. notes: 'Join a grouping by creating a membership record',
  11. },
  12. }
  13. const validators = {
  14. params: params.profileId,
  15. payload: groupingSchema.single
  16. .append({
  17. target_id: Joi.number().required(),
  18. role: Joi.string(),
  19. })
  20. .label('grouping_membership_single'),
  21. }
  22. const responseSchemas = {
  23. response: Joi.object({
  24. memberships: Joi.array().items(),
  25. hasMatch: Joi.boolean(),
  26. }).label('grouping_membership_list'),
  27. error: errorSchema.single,
  28. }
  29. module.exports = {
  30. method: 'POST',
  31. path: '/{profile_id}/join',
  32. options: {
  33. ...pluginConfig.docs,
  34. tags: ['api'],
  35. auth: false,
  36. cors: true,
  37. /**
  38. * Join a grouping by creating a membership record
  39. * @param {*} request
  40. * @param {*} h
  41. * @returns {object}
  42. */
  43. handler: async function (request, h) {
  44. try {
  45. const { membershipService } = request.server.services()
  46. /** Grab payload info */
  47. const profileId = request.params.profile_id
  48. const res = request.payload
  49. const groupingToWrite = {
  50. grouping_id: res.grouping_id,
  51. grouping_name: res.grouping_name,
  52. grouping_type: res.grouping_type,
  53. }
  54. /** Default to participant role */
  55. const role = res.role ? res.role : 'participant'
  56. // TODO: LIMIT the amount of groupings by checking type
  57. // !: You should only be able to match with the target_id ONCE
  58. // !: You should only be associated with a single company too
  59. /** User membership service method to create membership */
  60. const memberships = await membershipService.joinGrouping(
  61. profileId,
  62. res.target_id,
  63. groupingToWrite,
  64. role,
  65. )
  66. // console.log(memberships)
  67. const hasMatch = memberships.every(
  68. membership => membership && membership.is_active == true,
  69. )
  70. request.server.methods.notify(
  71. `${profileId}.stonk`,
  72. {
  73. name: `${res.target_id} Match Fffound`,
  74. type: 'info',
  75. },
  76. h,
  77. )
  78. request.server.methods.notify(
  79. `${res.target_id}.stonk`,
  80. {
  81. name: `${profileId} Match Fffound`,
  82. type: 'info',
  83. },
  84. h,
  85. )
  86. console.log('hasMatch :>> ', hasMatch)
  87. console.log('memberships :>> ', memberships)
  88. return h
  89. .response({
  90. ok: true,
  91. handler: pluginConfig.handlerType,
  92. data: {
  93. memberships,
  94. hasMatch,
  95. },
  96. })
  97. .code(200)
  98. } catch (err) {
  99. return h
  100. .response({
  101. ok: false,
  102. handler: pluginConfig.handlerType,
  103. data: { error: `${err}` },
  104. })
  105. .code(409)
  106. }
  107. },
  108. /** Validate based on validators object */
  109. validate: {
  110. ...validators,
  111. failAction: 'log',
  112. },
  113. /** Validate the server response */
  114. response: {
  115. status: {
  116. 200: apiSchema.single
  117. .append({
  118. data: responseSchemas.response,
  119. })
  120. .label('join_grouping_res'),
  121. 409: apiSchema.single
  122. .append({
  123. data: responseSchemas.error,
  124. })
  125. .label('error_single_res'),
  126. },
  127. },
  128. },
  129. }