Procházet zdrojové kódy

:recycle: formatting all the things

master
j před 5 roky
rodič
revize
9cffe6707a

+ 1
- 1
backend/knexfile.js Zobrazit soubor

@@ -21,4 +21,4 @@ module.exports = {
21 21
             directory: './db/seeds'
22 22
         }
23 23
     },
24
-};
24
+}

+ 8
- 10
backend/lib/auth/strategies/jwt.js Zobrazit soubor

@@ -1,10 +1,10 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-module.exports = (options) => {
3
+module.exports = options => {
4 4
     return {
5 5
         keys: {
6 6
             key: options.jwtKey,
7
-            algorithms: ['HS256']
7
+            algorithms: ['HS256'],
8 8
         },
9 9
         verify: {
10 10
             aud: 'urn:audience:test',
@@ -15,16 +15,14 @@ module.exports = (options) => {
15 15
             try {
16 16
                 return {
17 17
                     isValid: true,
18
-                    credentials: { user: artifacts.decoded.payload.user }
18
+                    credentials: { user: artifacts.decoded.payload.user },
19 19
                 }
20
-            }
21
-            catch (err) {
20
+            } catch (err) {
22 21
                 console.error(err)
23 22
                 return {
24
-                    isValid: false
23
+                    isValid: false,
25 24
                 }
26 25
             }
27
-
28
-        }
26
+        },
29 27
     }
30
-}
28
+}

+ 7
- 8
backend/lib/index.js Zobrazit soubor

@@ -1,6 +1,6 @@
1
-const UserPlugin = require('./plugins/user');
2
-const MembershipPlugin = require('./plugins/membership');
3
-const TestPlugin = require('./plugins/example');
1
+const UserPlugin = require('./plugins/user')
2
+const MembershipPlugin = require('./plugins/membership')
3
+const TestPlugin = require('./plugins/example')
4 4
 
5 5
 /**
6 6
  * A Hapi server instance
@@ -22,17 +22,16 @@ exports.plugin = {
22 22
      * @param {Object} options
23 23
      */
24 24
     register: async (server, options) => {
25
-
26 25
         await server.register(TestPlugin, {
27
-            routes: { prefix: `/example` }
26
+            routes: { prefix: `/example` },
28 27
         })
29 28
 
30 29
         await server.register(UserPlugin, {
31
-            routes: { prefix: `/user` }
30
+            routes: { prefix: `/user` },
32 31
         })
33 32
 
34 33
         await server.register(MembershipPlugin, {
35
-            routes: { prefix: `/membership` }
34
+            routes: { prefix: `/membership` },
36 35
         })
37 36
     },
38
-}
37
+}

