ソースを参照

:sparkles: reformatted /api/membership return to include complete profiles with grouping

tags/0.0.1
J 4年前
コミット
4565783008

+ 18
- 0
backend/lib/models/grouping.js ファイルの表示

@@ -1,10 +1,28 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2 2
 const groupingSchema = require('../schemas/groupings')
3
+const User = require('./user')
4
+const Profile = require('./profile')
3 5
 
4 6
 module.exports = class Grouping extends Schwifty.Model {
5 7
     static get tableName() {
6 8
         return 'groupings'
7 9
     }
10
+    static get relationMappings() {
11
+        return {
12
+            profiles: {
13
+                relation: Schwifty.Model.ManyToManyRelation,
14
+                modelClass: Profile,
15
+                join: {
16
+                    from: 'groupings.grouping_id',
17
+                    through: {
18
+                        from: 'memberships.grouping_id',
19
+                        to: 'memberships.profile_id'
20
+                    },
21
+                    to: 'profiles.profile_id'
22
+                },
23
+            },
24
+        }
25
+    }
8 26
     static get joiSchema() {
9 27
         return groupingSchema.single
10 28
     }

+ 28
- 4
backend/lib/routes/membership/active.js ファイルの表示

@@ -29,7 +29,7 @@ const validators = {
29 29
 
30 30
 const responseSchemas = {
31 31
     single: groupingSchema.single,
32
-    list: groupingSchema.list,
32
+    list: groupingSchema.listWithProfiles,
33 33
     error: errorSchema.single
34 34
 }
35 35
 
@@ -43,19 +43,43 @@ module.exports = {
43 43
         auth: false,
44 44
         cors: true,
45 45
         handler: async function (request, h) {
46
-            const { membershipService } = request.services()
46
+            const { membershipService, profileService } = request.server.services()
47 47
             const membershipType = request.query.type
48 48
 
49 49
             const profileId = request.params.profile_id
50
-            const groupings = await membershipService.findGroupingsByProfileId(
50
+            let groupings = await membershipService.findGroupingsByProfileId(
51 51
                 profileId,
52 52
                 membershipType
53 53
             )
54
+            
55
+            /**
56
+             * Heavily process the result by storing just a profile_id
57
+             * and attach complete profiles
58
+             */
59
+            let pIds = groupings.reduce((ids, grouping) => {
60
+                grouping.profiles.forEach(p => { 
61
+                    if(p.profile_id == profileId) return
62
+                    ids.push(p.profile_id)
63
+                    grouping.profile = p.profile_id
64
+                })
65
+                delete grouping.profiles
66
+                return ids
67
+            }, [])
68
+            
69
+            /** Assemble complete profiles to reference and pass */
70
+            const completedProfiles = await profileService.getProfilesFor(pIds, 'participant', false)
71
+            const reformattedGroupings = groupings.map(g => {
72
+                completedProfiles.forEach(p => {
73
+                    g.profile = g.profile == p.profile_id ? p : g.profile
74
+                })
75
+                return g
76
+            })
77
+
54 78
             try {
55 79
                 return {
56 80
                     ok: true,
57 81
                     handler: pluginConfig.handlerType,
58
-                    data: groupings,
82
+                    data: reformattedGroupings
59 83
                 }
60 84
             } catch (err) {
61 85
                 return {

+ 2
- 2
backend/lib/routes/profile/queue.js ファイルの表示

@@ -68,13 +68,13 @@ module.exports = {
68 68
             //     await profileService.getProfilesFor(queueIds),
69 69
             // )
70 70
             if(include_profile) {
71
-                res.data = await profileService.getProfilesFor(queueIds)
71
+                res.data = await profileService.getProfilesFor(queueIds, 'participant', false)
72 72
             }
73 73
             try {
74 74
                 return h.response(res).code(200)
75 75
             } catch (err) {
76 76
                 return h.response({
77
-                    ok:false,
77
+                    ok: false,
78 78
                     handler: pluginConfig.handlerType,
79 79
                     data: { error: `${err}`}
80 80
                 }).code(409)

+ 10
- 1
backend/lib/schemas/groupings.js ファイルの表示

@@ -8,7 +8,16 @@ const singleGrouping = Joi.object({
8 8
     grouping_type: Joi.string(),
9 9
 }).label('grouping_single')
10 10
 
11
+const singleWithProfile = Joi.object({
12
+    grouping_id: Joi.number(),
13
+    grouping_name: Joi.string(),
14
+    grouping_type: Joi.string(),
15
+    profile: Joi.object()
16
+}).label('grouping_single_with_profile')
17
+
11 18
 module.exports = {
12 19
     single: singleGrouping,
13
-    list: Joi.array().items(singleGrouping).label('grouping_list')
20
+    singleWithProfile,
21
+    list: Joi.array().items(singleGrouping).label('grouping_list'),
22
+    listWithProfiles: Joi.array().items(singleWithProfile).label('grouping_list_with_profiles')
14 23
 }

+ 3
- 2
backend/lib/services/membership.js ファイルの表示

@@ -28,8 +28,8 @@ module.exports = class MembershipService extends Schmervice.Service {
28 28
         } else if(active == 'any') {
29 29
             allMemberships = await Membership.query()
30 30
                 .where({ profile_id: profileId })
31
-        } else {
32
-            allMemberships = await Membership.query()
31
+            } else {
32
+                allMemberships = await Membership.query()
33 33
                 .where({ profile_id: profileId })
34 34
                 .where({ is_active: true })
35 35
         }
@@ -86,6 +86,7 @@ module.exports = class MembershipService extends Schmervice.Service {
86 86
         /** Grab just the Groupings this id has a Membership for */
87 87
         return await Grouping.query()
88 88
             .whereIn('grouping_id', dedupedGroupings)
89
+            .withGraphFetched('profiles')
89 90
     }
90 91
 
91 92
     async _groupingIdsInCommon(profileId, targetId) {

+ 10
- 4
backend/lib/services/profile.js ファイルの表示

@@ -113,13 +113,15 @@ module.exports = class ProfileService extends Schmervice.Service {
113 113
         })
114 114
     }
115 115
 
116
-    async getProfilesFor(profileIdArray, type) {
116
+    async getProfilesFor(profileIdArray, type, includeResponses = true) {
117 117
         const { Profile } = this.server.models()
118 118
         // profilesEntries is profiles in database row order 
119
-        const profilesEntries = await Profile.query()
119
+        const profilesEntries = includeResponses ? await Profile.query()
120 120
             .whereIn('profile_id', profileIdArray)
121 121
             .withGraphFetched('responses')
122
-            .withGraphFetched('user')
122
+            .withGraphFetched('user') : await Profile.query()
123
+            .whereIn('profile_id', profileIdArray)
124
+            .withGraphFetched('user') 
123 125
 
124 126
         // taking the info from profilesEntries
125 127
         // to repack into completeProfiles
@@ -128,7 +130,11 @@ module.exports = class ProfileService extends Schmervice.Service {
128 130
         profileIdArray.forEach(pid => {
129 131
             profilesEntries.forEach(entry => {
130 132
                 if (entry.profile_id == pid) {
131
-                    completeProfiles.push(new CompleteProfile(entry, type))
133
+                    const complete = new CompleteProfile(entry, type)
134
+                    if(!includeResponses) {
135
+                        delete complete['responses']
136
+                    }
137
+                    completeProfiles.push(complete)
132 138
                 }
133 139
             })
134 140
         })  

読み込み中…
キャンセル
保存