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

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