Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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