-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db8005d
commit f7fda73
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.