Passed
Pull Request — main (#12)
by Julia
02:06
created

server/src/models/user.js   A

Complexity

Total Complexity 6
Complexity/F 1

Size

Lines of Code 54
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 27
mnd 0
bc 0
fnc 6
dl 0
loc 54
rs 10
bpm 0
cpm 1
noi 2
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
     * 
38
     * @param {String | Int} what id or email to search for, for wilcard search add % before, after or both
39
     * @return {Array} if the search string is not a wildcard the array will only contain one object
40
     */
41
    userSearch: async function(what) {
42
        const result = await db.queryWithArgs(`CALL user_search(?);`, [what]);
43
        return result[0].map((user) => {
44
            return this.adjTypes(user);
45
        });
46
    },
47
    adjTypes(userObj) {
48
        userObj.balance = parseFloat(userObj.balance);
49
        userObj.active = userObj.active === 1;
50
        return userObj;
51
    },
52
};
53
54
export default user;
55