| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- '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
- const groupings = await membershipService.findGroupingsByProfileId(
- profileId,
- membershipType,
- )
- if (!groupings.length) {
- return {
- ok: true,
- handler: pluginConfig.handlerType,
- data: [],
- }
- }
- const groupingIds = groupings.map(grouping => grouping.grouping_id)
- const memberships =
- await membershipService.findMemberships(groupingIds)
- let profileIds = memberships
- .filter(membership => membership.profile_id !== profileId)
- .map(membership => membership.profile_id)
- profileIds =
- !profileIds.length || profileIds[0] === undefined
- ? []
- : profileIds
- /** Assemble complete profiles to reference and pass */
- const completedProfiles = !profileIds.length
- ? []
- : await profileService.getProfilesFor(profileIds, 'participant')
- /**
- * Heavily process the result by storing just a profile_id
- * and attach complete profiles
- * !: This still assumes only ONE other profile
- * TODO: should be refactored to many other profiles
- */
- const reformattedGroupings =
- groupings.length && completedProfiles.length
- ? groupings.map(grouping => {
- const otherPid = grouping.profiles.find(
- p => p.profile_id != profileId,
- ).profile_id
- grouping.profile = completedProfiles.find(
- p => otherPid == p.profile_id,
- )
- grouping.is_paired = _activeGroupingIds(
- memberships,
- ).includes(grouping.grouping_id)
- delete grouping.profiles
- return grouping
- })
- : []
- /** Grabs revealTags */
- const revealTags =
- profileIds.length && groupingIds.length
- ? await profileService.getTagsFor(
- profileIds,
- groupingIds,
- 'reveal',
- )
- : undefined
- console.log('revealTags :=>', revealTags)
-
- /** 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 user = completedProfiles.length
- ? await userService.findById(
- completedProfiles.map(p => p.user_id),
- )
- : undefined
- console.log('user :=>', user)
-
- // TODO: Refactor this. Is it safe to always use completedProfiles[0]?
- if (revealTags && user) {
- revealTags.forEach(t => {
- console.log('t :=>', t)
- if (!t.tag.tag_description) return
- completedProfiles[0][t.tag.tag_description] =
- user[t.tag.tag_description]
- })
- }
-
- 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'),
- },
- },
- }
|