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

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

tags/0.0.1
j пре 4 година
родитељ
комит
c8b8d25918

+ 0
- 5
backend/lib/index.js Прегледај датотеку

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

+ 0
- 28
backend/lib/plugins/matchqueue.js Прегледај датотеку

@@ -1,28 +0,0 @@
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 Прегледај датотеку

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

+ 0
- 67
backend/lib/routes/matchqueue/choosematch.js Прегледај датотеку

@@ -1,67 +0,0 @@
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 Прегледај датотеку

@@ -0,0 +1,54 @@
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 Прегледај датотеку

@@ -12,6 +12,7 @@ const pluginConfig = {
12 12
 
13 13
 const validators = {
14 14
     params: Joi.object({ profile_id: Joi.number() }),
15
+    query: Joi.object({ include_profile: Joi.bool() }),
15 16
 }
16 17
 
17 18
 module.exports = {
@@ -24,9 +25,15 @@ module.exports = {
24 25
         auth: false,
25 26
         handler: async function (request, h) {
26 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 38
         /** Validate based on validators object */
32 39
         validate: {

+ 5
- 5
backend/lib/routes/profile/score.js Прегледај датотеку

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

+ 11
- 12
backend/lib/services/matchqueue.js Прегледај датотеку

@@ -5,37 +5,36 @@ module.exports = class MatchQueueService extends Schmervice.Service {
5 5
         super(...args)
6 6
     }
7 7
 
8
-    async getPotentials(profileId) {
8
+    async getQueue(profileId) {
9 9
         const { MatchQueue } = this.server.models()
10
-        const allPotentials = await MatchQueue.query()
10
+        return await MatchQueue.query()
11 11
             .where('profile_id', profileId)
12 12
             .andWhere('is_deleted', false)
13
-        return allPotentials
14 13
     }
15 14
     /**
16 15
      * Saves Scored Profile Ids to MatchQue IN ORDER
17 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 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 23
         await MatchQueue.query()
25 24
             .patch({
26 25
                 is_deleted: true,
27 26
             })
28 27
             .where('profile_id', profileId)
29 28
 
30
-        for (let potentialProfileId of potentialProfileIds) {
29
+        for (let id of targetIds) {
31 30
             await MatchQueue.query().insert({
32 31
                 profile_id: profileId,
33
-                target_id: potentialProfileId,
32
+                target_id: id,
34 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 40
      * Set the rows deleted as true, does NOT DELETE from database
@@ -48,7 +47,7 @@ module.exports = class MatchQueueService extends Schmervice.Service {
48 47
         const { MatchQueue } = this.server.models()
49 48
         await MatchQueue.query()
50 49
             .patch({
51
-                deleted: true,
50
+                is_deleted: true,
52 51
             })
53 52
             .where('profile_id', profileId)
54 53
             .andWhere('target_id', targetId)
@@ -58,10 +57,10 @@ module.exports = class MatchQueueService extends Schmervice.Service {
58 57
             await MatchQueue.query().insert({
59 58
                 profile_id: profileId,
60 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 Прегледај датотеку

@@ -188,6 +188,18 @@ module.exports = class ProfileService extends Schmervice.Service {
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 204
      * Save responses in a profile
193 205
      * @param {number} userId

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