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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/{profile_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 profileId = request.params.profile_id
  38. const groupings = await membershipService.findGroupingsById(
  39. profileId,
  40. )
  41. try {
  42. return {
  43. ok: true,
  44. handler: pluginConfig.handlerType,
  45. data: groupings,
  46. }
  47. } catch (err) {
  48. return {
  49. ok: false,
  50. handler: pluginConfig.handlerType,
  51. data: { error: `${err}` },
  52. }
  53. }
  54. },
  55. /** Validate based on validators object */
  56. validate: {
  57. ...validators,
  58. failAction: 'log',
  59. },
  60. /** Validate the server response */
  61. response: {
  62. schema: Joi.object({
  63. ok: Joi.bool(),
  64. handler: Joi.string(),
  65. data: Joi.array().items(responseSchemas.groupingsList),
  66. }),
  67. },
  68. },
  69. }