+ 5
- 3
backend/lib/models/grouping.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class Grouping extends Schwifty.Model {
5
-    static get tableName() { return 'groupings' }
5
+    static get tableName() {
6
+        return 'groupings'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             grouping_id: Joi.number(),

+ 5
- 3
backend/lib/models/membership.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class Membership extends Schwifty.Model {
5
-    static get tableName() { return 'memberships' }
5
+    static get tableName() {
6
+        return 'memberships'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             membership_id: Joi.number(),

+ 5
- 3
backend/lib/models/message.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class Message extends Schwifty.Model {
5
-    static get tableName() { return 'messages' }
5
+    static get tableName() {
6
+        return 'messages'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             message_id: Joi.number().required(),

+ 5
- 3
backend/lib/models/profile.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class Profile extends Schwifty.Model {
5
-    static get tableName() { return 'profiles' }
5
+    static get tableName() {
6
+        return 'profiles'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             profile_id: Joi.number().required(),

+ 5
- 3
backend/lib/models/response-key.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class ResponseKey extends Schwifty.Model {
5
-    static get tableName() { return 'response_keys' }
5
+    static get tableName() {
6
+        return 'response_keys'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             response_key_id: Joi.number().required(),

+ 5
- 3
backend/lib/models/response.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class Response extends Schwifty.Model {
5
-    static get tableName() { return 'responses' }
5
+    static get tableName() {
6
+        return 'responses'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             response_id: Joi.number().required(),

+ 5
- 3
backend/lib/models/user.js Zobrazit soubor

@@ -1,8 +1,10 @@
1
-const Schwifty = require('@hapipal/schwifty');
2
-const Joi = require('joi');
1
+const Schwifty = require('@hapipal/schwifty')
2
+const Joi = require('joi')
3 3
 
4 4
 module.exports = class User extends Schwifty.Model {
5
-    static get tableName() { return 'users' }
5
+    static get tableName() {
6
+        return 'users'
7
+    }
6 8
     static get joiSchema() {
7 9
         return Joi.object({
8 10
             user_id: Joi.number().required(),

+ 2
- 2
backend/lib/plugins/example.js Zobrazit soubor

@@ -12,5 +12,5 @@ module.exports = {
12 12
     register: async (server, options) => {
13 13
         // Create a route for example
14 14
         server.route(example)
15
-    }
16
-}
15
+    },
16
+}

+ 11
- 11
backend/lib/plugins/membership.js Zobrazit soubor

@@ -1,14 +1,14 @@
1
-const Objection = require('objection');
2
-const Schmervice = require('@hapipal/schmervice');
1
+const Objection = require('objection')
2
+const Schmervice = require('@hapipal/schmervice')
3 3
 
4
-const GroupingModel = require('../models/grouping');
5
-const MembershipModel = require('../models/membership');
4
+const GroupingModel = require('../models/grouping')
5
+const MembershipModel = require('../models/membership')
6 6
 
7
-const MembershipService = require('../services/membership');
7
+const MembershipService = require('../services/membership')
8 8
 
9
-const MembershipJoinRoute = require('../routes/membership/join');
10
-const MembershipLeaveRoute = require('../routes/membership/leave');
11
-const MembershipActiveRoute = require('../routes/membership/active');
9
+const MembershipJoinRoute = require('../routes/membership/join')
10
+const MembershipLeaveRoute = require('../routes/membership/leave')
11
+const MembershipActiveRoute = require('../routes/membership/active')
12 12
 
13 13
 module.exports = {
14 14
     name: 'membership-plugin',
@@ -20,7 +20,7 @@ module.exports = {
20 20
         // Bind to global context
21 21
         // So we can use Objection transactions
22 22
         server.bind({
23
-            transaction: (fn) => Objection.transaction(server.knex(), fn)
23
+            transaction: fn => Objection.transaction(server.knex(), fn),
24 24
         })
25 25
 
26 26
         await server.register(Schmervice)
@@ -29,5 +29,5 @@ module.exports = {
29 29
         await server.route(MembershipJoinRoute)
30 30
         await server.route(MembershipLeaveRoute)
31 31
         await server.route(MembershipActiveRoute)
32
-    }
33
-}
32
+    },
33
+}

+ 12
- 13
backend/lib/plugins/user.js Zobrazit soubor

@@ -1,17 +1,17 @@
1
-const Objection = require('objection');
2
-const Schmervice = require('@hapipal/schmervice');
3
-const Schwifty = require('@hapipal/schwifty');
4
-const Jwt = require('@hapi/jwt');
1
+const Objection = require('objection')
2
+const Schmervice = require('@hapipal/schmervice')
3
+const Schwifty = require('@hapipal/schwifty')
4
+const Jwt = require('@hapi/jwt')
5 5
 const JwtStrategy = require('../auth/strategies/jwt')
6 6
 
7
-const UserModel = require('../models/user');
7
+const UserModel = require('../models/user')
8 8
 
9
-const UserCurrentRoute = require('../routes/user/current');
10
-const UserLoginRoute = require('../routes/user/login');
9
+const UserCurrentRoute = require('../routes/user/current')
10
+const UserLoginRoute = require('../routes/user/login')
11 11
 
12
-const MembershipService = require('../services/membership');
13
-const UserService = require('../services/user');
14
-const DisplayService = require('../services/display');
12
+const MembershipService = require('../services/membership')
13
+const UserService = require('../services/user')
14
+const DisplayService = require('../services/display')
15 15
 
16 16
 module.exports = {
17 17
     name: 'user-plugin',
@@ -30,7 +30,7 @@ module.exports = {
30 30
         // Bind to global context
31 31
         // So we can use Objection transactions
32 32
         server.bind({
33
-            transaction: (fn) => Objection.transaction(server.knex(), fn)
33
+            transaction: fn => Objection.transaction(server.knex(), fn),
34 34
         })
35 35
 
36 36
         await server.register(Schmervice)
@@ -39,6 +39,5 @@ module.exports = {
39 39
 
40 40
         await server.route(UserCurrentRoute)
41 41
         await server.route(UserLoginRoute)
42
-    }
42
+    },
43 43
 }
44
-

+ 3
- 3
backend/lib/routes/example/base.js Zobrazit soubor

@@ -23,6 +23,6 @@ module.exports = {
23 23
         description: 'Test',
24 24
         notes: 'This is an example route',
25 25
         tags: ['api'],
26
-        auth: false
27
-    }
28
-}
26
+        auth: false,
27
+    },
28
+}

+ 13
- 14
backend/lib/routes/membership/active.js Zobrazit soubor

@@ -1,13 +1,13 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Joi = require('joi');
3
+const Joi = require('joi')
4 4
 
5 5
 const pluginConfig = {
6 6
     handlerType: 'grouping',
7 7
     docs: {
8 8
         description: 'active memberships',
9
-        notes: 'A list of groupings with active membership'
10
-    }
9
+        notes: 'A list of groupings with active membership',
10
+    },
11 11
 }
12 12
 
13 13
 const validators = {
@@ -28,8 +28,8 @@ const responseSchemas = {
28 28
     groupingsList: Joi.object({
29 29
         grouping_id: Joi.number(),
30 30
         grouping_name: Joi.string(),
31
-        grouping_type: Joi.string()
32
-    })
31
+        grouping_type: Joi.string(),
32
+    }),
33 33
 }
34 34
 
35 35
 module.exports = {
@@ -49,10 +49,9 @@ module.exports = {
49 49
                 return {
50 50
                     ok: true,
51 51
                     handler: pluginConfig.handlerType,
52
-                    data: groupings
52
+                    data: groupings,
53 53
                 }
54
-            }
55
-            catch(err) {
54
+            } catch (err) {
56 55
                 return {
57 56
                     ok: false,
58 57
                     handler: pluginConfig.handlerType,
@@ -64,8 +63,8 @@ module.exports = {
64 63
         /** Validate based on validators object */
65 64
         validate: {
66 65
             ...validators,
67
-            failAction: 'log'
68
-         },
66
+            failAction: 'log',
67
+        },
69 68
 
70 69
         /** Validate the server response */
71 70
         response: {
@@ -73,7 +72,7 @@ module.exports = {
73 72
                 ok: Joi.bool(),
74 73
                 handler: Joi.string(),
75 74
                 data: Joi.array().items(responseSchemas.groupingsList),
76
-            })
77
-        }
78
-    }
75
+            }),
76
+        },
77
+    },
79 78
 }

+ 25
- 18
backend/lib/routes/membership/join.js Zobrazit soubor

@@ -1,13 +1,11 @@
1
-'use strict';
2
-
3
-const Joi = require('joi');
1
+const Joi = require('joi')
4 2
 
5 3
 const pluginConfig = {
6 4
     handlerType: 'grouping',
7 5
     docs: {
8 6
         description: 'join',
9
-        notes: 'Join a grouping by creating a membership record'
10
-    }
7
+        notes: 'Join a grouping by creating a membership record',
8
+    },
11 9
 }
12 10
 
13 11
 const validators = {
@@ -19,8 +17,8 @@ const validators = {
19 17
             grouping_name: Joi.string().allow(null),
20 18
             grouping_type: Joi.string().allow(null),
21 19
             role: Joi.string(),
22
-        })
23
-    }
20
+        }),
21
+    },
24 22
 }
