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

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