Procházet zdrojové kódy

:sparkles: add endpoint to respond to UNANSWERED survey questions

tags/0.0.1
J před 4 roky
rodič
revize
a86e323fc6

+ 2
- 0
backend/lib/plugins/profile.js Zobrazit soubor

@@ -8,6 +8,7 @@ const ProfileService = require('../services/profile')
8 8
 
9 9
 const ProfileScoreRoute = require('../routes/profile/score')
10 10
 const ProfileUpdateRoute = require('../routes/profile/update')
11
+const ProfileRespondRoute = require('../routes/profile/respond')
11 12
 
12 13
 module.exports = {
13 14
     name: 'profile-plugin',
@@ -26,6 +27,7 @@ module.exports = {
26 27
         server.registerService(ProfileService)
27 28
 
28 29
         await server.route(ProfileScoreRoute)
30
+        await server.route(ProfileRespondRoute)
29 31
         await server.route(ProfileUpdateRoute)
30 32
     },
31 33
 }

+ 4
- 1
backend/lib/routes/membership/active.js Zobrazit soubor

@@ -18,7 +18,7 @@ const validators = {
18 18
     params: Joi.object({ profile_id: Joi.number() }),
19 19
 
20 20
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
21
-    // query: true,
21
+    query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
22 22
 
23 23
     /** Validate the incoming payload (POST method) */
24 24
     // payload: true,
@@ -43,9 +43,12 @@ module.exports = {
43 43
 
44 44
         handler: async function (request, h) {
45 45
             const { membershipService } = request.services()
46
+            const membershipType = request.query.type
47
+
46 48
             const profileId = request.params.profile_id
47 49
             const groupings = await membershipService.findGroupingsById(
48 50
                 profileId,
51
+                membershipType
49 52
             )
50 53
             try {
51 54
                 return {

+ 109
- 0
backend/lib/routes/profile/respond.js Zobrazit soubor

@@ -0,0 +1,109 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+const profile = require('../../plugins/profile')
5
+
6
+const pluginConfig = {
7
+    handlerType: 'profile',
8
+    docs: {
9
+        description: 'Update profile',
10
+        notes: 'Update profile responses',
11
+    },
12
+}
13
+
14
+const responseSchemas = {
15
+    responses: Joi.array().items(
16
+        Joi.object({
17
+            response_id: Joi.number().required(),
18
+            profile_id: Joi.number().required(),
19
+            response_key_id: Joi.number().required(),
20
+            val: Joi.string().required(),
21
+        }),
22
+    ),
23
+    error: Joi.object({
24
+        error: Joi.string(),
25
+    }),
26
+}
27
+
28
+const validators = {
29
+    /** Validate the header (cookie check) */
30
+    // headers: true,
31
+
32
+    /** Validate the route params (/active/{thing}) */
33
+    params: Joi.object({ 
34
+        profile_id: Joi.number()
35
+    }),
36
+
37
+    /** Validate the route query (/active/{thing}?limit=10&offset=10) */
38
+    query: Joi.object({ 
39
+        response_key_id: Joi.number(),
40
+        val: Joi.string()
41
+    })
42
+    /** Validate the incoming payload (POST method) */
43
+    // payload: responseSchemas.responses,
44
+}
45
+
46
+module.exports = {
47
+    method: 'POST',
48
+    path: '/{profile_id}/respond',
49
+    options: {
50
+        ...pluginConfig.docs,
51
+        tags: ['api'],
52
+        /** Protect this route with authentication? */
53
+        auth: false,
54
+
55
+        handler: async function (request, h) {
56
+            const { profileService } = request.services()
57
+            
58
+            const profileId = request.params.profile_id
59
+            const responseToSave = {
60
+                profile_id: profileId,
61
+                response_key_id: request.query.response_key_id,
62
+                val: request.query.val
63
+            }
64
+            
65
+            try {
66
+                const allResponses = await profileService.saveResponseForProfile(profileId, responseToSave)
67
+                
68
+                // profileService.saveResponseForProfile() will return null if it exists
69
+                if(!allResponses){
70
+                    throw new RangeError('Response already exists')
71
+                }
72
+
73
+                return h.response({
74
+                    ok: true,
75
+                    handler: pluginConfig.handlerType,
76
+                    data: allResponses,
77
+                }).code(201)
78
+            } catch (err) {
79
+                return h.response({
80
+                    ok: false,
81
+                    handler: pluginConfig.handlerType,
82
+                    data: { error: `${err}` },
83
+                }).code(409)
84
+            }
85
+        },
86
+
87
+        /** Validate based on validators object */
88
+        validate: {
89
+            ...validators,
90
+            failAction: 'log',
91
+        },
92
+
93
+        /** Validate the server response */
94
+        response: {
95
+            status: {
96
+                201: Joi.object({
97
+                    ok: Joi.bool(),
98
+                    handler: Joi.string(),
99
+                    data: responseSchemas.responses,
100
+                }),
101
+                409: Joi.object({
102
+                    ok: Joi.bool(),
103
+                    handler: Joi.string(),
104
+                    data: responseSchemas.error,
105
+                }),
106
+            },
107
+        },
108
+    },
109
+}

+ 18
- 6
backend/lib/routes/profile/update.js Zobrazit soubor

@@ -19,6 +19,9 @@ const responseSchemas = {
19 19
             val: Joi.string().required(),
20 20
         }),
21 21
     ),
22
+    error: Joi.object({
23
+        error: Joi.string(),
24
+    }),
22 25
 }
23 26
 
24 27
 const validators = {
@@ -36,7 +39,7 @@ const validators = {
36 39
 
37 40
 module.exports = {
38 41
     method: 'PATCH',
39
-    path: '/{profile_id}/update',
42
+    path: '/{profile_id}/update/{response_id?}',
40 43
     options: {
41 44
         ...pluginConfig.docs,
42 45
         tags: ['api'],
@@ -47,6 +50,8 @@ module.exports = {
47 50
             const { profileService } = request.services()
48 51
             const profileId = request.params.profile_id
49 52
 
53
+            const responseId = request.params.response_id ? request.params.response_id : null
54
+            
50 55
             /** Grab payload info */
51 56
             const res = request.payload
52 57
 
@@ -75,11 +80,18 @@ module.exports = {
75 80
 
76 81
         /** Validate the server response */
77 82
         response: {
78
-            schema: Joi.object({
79
-                ok: Joi.bool(),
80
-                handler: Joi.string(),
81
-                data: responseSchemas.responses,
82
-            }),
83
+            status: {
84
+                201: Joi.object({
85
+                    ok: Joi.bool(),
86
+                    handler: Joi.string(),
87
+                    data: responseSchemas.responses,
88
+                }),
89
+                500: Joi.object({
90
+                    ok: Joi.bool(),
91
+                    handler: Joi.string(),
92
+                    data: responseSchemas.error,
93
+                }),
94
+            },
83 95
         },
84 96
     },
85 97
 }

+ 2
- 2
backend/lib/routes/user/create-profile.js Zobrazit soubor

@@ -85,7 +85,7 @@ module.exports = {
85 85
                         handler: pluginConfig.handlerType,
86 86
                         data: { error: `${err}` },
87 87
                     })
88
-                    .code(500)
88
+                    .code(409)
89 89
             }
90 90
         },
91 91
 
@@ -103,7 +103,7 @@ module.exports = {
103 103
                     handler: Joi.string(),
104 104
                     data: responseSchemas.response,
105 105
                 }),
106
-                500: Joi.object({
106
+                409: Joi.object({
107 107
                     ok: Joi.bool(),
108 108
                     handler: Joi.string(),
109 109
                     data: responseSchemas.error,

+ 15
- 6
backend/lib/services/membership.js Zobrazit soubor

@@ -10,13 +10,22 @@ module.exports = class MembershipService extends Schmervice.Service {
10 10
      * @param {number} profileId
11 11
      * @returns {Array} List of all grouping_ids for user
12 12
      */
13
-    async _getGroupIdsForProfileId(profileId) {
13
+    async _getGroupIdsForProfileId(profileId, type) {
14 14
         const { Membership } = this.server.models()
15 15
 
16 16
         /** Grab every Membership associated with this id */
17
-        const allMemberships = await Membership.query()
18
-            .where({ profile_id: profileId })
19
-            .where({ is_active: true })
17
+        let allMemberships
18
+        console.log()
19
+        if(type) {
20
+            allMemberships = await Membership.query()
21
+                .where({ profile_id: profileId })
22
+                .where({ membership_type: type })
23
+                .where({ is_active: true })
24
+        } else {
25
+            allMemberships = await Membership.query()
26
+                .where({ profile_id: profileId })
27
+                .where({ is_active: true })
28
+        }
20 29
         
21 30
         /** Copy a list of the just the Groupings */
22 31
         const groupingIdsToGrab = allMemberships.map(
@@ -57,10 +66,10 @@ module.exports = class MembershipService extends Schmervice.Service {
57 66
      * @param {number} profileId
58 67
      * @returns {Array}
59 68
      */
60
-    async findGroupingsById(profileId) {
69
+    async findGroupingsById(profileId, type) {
61 70
         const { Grouping } = this.server.models()
62 71
 
63
-        const dedupedGroupings = await this._getGroupIdsForProfileId(profileId)
72
+        const dedupedGroupings = await this._getGroupIdsForProfileId(profileId, type)
64 73
 
65 74
         /** Grab just the Groupings this id has a Membership for */
66 75
         return await Grouping.query()

+ 19
- 0
backend/lib/services/profile.js Zobrazit soubor

@@ -124,6 +124,25 @@ module.exports = class ProfileService extends Schmervice.Service {
124 124
         })
125 125
     }
126 126
 
127
+    /** Add response
128
+     * @param {Object} response to save
129
+     * @returns {null} updated responses
130
+     * @returns {Array} updated responses
131
+     */
132
+    async saveResponseForProfile(profileId, responseToSave) {
133
+        const { Response } = this.server.models()
134
+        let allResponses = await Response.query().where({
135
+            profile_id: profileId,
136
+        })
137
+        const matchingResponses = allResponses.filter(response => response.response_key_id == responseToSave.response_key_id)
138
+        
139
+        // ?:Maybe bad idea
140
+        if(matchingResponses.length > 0) { return null }
141
+
142
+        await await Response.query().insert(responseToSave)
143
+        return allResponses
144
+    }
145
+
127 146
     /**
128 147
      * Delete a profile
129 148
      * @param {number} userId

Načítá se…
Zrušit
Uložit