'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const groupingSchema = require('../../schemas/groupings') const params = require('../../schemas/params') 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: params.profileId, /** 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 = { single: groupingSchema.single, list: groupingSchema.listWithProfiles, error: errorSchema.single, } const _activeGroupingIds = allMemberships => { const active = {} allMemberships.forEach(membership => { if (!membership.is_active) return if (!active[membership.grouping_id]) { active[membership.grouping_id] = [] } active[membership.grouping_id].push(membership) }) const ids = [] Object.values(active).forEach(profileListInGrouping => { if (profileListInGrouping.length == 2) { ids.push(profileListInGrouping[0].grouping_id) } }) return ids } 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, profileService, userService } = request.server.services() const membershipType = request.query.type const profileId = request.params.profile_id let groupings = await membershipService.findGroupingsByProfileId( profileId, membershipType, ) let memberships = await membershipService.findMemberships( groupings.map(grouping => grouping.grouping_id), ) /** * Heavily process the result by storing just a profile_id * and attach complete profiles */ let pIds = groupings.reduce((ids, grouping) => { grouping.profiles.forEach(p => { if (p.profile_id == profileId) return ids.push(p.profile_id) grouping.profile = p.profile_id }) delete grouping.profiles return ids }, []) /** Assemble complete profiles to reference and pass */ const completedProfiles = await profileService.getProfilesFor( pIds, 'participant', ) /** Grabs revealTags */ const profileIdsFromCompletedProfiles = completedProfiles.map( p => p.profile_id, ) const groupingIdsFromGroupings = groupings.map(g => g.grouping_id) const revealTags = await profileService.getTagsFor( profileIdsFromCompletedProfiles, groupingIdsFromGroupings, 'reveal', ) /** If the revealTags exist, the completedProfile's hidden info is * removed and replaced with the completedProfile's user information * Otherwise the completedProfiles remain unchanged */ const userIdsFromCompletedProfiles = completedProfiles.map( p => p.user_id, ) const user = await userService.findById( userIdsFromCompletedProfiles, ) if (revealTags && user) { for (const t of revealTags) { if (t.tag.tag_description) { completedProfiles[0][t.tag.tag_description] = user[t.tag.tag_description] } } } const reformattedGroupings = groupings.map(g => { completedProfiles.forEach(p => { g.profile = g.profile == p.profile_id ? p : g.profile }) g.is_paired = _activeGroupingIds(memberships).includes( g.grouping_id, ) return g }) try { return { ok: true, handler: pluginConfig.handlerType, data: reformattedGroupings, } } 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: apiSchema.single .append({ data: responseSchemas.list, }) .label('grouping_list_res'), }, }, }