Просмотр исходного кода

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

tags/0.0.1
J 4 лет назад
Родитель
Сommit
4565783008

+ 18
- 0
backend/lib/models/grouping.js Просмотреть файл

1
 const Schwifty = require('@hapipal/schwifty')
1
 const Schwifty = require('@hapipal/schwifty')
2
 const groupingSchema = require('../schemas/groupings')
2
 const groupingSchema = require('../schemas/groupings')
3
+const User = require('./user')
4
+const Profile = require('./profile')
3
 
5
 
4
 module.exports = class Grouping extends Schwifty.Model {
6
 module.exports = class Grouping extends Schwifty.Model {
5
     static get tableName() {
7
     static get tableName() {
6
         return 'groupings'
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
     static get joiSchema() {
26
     static get joiSchema() {
9
         return groupingSchema.single
27
         return groupingSchema.single
10
     }
28
     }

+ 28
- 4
backend/lib/routes/membership/active.js Просмотреть файл

29
 
29
 
30
 const responseSchemas = {
30
 const responseSchemas = {
31
     single: groupingSchema.single,
31
     single: groupingSchema.single,
32
-    list: groupingSchema.list,
32
+    list: groupingSchema.listWithProfiles,
33
     error: errorSchema.single
33
     error: errorSchema.single
34
 }
34
 }
35
 
35
 
43
         auth: false,
43
         auth: false,
44
         cors: true,
44
         cors: true,
45
         handler: async function (request, h) {
45
         handler: async function (request, h) {
46
-            const { membershipService } = request.services()
46
+            const { membershipService, profileService } = request.server.services()
47
             const membershipType = request.query.type
47
             const membershipType = request.query.type
48
 
48
 
49
             const profileId = request.params.profile_id
49
             const profileId = request.params.profile_id
50
-            const groupings = await membershipService.findGroupingsByProfileId(
50
+            let groupings = await membershipService.findGroupingsByProfileId(
51
                 profileId,
51
                 profileId,
52
                 membershipType
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
             try {
78
             try {
55
                 return {
79
                 return {
56
                     ok: true,
80
                     ok: true,
57
                     handler: pluginConfig.handlerType,
81
                     handler: pluginConfig.handlerType,
58
-                    data: groupings,
82
+                    data: reformattedGroupings
59
                 }
83
                 }
60
             } catch (err) {
84
             } catch (err) {
61
                 return {
85
                 return {

+ 2
- 2
backend/lib/routes/profile/queue.js Просмотреть файл

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

+ 10
- 1
backend/lib/schemas/groupings.js Просмотреть файл

8
     grouping_type: Joi.string(),
8
     grouping_type: Joi.string(),
9
 }).label('grouping_single')
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
 module.exports = {
18
 module.exports = {
12
     single: singleGrouping,
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
         } else if(active == 'any') {
28
         } else if(active == 'any') {
29
             allMemberships = await Membership.query()
29
             allMemberships = await Membership.query()
30
                 .where({ profile_id: profileId })
30
                 .where({ profile_id: profileId })
31
-        } else {
32
-            allMemberships = await Membership.query()
31
+            } else {
32
+                allMemberships = await Membership.query()
33
                 .where({ profile_id: profileId })
33
                 .where({ profile_id: profileId })
34
                 .where({ is_active: true })
34
                 .where({ is_active: true })
35
         }
35
         }
86
         /** Grab just the Groupings this id has a Membership for */
86
         /** Grab just the Groupings this id has a Membership for */
87
         return await Grouping.query()
87
         return await Grouping.query()
88
             .whereIn('grouping_id', dedupedGroupings)
88
             .whereIn('grouping_id', dedupedGroupings)
89
+            .withGraphFetched('profiles')
89
     }
90
     }
90
 
91
 
91
     async _groupingIdsInCommon(profileId, targetId) {
92
     async _groupingIdsInCommon(profileId, targetId) {

+ 10
- 4
backend/lib/services/profile.js Просмотреть файл

113
         })
113
         })
114
     }
114
     }
115
 
115
 
116
-    async getProfilesFor(profileIdArray, type) {
116
+    async getProfilesFor(profileIdArray, type, includeResponses = true) {
117
         const { Profile } = this.server.models()
117
         const { Profile } = this.server.models()
118
         // profilesEntries is profiles in database row order 
118
         // profilesEntries is profiles in database row order 
119
-        const profilesEntries = await Profile.query()
119
+        const profilesEntries = includeResponses ? await Profile.query()
120
             .whereIn('profile_id', profileIdArray)
120
             .whereIn('profile_id', profileIdArray)
121
             .withGraphFetched('responses')
121
             .withGraphFetched('responses')
122
-            .withGraphFetched('user')
122
+            .withGraphFetched('user') : await Profile.query()
123
+            .whereIn('profile_id', profileIdArray)
124
+            .withGraphFetched('user') 
123
 
125
 
124
         // taking the info from profilesEntries
126
         // taking the info from profilesEntries
125
         // to repack into completeProfiles
127
         // to repack into completeProfiles
128
         profileIdArray.forEach(pid => {
130
         profileIdArray.forEach(pid => {
129
             profilesEntries.forEach(entry => {
131
             profilesEntries.forEach(entry => {
130
                 if (entry.profile_id == pid) {
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
         })  

Загрузка…
Отмена
Сохранить