Test Setup Failed
Pull Request — main (#14)
by Julia
05:38 queued 02:03
created

server/src/models/api-key.js   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 32
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 12
mnd 1
bc 1
fnc 3
dl 0
loc 32
bpm 0.3333
cpm 1.3333
noi 0
c 0
b 0
f 0
rs 10
1
import { db } from "./db.js"
2
3
4
const apiKey = {
5
    keys: [],
6
7
    /**
8
     * Gets all active api keys from
9
     * DB and stores in the keys array
10
     */
11
    getActiveFromDB: async function() {
12
        const result = await db.queryNoArgs(`CALL active_api_keys();`);
13
        this.keys = result[0].map((elem) => {
14
            return elem.key;
15
        })
16
    },
17
18
    /**
19
     * Returns true if the key is valid and active
20
     * @param {String} apiKey 
21
     * @returns {Promise<Boolean>}
22
     */
23
    checkOne: async function(apiKey) {
24
        if (this.keys.length === 0) {
25
            await this.getActiveFromDB();
26
        }
27
        return this.keys.includes(apiKey);
28
    },
29
30
};
31
32
export default apiKey;
33