Skip to content

Commit

Permalink
Merge pull request #154 from bcgov/Release/v1.6.1
Browse files Browse the repository at this point in the history
Release/v1.6.1
  • Loading branch information
michaeltangbcgov authored Mar 8, 2024
2 parents f40235d + fd36786 commit 185a03b
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 252 deletions.
34 changes: 33 additions & 1 deletion backend/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,38 @@ function addDistrictLabels(jsonData, districtList) {

return nonPubliclyAvailableCodes;
}
function addFundingGroups(schools, fundingGroups) {
try {
// Process each school in the array
const schoolsWithFunding = schools.map(school => {
// Find the corresponding funding group by mincode
const matchingFundingGroup = fundingGroups.find(fundingGroup =>
fundingGroup.mincode === school.mincode
);

// Add fundingGroupCode to the school
if (matchingFundingGroup) {
Object.assign(school, {
fundingGroupCode: matchingFundingGroup.fundingGroupCode,
});
} else {
// If mincode is not found in fundingGroups, add null values
Object.assign(school, {
fundingGroupCode: null,
});
}

return school;
});

return schoolsWithFunding;
} catch (error) {
// Handle the error here, you can log it or perform other actions
console.error("An error occurred in addFundingGroups:", error);
// Optionally, you can rethrow the error if needed
throw error;
}
}
function getArrayofPubliclyAvailableCodes(codes, field) {
if (!Array.isArray(codes)) {
throw new Error('Invalid input. Expecting an array of objects.');
Expand Down Expand Up @@ -377,4 +409,4 @@ function addDistrictLabels(jsonData, districtList) {
return school;
});
}
module.exports = {filterByOpenedAndClosedDate, filterByPubliclyAvailableCodes, getArrayofPubliclyAvailableCodes, filterByExpiryDate, filterRemoveByField,filterIncludeByField, sortByProperty,getArrayofNonPubliclyAvailableCodes,filterByField,appendMailingAddressDetailsAndRemoveAddresses,sortJSONBySchoolCode,sortJSONByDistrictNumber,normalizeJsonObject, removeFieldsByCriteria, createList, isSafeFilePath,isAllowedSchoolCategory, addDistrictLabels, districtNumberSort, createSchoolCache, formatGrades, rearrangeAndRelabelObjectProperties};
module.exports = {addFundingGroups, filterByOpenedAndClosedDate, filterByPubliclyAvailableCodes, getArrayofPubliclyAvailableCodes, filterByExpiryDate, filterRemoveByField,filterIncludeByField, sortByProperty,getArrayofNonPubliclyAvailableCodes,filterByField,appendMailingAddressDetailsAndRemoveAddresses,sortJSONBySchoolCode,sortJSONByDistrictNumber,normalizeJsonObject, removeFieldsByCriteria, createList, isSafeFilePath,isAllowedSchoolCategory, addDistrictLabels, districtNumberSort, createSchoolCache, formatGrades, rearrangeAndRelabelObjectProperties};
1 change: 1 addition & 0 deletions backend/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nconf.defaults({
maxAge: +process.env.SESSION_MAX_AGE
},
instituteAPIURL: process.env.INSTITUTE_API_URL,
schoolsAPIURL: process.env.SCHOOLS_API_URL,
instituteAPITokenExpiry: process.env.INSTITUTE_API_EXPIRY,
clearFilesKey: process.env.CLEAR_FILES_KEY
},
Expand Down
67 changes: 12 additions & 55 deletions backend/src/routes/district-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,8 @@ const axios = require("axios");
const fs = require("fs");
const path = require("path");
const { checkToken } = require("../components/auth");
const { listCache } = require("../components/cache");
const {
getArrayofPubliclyAvailableCodes,
filterRemoveByField,
filterByExpiryDate,
getArrayofNonPubliclyAvailableCodes,
filterByField,
appendMailingAddressDetailsAndRemoveAddresses,
rearrangeAndRelabelObjectProperties,
addDistrictLabels,
normalizeJsonObject,
sortJSONByDistrictNumber,
removeFieldsByCriteria,
filterByPubliclyAvailableCodes,
} = require("../components/utils.js");
const { listCache} = require("../components/cache");
const {addFundingGroups, getArrayofPubliclyAvailableCodes,filterRemoveByField, filterByExpiryDate, getArrayofNonPubliclyAvailableCodes, filterByField,appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, addDistrictLabels, normalizeJsonObject, sortJSONByDistrictNumber, removeFieldsByCriteria, filterByPubliclyAvailableCodes} = require("../components/utils.js")

