Quellcode durchsuchen

:recycle: alterations to unseed and test user creation

tags/0.0.1
J vor 4 Jahren
Ursprung
Commit
bc538d8912

+ 1
- 0
backend/db/data-generator/classes.js Datei anzeigen

@@ -5,6 +5,7 @@ class User {
5 5
         this.user_email = ''
6 6
         this.is_admin = false
7 7
         this.is_poster = false
8
+        this.is_verified = false
8 9
     }
9 10
 }
10 11
 class Profile {

+ 1
- 0
backend/db/migrations/20210524105357_create_users_table.js Datei anzeigen

@@ -5,6 +5,7 @@ exports.up = function (knex) {
5 5
         table.string('user_email', 90).notNullable()
6 6
         table.boolean('is_admin').notNullable()
7 7
         table.boolean('is_poster').notNullable()
8
+        table.boolean('is_verified').notNullable()
8 9
     })
9 10
 }
10 11
 

+ 4
- 1
backend/lib/models/user.js Datei anzeigen

@@ -7,9 +7,12 @@ module.exports = class User extends Schwifty.Model {
7 7
     }
8 8
     static get joiSchema() {
9 9
         return Joi.object({
10
-            user_id: Joi.number().required(),
10
+            user_id: Joi.number(),
11 11
             user_name: Joi.string(),
12 12
             user_email: Joi.string().required(),
13
+            is_poster: Joi.number().required(),
14
+            is_admin: Joi.number().required(),
15
+            is_verified: Joi.number().required(),
13 16
         })
14 17
     }
15 18
 }

+ 2
- 0
backend/lib/plugins/user.js Datei anzeigen

@@ -10,6 +10,7 @@ const UserCurrentRoute = require('../routes/user/current')
10 10
 const UserProfileCreateRoute = require('../routes/user/create-profile')
11 11
 const UserProfilesListRoute = require('../routes/user/list-profiles')
12 12
 const UserLoginRoute = require('../routes/user/login')
13
+const UserSignupRoute = require('../routes/user/signup')
13 14
 
14 15
 const UserService = require('../services/user')
15 16
 const DisplayService = require('../services/display')
@@ -40,6 +41,7 @@ module.exports = {
40 41
 
41 42
         await server.route(UserCurrentRoute)
42 43
         await server.route(UserLoginRoute)
44
+        await server.route(UserSignupRoute)
43 45
         await server.route(UserProfileCreateRoute)
44 46
         await server.route(UserProfilesListRoute)
45 47
     },

+ 103
- 0
backend/lib/routes/user/signup.js Datei anzeigen

@@ -0,0 +1,103 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+const errorSchema = require('../../schemas/errors')
5
+const userSchema = require('../../schemas/user')
6
+
7
+const pluginConfig = {
8
+    handlerType: 'user',
9
+    docs: {
10
+        description: 'Create a user',
11
+        notes: 'Create a user and other things',
12
+    },
13
+}
14
+
15
+const validators = {
16
+    post: {
17
+        payload: Joi.object({
18
+            user: userSchema.userSignup,
19
+            error: errorSchema.single,
20
+        }).append().label('signup_payload')
21
+    }
22
+}
23
+
24
+const responseSchemas = {
25
+    response: Joi.object({
26
+        user_id: Joi.number(),
27
+        user_name: Joi.string(),
28
+        user_email: Joi.string(),
29
+        is_poster: Joi.number(),
30
+        is_admin: Joi.number(),
31
+        is_verified: Joi.number(),
32
+    }).label('created_user'),
33
+    error: errorSchema.single
34
+}
35
+
36
+module.exports = {
37
+    method: 'POST',
38
+    path: '/signup',
39
+    options: {
40
+        ...pluginConfig.docs,
41
+        tags: ['api'],
42
+        /** Protect this route with authentication? */
43
+        auth: false,
44
+
45
+        handler: async function (request, h) {
46
+            const { userService } = request.server.services()
47
+            const res = request.payload
48
+            const userName = res.user.user_name
49
+            const userEmail = res.user.user_email
50
+            const userType = res.user.user_type == 'poster' ? 1 : 0
51
+            const userPw = res.user.user_pass ? res.user.user_pass : 'changeme'
52
+            try {
53
+                const user = await userService.signup({
54
+                    password: userPw,
55
+                    userInfo: {
56
+                        user_name: userName,
57
+                        user_email: userEmail,
58
+                        is_poster: userType,
59
+                        is_admin: 0,
60
+                        is_verified: 0,
61
+                    }
62
+                })
63
+                return h
64
+                    .response({
65
+                        ok: true,
66
+                        handler: pluginConfig.handlerType,
67
+                        data: user,
68
+                    })
69
+                    .code(201)
70
+            } catch (err) {
71
+                return h
72
+                    .response({
73
+                        ok: false,
74
+                        handler: pluginConfig.handlerType,
75
+                        data: { error: `${err}` },
76
+                    })
77
+                    .code(409)
78
+            }
79
+        },
80
+
81
+        /** Validate based on validators object */
82
+        validate: {
83
+            ...validators.post,
84
+            failAction: 'log',
85
+        },
86
+
87
+        /** Validate the server response */
88
+        response: {
89
+            status: {
90
+                201: Joi.object({
91
+                    ok: Joi.bool(),
92
+                    handler: Joi.string(),
93
+                    data: responseSchemas.response,
94
+                }).label('created_user_res'),
95
+                409: Joi.object({
96
+                    ok: Joi.bool(),
97
+                    handler: Joi.string(),
98
+                    data: responseSchemas.error,
99
+                }).label('error_single_res'),
100
+            },
101
+        },
102
+    },
103
+}

