| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 'use strict'
-
- const Joi = require('joi')
-
- const pluginConfig = {
- handlerType: 'grouping',
- docs: {
- description: 'active memberships',
- notes: 'A list of groupings with active membership',
- },
- }
-
- const validators = {
- /** Validate the header (cookie check) */
- // headers: true,
-
- /** Validate the route params (/active/{thing}) */
- params: Joi.object({ user_id: Joi.number() }),
-
- /** Validate the route query (/active/{thing}?limit=10&offset=10) */
- // query: true,
-
- /** Validate the incoming payload (POST method) */
- // payload: true,
- }
-
- const responseSchemas = {
- groupingsList: Joi.object({
- grouping_id: Joi.number(),
- grouping_name: Joi.string(),
- grouping_type: Joi.string(),
- }),
- }
-
- module.exports = {
- method: 'GET',
- path: '/active/{user_id}',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
-
- handler: async function (request, h) {
- const { membershipService } = request.services()
- const userId = request.params.user_id
- const groupings = await membershipService.findGroupingsById(userId)
- try {
- return {
- ok: true,
- handler: pluginConfig.handlerType,
- data: groupings,
- }
- } catch (err) {
- return {
- ok: false,
- handler: pluginConfig.handlerType,
- data: { error: `${err}` },
- }
- }
- },
-
- /** Validate based on validators object */
- validate: {
- ...validators,
- failAction: 'log',
- },
-
- /** Validate the server response */
- response: {
- schema: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: Joi.array().items(responseSchemas.groupingsList),
- }),
- },
- },
- }
|