Skip to content

Commit

Permalink
added school/list to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlumbcgov committed Jul 18, 2023
1 parent f9d65ab commit 8c5300a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/src/routes/institute-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const jsonwebtoken = require("jsonwebtoken");
const axios = require("axios");

const cache = new NodeCache({ stdTTL: config.get("server:instituteAPITokenExpiry") });
const listCache = new NodeCache({ stdTTL: 1800 });
const clientId = config.get("oidc:clientId");
const clientSecret = config.get("oidc:clientSecret")
const tokenEndpoint = config.get("oidc:tokenEndpoint")
Expand All @@ -19,8 +20,41 @@ const data = {
};

//Batch Routes
router.get('/school/list', checkToken, getSchoolList);
router.get('/*',checkToken, getInstituteAPI);


function createSchoolList(schools) {
return schools.map(function(school) {
return {
mincode: school.mincode,
displayName: school.displayName
};
});
}

async function getSchoolList(req, res) {
const memToken = await cache.get("token");
if(await !listCache.has("schoollist")){
const url = `${config.get('server:instituteAPIURL')}/institute/school`; // Update the URL according to your API endpoint
axios
.get(url, { headers: { Authorization: `Bearer ${memToken}` } })
.then((response) => {
const schoolList = createSchoolList(response.data)
res.json(schoolList);
listCache.set("schoollist", schoolList)
log.info(req.url);
})
.catch((e) => {
log.error('getSchoolsList Error', e.response ? e.response.status : e.message);
});
}else{
schoolList = await listCache.get("schoollist")
res.json(schoolList)
}

}

async function getNewToken(req, res, next) {
await axios
.post(tokenEndpoint, data, {
Expand All @@ -47,6 +81,7 @@ async function checkToken(req, res, next) {
}
}
async function getInstituteAPI(req, res) {
console.log("GET INSTITUTE")
const memToken = await cache.get("token");
const url = `${config.get('server:instituteAPIURL')}/institute` + req.url;
axios
Expand Down

0 comments on commit 8c5300a

Please sign in to comment.