server/src/models/card.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 48
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 15
mnd 2
bc 2
fnc 3
dl 0
loc 48
ccs 10
cts 10
cp 1
rs 10
bpm 0.6666
cpm 1.6666
noi 0
c 0
b 0
f 0
1
import { db } from "./db.js"
2
3
4 1
const card = {
5
    /**
6
     * Returns all valid cards.
7
     * Each card objects contains
8
     * id (int) and name (string)
9
     * @returns {Promise<Array>}
10
     */
11
    getTypes: async function() {
12 1
        const result = await db.queryNoArgs(`CALL card_types();`);
13 1
        return result[0];
14
    },
15
    /**
16
     * Returns card_nr, card_type
17
     * and card_type_descr
18
     * @param {Number} userId 
19
     * @returns {Promise<Object>}
20
     */
21
    userDetails: async function(userId) {
22 7
        const result = await db.queryWithArgs(`CALL user_card(?);`, [userId]);
23 7
        return result[0][0];
24
    },
25
    /**
26
     * Updates cardnr and cardtype
27
     * of a user. Returns the updated
28
     * card_nr, card_type and card_type_descr
29
     * @param {Number} userId 
30
     * @param {String} cardnr 
31
     * @param {Number} cardType 
32
     * @returns {Promise<Object>}
33
     */
34
    updUserDetails: async function(userId, cardnr, cardType) {
35
        // cardnr needs to contain 13-19 digits
36 4
        const check = cardnr ? cardnr.toString().replace(/\D/g, '') : "";
37
38 4
        if (!/^\d{13,19}$/.test(check)) {
39 2
            throw new Error('invalid cardnr');
40
        }
41
42 2
        const result = await db.queryWithArgs(`CALL upd_user_card(?, ?, ?);`, [userId, cardnr, cardType]);
43
44 1
        return result[0][0];
45
    },
46
};
47
48
export default card;
49