//Batch Routes
router.get("/all-contacts", checkToken, getAllDistrictContacts);
Expand Down Expand Up @@ -394,12 +381,13 @@ async function getDistrict(req, res) {
});

const contactTypeCodes = await getDistrictCodes(req);
const schoolCategoryCodes = await listCache.get("categoryCodes");
const facilityCodes = await listCache.get("facilityCodes");
const districtContactCodeTypes = await listCache.get("codesList");
const nonPublicContactTypeCodes =
getNonPublicContactTypeCodes(contactTypeCodes);

const schoolCategoryCodes = await listCache.get("categoryCodes")
const facilityCodes = await listCache.get("facilityCodes")
const fundingGroups = await listCache.get("fundingGroups")
const districtContactCodeTypes = await listCache.get("codesList")
const nonPublicContactTypeCodes = getNonPublicContactTypeCodes(contactTypeCodes);


const districtDataPublic = removeContacts(
districtDataResponse.data,
nonPublicContactTypeCodes
Expand All @@ -420,40 +408,9 @@ async function getDistrict(req, res) {
districtDataPublicWithLabels.contacts
);

districtSchoolsResponse.data.content = normalizeJsonObject(
districtSchoolsResponse.data.content,
schoolCategoryCodes,
"schoolCategoryCode",
null,
["label", "description"]
);
districtSchoolsResponse.data.content = normalizeJsonObject(
districtSchoolsResponse.data.content,
facilityCodes,
"faciltyTypeCode",
null,
["label", "description"]
);

const today = new Date();
const filteredSchoolsResponse = districtSchoolsResponse.data.content.filter(
(obj) => {
// if openedDate is a valid date is less than today, keep the object
const openedDate = new Date(obj.openedDate);

// If closedDate is a valid date greater than today, keep the object
const closedDate = new Date(obj.closedDate);

// return obj IF closedDate does not exist OR is after than current date
// AND openedDate exists AND is before current date
return (
(!obj.closedDate || closedDate > today) &&
obj.openedDate &&
openedDate < today
);
}
);

districtSchoolsResponse.data.content = normalizeJsonObject(districtSchoolsResponse.data.content, schoolCategoryCodes, "schoolCategoryCode", null, ["label", "description"]);
districtSchoolsResponse.data.content = normalizeJsonObject(districtSchoolsResponse.data.content, facilityCodes, "faciltyTypeCode", null, ["label", "description"]);
districtSchoolsResponse.data.content = addFundingGroups(districtSchoolsResponse.data.content, fundingGroups)
const districtJSON = {
districtData: districtDataPublicWithLabels,
districtSchools: filteredSchoolsResponse,
Expand Down
29 changes: 26 additions & 3 deletions backend/src/routes/institute-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,33 @@ router.get("/district/list", checkToken, getDistrictList);
router.get("/district/contact/*", checkToken, getDistrictContactsAPI);
router.get("/create-cache", checkToken, createCache);
router.get("/category-codes", checkToken, getCategoryCodes);

router.get("/*", checkToken, getInstituteAPI);

async function createCache(req, res) {
if (await !listCache.has("fundingGroups")) {
//const codes = [];

try {
const fundingGroupsResponse = await axios.get(
`${config.get(
"server:schoolsAPIURL"
)}/schools/fundingGroups`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);
listCache.set("fundingGroups", fundingGroupsResponse.data);
res.json(fundingGroupsResponse.data);
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("getFunding Groups Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
} else {
const cachedFundingGroupList = await listCache.get("fundingGroups");
res.json(cachedFundingGroupList);
}
if (await !listCache.has("districtlist")) {
const url = `${config.get("server:instituteAPIURL")}/institute/district`; // Update the URL according to your API endpoint
axios
Expand Down Expand Up @@ -243,7 +268,6 @@ async function createCache(req, res) {
log.error("getCodesList Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
listCache.set("codesList", codes);
}
res.status(200).json({ success: true });

Expand Down Expand Up @@ -287,14 +311,13 @@ async function getContactTypeCodes(req, res) {
districtContactTypeCodes: removeFieldsByCriteria(districtContactTypeCodesResponse.data,[{ fieldToRemove: "publiclyAvailable", value: false }]),
schoolContactTypeCodes: removeFieldsByCriteria(schoolContactTypeCodesResponse.data,[{ fieldToRemove: "publiclyAvailable", value: false }]),
};
res.json(codes);
listCache.set("codesList", { codesList: codes });
res.json(codes);
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("getContactCodeList Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
listCache.set("codesList", codes);
} else {
const cachedCodeList = await listCache.get("codesList");
res.json(cachedCodeList);
Expand Down
Loading

0 comments on commit 185a03b

Please sign in to comment.