Source: phase/index.js

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