25 23
 
26 24
 module.exports = {
@@ -37,7 +35,7 @@ module.exports = {
37 35
          * @param {*} h
38 36
          * @returns {object}
39 37
          */
40
-        handler: async function (request, h) {
38
+        handler: async function (request) {
41 39
             try {
42 40
                 const { membershipService } = request.services()
43 41
 
@@ -46,22 +44,31 @@ module.exports = {
46 44
                 const groupingToWrite = {
47 45
                     grouping_id: res.grouping_id,
48 46
                     grouping_name: res.grouping_name,
49
-                    grouping_type: res.grouping_type
47
+                    grouping_type: res.grouping_type,
50 48
                 }
51
-               
49
+
52 50
                 /** Default to participant role */
53 51
                 const role = res.role ? res.role : 'participant'
54 52
 
55 53
                 /** User membership service method to create membership */
56
-                const memberships = await membershipService.joinGrouping(res.user_id, res.target_id, groupingToWrite, role)
54
+                const memberships = await membershipService.joinGrouping(
55
+                    res.user_id,
56
+                    res.target_id,
57
+                    groupingToWrite,
58
+                    role,
59
+                )
57 60
 
58 61
                 return {
59 62
                     ok: true,
60 63
                     handler: pluginConfig.handlerType,
61
-                    data: { memberships, hasMatch: memberships.every(membership => membership.is_active == true) }
64
+                    data: {
65
+                        memberships,
66
+                        hasMatch: memberships.every(
67
+                            membership => membership.is_active == true,
68
+                        ),
69
+                    },
62 70
                 }
63
-            }
64
-            catch(err) {
71
+            } catch (err) {
65 72
                 return {
66 73
                     ok: false,
67 74
                     handler: pluginConfig.handlerType,
@@ -74,9 +81,9 @@ module.exports = {
74 81
             schema: Joi.object({
75 82
                 ok: Joi.bool(),
76 83
                 handler: Joi.string(),
77
-                data: Joi.object()
84
+                data: Joi.object(),
78 85
             }),
79
-            failAction: 'log'
80
-        }
81
-    }
86
+            failAction: 'log',
87
+        },
88
+    },
82 89
 }

+ 12
- 13
backend/lib/routes/membership/leave.js Zobrazit soubor

@@ -1,19 +1,19 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Joi = require('joi');
3
+const Joi = require('joi')
4 4
 
5 5
 const pluginConfig = {
6 6
     handlerType: 'grouping',
7 7
     docs: {
8 8
         description: 'leave',
9
-        notes: 'Leave a grouping by creating a membership record'
10
-    }
9
+        notes: 'Leave a grouping by creating a membership record',
10
+    },
11 11
 }
12 12
 
13 13
 const validators = {
14 14
     leave: {
15
-        payload: Joi.object()
16
-    }
15
+        payload: Joi.object(),
16
+    },
17 17
 }
18 18
 
19 19
 module.exports = {
@@ -28,10 +28,9 @@ module.exports = {
28 28
                 return {
29 29
                     ok: true,
30 30
                     handler: pluginConfig.handlerType,
31
-                    data: { foo: 'bar' }
31
+                    data: { foo: 'bar' },
32 32
                 }
33
-            }
34
-            catch(err) {
33
+            } catch (err) {
35 34
                 return {
36 35
                     ok: false,
37 36
                     handler: pluginConfig.handlerType,
@@ -44,9 +43,9 @@ module.exports = {
44 43
             schema: Joi.object({
45 44
                 ok: Joi.bool(),
46 45
                 handler: Joi.string(),
47
-                data: validators.leave
46
+                data: validators.leave,
48 47
             }),
49
-            failAction: 'log'
50
-        }
51
-    }
48
+            failAction: 'log',
49
+        },
50
+    },
52 51
 }

