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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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({ user_id: Joi.number() }),
  15. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  16. // query: true,
  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: '/active/{user_id}',
  30. options: {
  31. ...pluginConfig.docs,
  32. tags: ['api'],
  33. /** Protect this route with authentication? */
  34. auth: false,
  35. handler: async function (request, h) {
  36. const { membershipService } = request.services()
  37. const userId = request.params.user_id
  38. const groupings = await membershipService.findGroupingsById(userId)
  39. try {
  40. return {
  41. ok: true,
  42. handler: pluginConfig.handlerType,
  43. data: groupings,
  44. }
  45. } catch (err) {
  46. return {
  47. ok: false,
  48. handler: pluginConfig.handlerType,
  49. data: { error: `${err}` },
  50. }
  51. }
  52. },
  53. /** Validate based on validators object */
  54. validate: {
  55. ...validators,
  56. failAction: 'log',
  57. },
  58. /** Validate the server response */
  59. response: {
  60. schema: Joi.object({
  61. ok: Joi.bool(),
  62. handler: Joi.string(),
  63. data: Joi.array().items(responseSchemas.groupingsList),
  64. }),
  65. },
  66. },
  67. }