選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

join.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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, groupings } =
  62. await membershipService.joinGrouping(
  63. profileId,
  64. res.target_id,
  65. groupingToWrite,
  66. role,
  67. )
  68. const hasMatch = memberships.every(
  69. membership => membership && membership.is_active == true,
  70. )
  71. if (hasMatch) {
  72. request.server.methods.notify(
  73. `${profileId}.stonk`,
  74. {
  75. name: `${res.target_id} Match Fffound`,
  76. // TODO: add urls for chat
  77. url: `<a href="/profile/${res.target_id}">url</a>`,
  78. type: 'info',
  79. },
  80. h,
  81. )
  82. request.server.methods.notify(
  83. `${res.target_id}.stonk`,
  84. {
  85. name: `${profileId} Match Fffound`,
  86. // TODO: add urls for chat
  87. url: `<a href="/profile/${profileId}">url</a>`,
  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. }