+ 10
- 1
backend/lib/schemas/user.js Datei anzeigen

@@ -11,6 +11,15 @@ const singleUser = Joi.object({
11 11
     token: Joi.string()
12 12
 }).label('user_single')
13 13
 
14
+const userSignup = Joi.object({
15
+    user_name: Joi.string(),
16
+    user_email: Joi.string(),
17
+    is_poster: Joi.number(),
18
+    is_admin: Joi.number(),
19
+    is_verified: Joi.number(),
20
+}).label('user_signup')
21
+
14 22
 module.exports = {
15
-    single: singleUser
23
+    single: singleUser,
24
+    userSignup
16 25
 }

+ 20
- 12
backend/lib/services/user.js Datei anzeigen

@@ -56,11 +56,18 @@ module.exports = class UserService extends Schmervice.Service {
56 56
      * @param {*} txn
57 57
      * @returns
58 58
      */
59
-    async signup({ password, ...userInfo }, txn) {
59
+    async signup({ password, userInfo }, txn) {
60 60
         const { User } = this.server.models()
61
-        const { id } = await User.query(txn).insert(userInfo)
62
-        await this.changePassword(id, password, txn)
63
-        return id
61
+        const matchingEmails = await User.query().where('user_email', userInfo.user_email)
62
+
63
+        if(matchingEmails.length > 0) { 
64
+            throw `User ${userInfo.user_email} already exists: Cannot create a user without a unique email`
65
+        }
66
+        const user = await User.query(txn).insert(userInfo)
67
+        user.user_id = user.id
68
+        delete user.id
69
+        // await this.changePassword(id, password, txn)
70
+        return user
64 71
     }
65 72
 
66 73
     /**
@@ -146,13 +153,14 @@ module.exports = class UserService extends Schmervice.Service {
146 153
      */
147 154
     async changePassword(id, password, txn) {
148 155
         const { User } = this.server.models()
149
-
150
-        await User.query(txn)
151
-            .throwIfNotFound()
152
-            .where({ id })
153
-            .patch({
154
-                password: await this.pwd.hash(Buffer.from(password)),
155
-            })
156
-        return id
156
+        return 'done'
157
+
158
+        // await User.query(txn)
159
+        //     .throwIfNotFound()
160
+        //     .where({ id })
161
+        //     .patch({
162
+        //         password: await this.pwd.hash(Buffer.from(password)),
163
+        //     })
164
+        // return id
157 165
     }
158 166
 }

+ 1
- 0
backend/package.json Datei anzeigen

@@ -8,6 +8,7 @@
8 8
         "connect": "USE_LOCAL_DB=false pscale connect $(grep '^PSCALE_DB_NAME' .env | cut -d '=' -f2) $(grep '^PSCALE_DB_BRANCH' .env | cut -d '=' -f2)",
9 9
         "migrate": "knex migrate:latest",
10 10
         "unmigrate": "knex migrate:down",
11
+        "unseed": "knex migrate:rollback --all && knex migrate:latest",
11 12
         "reseed": "knex migrate:rollback --all && knex migrate:latest && knex seed:run",
12 13
         "generate": "node ./db/data-generator/index.js",
13 14
         "seed": "knex seed:run",

+ 0
- 1
frontend/src/views/SurveyView.vue Datei anzeigen

@@ -46,7 +46,6 @@ main.view--survey.f-col.start.w-full
46 46
                     p(@click="step = 0").p-1 start over
47 47
                     button(@click="$router.push({ name: 'HomeView' })") save
48 48
 
49
-    MainNav(@show-sidebar="$emit('show-sidebar')")
50 49
 </template>
51 50
 
52 51
 <script>

Laden…
Abbrechen
Speichern