Просмотр исходного кода

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

tags/0.0.1
J 4 лет назад
Родитель
Сommit
30ce46940d
2 измененных файлов: 105 добавлений и 26 удалений
  1. 55
    14
      backend/lib/routes/profile/patch-queue.js
  2. 50
    12
      backend/lib/routes/profile/queue.js

+ 55
- 14
backend/lib/routes/profile/patch-queue.js Просмотреть файл

@@ -10,9 +10,27 @@ const pluginConfig = {
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 31
 const validators = {
14 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 36
 module.exports = {
@@ -25,14 +43,32 @@ module.exports = {
25 43
         auth: false,
26 44
         handler: async function (request, h) {
27 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 49
                 profile_id,
33 50
                 target_id,
34 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 73
         /** Validate based on validators object */
38 74
         validate: {
@@ -40,15 +76,20 @@ module.exports = {
40 76
             failAction: 'log',
41 77
         },
42 78
 
43
-        // couldn't get validate server response working...
44
-
45 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,6 +10,23 @@ const pluginConfig = {
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 30
 const validators = {
14 31
     params: Joi.object({ profile_id: Joi.number() }),
15 32
     query: Joi.object({ include_profile: Joi.bool() }),
@@ -31,9 +48,25 @@ module.exports = {
31 48
 
32 49
             const queue = await matchQueueService.getQueue(profile_id)
33 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 71
         /** Validate based on validators object */
39 72
         validate: {
@@ -41,15 +74,20 @@ module.exports = {
41 74
             failAction: 'log',
42 75
         },
43 76
 
44
-        // couldn't get validate server response working...
45
-
46 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
 }

Загрузка…
Отмена
Сохранить