Quellcode durchsuchen

:gears: migrating filtering logic to backend in new filter plugin

tags/0.0.4
juancarbajal98 vor 2 Jahren
Ursprung
Commit
f4af0e4376

+ 5
- 0
backend/lib/index.js Datei anzeigen

@@ -4,6 +4,7 @@ const SurveyPlugin = require('./plugins/survey')
4 4
 const ProfilePlugin = require('./plugins/profile')
5 5
 const NotificationPlugin = require('./plugins/notification')
6 6
 const HealthPlugin = require('./plugins/health')
7
+const FilterPlugin = require('./plugins/filter')
7 8
 
8 9
 /**
9 10
  * A Hapi server instance
@@ -50,5 +51,9 @@ exports.plugin = {
50 51
         await server.register(HealthPlugin, {
51 52
             routes: { prefix: '/health' },
52 53
         })
54
+
55
+        await server.register(FilterPlugin, {
56
+            routes: { prefix: '/filter' },
57
+        })
53 58
     },
54 59
 }

+ 9
- 0
backend/lib/plugins/filter.js Datei anzeigen

@@ -0,0 +1,9 @@
1
+const FilterRoute = require('../routes/filter/get')
2
+
3
+module.exports = {
4
+    name: 'filter-plugin',
5
+    version: '1.0.0',
6
+    register: async(server, options) => {
7
+        await server.route(FilterRoute)
8
+    },
9
+}

+ 80
- 0
backend/lib/routes/filter/get.js Datei anzeigen

@@ -0,0 +1,80 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+const apiSchema = require('../../schemas/api')
5
+const errorSchema = require('../../schemas/errors')
6
+const filterSchema = require('../../schemas/filter')
7
+const profileSchema = require('../../schemas/profiles')
8
+
9
+const pluginConfig = {
10
+    handlerType: 'filter',
11
+    docs: {
12
+        description: 'Filter match pool',
13
+        notes: 'Returns filtered subset of match pool'
14
+    }
15
+}
16
+
17
+const validators = {
18
+    query: Joi.object({
19
+        match_pool: Joi.array().items(profileSchema.single),
20
+        distance: Joi.string(),
21
+        presence: Joi.string()
22
+    })
23
+}
24
+
25
+const responseSchemas = {
26
+    filteredMatchPool: filterSchema.matchPool, // array of profiles
27
+    error: errorSchema.single
28
+}
29
+
30
+module.exports = {
31
+    method: 'GET',
32
+    path: '/',
33
+    options:{
34
+        ...pluginConfig.docs,
35
+        tags: ['api'],
36
+        auth: false,
37
+        cors: true,
38
+        handler: async function (request, h) {
39
+            const { filterService } = request.server.services()
40
+            let matchPool = request.query.match_pool
41
+            matchPool = filterService.byDistance(matchPool, request.query.distance)
42
+            matchPool = filterService.byPresence(matchPool, request.query.presence)
43
+      
44
+            try {
45
+                return h.response(({
46
+                    ok:true,
47
+                    handler: pluginConfig.handlerType,
48
+                    data: matchPool
49
+                })).code(200)
50
+            } catch (err) {
51
+                return h
52
+                    .response({
53
+                        ok: false,
54
+                        handler: pluginConfig.handlerType,
55
+                        data: {error: `${err}`}
56
+                    })
57
+                    .code(409)
58
+            }
59
+        },
60
+        validate: {
61
+            ...validators,
62
+            failAction: 'log'
63
+        },
64
+
65
+        response: {
66
+            status: {
67
+                200: apiSchema.single
68
+                    .append({
69
+                        data: responseSchemas.filteredMatchPool,
70
+                    })
71
+                    .label('api_single_res'),
72
+                409: apiSchema.single
73
+                    .append({
74
+                        data: responseSchemas.error,
75
+                    })
76
+                    .label('error_single_res'),
77
+            },
78
+        },
79
+    },
80
+}

+ 8
- 0
backend/lib/schemas/filter.js Datei anzeigen

@@ -0,0 +1,8 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+const profileSchema = require('./profiles')
5
+
6
+module.exports = { 
7
+    matchPool: Joi.array().items(profileSchema.single).label('matchPool')
8
+}

+ 2
- 2
backend/lib/services/filter.js Datei anzeigen

@@ -9,7 +9,7 @@ const byNullZip = (profileList) => {
9 9
         return zipcoder.getZipCodeFromProfile(profile) ? true : false
10 10
     })
11 11
 }
12
-const byMaxDistance = (profileList, max) => {
12
+const byDistance = (profileList, max) => {
13 13
     return profileList.filter(profile => {
14 14
         const profileDistance = Math.floor(parseFloat(profile.distance) * 100)
15 15
         const adjustedMaxDistance = Math.floor(parseFloat(max) * 100)
@@ -41,7 +41,7 @@ const byCertifications = (profileList, certifications) => {
41 41
 module.exports = {
42 42
     byProfileType,
43 43
     byNullZip,
44
-    byMaxDistance,
44
+    byDistance,
45 45
     byDuration,
46 46
     byPresence,
47 47
     byCertifications,

+ 3
- 3
backend/lib/services/profile/index.js Datei anzeigen

@@ -275,7 +275,7 @@ module.exports = class ProfileService extends Schmervice.Service {
275 275
             distanceUnit,
276 276
             userZip,
277 277
         )
278
-        matchPool = filter.byMaxDistance(matchPool, maxDistance)
278
+        matchPool = filter.byDistance(matchPool, maxDistance)
279 279
         matchPool = filter.byDuration(matchPool, duration)
280 280
         matchPool = filter.byPresence(matchPool, presence)
281 281
         matchPool = filter.byCertifications(matchPool, certifications)
@@ -355,8 +355,8 @@ module.exports = class ProfileService extends Schmervice.Service {
355 355
         await this._setTagLookup()
356 356
         let associations = groupingId
357 357
             ? await TagAssociation.query()
358
-                  .where('grouping_id', groupingId)
359
-                  .andWhere('profile_id', profileId)
358
+                .where('grouping_id', groupingId)
359
+                .andWhere('profile_id', profileId)
360 360
             : await TagAssociation.query().andWhere('profile_id', profileId)
361 361
         return associations
362 362
             .map(assoc => ({

Laden…
Abbrechen
Speichern