Преглед изворни кода

:recycle: Greatly simplified refactor using map over reduce

tags/0.0.3^2
tomit4 пре 3 година
родитељ
комит
33be343372
1 измењених фајлова са 12 додато и 33 уклоњено
  1. 12
    33
      backend/lib/routes/membership/active.js

+ 12
- 33
backend/lib/routes/membership/active.js Прегледај датотеку

@@ -95,45 +95,24 @@ module.exports = {
95 95
                 false,
96 96
             )
97 97
 
98
-            /** Grabs the reveal tags for the target profile */
99
-            const revealTags = await groupings.reduce(
100
-                async (revealTags, grouping) => {
101
-                    for (const profile of completedProfiles) {
102
-                        revealTags = await profileService.
103
-                            getTagsFor(
104
-                                profile.profile_id,
105
-                                grouping.grouping_id,
106
-                                'reveal')
107
-                        if (revealTags.length) return revealTags
108
-                    }
109
-                }, [])
98
+            /** Grabs revealTags */
99
+            const profileIdsFromCompletedProfiles = completedProfiles.map(p => p.profile_id)
100
+            const groupingIdsFromGroupings = groupings.map(g => g.grouping_id)
101
+            const revealTags = await profileService.getTagsFor(profileIdsFromCompletedProfiles, groupingIdsFromGroupings, 'reveal')
110 102
 
111 103
             /** If the revealTags exist, the completedProfile's hidden info is
112 104
              * removed and replaced with the completedProfile's user information
113 105
              * Otherwise the completedProfiles remain unchanged
114 106
              */
115
-            completedProfiles = await completedProfiles.reduce(
116
-                async (profilesWithRevealed, profile) => {
117
-                    if (!revealTags) return completedProfiles
118
-                    const user = await userService.findById(profile.user_id)
119
-                    if (user) {
120
-                        for (const t of revealTags)
121
-                            if (t.tag.tag_description)
122
-                                profile[t.tag.tag_description] = 
123
-                                    user[t.tag.tag_description]
124
-                        profilesWithRevealed = profile
125
-                        return profilesWithRevealed = 
126
-                            profilesWithRevealed.length ? 
127
-                                profilesWithRevealed : 
128
-                                completedProfiles
129
-                    } else {
130
-                        // TODO: consider removing, throwIfNotFound used in findById
131
-                        console.error(
132
-                            '[User Service Error] no user id found for profile id >> ', 
133
-                            profile.profile_id)
134
-                        return completedProfiles
107
+            const userIdsFromCompletedProfiles = completedProfiles.map(p => p.user_id)
108
+            const user = await userService.findById(userIdsFromCompletedProfiles)
109
+            if (revealTags && user) {
110
+                for (const t of revealTags) {
111
+                    if (t.tag.tag_description) {
112
+                        completedProfiles[0][t.tag.tag_description] = user[t.tag.tag_description]
135 113
                     }
136
-                }, [])
114
+                }
115
+            }
137 116
 
138 117
             const reformattedGroupings = groupings.map(g => {
139 118
                 completedProfiles.forEach(p => {

Loading…
Откажи
Сачувај