+ 15
- 19
backend/lib/routes/user/current.js Zobrazit soubor

@@ -1,26 +1,25 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Joi = require('joi');
3
+const Joi = require('joi')
4 4
 
5 5
 const pluginConfig = {
6 6
     handlerType: 'user',
7 7
     docs: {
8 8
         get: {
9 9
             description: 'Get user',
10
-            notes: 'Returns a user item by the id passed in the path'
11
-        }
12
-    }
10
+            notes: 'Returns a user item by the id passed in the path',
11
+        },
12
+    },
13 13
 }
14 14
 
15
-
16 15
 /** Validator functions by request method */
17 16
 const validators = {
18 17
     get: {
19 18
         params: Joi.object({
20 19
             name: Joi.string().min(3).max(11),
21
-            all: Joi.array()
22
-        })
23
-    }
20
+            all: Joi.array(),
21
+        }),
22
+    },
24 23
 }
25 24
 
26 25
 module.exports = {
@@ -36,14 +35,13 @@ module.exports = {
36 35
             try {
37 36
                 const auth = {
38 37
                     credentials: request.auth.credentials,
39
-                    token: request.auth.artifacts.token
38
+                    token: request.auth.artifacts.token,
40 39
                 }
41 40
 
42 41
                 // /** Get the data for your endpoint */
43 42
                 // const { User } = request.models()
44 43
                 // const all = await User.query()
45 44
 
46
-
47 45
                 const { displayService } = request.services()
48 46
                 const user = displayService.user(auth.credentials, auth.token)
49 47
 
@@ -52,8 +50,7 @@ module.exports = {
52 50
                     handler: pluginConfig.handlerType,
53 51
                     data: { name: request.params.name },
54 52
                 }
55
-            }
56
-            catch(err) {
53
+            } catch (err) {
57 54
                 return {
58 55
                     ok: false,
59 56
                     handler: pluginConfig.handlerType,
@@ -66,10 +63,9 @@ module.exports = {
66 63
             schema: Joi.object({
67 64
                 ok: Joi.bool(),
68 65
                 handler: Joi.string(),
69
-                data: validators.get.params
66
+                data: validators.get.params,
70 67
             }),
71
-            failAction: 'log'
72
-        }
73
-    }
74
-
75
-}
68
+            failAction: 'log',
69
+        },
70
+    },
71
+}

