Total Complexity | 5 |
Complexity/F | 1.67 |
Lines of Code | 48 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 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 |