Skip to content

Commit

Permalink
added aching for funding group code
Browse files Browse the repository at this point in the history
  • Loading branch information
SodhiA1 committed Aug 11, 2023
1 parent d3630a2 commit caa24b0
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 37 deletions.
24 changes: 24 additions & 0 deletions backend/src/components/sdc/sdc-cache.js
Original file line number Diff line number Diff line change
@@ -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
};
14 changes: 0 additions & 14 deletions backend/src/components/sdc/sdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -163,7 +150,6 @@ function hasSchoolAdminRole(req, school){


module.exports = {
getFundingGroupsForSchool,
getFundingGroupDataForSchool,
deleteFundingDataForSchool,
updateFundingDataForSchool,
Expand Down
6 changes: 4 additions & 2 deletions backend/src/routes/sdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ 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);
router.delete('/funding-groups/:schoolID/funding/:schoolFundingGroupID', passport.authenticate('jwt', {session: false}, undefined), auth.isLoggedInUser, extendSession, deleteFundingDataForSchool);
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;

5 changes: 5 additions & 0 deletions backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion backend/src/util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/common/apiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 7 additions & 19 deletions frontend/src/components/institute/common/SchoolFunding.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<v-col>
<v-select
v-model="item.item.raw[header.value]"
:items="fundingGroups"
:items="schoolFundingGroups"
item-value="schoolFundingGroupCode"
item-title="label"
variant="underlined"
Expand Down Expand Up @@ -168,7 +168,7 @@
<AddSchoolFunding
v-if="addFundingSheet"
:school-i-d="schoolID"
:funding-groups="fundingGroups"
:funding-groups="schoolFundingGroups"
:filteredgrade-codes="sortedGrades"
@closeAddFunding="addFundingSheet = !addFundingSheet"
@saveNewFundingData="saveNewFundingData"
Expand Down Expand Up @@ -226,7 +226,7 @@ export default {
hoveredOveredRow: null,
hoveredOveredHeader: null,
isValidFundingForm: false,
fundingGroups: [],
schoolFundingGroups: [],
isEditing: false,
collectionDates: ['2017/09/30', '2016/09/30'],
selectedCollectionDate: '',
Expand All @@ -240,14 +240,13 @@ export default {
};
},
computed: {
...mapState(appStore, ['schoolMap', 'districtMap', 'independentAuthorityMap']),
...mapState(appStore, ['fundingGroups']),
...mapState(instituteStore, ['gradeCodes']),
},
beforeCreate() {
instituteStore().getAllGradeCodes().then(() => this.loadFundingGroups());
},
mounted() {
instituteStore().getAllGradeCodes();
appStore().getCodes().then(() => {
this.schoolFundingGroups = this.fundingGroups;
this.getHistoricalCollectionsForSchool();
this.loadSchoolsFundingData();
});
Expand Down Expand Up @@ -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 => {
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/store/modules/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const appStore = defineStore('app', {
alertNotificationText: '',
alertNotificationQueue: [],
alertNotification: false,
config: ''
config: '',
fundingGroupsMap: new Map(),
fundingGroups: []
}),
actions: {
async setConfig(config){
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand All @@ -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);
}
},
},
Expand Down

0 comments on commit caa24b0

Please sign in to comment.