Przeglądaj źródła

:recycle: more refactoring match queue | saving queue from scoring

tags/0.0.1
j 4 lat temu
rodzic
commit
c8b8d25918

+ 0
- 5
backend/lib/index.js Wyświetl plik

2
 const MembershipPlugin = require('./plugins/membership')
2
 const MembershipPlugin = require('./plugins/membership')
3
 const SurveyPlugin = require('./plugins/survey')
3
 const SurveyPlugin = require('./plugins/survey')
4
 const ProfilePlugin = require('./plugins/profile')
4
 const ProfilePlugin = require('./plugins/profile')
5
-const MatchQueuePlugin = require('./plugins/matchqueue')
6
 
5
 
7
 /**
6
 /**
8
  * A Hapi server instance
7
  * A Hapi server instance
28
             routes: { prefix: '/user' },
27
             routes: { prefix: '/user' },
29
         })
28
         })
30
 
29
 
31
-        await server.register(MatchQueuePlugin, {
32
-            routes: { prefix: '/matchqueue' },
33
-        })
34
-
35
         await server.register(MembershipPlugin, {
30
         await server.register(MembershipPlugin, {
36
             routes: { prefix: '/membership' },
31
             routes: { prefix: '/membership' },
37
         })
32
         })

+ 0
- 28
backend/lib/plugins/matchqueue.js Wyświetl plik

1
-const Objection = require('objection')
2
-const Schmervice = require('@hapipal/schmervice')
3
-const Schwifty = require('@hapipal/schwifty')
4
-
5
-const MatchQueueService = require('../services/matchqueue')
6
-
7
-const MatchQueueModel = require('../models/matchqueue')
8
-const MatchQueueChooseRoute = require('../routes/matchqueue/choosematch')
9
-
10
-module.exports = {
11
-    name: 'matchqueue-plugin',
12
-    version: '1.0.0',
13
-    register: async (server, options) => {
14
-        await server.register(Schwifty)
15
-
16
-        await server.registerModel(MatchQueueModel)
17
-
18
-        server.bind({
19
-            transaction: fn => Objection.transaction(server.knex(), fn),
20
-        })
21
-
22
-        await server.register(Schmervice)
23
-
24
-        server.registerService(MatchQueueService)
25
-
26
-        await server.route(MatchQueueChooseRoute)
27
-    },
28
-}

+ 7
- 1
backend/lib/plugins/profile.js Wyświetl plik

4
 const ProfileModel = require('../models/profile')
4
 const ProfileModel = require('../models/profile')
5
 const ResponseModel = require('../models/response')
5
 const ResponseModel = require('../models/response')
6
 const ZipCodeModel = require('../models/zip-code')
6
 const ZipCodeModel = require('../models/zip-code')
7
+const MatchQueueModel = require('../models/matchqueue')
7
 
8
 
8
 const ProfileService = require('../services/profile')
9
 const ProfileService = require('../services/profile')
10
+const MatchQueueService = require('../services/matchqueue')
9
 
11
 
10
 const ProfileScoreRoute = require('../routes/profile/score')
12
 const ProfileScoreRoute = require('../routes/profile/score')
11
 const ProfileUpdateRoute = require('../routes/profile/update')
13
 const ProfileUpdateRoute = require('../routes/profile/update')
12
 const ProfileRespondRoute = require('../routes/profile/respond')
14
 const ProfileRespondRoute = require('../routes/profile/respond')
13
 const ProfileMatchRoute = require('../routes/profile/match')
15
 const ProfileMatchRoute = require('../routes/profile/match')
14
 const ProfileQueueRoute = require('../routes/profile/queue')
16
 const ProfileQueueRoute = require('../routes/profile/queue')
17
+const ProfilePatchQueueRoute = require('../routes/profile/patch-queue')
15
 
18
 
16
 module.exports = {
19
 module.exports = {
17
     name: 'profile-plugin',
20
     name: 'profile-plugin',
20
         await server.registerModel(ProfileModel)
23
         await server.registerModel(ProfileModel)
21
         await server.registerModel(ResponseModel)
24
         await server.registerModel(ResponseModel)
22
         await server.registerModel(ZipCodeModel)
25
         await server.registerModel(ZipCodeModel)
26
+        await server.registerModel(MatchQueueModel)
23
 
27
 
24
         // Bind to global context
28
         // Bind to global context
25
         // So we can use Objection transactions
29
         // So we can use Objection transactions
28
         })
32
         })
29
 
33
 
30
         await server.register(Schmervice)
34
         await server.register(Schmervice)
31
-        server.registerService(ProfileService)
35
+        await server.registerService(ProfileService)
36
+        await server.registerService(MatchQueueService)
32
 
37
 
33
         await server.route(ProfileScoreRoute)
38
         await server.route(ProfileScoreRoute)
34
         await server.route(ProfileRespondRoute)
39
         await server.route(ProfileRespondRoute)
35
         await server.route(ProfileUpdateRoute)
40
         await server.route(ProfileUpdateRoute)
36
         await server.route(ProfileMatchRoute)
41
         await server.route(ProfileMatchRoute)
37
         await server.route(ProfileQueueRoute)
42
         await server.route(ProfileQueueRoute)
43
+        await server.route(ProfilePatchQueueRoute)
38
     },
44
     },
39
 }
45
 }

+ 0
- 67
backend/lib/routes/matchqueue/choosematch.js Wyświetl plik

1
-'use strict'
2
-
3
-const Joi = require('joi')
4
-
5
-const pluginConfig = {
6
-    handlerType: 'matchque',
7
-    docs: {
8
-        description: 'updates matchque',
9
-        notes: 'Updates MatchQue Table if User chooses to match with another or chooses to delete match',
10
-    },
11
-}
12
-
13
-const validators = {
14
-    //     /** Validate the header (cookie check) */
15
-    //     // headers: true,
16
-
17
-    //     /** Validate the route params (/active/{thing}) */
18
-    params: Joi.object({ profile_id: Joi.number() }),
19
-
20
-    //     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
21
-    //     // query: true,
22
-
23
-    //     /** Validate the incoming payload (POST method) */
24
-
25
-    payload: Joi.object({
26
-        profile_id_2: Joi.number(),
27
-        reinsert: Joi.boolean(),
28
-    }),
29
-}
30
-
31
-module.exports = {
32
-    method: 'POST',
33
-    path: '/{profile_id}/choose',
34
-    options: {
35
-        ...pluginConfig.docs,
36
-        tags: ['api'],
37
-        /** Protect this route with authentication? */
38
-        auth: false,
39
-        handler: async function (request, h) {
40
-            const { profile_id } = request.params
41
-            const { profile_id_2, reinsert } = request.payload
42
-            const { matchQueService } = request.server.services()
43
-
44
-            return await matchQueService.markAsDeleted(
45
-                profile_id,
46
-                profile_id_2,
47
-                reinsert,
48
-            )
49
-        },
50
-        /** Validate based on validators object */
51
-        validate: {
52
-            ...validators,
53
-            failAction: 'log',
54
-        },
55
-
56
-        // couldn't get validate server response working...
57
-
58
-        /** Validate the server response */
59
-        // response: {
60
-        //     schema: Joi.object({
61
-        //         ok: Joi.bool(),
62
-        //         handler: Joi.string(),
63
-        //         data: Joi.object(),
64
-        //     }),
65
-        // },
66
-    },
67
-}

