From caa24b084a0d6b0f5425660f7d37967a4929ae18 Mon Sep 17 00:00:00 2001 From: SodhiA1 <38086281+SodhiA1@users.noreply.github.com> Date: Fri, 11 Aug 2023 14:15:56 -0700 Subject: [PATCH] added aching for funding group code --- backend/src/components/sdc/sdc-cache.js | 24 +++++++++++++++++ backend/src/components/sdc/sdc.js | 14 ---------- backend/src/routes/sdc.js | 6 +++-- backend/src/server.js | 5 ++++ backend/src/util/constants.js | 3 ++- frontend/src/common/apiService.js | 1 + .../institute/common/SchoolFunding.vue | 26 +++++-------------- frontend/src/store/modules/app.js | 18 ++++++++++++- 8 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 backend/src/components/sdc/sdc-cache.js diff --git a/backend/src/components/sdc/sdc-cache.js b/backend/src/components/sdc/sdc-cache.js new file mode 100644 index 000000000..2f5e941d3 --- /dev/null +++ b/backend/src/components/sdc/sdc-cache.js @@ -0,0 +1,24 @@ +'use strict'; +const { logApiError, errorResponse} = require('../utils'); +const HttpStatus = require('http-status-codes'); +const cacheService = require('../cache-service'); + +function getCachedSDCData(cacheKey,url){ + return async function handler(req, res) { + try { + if (req.query.refreshCache === 'true') { + await cacheService.loadDataToCache(cacheKey, url); + } + const cachedData = cacheService.getCachedData(); + const dataResponse = req.query.active === 'true' ? cachedData[cacheKey].activeRecords : cachedData[cacheKey].records; + return res.status(HttpStatus.OK).json(dataResponse); + } catch (e) { + await logApiError(e, 'getCachedSDCData', `Error occurred while attempting to GET ${cacheKey}.`); + return errorResponse(res); + } + }; +} + +module.exports = { + getCachedSDCData +}; diff --git a/backend/src/components/sdc/sdc.js b/backend/src/components/sdc/sdc.js index 41a51095e..d9fc05030 100644 --- a/backend/src/components/sdc/sdc.js +++ b/backend/src/components/sdc/sdc.js @@ -47,19 +47,6 @@ async function getSnapshotFundingDataForSchool(req, res) { } } -async function getFundingGroupsForSchool(req, res) { - try { - const accessToken = getBackendToken(req); - validateAccessToken(accessToken, res); - - const data = await getData(accessToken, `${config.get('sdc:fundingGroupsURL')}`); - return res.status(HttpStatus.OK).json(data); - } catch (e) { - logApiError(e, 'getFundingGroupsForSchool', 'Error getting funding groups'); - return errorResponse(res); - } -} - async function deleteFundingDataForSchool(req, res) { try { const accessToken = getBackendToken(req); @@ -163,7 +150,6 @@ function hasSchoolAdminRole(req, school){ module.exports = { - getFundingGroupsForSchool, getFundingGroupDataForSchool, deleteFundingDataForSchool, updateFundingDataForSchool, diff --git a/backend/src/routes/sdc.js b/backend/src/routes/sdc.js index c0a86721a..64116e7ce 100644 --- a/backend/src/routes/sdc.js +++ b/backend/src/routes/sdc.js @@ -3,9 +3,11 @@ const express = require('express'); const router = express.Router(); const utils = require('../components/utils'); const extendSession = utils.extendSession(); -const { getFundingGroupsForSchool, getFundingGroupDataForSchool, deleteFundingDataForSchool, updateFundingDataForSchool, +const { getFundingGroupDataForSchool, deleteFundingDataForSchool, updateFundingDataForSchool, getSnapshotFundingDataForSchool, addNewFundingForSchool, getAllCollectionsForSchool} = require('../components/sdc/sdc'); const auth = require('../components/auth'); +const {getCachedSDCData} = require('../components/sdc/sdc-cache'); +const constants = require('../util/constants'); router.post('/funding-groups/:schoolID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, addNewFundingForSchool); router.get('/funding-groups/:schoolID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, getFundingGroupDataForSchool); @@ -13,7 +15,7 @@ router.delete('/funding-groups/:schoolID/funding/:schoolFundingGroupID', passpor router.put('/funding-groups/:schoolID/funding/:schoolFundingGroupID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, updateFundingDataForSchool); router.get('/funding-groups/snapshot/:schoolID/:collectionID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, getSnapshotFundingDataForSchool); -router.get('/funding-groups', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, getFundingGroupsForSchool); +router.get('/funding-groups', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, getCachedSDCData(constants.CACHE_KEYS.SDC_FUNDING_GROUPS, 'sdc:fundingGroupsURL')); router.get('/sdcSchoolCollection/:schoolID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, getAllCollectionsForSchool); module.exports = router; diff --git a/backend/src/server.js b/backend/src/server.js index 8e134ab1c..7fe11b6b8 100644 --- a/backend/src/server.js +++ b/backend/src/server.js @@ -107,6 +107,11 @@ cacheService.loadDataToCache( constants.CACHE_KEYS.AUTHORITY_CONTACT_TYPES, 'ser }).catch((e) => { log.error('Error loading AUTHORITY_CONTACT_TYPES data during boot .', e); }); +cacheService.loadDataToCache( constants.CACHE_KEYS.SDC_FUNDING_GROUPS, 'sdc:fundingGroupsURL').then(() => { + log.info('Loaded FUNDING_GROUPS data to memory'); +}).catch((e) => { + log.error('Error loading FUNDING_GROUPS data during boot .', e); +}); cacheService.loadAllAuthoritiesToMap().then(() => { log.info('Loaded authorities data to memory'); diff --git a/backend/src/util/constants.js b/backend/src/util/constants.js index 7fab08455..3df3c177b 100644 --- a/backend/src/util/constants.js +++ b/backend/src/util/constants.js @@ -18,7 +18,8 @@ cacheKeys = { SCHOOL_FACILITY_TYPES: 'school_facilityTypes', SCHOOL_NEIGHBOURHOOD_LEARNING_TYPES: 'school-neighborhood-learning-types', SCHOOL_ORGANIZATION_TYPES: 'school-organization-types', - SCHOOL_REPORTING_REQUIREMENT_CODES: 'school-reporting-requirement-codes' + SCHOOL_REPORTING_REQUIREMENT_CODES: 'school-reporting-requirement-codes', + SDC_FUNDING_GROUPS: 'sdc_funding_groups' }; const CACHE_KEYS = Object.freeze(cacheKeys); diff --git a/frontend/src/common/apiService.js b/frontend/src/common/apiService.js index cf913f1d7..8e86ce15e 100644 --- a/frontend/src/common/apiService.js +++ b/frontend/src/common/apiService.js @@ -117,6 +117,7 @@ export default { getAllActiveInstituteGradeCodes: getCodes(`${Routes.cache.GRADE_TYPES_URL}?active=true`), getAllActiveInstituteProvinceCodes: getCodes(`${Routes.cache.PROVINCES_URL}?active=true`), getAllActiveInstituteCountryCodes: getCodes(`${Routes.cache.COUNTRIES_URL}?active=true`), + getAllFundingGroups: getCodes(`${Routes.sdc.FUNDING_DATA_URL}`), async getConfig() { try { const response = await apiAxios.get(Routes.CONFIG); diff --git a/frontend/src/components/institute/common/SchoolFunding.vue b/frontend/src/components/institute/common/SchoolFunding.vue index a22dae7f1..6591e673c 100644 --- a/frontend/src/components/institute/common/SchoolFunding.vue +++ b/frontend/src/components/institute/common/SchoolFunding.vue @@ -66,7 +66,7 @@ this.loadFundingGroups()); - }, mounted() { + instituteStore().getAllGradeCodes(); appStore().getCodes().then(() => { + this.schoolFundingGroups = this.fundingGroups; this.getHistoricalCollectionsForSchool(); this.loadSchoolsFundingData(); }); @@ -275,20 +274,9 @@ export default { mapFundingData(incomingFundingData) { incomingFundingData.gradeLabel = this.gradeCodes?.find(grade => grade?.schoolGradeCode === incomingFundingData?.schoolGradeCode)?.label; incomingFundingData.displayOrder = this.gradeCodes?.find(grade => grade?.schoolGradeCode === incomingFundingData?.schoolGradeCode)?.displayOrder; - incomingFundingData.fundingGroupLabel = this.fundingGroups?.find(group => group.schoolFundingGroupCode === incomingFundingData.schoolFundingGroupCode)?.label; + incomingFundingData.fundingGroupLabel = this.schoolFundingGroups?.find(group => group.schoolFundingGroupCode === incomingFundingData.schoolFundingGroupCode)?.label; incomingFundingData.updateDate = this.formatDate(incomingFundingData.updateDate); }, - loadFundingGroups() { - ApiService.apiAxios.get(`${Routes.sdc.FUNDING_DATA_URL}`) - .then(response => { - this.fundingGroups = response.data; - }).catch(error => { - console.error(error); - this.setFailureAlert(error.response?.data?.message || error.message); - }).finally(() => { - this.loading = false; - }); - }, getHistoricalCollectionsForSchool() { ApiService.apiAxios.get(`${Routes.sdc.SDC_SCHOOL_COLLECTION}/${this.schoolID}`) .then(response => { diff --git a/frontend/src/store/modules/app.js b/frontend/src/store/modules/app.js index ebff37528..7ba56f09d 100644 --- a/frontend/src/store/modules/app.js +++ b/frontend/src/store/modules/app.js @@ -26,7 +26,9 @@ export const appStore = defineStore('app', { alertNotificationText: '', alertNotificationQueue: [], alertNotification: false, - config: '' + config: '', + fundingGroupsMap: new Map(), + fundingGroups: [] }), actions: { async setConfig(config){ @@ -107,6 +109,13 @@ export const appStore = defineStore('app', { this.schoolApiDistrictCodes.add(element.mincode?.substring(0, 3)); }); }, + async setFundingGroups(fundingGroups) { + this.fundingGroups = fundingGroups; + this.fundingGroupsMap = new Map(); + fundingGroups.forEach(element => { + this.fundingGroupsMap.set(element.schoolFundingGroupCode, element); + }); + }, async getCodes() { if(localStorage.getItem('jwtToken')) { // DONT Call api if there is not token. if(this.mincodeSchoolNames.size === 0) { @@ -133,6 +142,10 @@ export const appStore = defineStore('app', { const response = await ApiService.getSchoolApiMincodeSchoolNames(); await this.setSchoolApiMincodeSchoolNameAndDistrictCodes(response.data); } + if(this.fundingGroupsMap.size === 0) { + const response = await ApiService.getAllFundingGroups(); + await this.setFundingGroups(response.data); + } } }, async getConfig() { @@ -158,6 +171,9 @@ export const appStore = defineStore('app', { const responseSchoolApiMin = await ApiService.getSchoolApiMincodeSchoolNames(); await this.setSchoolApiMincodeSchoolNameAndDistrictCodes(responseSchoolApiMin.data); + + const responseFunding = await ApiService.getAllFundingGroups(); + await this.setFundingGroups(responseFunding.data); } }, },