|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+'use strict'
|
|
|
2
|
+
|
|
|
3
|
+const Joi = require('joi')
|
|
|
4
|
+
|
|
|
5
|
+const pluginConfig = {
|
|
|
6
|
+ handlerType: 'profile',
|
|
|
7
|
+ docs: {
|
|
|
8
|
+ description: 'Update profile',
|
|
|
9
|
+ notes: 'Update profile responses',
|
|
|
10
|
+ },
|
|
|
11
|
+}
|
|
|
12
|
+
|
|
|
13
|
+const responseSchemas = {
|
|
|
14
|
+ responses: Joi.array().items(
|
|
|
15
|
+ Joi.object({
|
|
|
16
|
+ response_id: Joi.number().required(),
|
|
|
17
|
+ profile_id: Joi.number().required(),
|
|
|
18
|
+ response_key_id: Joi.number().required(),
|
|
|
19
|
+ val: Joi.string().required(),
|
|
|
20
|
+ }),
|
|
|
21
|
+ ),
|
|
|
22
|
+}
|
|
|
23
|
+
|
|
|
24
|
+const validators = {
|
|
|
25
|
+ /** Validate the header (cookie check) */
|
|
|
26
|
+ // headers: true,
|
|
|
27
|
+
|
|
|
28
|
+ /** Validate the route params (/active/{thing}) */
|
|
|
29
|
+ params: Joi.object({ profile_id: Joi.number() }),
|
|
|
30
|
+
|
|
|
31
|
+ /** Validate the route query (/active/{thing}?limit=10&offset=10) */
|
|
|
32
|
+ // query: true,
|
|
|
33
|
+ /** Validate the incoming payload (POST method) */
|
|
|
34
|
+ payload: responseSchemas.responses,
|
|
|
35
|
+}
|
|
|
36
|
+
|
|
|
37
|
+module.exports = {
|
|
|
38
|
+ method: 'PATCH',
|
|
|
39
|
+ path: '/{profile_id}/update',
|
|
|
40
|
+ options: {
|
|
|
41
|
+ ...pluginConfig.docs,
|
|
|
42
|
+ tags: ['api'],
|
|
|
43
|
+ /** Protect this route with authentication? */
|
|
|
44
|
+ auth: false,
|
|
|
45
|
+
|
|
|
46
|
+ handler: async function (request) {
|
|
|
47
|
+ const { profileService } = request.services()
|
|
|
48
|
+ const profileId = request.params.profile_id
|
|
|
49
|
+
|
|
|
50
|
+ /** Grab payload info */
|
|
|
51
|
+ const res = request.payload
|
|
|
52
|
+
|
|
|
53
|
+ const updatedResponses =
|
|
|
54
|
+ await profileService.updateResponsesInProfile(profileId, res)
|
|
|
55
|
+ try {
|
|
|
56
|
+ return {
|
|
|
57
|
+ ok: true,
|
|
|
58
|
+ handler: pluginConfig.handlerType,
|
|
|
59
|
+ data: updatedResponses,
|
|
|
60
|
+ }
|
|
|
61
|
+ } catch (err) {
|
|
|
62
|
+ return {
|
|
|
63
|
+ ok: false,
|
|
|
64
|
+ handler: pluginConfig.handlerType,
|
|
|
65
|
+ data: { error: `${err}` },
|
|
|
66
|
+ }
|
|
|
67
|
+ }
|
|
|
68
|
+ },
|
|
|
69
|
+
|
|
|
70
|
+ /** Validate based on validators object */
|
|
|
71
|
+ validate: {
|
|
|
72
|
+ ...validators,
|
|
|
73
|
+ failAction: 'log',
|
|
|
74
|
+ },
|
|
|
75
|
+
|
|
|
76
|
+ /** Validate the server response */
|
|
|
77
|
+ response: {
|
|
|
78
|
+ schema: Joi.object({
|
|
|
79
|
+ ok: Joi.bool(),
|
|
|
80
|
+ handler: Joi.string(),
|
|
|
81
|
+ data: responseSchemas.responses,
|
|
|
82
|
+ }),
|
|
|
83
|
+ },
|
|
|
84
|
+ },
|
|
|
85
|
+}
|