+ 54
- 0
backend/lib/routes/profile/patch-queue.js Wyświetl plik

1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+
5
+const pluginConfig = {
6
+    handlerType: 'profile',
7
+    docs: {
8
+        description: 'Updates match queue in place',
9
+        notes: 'Updates in place and does not delete from table',
10
+    },
11
+}
12
+
13
+const validators = {
14
+    params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
15
+    query: Joi.object({ reinsert: Joi.boolean() }),
16
+}
17
+
18
+module.exports = {
19
+    method: 'PATCH',
20
+    path: '/{profile_id}/queue/{target_id}/delete',
21
+    options: {
22
+        ...pluginConfig.docs,
23
+        tags: ['api'],
24
+        /** Protect this route with authentication? */
25
+        auth: false,
26
+        handler: async function (request, h) {
27
+            const { profile_id, target_id } = request.params
28
+            const { reinsert } = request.query
29
+            const { matchQueueService } = request.server.services()
30
+
31
+            return await matchQueueService.markAsDeleted(
32
+                profile_id,
33
+                target_id,
34
+                reinsert,
35
+            )
36
+        },
37
+        /** Validate based on validators object */
38
+        validate: {
39
+            ...validators,
40
+            failAction: 'log',
41
+        },
42
+
43
+        // couldn't get validate server response working...
44
+
45
+        /** Validate the server response */
46
+        // response: {
47
+        //     schema: Joi.object({
48
+        //         ok: Joi.bool(),
49
+        //         handler: Joi.string(),
50
+        //         data: Joi.object(),
51
+        //     }),
52
+        // },
53
+    },
54
+}

