Sfoglia il codice sorgente

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

tags/0.0.4
juancarbajal98 2 anni fa
parent
commit
f4af0e4376

+ 5
- 0
backend/lib/index.js Vedi File

4
 const ProfilePlugin = require('./plugins/profile')
4
 const ProfilePlugin = require('./plugins/profile')
5
 const NotificationPlugin = require('./plugins/notification')
5
 const NotificationPlugin = require('./plugins/notification')
6
 const HealthPlugin = require('./plugins/health')
6
 const HealthPlugin = require('./plugins/health')
7
+const FilterPlugin = require('./plugins/filter')
7
 
8
 
8
 /**
9
 /**
9
  * A Hapi server instance
10
  * A Hapi server instance
50
         await server.register(HealthPlugin, {
51
         await server.register(HealthPlugin, {
51
             routes: { prefix: '/health' },
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 Vedi File

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 Vedi File

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 Vedi File

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 Vedi File

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

+ 3
- 3
backend/lib/services/profile/index.js Vedi File

275
             distanceUnit,
275
             distanceUnit,
276
             userZip,
276
             userZip,
277
         )
277
         )
278
-        matchPool = filter.byMaxDistance(matchPool, maxDistance)
278
+        matchPool = filter.byDistance(matchPool, maxDistance)
279
         matchPool = filter.byDuration(matchPool, duration)
279
         matchPool = filter.byDuration(matchPool, duration)
280
         matchPool = filter.byPresence(matchPool, presence)
280
         matchPool = filter.byPresence(matchPool, presence)
281
         matchPool = filter.byCertifications(matchPool, certifications)
281
         matchPool = filter.byCertifications(matchPool, certifications)
355
         await this._setTagLookup()
355
         await this._setTagLookup()
356
         let associations = groupingId
356
         let associations = groupingId
357
             ? await TagAssociation.query()
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
             : await TagAssociation.query().andWhere('profile_id', profileId)
360
             : await TagAssociation.query().andWhere('profile_id', profileId)
361
         return associations
361
         return associations
362
             .map(assoc => ({
362
             .map(assoc => ({

Loading…
Annulla
Salva