Explorar el Código

:bug: refactor bug fixes for active memberships

tags/0.0.3^2
j hace 3 años
padre
commit
c311fc9689

+ 37
- 42
backend/lib/routes/membership/active.js Ver fichero

@@ -67,41 +67,48 @@ module.exports = {
67 67
             const membershipType = request.query.type
68 68
 
69 69
             const profileId = request.params.profile_id
70
-            let groupings = await membershipService.findGroupingsByProfileId(
70
+            const groupings = await membershipService.findGroupingsByProfileId(
71 71
                 profileId,
72 72
                 membershipType,
73 73
             )
74
-            let memberships = await membershipService.findMemberships(
75
-                groupings.map(grouping => grouping.grouping_id),
74
+            const groupingIds = groupings.map(grouping => grouping.grouping_id)
75
+            const memberships = await membershipService.findMemberships(
76
+                groupingIds,
77
+            )
78
+            const profileIds = memberships
79
+                .filter(membership => membership.profile_id != profileId)
80
+                .map(membership => membership.profile_id)
81
+
82
+            /** Assemble complete profiles to reference and pass */
83
+            const completedProfiles = await profileService.getProfilesFor(
84
+                profileIds,
85
+                'participant',
76 86
             )
77 87
 
78 88
             /**
79 89
              * Heavily process the result by storing just a profile_id
80 90
              * and attach complete profiles
91
+             * !: This still assumes only ONE other profile
92
+             * TODO: should be refactored to many other profiles
81 93
              */
82
-            let pIds = groupings.reduce((ids, grouping) => {
83
-                grouping.profiles.forEach(p => {
84
-                    if (p.profile_id == profileId) return
85
-                    ids.push(p.profile_id)
86
-                    grouping.profile = p.profile_id
87
-                })
94
+            const reformattedGroupings = groupings.map(grouping => {
95
+                const otherPid = grouping.profiles.find(
96
+                    p => p.profile_id != profileId,
97
+                ).profile_id
98
+                grouping.profile = completedProfiles.find(
99
+                    p => otherPid == p.profile_id,
100
+                )
101
+                grouping.is_paired = _activeGroupingIds(memberships).includes(
102
+                    grouping.grouping_id,
103
+                )
88 104
                 delete grouping.profiles
89
-                return ids
90
-            }, [])
91
-            /** Assemble complete profiles to reference and pass */
92
-            const completedProfiles = await profileService.getProfilesFor(
93
-                pIds,
94
-                'participant',
95
-            )
105
+                return grouping
106
+            })
96 107
 
97 108
             /** Grabs revealTags */
98
-            const profileIdsFromCompletedProfiles = completedProfiles.map(
99
-                p => p.profile_id,
100
-            )
101
-            const groupingIdsFromGroupings = groupings.map(g => g.grouping_id)
102 109
             const revealTags = await profileService.getTagsFor(
103
-                profileIdsFromCompletedProfiles,
104
-                groupingIdsFromGroupings,
110
+                profileIds,
111
+                groupingIds,
105 112
                 'reveal',
106 113
             )
107 114
 
@@ -109,30 +116,18 @@ module.exports = {
109 116
              * removed and replaced with the completedProfile's user information
110 117
              * Otherwise the completedProfiles remain unchanged
111 118
              */
112
-            const userIdsFromCompletedProfiles = completedProfiles.map(
113
-                p => p.user_id,
114
-            )
115 119
             const user = await userService.findById(
116
-                userIdsFromCompletedProfiles,
120
+                completedProfiles.map(p => p.user_id),
117 121
             )
118
-            if (revealTags && user) {
119
-                for (const t of revealTags) {
120
-                    if (t.tag.tag_description) {
121
-                        completedProfiles[0][t.tag.tag_description] =
122
-                            user[t.tag.tag_description]
123
-                    }
124
-                }
125
-            }
126 122
 
127
-            const reformattedGroupings = groupings.map(g => {
128
-                completedProfiles.forEach(p => {
129
-                    g.profile = g.profile == p.profile_id ? p : g.profile
123
+            // TODO: Refactor this. Is it safe to always use completedProfiles[0]?
124
+            if (revealTags && user) {
125
+                revealTags.forEach(t => {
126
+                    if (!t.tag.tag_description) return
127
+                    completedProfiles[0][t.tag.tag_description] =
128
+                        user[t.tag.tag_description]
130 129
                 })
131
-                g.is_paired = _activeGroupingIds(memberships).includes(
132
-                    g.grouping_id,
133
-                )
134
-                return g
135
-            })
130
+            }
136 131
 
137 132
             try {
138 133
                 return {

+ 1
- 1
backend/lib/services/profile/index.js Ver fichero

@@ -89,7 +89,7 @@ module.exports = class ProfileService extends Schmervice.Service {
89 89
         await this._setScoreLookup()
90 90
         await this._setTagLookup()
91 91
 
92
-        // profilesEntries is profiles in dataaspect_labelsbase row order
92
+        // profilesEntries is profiles in database row order
93 93
         const profilesEntries = await Profile.query()
94 94
             .whereIn('profile_id', profileIdArray)
95 95
             .withGraphFetched('tags')

+ 25
- 7
backend/lib/services/profile/profiler.js Ver fichero

@@ -69,9 +69,18 @@ class CompleteProfile {
69 69
 }
70 70
 const _makeCompleteProfile = (profileEntry, type, tagLookup) => {
71 71
     profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id])
72
-    return new CompleteProfile(profileEntry, type)
72
+    const complete = new CompleteProfile(profileEntry, type)
73
+    return complete
73 74
 }
74 75
 
76
+/**
77
+ * Get complete profiles and return in order
78
+ * @param {Array} orderedProfileIds
79
+ * @param {Array} profilesEntries
80
+ * @param {String} type
81
+ * @param {Object} tagLookup
82
+ * @returns {Array}
83
+ */
75 84
 const makeOrderedCompleteProfiles = (
76 85
     orderedProfileIds,
77 86
     profilesEntries,
@@ -79,14 +88,23 @@ const makeOrderedCompleteProfiles = (
79 88
     tagLookup,
80 89
 ) => {
81 90
     return orderedProfileIds.map(pid => {
82
-        const foundEntry = profilesEntries.find(entry => {
83
-            pid == entry.profile_id
84
-        })
85
-        return _makeCompleteProfile(foundEntry, type, tagLookup)
91
+        const found = profilesEntries.find(entry => entry.profile_id == pid)
92
+        return _makeCompleteProfile(found, type, tagLookup)
86 93
     })
87 94
 }
88
-const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) =>
89
-    profilesEntries.map(entry => _makeCompleteProfile(entry, type, tagLookup))
95
+
96
+/**
97
+ * Get complete profiles from dB rows
98
+ * @param {Array} profilesEntries
99
+ * @param {String} type
100
+ * @param {Object} tagLookup
101
+ * @returns {Array}
102
+ */
103
+const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
104
+    return profilesEntries.map(entry =>
105
+        _makeCompleteProfile(entry, type, tagLookup),
106
+    )
107
+}
90 108
 
91 109
 module.exports = {
92 110
     CompleteProfile,

Loading…
Cancelar
Guardar