You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

join.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const Joi = require('joi')
  2. const pluginConfig = {
  3. handlerType: 'grouping',
  4. docs: {
  5. description: 'join',
  6. notes: 'Join a grouping by creating a membership record',
  7. },
  8. }
  9. const validators = {
  10. params: Joi.object({ profile_id: Joi.number() }),
  11. payload: Joi.object({
  12. target_id: Joi.number().required(),
  13. grouping_id: Joi.number().allow(null),
  14. grouping_name: Joi.string().allow(null),
  15. grouping_type: Joi.string().allow(null),
  16. role: Joi.string(),
  17. }),
  18. }
  19. const responseSchemas = {
  20. response: Joi.object({
  21. memberships: Joi.array().items(),
  22. hasMatch: Joi.boolean()
  23. }),
  24. error: Joi.object({
  25. error: Joi.string(),
  26. }),
  27. }
  28. module.exports = {
  29. method: 'POST',
  30. path: '/{profile_id}/join',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. auth: false,
  35. cors: true,
  36. /**
  37. * Join a grouping by creating a membership record
  38. * @param {*} request
  39. * @param {*} h
  40. * @returns {object}
  41. */
  42. handler: async function (request, h) {
  43. try {
  44. const { membershipService } = request.server.services()
  45. /** Grab payload info */
  46. const profileId = request.params.profile_id
  47. const res = request.payload
  48. const groupingToWrite = {
  49. grouping_id: res.grouping_id,
  50. grouping_name: res.grouping_name,
  51. grouping_type: res.grouping_type,
  52. }
  53. /** Default to participant role */
  54. const role = res.role ? res.role : 'participant'
  55. // TODO: LIMIT the amount of groupings by checking type
  56. // !: You should only be able to match with the target_id ONCE
  57. // !: You should only be associated with a single company too
  58. /** User membership service method to create membership */
  59. const memberships = await membershipService.joinGrouping(
  60. profileId,
  61. res.target_id,
  62. groupingToWrite,
  63. role,
  64. )
  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: Joi.object({
  96. ok: Joi.bool(),
  97. handler: Joi.string(),
  98. data: responseSchemas.response,
  99. }),
  100. 409: Joi.object({
  101. ok: Joi.bool(),
  102. handler: Joi.string(),
  103. data: responseSchemas.error,
  104. }),
  105. },
  106. },
  107. },
  108. }