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

:sparkles: added matchqueue service and routes

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

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

@@ -0,0 +1,21 @@
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3
+
4
+const Profile = require('./profile')
5
+// const User = require('./user')
6
+// const Membership = require('./membership')
7
+
8
+module.exports = class MatchQue extends Schwifty.Model {
9
+    static get tableName() {
10
+        return 'matchque'
11
+    }
12
+
13
+    static get joiSchema() {
14
+        return Joi.object({
15
+            match_id: Joi.number(),
16
+            profile_id: Joi.number().required(),
17
+            profile_id_2: Joi.number().required(),
18
+            deleted: Joi.boolean().required(),
19
+        })
20
+    }
21
+}

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

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

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

@@ -0,0 +1,67 @@
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
+}

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

@@ -0,0 +1,79 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+
5
+const pluginConfig = {
6
+    handlerType: 'matchque',
7
+    docs: {
8
+        description: 'inserts scored matches',
9
+        notes: 'Waits for Scoring Service, and inserts any that does not exist in MatchQue Table',
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
+        maxDistance: Joi.number().required(),
27
+        distanceUnit: Joi.string().required(),
28
+    }),
29
+}
30
+
31
+module.exports = {
32
+    method: 'POST',
33
+    path: '/{profile_id}/matches',
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 { maxDistance, distanceUnit } = request.payload
42
+            const { matchQueService, profileService } =
43
+                request.server.services()
44
+            // get results back from profileServices.scoreProfilesFor(profile_id, maxDistance, distanceUnit)
45
+            const potentials = await profileService.scoreProfilesFor(
46
+                profile_id,
47
+                maxDistance,
48
+                distanceUnit,
49
+            )
50
+            let potentialProfileIds = potentials.map(
51
+                potential => potential.profile_id,
52
+            )
53
+            potentialProfileIds = [...new Set(potentialProfileIds)]
54
+
55
+            await matchQueService.insertScoredProfilesIntoMatchQue(
56
+                profile_id,
57
+                potentialProfileIds,
58
+            )
59
+
60
+            return matchQueService.getPotentials(profile_id)
61
+        },
62
+        /** Validate based on validators object */
63
+        validate: {
64
+            ...validators,
65
+            failAction: 'log',
66
+        },
67
+
68
+        // couldn't get validate server response working...
69
+
70
+        /** Validate the server response */
71
+        // response: {
72
+        //     schema: Joi.object({
73
+        //         ok: Joi.bool(),
74
+        //         handler: Joi.string(),
75
+        //         data: Joi.object(),
76
+        //     }),
77
+        // },
78
+    },
79
+}

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

@@ -0,0 +1,67 @@
1
+const Schmervice = require('@hapipal/schmervice')
2
+
3
+module.exports = class MatchQueService extends Schmervice.Service {
4
+    constructor(...args) {
5
+        super(...args)
6
+    }
7
+
8
+    async getPotentials(profileId) {
9
+        const { MatchQue } = this.server.models()
10
+        const allPotentials = await MatchQue.query()
11
+            .where('profile_id', profileId)
12
+            .andWhere('deleted', false)
13
+        return allPotentials
14
+    }
15
+    /**
16
+     * Saves Scored Profile Ids to MatchQue IN ORDER
17
+     * @param {number} profileId
18
+     * @param {array} potentialProfileIds
19
+     */
20
+    async insertScoredProfilesIntoMatchQue(profileId, potentialProfileIds) {
21
+        const { MatchQue } = this.server.models()
22
+
23
+        // returns an array of all matches for the profileId where the profile_id_2 already exists in the potentialProfileIds array
24
+        await MatchQue.query()
25
+            .patch({
26
+                deleted: true,
27
+            })
28
+            .where('profile_id', profileId)
29
+
30
+        for (let potentialProfileId of potentialProfileIds) {
31
+            await MatchQue.query().insert({
32
+                profile_id: profileId,
33
+                profile_id_2: potentialProfileId,
34
+                deleted: false,
35
+            })
36
+        }
37
+
38
+        return await this.getPotentials(profileId)
39
+    }
40
+    /**
41
+     * Set the rows deleted as true, does NOT DELETE from database
42
+     * @param {number} profileId
43
+     * @param {number} profileId2
44
+     * @param {boolean} reinsert
45
+     * @returns
46
+     */
47
+    async markAsDeleted(profileId, profileId2, reinsert) {
48
+        const { MatchQue } = this.server.models()
49
+        await MatchQue.query()
50
+            .patch({
51
+                deleted: true,
52
+            })
53
+            .where('profile_id', profileId)
54
+            .andWhere('profile_id_2', profileId2)
55
+            .first()
56
+
57
+        if (reinsert) {
58
+            await MatchQue.query().insert({
59
+                profile_id: profileId,
60
+                profile_id_2: profileId2,
61
+                deleted: false,
62
+            })
63
+        }
64
+
65
+        return await this.getPotentials(profileId)
66
+    }
67
+}

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