Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

join.js 3.5KB

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