+ 21
- 20
backend/lib/routes/user/login.js Zobrazit soubor

@@ -1,23 +1,22 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Joi = require('joi');
3
+const Joi = require('joi')
4 4
 
5 5
 const pluginConfig = {
6 6
     handlerType: 'user',
7 7
     docs: {
8 8
         description: 'login',
9
-        notes: 'Attempt login'
10
-    }
9
+        notes: 'Attempt login',
10
+    },
11 11
 }
12 12
 
13
-
14 13
 /** Validator functions by request method */
15 14
 const validators = {
16 15
     post: {
17 16
         payload: Joi.object({
18 17
             user: Joi.object(),
19
-            error: Joi.string()
20
-        })
18
+            error: Joi.string(),
19
+        }),
21 20
     },
22 21
     user: Joi.object({
23 22
         user_id: Joi.number(),
@@ -26,7 +25,7 @@ const validators = {
26 25
         created_at: Joi.date(),
27 26
         updated_at: Joi.date(),
28 27
         token: Joi.string(),
29
-    })
28
+    }),
30 29
 }
31 30
 
32 31
 module.exports = {
@@ -43,11 +42,14 @@ module.exports = {
43 42
                 const res = request.payload
44 43
 
45 44
                 // Callback to use as transaction
46
-                const login = async (txn) => {
47
-                    return await userService.login({
48
-                        email: res.user.email,
49
-                        password: res.user.password
50
-                    }, txn)
45
+                const login = async txn => {
46
+                    return await userService.login(
47
+                        {
48
+                            email: res.user.email,
49
+                            password: res.user.password,
50
+                        },
51
+                        txn,
52
+                    )
51 53
                 }
52 54
 
53 55
                 // Bound context from your plugin server declaration
@@ -57,10 +59,9 @@ module.exports = {
57 59
                 return {
58 60
                     ok: true,
59 61
                     handler: pluginConfig.handlerType,
60
-                    data: displayService.user(user, token)
62
+                    data: displayService.user(user, token),
61 63
                 }
62
-            }
63
-            catch(err) {
64
+            } catch (err) {
64 65
                 console.error(err)
65 66
                 return {
66 67
                     ok: false,
@@ -74,9 +75,9 @@ module.exports = {
74 75
             schema: Joi.object({
75 76
                 ok: Joi.bool(),
76 77
                 handler: Joi.string(),
77
-                data: validators.user
78
+                data: validators.user,
78 79
             }),
79
-            failAction: 'log'
80
-        }
81
-    }
80
+            failAction: 'log',
81
+        },
82
+    },
82 83
 }

+ 15
- 11
backend/lib/services/display.js Zobrazit soubor

@@ -1,7 +1,7 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Schmervice = require('@hapipal/schmervice');
4
-const internals = {};
3
+const Schmervice = require('@hapipal/schmervice')
4
+const internals = {}
5 5
 
6 6
 module.exports = class DisplayService extends Schmervice.Service {
7 7
     user({ password, ...user }, token) {
@@ -26,16 +26,20 @@ module.exports = class DisplayService extends Schmervice.Service {
26 26
      * @param {*} txn
27 27
      * @returns
28 28
      */
29
-    async profile(currentUserId, user, txn)  {
29
+    async profile(currentUserId, user, txn) {
30 30
         const { User } = this.server.models()
31 31
         const { toProfile } = internals
32 32
 
33
-        const result = await User.fetchGraph(user, `[
33
+        const result = await User.fetchGraph(
34
+            user,
35
+            `[
34 36
             followedBy(currentUser) as following
35
-        ]`, {
36
-            txn
37
-        }).modifiers({
38
-            currentUser: (builder) => builder.where('Users.id', currentUserId)
37
+        ]`,
38
+            {
39
+                txn,
40
+            },
41
+        ).modifiers({
42
+            currentUser: builder => builder.where('Users.id', currentUserId),
39 43
         })
40 44
 
41 45
         return toProfile(result)
@@ -44,5 +48,5 @@ module.exports = class DisplayService extends Schmervice.Service {
44 48
 
45 49
 internals.toProfile = ({ password, email, following, ...user }) => ({
46 50
     ...user,
47
-    following: (following.length > 0)
48
-})
51
+    following: following.length > 0,
52
+})

+ 68
- 47
backend/lib/services/membership.js Zobrazit soubor

@@ -1,10 +1,9 @@
1
-'use strict';
2
-
3
-const Schmervice = require('@hapipal/schmervice');
4
-
1
+const Schmervice = require('@hapipal/schmervice')
5 2
 
6 3
 module.exports = class MembershipService extends Schmervice.Service {
7
-    constructor(...args) { super(...args) }
4
+    constructor(...args) {
5
+        super(...args)
6
+    }
8 7
 
9 8
     /**
10 9
      * Internal method to get list of grouping_ids for this user
@@ -14,15 +13,16 @@ module.exports = class MembershipService extends Schmervice.Service {
14 13
     async _getGroupIdsForUserId(userId) {
15 14
         const { Membership } = this.server.models()
16 15
 
17
-            /** Grab every Membership associated with this id */
18
-            const allMemberships = await Membership.query()
19
-                .where('user_id', userId)
16
+        /** Grab every Membership associated with this id */
17
+        const allMemberships = await Membership.query().where('user_id', userId)
20 18
 
21
-            /** Copy a list of the just the Groupings */
22
-            const groupingIdsToGrab = allMemberships.map(membership => membership.grouping_id)
19
+        /** Copy a list of the just the Groupings */
20
+        const groupingIdsToGrab = allMemberships.map(
21
+            membership => membership.grouping_id,
22
+        )
23 23
 
24
-            /** Uncomment to dedupe the list just in case */
25
-            return [...new Set(groupingIdsToGrab)]
24
+        /** Uncomment to dedupe the list just in case */
25
+        return [...new Set(groupingIdsToGrab)]
26 26
     }
27 27
 
28 28
     /**
@@ -37,13 +37,12 @@ module.exports = class MembershipService extends Schmervice.Service {
37 37
             grouping_name: groupingToTry.grouping_name,
38 38
             grouping_type: groupingToTry.grouping_type,
39 39
         }
40
-        return await Grouping.query(txn)
41
-            .insert(groupingInfo)
40
+        return await Grouping.query(txn).insert(groupingInfo)
42 41
     }
43 42
 
44 43
     async findOrCreateGrouping(groupingToTry) {
45 44
         let idToReturn = groupingToTry.grouping_id
46
-        if(!idToReturn) {
45
+        if (!idToReturn) {
47 46
             /** ?: For some reason this returns the key id */
48 47
             const grouping = await this._createGrouping(groupingToTry)
49 48
             idToReturn = grouping.id
@@ -69,16 +68,20 @@ module.exports = class MembershipService extends Schmervice.Service {
69 68
 
70 69
     async _groupingIdsInCommon(userId, targetId) {
71 70
         const dedupedUserGroupingIds = await this._getGroupIdsForUserId(userId)
72
-        const dedupedTargetGroupingIds = await this._getGroupIdsForUserId(targetId)
71
+        const dedupedTargetGroupingIds = await this._getGroupIdsForUserId(
72
+            targetId,
73
+        )
73 74
 
74 75
         /** Return true if both people have a group in common */
75
-        return dedupedUserGroupingIds.filter(groupingId => dedupedTargetGroupingIds.includes(groupingId))
76
+        return dedupedUserGroupingIds.filter(groupingId =>
77
+            dedupedTargetGroupingIds.includes(groupingId),
78
+        )
76 79
     }
77 80
     async _patchMembership(memberships, userId, patch) {
78 81
         const { Membership } = this.server.models()
79 82
 
80 83
         /** Set membership as active only if the user initiates it */
81
-        for(let membershipInfo of memberships) {
84
+        for (let membershipInfo of memberships) {
82 85
             await Membership.query()
83 86
                 .where('membership_id', membershipInfo.membership_id)
84 87
                 .where('user_id', userId)
@@ -86,25 +89,35 @@ module.exports = class MembershipService extends Schmervice.Service {
86 89
         }
87 90
     }
88 91
 
89
-    async attemptMatch(userId, targetId, txn) {
92
+    async attemptMatch(userId, targetId) {
90 93
         const { Membership } = this.server.models()
91 94
 
92 95
         /** If both people have groups in common */
93
-        const matchingGroupingIds = await this._groupingIdsInCommon(userId, targetId)
94
-
95
-        if(matchingGroupingIds.length) {
96
+        const matchingGroupingIds = await this._groupingIdsInCommon(
97
+            userId,
98
+            targetId,
99
+        )
96 100
 
101
+        if (matchingGroupingIds.length) {
97 102
             /** Grab all memberships associated with groupingIds */
98
-            const memberships = await Membership.query().whereIn('grouping_id', matchingGroupingIds)
99
-    
103
+            const memberships = await Membership.query().whereIn(
104
+                'grouping_id',
105
+                matchingGroupingIds,
106
+            )
107
+
100 108
             /** Set membership as active only if the user initiates it */
101
-            await this._patchMembership(memberships, userId, { is_active: true })
109
+            await this._patchMembership(memberships, userId, {
110
+                is_active: true,
111
+            })
102 112
 
103 113
             /** Make a new query to get updated information */
104
-            return await Membership.query().whereIn('grouping_id', matchingGroupingIds)
114
+            return await Membership.query().whereIn(
115
+                'grouping_id',
116
+                matchingGroupingIds,
117
+            )
105 118
         }
106 119
     }
107
-    
120
+
108 121
     /**
109 122
      * Check for grouping membership then add membership record and set to active
110 123
      * or create a new grouping and add membership record for user and membership record for target
@@ -118,25 +131,33 @@ module.exports = class MembershipService extends Schmervice.Service {
118 131
         const { Membership } = this.server.models()
119 132
 
120 133
         /** If both people have groups in common */
121
-        const matchingGroupingIds = await this._groupingIdsInCommon(userId, targetId)
122
-
123
-        if(matchingGroupingIds.length) {
134
+        const matchingGroupingIds = await this._groupingIdsInCommon(
135
+            userId,
136
+            targetId,
137
+        )
124 138
 
139
+        if (matchingGroupingIds.length) {
125 140
             /** Grab all memberships associated with groupingIds */
126
-            const memberships = await Membership.query().whereIn('grouping_id', matchingGroupingIds)
127
-    
141
+            const memberships = await Membership.query().whereIn(
142
+                'grouping_id',
143
+                matchingGroupingIds,
144
+            )
145
+
128 146
             /** Set membership as active only if the user initiates it */
129
-            await this._patchMembership(memberships, userId, { is_active: true })
147
+            await this._patchMembership(memberships, userId, {
148
+                is_active: true,
149
+            })
130 150
 
131 151
             /** Make a new query to get updated information */
132
-            return await Membership.query().whereIn('grouping_id', matchingGroupingIds)
133
-        }
134
-        
135
-        /** 
136
-         * If both have NO grouping in common create a membership
137
-         * for both to new group but set membership as inactive for target
138
-         * */
139
-        else {
152
+            return await Membership.query().whereIn(
153
+                'grouping_id',
154
+                matchingGroupingIds,
155
+            )
156
+        } else {
157
+            /**
158
+             * If both have NO grouping in common create a membership
159
+             * for both to new group but set membership as inactive for target
160
+             * */
140 161
             /** Check if the grouping exists and if NOT creat it */
141 162
             const groupingId = await this.findOrCreateGrouping(groupingToWrite)
142 163
 
@@ -145,17 +166,17 @@ module.exports = class MembershipService extends Schmervice.Service {
145 166
                 grouping_id: groupingId,
146 167
                 membership_type: role,
147 168
                 can_edit: false,
148
-                is_active: true
169
+                is_active: true,
149 170
             })
150
-    
171
+
151 172
             const targetMembership = await Membership.query(txn).insert({
152 173
                 user_id: targetId,
153 174
                 grouping_id: groupingId,
154 175
                 membership_type: role,
155 176
                 can_edit: false,
156
-                is_active: false
177
+                is_active: false,
157 178
             })
158
-            
179
+
159 180
             return [userMembership, targetMembership]
160 181
         }
161 182
     }
@@ -172,10 +193,10 @@ module.exports = class MembershipService extends Schmervice.Service {
172 193
         const dedupedGroupings = await this._getGroupIdsForUserId(userId)
173 194
 
174 195
         /** Do NOTHING if NOT in Grouping */
175
-        if(!dedupedGroupings.includes(groupingId)) return
196
+        if (!dedupedGroupings.includes(groupingId)) return
176 197
 
177 198
         return await Membership.query()
178 199
             .delete()
179 200
             .where('grouping_id', groupingId)
180 201
     }
181
-}
202
+}

+ 35
- 23
backend/lib/services/user.js Zobrazit soubor

@@ -1,9 +1,9 @@
1
-'use strict';
1
+'use strict'
2 2
 
3
-const Util = require('util');
4
-const Jwt = require('@hapi/jwt');
5
-const Schmervice = require('@hapipal/schmervice');
6
-const SecurePassword = require('secure-password');
3
+const Util = require('util')
4
+const Jwt = require('@hapi/jwt')
5
+const Schmervice = require('@hapipal/schmervice')
6
+const SecurePassword = require('secure-password')
7 7
 
8 8
 /** Class for methods used in the User plugin */
9 9
 module.exports = class UserService extends Schmervice.Service {
@@ -16,7 +16,7 @@ module.exports = class UserService extends Schmervice.Service {
16 16
         const pwd = new SecurePassword()
17 17
         this.pwd = {
18 18
             hash: Util.promisify(pwd.hash.bind(pwd)),
19
-            verify: Util.promisify(pwd.verify.bind(pwd))
19
+            verify: Util.promisify(pwd.verify.bind(pwd)),
20 20
         }
21 21
     }
22 22
 
@@ -40,7 +40,10 @@ module.exports = class UserService extends Schmervice.Service {
40 40
     async findByUsername(username, txn) {
41 41
         const { User } = this.server.models()
42 42
 
43
-        return await User.query(txn).throwIfNotFound().first().where({ username })
43
+        return await User.query(txn)
44
+            .throwIfNotFound()
45
+            .first()
46
+            .where({ username })
44 47
     }
45 48
 
46 49
     /**
@@ -67,7 +70,10 @@ module.exports = class UserService extends Schmervice.Service {
67 70
         const { User } = this.server.models()
68 71
 
69 72
         if (Object.keys(userInfo).length > 0) {
70
-            await User.query(txn).throwIfNotFound().where({ id }).patch(userInfo)
73
+            await User.query(txn)
74
+                .throwIfNotFound()
75
+                .where({ id })
76
+                .patch(userInfo)
71 77
         }
72 78
 
73 79
         if (password) {
@@ -91,7 +97,6 @@ module.exports = class UserService extends Schmervice.Service {
91 97
             .first()
92 98
             .where({ user_email: email })
93 99
 
94
-
95 100
         /** Uncomment to run password check using SecurePassword */
96 101
         // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
97 102
         // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
@@ -112,16 +117,20 @@ module.exports = class UserService extends Schmervice.Service {
112 117
     createToken(user) {
113 118
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
114 119
 
115
-        return Jwt.token.generate({
116
-            aud: 'urn:audience:test',
117
-            iss: 'urn:issuer:test',
118
-            email: user.user_email
119
-        }, {
120
-            key: key,
121
-            algorithm: 'HS256'
122
-        }, {
123
-            ttlSec: 4 * 60 * 60 // 7 days
124
-        })
120
+        return Jwt.token.generate(
121
+            {
122
+                aud: 'urn:audience:test',
123
+                iss: 'urn:issuer:test',
124
+                email: user.user_email,
125
+            },
126
+            {
127
+                key: key,
128
+                algorithm: 'HS256',
129
+            },
130
+            {
131
+                ttlSec: 4 * 60 * 60, // 7 days
132
+            },
133
+        )
125 134
     }
126 135
 
127 136
     /**
@@ -134,9 +143,12 @@ module.exports = class UserService extends Schmervice.Service {
134 143
     async changePassword(id, password, txn) {
135 144
         const { User } = this.server.models()
136 145
 
137
-        await User.query(txn).throwIfNotFound().where({ id }).patch({
138
-            password: await this.pwd.hash(Buffer.from(password))
139
-        })
146
+        await User.query(txn)
147
+            .throwIfNotFound()
148
+            .where({ id })
149
+            .patch({
150
+                password: await this.pwd.hash(Buffer.from(password)),
151
+            })
140 152
         return id
141 153
     }
142
-}
154
+}

+ 1290
- 0
backend/package-lock.json
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor


+ 1
- 0
backend/package.json Zobrazit soubor

@@ -32,6 +32,7 @@
32 32
   },
33 33
   "devDependencies": {
34 34
     "ava": "^3.15.0",
35
+    "eslint": "^7.28.0",
35 36
     "nodemon": "^2.0.7",
36 37
     "nyc": "^15.1.0"
37 38
   }

+ 4
- 4
backend/server/index.js Zobrazit soubor

@@ -1,6 +1,6 @@
1
-const Glue = require('@hapi/glue');
2
-const Exiting = require('exiting');
3
-const Manifest = require('./manifest');
1
+const Glue = require('@hapi/glue')
2
+const Exiting = require('exiting')
3
+const Manifest = require('./manifest')
4 4
 /**
5 5
  * Our main app server
6 6
  * @param {boolean} start
@@ -12,7 +12,7 @@ exports.deployment = async ({ start } = {}) => {
12 12
 
13 13
     if (start) {
14 14
         await Exiting.createManager(server).start()
15
-        server.log(['start'], `Server started at ${server.info.uri}`);
15
+        server.log(['start'], `Server started at ${server.info.uri}`)
16 16
         return server
17 17
     }
18 18
 

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