Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlumbcgov committed Aug 9, 2023
1 parent db8005d commit f7fda73
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
47 changes: 47 additions & 0 deletions backend/src/components/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const NodeCache = require("node-cache");
const axios = require("axios");
const config = require("../config/index");

const tokenCache = new NodeCache({
stdTTL: config.get("server:instituteAPITokenExpiry"),
});

const clientId = config.get("oidc:clientId");
const clientSecret = config.get("oidc:clientSecret");
const tokenEndpoint = config.get("oidc:tokenEndpoint");

const data = {
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
};

async function getNewToken() {
try {
const response = await axios.post(tokenEndpoint, data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});

const accessToken = response.data.access_token;
tokenCache.set("token", accessToken);
} catch (error) {
console.error("Error:", error.response.data);
}
}

async function checkToken(req, res, next) {
try {
if (!tokenCache.has("token")) {
await getNewToken();
}
// Set the token as a property on the request object
req.accessToken = await tokenCache.get("token");
next();
} catch (error) {
console.log(error);
}
}

module.exports = { checkToken };
17 changes: 17 additions & 0 deletions backend/src/components/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function createSchoolList(list, fields) {
return list
.map(function (item) {
if (item.closedDate !== null) {
const itemData = {};
fields.forEach(field => {
itemData[field] = item[field];
});
return itemData;
}
})
.filter(function (item) {
return item !== undefined;
});
}

module.exports = { createSchoolList };
85 changes: 85 additions & 0 deletions backend/src/routes/district-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

const express = require("express");
const router = express.Router();
const log = require("../components/logger");
const config = require("../config/index");
const NodeCache = require("node-cache");
const axios = require("axios");
const { checkToken } = require("../components/auth");
const { createSchoolList } = require("../components/utils");


const listCache = new NodeCache({ stdTTL: 21600 });
const schoolListFields = ['mincode', 'displayName'];
//Batch Routes
router.get('/:id', checkToken, getDistrict);


async function getSchoolList(req, res) {

if(await !listCache.has("schoollist")){
console.log("GETTING NEW SCHOOL LIST")
const url = `${config.get('server:instituteAPIURL')}/institute/school`; // Update the URL according to your API endpoint
axios
.get(url, { headers: { Authorization: `Bearer ${req.accessToken}` } })
.then((response) => {
const schoolList = createSchoolList(response.data, schoolListFields);
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{
console.log("USING SCHOOL LIST CACHE")
schoolList = await listCache.get("schoollist")
res.json(schoolList)
}

}
//api/v1/institute/district/12342525
async function getDistrict(req, res) {
const { id } = req.params;

const params = [
{
condition: null,
searchCriteriaList: [
{
key: 'districtID',
operation: 'eq',
value: id,
valueType: 'UUID',
condition: 'AND'
}
]
}
];

const jsonString = JSON.stringify(params)
const encodedParams = encodeURIComponent(jsonString)

const url = `${config.get('server:instituteAPIURL')}/institute/district/${id}`;
const districtSchoolsUrl = `${config.get('server:instituteAPIURL')}/institute/school/paginated?pageNumber=1&pageSize=10&searchCriteriaList=${encodedParams}`;
//const districtSchoolsUrl = `${config.get('server:instituteAPIURL')}/institute/school/paginated?pageNumber=1&pageSize=10`;
console.log(districtSchoolsUrl)




try {
const districtDataResponse = await axios.get(url, { headers: { Authorization: `Bearer ${req.accessToken}` } });
const districtSchoolsResponse = await axios.get(districtSchoolsUrl, { headers: { Authorization: `Bearer ${req.accessToken}` } });
const districtJSON = {
districtData: districtDataResponse.data,
districtSchools: districtSchoolsResponse.data.content
};

res.json(districtJSON);
log.info(req.url);
} catch (e) {
log.error('getData Error', e.response ? e.response.status : e.message);
}
}
module.exports = router;
Empty file added backend/src/util/constants.js
Empty file.

0 comments on commit f7fda73

Please sign in to comment.