Passed
Push — main ( 55cb12...5d5954 )
by Martin
01:05 queued 16s
created

server/src/models/user.js   A

Complexity

Total Complexity 13
Complexity/F 1

Size

Lines of Code 112
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 52
mnd 0
bc 0
fnc 13
dl 0
loc 112
rs 10
bpm 0
cpm 1
noi 5
c 0
b 0
f 0
1
import jwt from "jsonwebtoken";
2
import { db } from "./db.js"
3
4
const user = {
5
    extractEmail: async function(githubToken) {
0 ignored issues
show
Unused Code introduced by
The parameter githubToken is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
6
        // add logic here for extracting user email from Github
7
        // await ....
8
    },
9
    insertIntoDB: async function(email, cardnr, cardtype) {
10
11
        const result = await db.queryWithArgs(`CALL new_user(?, ?, ?);`, [email, cardnr, cardtype]);
12
13
        return result[0][0];
14
15
    },
16
    /**
17
     * body should contain Github Token,
18
     * Card nr as string and card type as int
19
     * @param {*} req 
20
     * @param {*} res 
21
     * @param {*} next 
22
     */
23
    register: async function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
24
        const email = this.extractEmail(req.body.token)
25
        const payload = await this.newUser(email, req.body.cardnr, req.body.cardtype);
26
        const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" });
27
        return res.json({
28
            data: {
29
                type: "success",
30
                message: "User logged in",
31
                user: payload,
32
                token: jwtToken
33
            }
34
        });
35
    },
36
    /**
37
     * body should contain Github Token
38
     * @param {*} req 
39
     * @param {*} res 
40
     * @param {*} next 
41
     */
42
    login: async function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
43
        const email = this.extractEmail(req.body.token)
44
        const payload = await db.queryWithArgs(`CALL user_login(?);`, [email]);
45
;
46
        const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" });
47
        return res.json({
48
            data: {
49
                type: "success",
50
                message: "User logged in",
51
                user: payload,
52
                token: jwtToken
53
            }
54
        });
55
    },
56
    /**
57
     * 
58
     * @param {String | Int} what id or email to search for, for wilcard search add % before, after or both
59
     * @return {Array} if the search string is not a wildcard the array will only contain one object
60
     */
61
    userSearch: async function(what) {
62
        const result = await db.queryWithArgs(`CALL user_search(?);`, [what]);
63
        return result[0].map((user) => {
64
            return this.adjTypes(user);
65
        });
66
    },
67
    /**
68
     * 
69
     * @param {Int} userId 
70
     * @param {Boolean} active 
71
     * @returns {Object}
72
     */
73
    updStatus: async function(userId, active) {
74
        const result = await db.queryWithArgs(`CALL upd_user_status(?, ?);`, [userId, active]);
75
        return this.adjTypes(result[0][0]);
76
    },
77
    /**
78
     * 
79
     * @param {Int} userId
80
     * @param {String} email
81
     * @returns {Object}
82
     */
83
    updEmail: async function(userId, email) {
84
        const result = await db.queryWithArgs(`CALL upd_user_email(?, ?);`, [userId, email]);
85
        return this.adjTypes(result[0][0]);
86
    },
87
    /**
88
     * 
89
     * @param {Int} offset
90
     * @param {Int} limit
91
     * @returns {Array}
92
     */
93
    allPag: async function(offset, limit) {
94
        const result = await db.queryWithArgs(`CALL upd_user_email(?, ?);`, [offset, limit]);
95
        return result[0].map((user) => {
96
            return this.adjTypes(user);
97
        });
98
    },
99
    all: async function() {
100
        const result = await db.queryNoArgs(`CALL all_users_pag();`, [offset, limit]);
0 ignored issues
show
Bug introduced by
The variable limit seems to be never declared. If this is a global, consider adding a /** global: limit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable offset seems to be never declared. If this is a global, consider adding a /** global: offset */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
101
        return result[0].map((user) => {
102
            return this.adjTypes(user);
103
        });
104
    },
105
    adjTypes(userObj) {
106
        userObj.balance = parseFloat(userObj.balance);
107
        userObj.active = userObj.active === 1;
108
        return userObj;
109
    },
110
};
111
112
export default user;
113