Переглянути джерело

:sparkles: validating profile id list OR list of profiles | adjusting return format to match other routes

tags/0.0.1
J 4 роки тому
джерело
коміт
30ce46940d

+ 55
- 14
backend/lib/routes/profile/patch-queue.js Переглянути файл

10
     },
10
     },
11
 }
11
 }
12
 
12
 
13
+const responseSchemas = {
14
+    responses: Joi.array().items(
15
+        Joi.alternatives().try(
16
+            Joi.number(),
17
+            Joi.object({
18
+                profile_id: Joi.number(),
19
+                user_id: Joi.number(),
20
+                responses: Joi.array().items(),
21
+                user_type: Joi.any(),
22
+            }),
23
+        )
24
+    ),
25
+    error: Joi.object({
26
+        error: Joi.string(),
27
+    }),
28
+}
29
+
30
+
13
 const validators = {
31
 const validators = {
14
     params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
32
     params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
15
-    query: Joi.object({ reinsert: Joi.boolean() }),
33
+    query: Joi.object({ include_profile:Joi.bool(), reinsert: Joi.boolean() }),
16
 }
34
 }
17
 
35
 
18
 module.exports = {
36
 module.exports = {
25
         auth: false,
43
         auth: false,
26
         handler: async function (request, h) {
44
         handler: async function (request, h) {
27
             const { profile_id, target_id } = request.params
45
             const { profile_id, target_id } = request.params
28
-            const { reinsert } = request.query
29
-            const { matchQueueService } = request.server.services()
30
-
31
-            return await matchQueueService.markAsDeleted(
46
+            const { include_profile, reinsert } = request.query
47
+            const { profileService, matchQueueService } = request.server.services()
48
+            const updatedQueue = await matchQueueService.markAsDeleted(
32
                 profile_id,
49
                 profile_id,
33
                 target_id,
50
                 target_id,
34
                 reinsert,
51
                 reinsert,
35
             )
52
             )
53
+            const queueIds = updatedQueue.map(entry => entry.target_id)
54
+            const res =  {
55
+                ok:true,
56
+                handler: pluginConfig.handlerType,
57
+                data: queueIds
58
+            }
59
+            if(include_profile) {
60
+                res.data = await profileService.getProfilesFor(queueIds)
61
+            }
62
+            try {
63
+                return h.response(res).code(200)
64
+            } catch (err) {
65
+                return h.response({
66
+                    ok:false,
67
+                    handler: pluginConfig.handlerType,
68
+                    data: { error: `${err}`}
69
+                }).code(409)
70
+
71
+            }
36
         },
72
         },
37
         /** Validate based on validators object */
73
         /** Validate based on validators object */
38
         validate: {
74
         validate: {
40
             failAction: 'log',
76
             failAction: 'log',
41
         },
77
         },
42
 
78
 
43
-        // couldn't get validate server response working...
44
-
45
         /** Validate the server response */
79
         /** Validate the server response */
46
-        // response: {
47
-        //     schema: Joi.object({
48
-        //         ok: Joi.bool(),
49
-        //         handler: Joi.string(),
50
-        //         data: Joi.object(),
51
-        //     }),
52
-        // },
80
+        response: {
81
+            status: {
82
+                200: Joi.object({
83
+                    ok: Joi.bool(),
84
+                    handler: Joi.string(),
85
+                    data: responseSchemas.responses,
86
+                }),
87
+                409: Joi.object({
88
+                    ok: Joi.bool(),
89
+                    handler: Joi.string(),
90
+                    data: responseSchemas.error,
91
+                }),
92
+            },
93
+        },
53
     },
94
     },
54
 }
95
 }

+ 50
- 12
backend/lib/routes/profile/queue.js Переглянути файл

10
     },
10
     },
11
 }
11
 }
12
 
12
 
13
+const responseSchemas = {
14
+    responses: Joi.array().items(
15
+        Joi.alternatives().try(
16
+            Joi.number(),
17
+            Joi.object({
18
+                profile_id: Joi.number(),
19
+                user_id: Joi.number(),
20
+                responses: Joi.array().items(),
21
+                user_type: Joi.any(),
22
+            }),
23
+        )
24
+    ),
25
+    error: Joi.object({
26
+        error: Joi.string(),
27
+    }),
28
+}
29
+
13
 const validators = {
30
 const validators = {
14
     params: Joi.object({ profile_id: Joi.number() }),
31
     params: Joi.object({ profile_id: Joi.number() }),
15
     query: Joi.object({ include_profile: Joi.bool() }),
32
     query: Joi.object({ include_profile: Joi.bool() }),
31
 
48
 
32
             const queue = await matchQueueService.getQueue(profile_id)
49
             const queue = await matchQueueService.getQueue(profile_id)
33
             const queueIds = queue.map(entry => entry.target_id)
50
             const queueIds = queue.map(entry => entry.target_id)
34
-            return include_profile
35
-                ? profileService.getProfilesFor(queueIds)
36
-                : queueIds
51
+            const res =  {
52
+                ok:true,
53
+                handler: pluginConfig.handlerType,
54
+                data: queueIds
55
+            }
56
+            if(include_profile) {
57
+                res.data = await profileService.getProfilesFor(queueIds)
58
+            }
59
+            console.log(res.data)
60
+            try {
61
+                return h.response(res).code(200)
62
+            } catch (err) {
63
+                return h.response({
64
+                    ok:false,
65
+                    handler: pluginConfig.handlerType,
66
+                    data: { error: `${err}`}
67
+                }).code(409)
68
+
69
+            }
37
         },
70
         },
38
         /** Validate based on validators object */
71
         /** Validate based on validators object */
39
         validate: {
72
         validate: {
41
             failAction: 'log',
74
             failAction: 'log',
42
         },
75
         },
43
 
76
 
44
-        // couldn't get validate server response working...
45
-
46
         /** Validate the server response */
77
         /** Validate the server response */
47
-        // response: {
48
-        //     schema: Joi.object({
49
-        //         ok: Joi.bool(),
50
-        //         handler: Joi.string(),
51
-        //         data: Joi.object(),
52
-        //     }),
53
-        // },
78
+        response: {
79
+            status: {
80
+                200: Joi.object({
81
+                    ok: Joi.bool(),
82
+                    handler: Joi.string(),
83
+                    data: responseSchemas.responses,
84
+                }),
85
+                409: Joi.object({
86
+                    ok: Joi.bool(),
87
+                    handler: Joi.string(),
88
+                    data: responseSchemas.error,
89
+                }),
90
+            },
91
+        },
54
     },
92
     },
55
 }
93
 }

Завантаження…
Відмінити
Зберегти