Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

join.js 4.8KB

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. 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. // TODO: add urls for chat
  78. url: `<a href="/profile/${res.target_id}">url</a>`,
  79. type: 'info',
  80. },
  81. h,
  82. )
  83. request.server.methods.notify(
  84. `${res.target_id}.stonk`,
  85. {
  86. name: `${profileId} Match Fffound`,
  87. // TODO: add urls for chat
  88. url: `<a href="/profile/${profileId}">url</a>`,
  89. type: 'info',
  90. },
  91. h,
  92. )
  93. }
  94. return h
  95. .response({
  96. ok: true,
  97. handler: pluginConfig.handlerType,
  98. data: {
  99. memberships,
  100. hasMatch,
  101. groupings,
  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. }