|
|
@@ -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
|
+}
|