Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

join.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. const { membershipService } = request.server.services()
  46. /** Grab payload info */
  47. const profileId = request.params.profile_id
  48. const res = request.payload
  49. const groupingToWrite = {
  50. grouping_id: res.grouping_id,
  51. grouping_name: res.grouping_name,
  52. grouping_type: res.grouping_type,
  53. }
  54. /** Default to participant role */
  55. const role = res.role ? res.role : 'participant'
  56. // TODO: LIMIT the amount of groupings by checking type
  57. // !: You should only be able to match with the target_id ONCE
  58. // !: You should only be associated with a single company too
  59. /** User membership service method to create membership */
  60. const memberships = await membershipService.joinGrouping(
  61. profileId,
  62. res.target_id,
  63. groupingToWrite,
  64. role,
  65. )
  66. // console.log(memberships)
  67. return h
  68. .response({
  69. ok: true,
  70. handler: pluginConfig.handlerType,
  71. data: {
  72. memberships,
  73. hasMatch: memberships.every(
  74. membership => membership.is_active == true,
  75. ),
  76. },
  77. })
  78. .code(200)
  79. } catch (err) {
  80. return h
  81. .response({
  82. ok: false,
  83. handler: pluginConfig.handlerType,
  84. data: { error: `${err}` },
  85. })
  86. .code(409)
  87. }
  88. },
  89. /** Validate based on validators object */
  90. validate: {
  91. ...validators,
  92. failAction: 'log',
  93. },
  94. /** Validate the server response */
  95. response: {
  96. status: {
  97. 200: apiSchema.single
  98. .append({
  99. data: responseSchemas.response,
  100. })
  101. .label('join_grouping_res'),
  102. 409: apiSchema.single
  103. .append({
  104. data: responseSchemas.error,
  105. })
  106. .label('error_single_res'),
  107. },
  108. },
  109. },
  110. }