Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

join.js 4.4KB

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