| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- '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({ profile_id: Joi.number() }),
-
- /** Validate the route query (/active/{thing}?limit=10&offset=10) */
- query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
-
- /** 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: '/{profile_id}',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
- cors: true,
- handler: async function (request, h) {
- const { membershipService } = request.services()
- const membershipType = request.query.type
-
- const profileId = request.params.profile_id
- const groupings = await membershipService.findGroupingsByProfileId(
- profileId,
- membershipType
- )
- 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),
- }),
- },
- },
- }
|