All files / modules/phase index.js

100% Statements 15/15
100% Branches 2/2
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42      1x 1x               1x 1x 1x 2x 2x   1x                 1x 2x 1x   1x 1x 1x       1x        
/**
 * @module phase
 */
const phaseRepo = require("../../repositories/phase");
const cache = require("../cache");
 
/**
 * Get phase array by converting phase ids to
 * titles.
 * @param {Array} phaseRecords
 * @return {Array} phase titles
 */
const getPhaseArray = async (phaseRecords) => {
    const phaseArray = [];
    for await (const coursePhase of phaseRecords) {
        const phaseTitle = (await phaseRepo.findById(coursePhase.phaseId)).rows[0].title;
        phaseArray.push(phaseTitle);
    };
    return phaseArray;
};
 
/**
 * Fetch all phases. If cached,
 * return from cache, otherwise, fetch from
 * database and cache the values.
 * @return {Array} all phase objects
 */
const fetchAll = async () => {
    if (cache.has("phases")) {
        return cache.get("phases");
    } else {
        const findResult = await phaseRepo.findAll();
        cache.set("phases", findResult.rows);
        return findResult.rows;
    }
};
 
module.exports = {
    getPhaseArray,
    fetchAll,
};