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.

active.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'grouping',
  5. docs: {
  6. description: 'active memberships',
  7. notes: 'A list of groupings with active membership',
  8. },
  9. }
  10. const validators = {
  11. /** Validate the header (cookie check) */
  12. // headers: true,
  13. /** Validate the route params (/active/{thing}) */
  14. params: Joi.object({ profile_id: Joi.number() }),
  15. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  16. query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
  17. /** Validate the incoming payload (POST method) */
  18. // payload: true,
  19. }
  20. const responseSchemas = {
  21. groupingsList: Joi.object({
  22. grouping_id: Joi.number(),
  23. grouping_name: Joi.string(),
  24. grouping_type: Joi.string(),
  25. }),
  26. }
  27. module.exports = {
  28. method: 'GET',
  29. path: '/{profile_id}',
  30. options: {
  31. ...pluginConfig.docs,
  32. tags: ['api'],
  33. /** Protect this route with authentication? */
  34. auth: false,
  35. cors: true,
  36. handler: async function (request, h) {
  37. const { membershipService } = request.services()
  38. const membershipType = request.query.type
  39. const profileId = request.params.profile_id
  40. const groupings = await membershipService.findGroupingsByProfileId(
  41. profileId,
  42. membershipType
  43. )
  44. try {
  45. return {
  46. ok: true,
  47. handler: pluginConfig.handlerType,
  48. data: groupings,
  49. }
  50. } catch (err) {
  51. return {
  52. ok: false,
  53. handler: pluginConfig.handlerType,
  54. data: { error: `${err}` },
  55. }
  56. }
  57. },
  58. /** Validate based on validators object */
  59. validate: {
  60. ...validators,
  61. failAction: 'log',
  62. },
  63. /** Validate the server response */
  64. response: {
  65. schema: Joi.object({
  66. ok: Joi.bool(),
  67. handler: Joi.string(),
  68. data: Joi.array().items(responseSchemas.groupingsList),
  69. }),
  70. },
  71. },
  72. }