+ 9
- 2
backend/lib/routes/profile/queue.js Wyświetl plik

12
 
12
 
13
 const validators = {
13
 const validators = {
14
     params: Joi.object({ profile_id: Joi.number() }),
14
     params: Joi.object({ profile_id: Joi.number() }),
15
+    query: Joi.object({ include_profile: Joi.bool() }),
15
 }
16
 }
16
 
17
 
17
 module.exports = {
18
 module.exports = {
24
         auth: false,
25
         auth: false,
25
         handler: async function (request, h) {
26
         handler: async function (request, h) {
26
             const { profile_id } = request.params
27
             const { profile_id } = request.params
27
-            const { matchQueueService } = request.server.services()
28
+            const { include_profile } = request.query
29
+            const { profileService, matchQueueService } =
30
+                request.server.services()
28
 
31
 
29
-            return await matchQueueService.getPotentials(profile_id)
32
+            const queue = await matchQueueService.getQueue(profile_id)
33
+            const queueIds = queue.map(entry => entry.target_id)
34
+            return include_profile
35
+                ? profileService.getProfilesFor(queueIds)
36
+                : queueIds
30
         },
37
         },
31
         /** Validate based on validators object */
38
         /** Validate based on validators object */
32
         validate: {
39
         validate: {

+ 5
- 5
backend/lib/routes/profile/score.js Wyświetl plik

53
             const distanceUnit = request.query.unit
53
             const distanceUnit = request.query.unit
54
                 ? request.query.unit
54
                 ? request.query.unit
55
                 : 'mile'
55
                 : 'mile'
56
-            const profiles = await profileService.scoreProfilesFor(
56
+            const scoredProfiles = await profileService.scoreProfilesFor(
57
                 profileId,
57
                 profileId,
58
                 maxDistanceMiles,
58
                 maxDistanceMiles,
59
                 distanceUnit,
59
                 distanceUnit,
60
             )
60
             )
61
-            await matchQueueService.insertScoredProfilesIntoMatchQueue(
61
+            await matchQueueService.saveMatchQueue(
62
                 profileId,
62
                 profileId,
63
-                profiles.map(profile => profile.profile_id),
63
+                scoredProfiles.map(profile => profile.profile_id),
64
             )
64
             )
65
             try {
65
             try {
66
-                if (!profiles) {
66
+                if (!scoredProfiles) {
67
                     throw new RangeError('Unable to score profiles')
67
                     throw new RangeError('Unable to score profiles')
68
                 }
68
                 }
69
 
69
 
71
                     .response({
71
                     .response({
72
                         ok: true,
72
                         ok: true,
73
                         handler: pluginConfig.handlerType,
73
                         handler: pluginConfig.handlerType,
74
-                        data: profiles,
74
+                        data: scoredProfiles,
75
                     })
75
                     })
76
                     .code(200)
76
                     .code(200)
77
             } catch (err) {
77
             } catch (err) {

+ 11
- 12
backend/lib/services/matchqueue.js Wyświetl plik

5
         super(...args)
5
         super(...args)
6
     }
6
     }
7
 
7
 
8
-    async getPotentials(profileId) {
8
+    async getQueue(profileId) {
9
         const { MatchQueue } = this.server.models()
9
         const { MatchQueue } = this.server.models()
10
-        const allPotentials = await MatchQueue.query()
10
+        return await MatchQueue.query()
11
             .where('profile_id', profileId)
11
             .where('profile_id', profileId)
12
             .andWhere('is_deleted', false)
12
             .andWhere('is_deleted', false)
13
-        return allPotentials
14
     }
13
     }
15
     /**
14
     /**
16
      * Saves Scored Profile Ids to MatchQue IN ORDER
15
      * Saves Scored Profile Ids to MatchQue IN ORDER
17
      * @param {number} profileId
16
      * @param {number} profileId
18
-     * @param {array} potentialProfileIds
17
+     * @param {array} targetIds
19
      */
18
      */
20
-    async insertScoredProfilesIntoMatchQueue(profileId, potentialProfileIds) {
19
+    async saveMatchQueue(profileId, targetIds) {
21
         const { MatchQueue } = this.server.models()
20
         const { MatchQueue } = this.server.models()
22
 
21
 
23
-        // returns an array of all matches for the profileId where the profile_id_2 already exists in the potentialProfileIds array
22
+        // returns an array of all matches for the profileId where the target_id already exists in the targetIds array
24
         await MatchQueue.query()
23
         await MatchQueue.query()
25
             .patch({
24
             .patch({
26
                 is_deleted: true,
25
                 is_deleted: true,
27
             })
26
             })
28
             .where('profile_id', profileId)
27
             .where('profile_id', profileId)
29
 
28
 
30
-        for (let potentialProfileId of potentialProfileIds) {
29
+        for (let id of targetIds) {
31
             await MatchQueue.query().insert({
30
             await MatchQueue.query().insert({
32
                 profile_id: profileId,
31
                 profile_id: profileId,
33
-                target_id: potentialProfileId,
32
+                target_id: id,
34
                 is_deleted: false,
33
                 is_deleted: false,
35
             })
34
             })
36
         }
35
         }
37
 
36
 
38
-        return await this.getPotentials(profileId)
37
+        return await this.getQueue(profileId)
39
     }
38
     }
40
     /**
39
     /**
41
      * Set the rows deleted as true, does NOT DELETE from database
40
      * Set the rows deleted as true, does NOT DELETE from database
48
         const { MatchQueue } = this.server.models()
47
         const { MatchQueue } = this.server.models()
49
         await MatchQueue.query()
48
         await MatchQueue.query()
50
             .patch({
49
             .patch({
51
-                deleted: true,
50
+                is_deleted: true,
52
             })
51
             })
53
             .where('profile_id', profileId)
52
             .where('profile_id', profileId)
54
             .andWhere('target_id', targetId)
53
             .andWhere('target_id', targetId)
58
             await MatchQueue.query().insert({
57
             await MatchQueue.query().insert({
59
                 profile_id: profileId,
58
                 profile_id: profileId,
60
                 target_id: targetId,
59
                 target_id: targetId,
61
-                deleted: false,
60
+                is_deleted: false,
62
             })
61
             })
63
         }
62
         }
64
 
63
 
65
-        return await this.getPotentials(profileId)
64
+        return await this.getQueue(profileId)
66
     }
65
     }
67
 }
66
 }

+ 12
- 0
backend/lib/services/profile.js Wyświetl plik

188
         })
188
         })
189
     }
189
     }
190
 
190
 
191
+    async getProfilesFor(profileIdArray, type) {
192
+        const { Profile } = this.server.models()
193
+
194
+        const profilesEntries = await Profile.query()
195
+            .whereIn('profile_id', profileIdArray)
196
+            .withGraphFetched('responses')
197
+
198
+        return profilesEntries.map(profile => {
199
+            return new CompleteProfile(profile, type)
200
+        })
201
+    }
202
+
191
     /**
203
     /**
192
      * Save responses in a profile
204
      * Save responses in a profile
193
      * @param {number} userId
205
      * @param {number} userId

Ładowanie…
Anuluj
Zapisz