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

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