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

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