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.

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