Skip to content

Commit

Permalink
cleande up site and extracts
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlumbcgov committed Nov 20, 2023
1 parent 054bba3 commit 0bf853b
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 66 deletions.
6 changes: 4 additions & 2 deletions backend/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,18 @@ function addDistrictLabels(jsonData, districtList) {
return reorderedObject;
}
function normalizeJsonObject(sourceArray, referenceArray, matchKey, condition, includeFields) {

return sourceArray.map((item) => {
const matchingItem = referenceArray.find(
(info) => info[matchKey] === item[matchKey] && (!condition || condition(info))
);

if (matchingItem) {
return {
...item,
...includeFields.reduce((result, field) => {
result[field] = matchingItem[field];
result[matchKey + "_" + field] = matchingItem[field];


return result;
}, {}),
};
Expand Down
20 changes: 0 additions & 20 deletions backend/src/routes/authority-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ router.get("/all-mailing/:type", checkToken, getAllAuthorityMailing);
router.get("/:id", checkToken, getAuthority);
//router.get("/all-contacts", checkToken, getAllAuthorityContacts);


// async function removeItemsFromDistrictDataResponse(response, itemsToRemove) {
// if (response && response.data) {
// const newData = { ...response.data };

// if (itemsToRemove && Array.isArray(itemsToRemove)) {
// itemsToRemove.forEach(item => {
// if (newData[item]) {
// delete newData[item];
// }
// });
// }

// response.data = newData;
// }
// }

async function getAllAuthorityMailing(req, res) {
//type = OFFSHORE or INDEPENDNT
const {type} = req.params
Expand Down Expand Up @@ -150,9 +133,6 @@ async function getAuthority(req, res) {
const authoritySchoolsResponse = await axios.get(authoritySchoolsUrl, {
headers: { Authorization: `Bearer ${req.accessToken}` },
});
// const contactTypeCodes = await getDistrictCodes(req)
// const nonPublicContactTypeCodes = getNonPublicContactTypeCodes(contactTypeCodes)
// const districtDataPublic = removeContacts(districtDataResponse.data,nonPublicContactTypeCodes)
const authorityJSON = {
authorityData: authorityDataResponse.data,
authoritySchools: authoritySchoolsResponse.data.content,
Expand Down
35 changes: 25 additions & 10 deletions backend/src/routes/district-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function removeContacts(districtDataResponse, nonPublicContactTypeCodes) {
async function getAllDistrictContacts(req, res) {
const districtList = await listCache.get("districtlist")
const contactTypeCodes= await listCache.get("codesList")
const districtAddresses = await listCache.get("districtAddresses")
let currentDate = new Date().toISOString().substring(0, 19)
const params = [
{
Expand Down Expand Up @@ -154,35 +155,49 @@ async function getAllDistrictContacts(req, res) {
headers: { Authorization: `Bearer ${req.accessToken}` },
});
const propertyOrder = [
{ property: "districtNumber", label: "District Number" },
{ property: "displayName", label: "District Name" },
{ property: "districtId_districtNumber", label: "District Number" },
{ property: "districtId_displayName", label: "District Name" },
{ property: "description", label: "District Contact" },
{ property: "firstName", label: "Contact First Name" },
{ property: "lastName", label: "Contact Last name" },
{ property: "jobTitle", label: "Position Title" },
{ property: "label", label: "Contact Type" },
{ property: "districtId_mailing_addressLine1", label: "Address Line 1" },
{ property: "districtId_mailing_addressLine2", label: "Address Line 2" },
{ property: "districtId_mailing_city", label: "City" },
{ property: "districtId_mailing_provinceCode", label: "Province" },
{ property: "districtId_mailing_postal", label: "Postal Code" },
{ property: "districtId_mailing_countryCode", label: "Country" },
{ property: "districtId_physical_addressLine1", label: "Courier Address Line 1" },
{ property: "districtId_physical_addressLine2", label: "Courier Address Line 2" },
{ property: "districtId_physical_city", label: "Courier City" },
{ property: "districtId_physical_provinceCode", label: "Courier Province" },
{ property: "districtId_physical_postal", label: "Courier Postal Code" },
{ property: "districtId_physical_countryCode", label: "Courier Country" },
{ property: "phoneNumber", label: "Contact Phone" },
{ property: "phoneExtension", label: "Contact Phone Extension" },
{ property: "email", label: "Contact Email" },

];

//let content = addDistrictLabels(districtContactResponse.data,districtList);

const includedFields = ['createUser', 'updateUser', 'districtContactTypeCode', 'label', 'description'];
const includedFields = ['districtContactTypeCode', 'label', 'description'];
let content = normalizeJsonObject(districtContactResponse.data.content, contactTypeCodes.codesList.districtContactTypeCodes, 'districtContactTypeCode', (info) => info.publiclyAvailable === true, includedFields);
content = normalizeJsonObject(content, districtList, 'districtId', null, ['displayName', 'districtNumber']);
content = normalizeJsonObject(content, districtAddresses, 'districtId', null, ['mailing_addressLine1','mailing_addressLine2','mailing_city','mailing_postal','mailing_provinceCode','mailing_countryCode','mailing_districtAddressId','mailing_districtId','physical_addressLine1','physical_addressLine2','physical_city','physical_postal','physical_provinceCode','physical_countryCode']);
content = normalizeJsonObject(content, districtList, 'districtId', null, ['displayName', 'districtNumber']);
content = filterByPubliclyAvailableCodes(content,"districtContactTypeCode",getArrayofPubliclyAvailableCodes(contactTypeCodes.codesList.districtContactTypeCodes, "districtContactTypeCode"))
content = filterByExpiryDate(content)


let filteredData = filterByField(content, 'districtNumber', ['']);
let filteredData = filterByField(content, 'districtId_districtNumber', ['']);
filteredData.forEach((currentElement, index, array) => {
const rearrangedElement = rearrangeAndRelabelObjectProperties(currentElement, propertyOrder);
array[index] = rearrangedElement;
});
let sortedData = sortJSONByDistrictNumber(filteredData)
const validDistricts = filterRemoveByField(sortedData,"District Number", ["098","102","103"])

res.json(validDistricts);
//res.json(districtContactsReorderedAndRelabeled );

} catch (e) {
log.error("getData Error", e.response ? e.response.status : e.message);
}
Expand All @@ -193,8 +208,8 @@ async function getAllDistrictMailing(req, res) {
const contactTypeCodes= await listCache.get("codesList")

const propertyOrder = [
{ property: "districtNumber", label: "District Number" },
{ property: "displayName", label: "District Name" },
{ property: "districtId_districtNumber", label: "District Number" },
{ property: "districtId_displayName", label: "District Name" },
{ property: "mailingAddressLine1", label: "Address Line 1" },
{ property: "mailingAddressLine2", label: "Address Line 2" },
{ property: "mailingCity", label: "City" },
Expand Down
146 changes: 146 additions & 0 deletions backend/src/routes/institute-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,153 @@ router.get("/school/list", checkToken, getSchoolList);
router.get("/authority/list", checkToken, getAuthorityList);
router.get("/district/list", checkToken, getDistrictList);
router.get("/district/contact/*", checkToken, getDistrictContactsAPI);
router.get("/create-cache", checkToken, createCache);
router.get("/*", checkToken, getInstituteAPI);
async function createCache(req, res) {

if (!listCache.has("categoryCodes")) {
//const codes = [];

try {
const categoryCodesResponse = await axios.get(
`${config.get(
"server:instituteAPIURL"
)}/institute/category-codes`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);
listCache.set("categoryCodes", categoryCodesResponse.data);
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("Category Code Caching Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
}

if (!listCache.has("facilityCodes")) {
//const codes = [];

try {
const facilityCodesResponse = await axios.get(
`${config.get(
"server:instituteAPIURL"
)}/institute/facility-codes`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);
listCache.set("facilityCodes", facilityCodesResponse.data);
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("Faility Code Caching Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
}
if (!listCache.has("districtAddresses")) {
//const codes = [];

try {
const districtsResponse = await axios.get(
`${config.get("server:instituteAPIURL")}/institute/district/paginated?pageSize=200`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);

districtsResponse.data.content.forEach((district) => {
district.addresses.forEach((address) => {

if (address.addressTypeCode === "MAILING") {
Object.keys(address).forEach((field) => {
// Exclude the specified fields
if (![
"createUser",
"updateUser",
"createDate",
"updateDate",
"schoolAddressId",
"schoolId",
"addressTypeCode"
].includes(field)) {
district[`mailing_${field}`] = address[field];
}
});
} else if (address.addressTypeCode === "PHYSICAL") {
Object.keys(address).forEach((field) => {
if (![
"createUser",
"updateUser",
"createDate",
"updateDate",
"schoolAddressId",
"schoolId",
"addressTypeCode"
].includes(field)) {
district[`physical_${field}`] = address[field];
}
});
}
});
});
listCache.set("districtAddresses", districtsResponse.data.content);
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("District Code Caching Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
}


if (await !listCache.has("codesList")) {
//const codes = [];

try {
const authorityContactTypeCodesResponse = await axios.get(
`${config.get(
"server:instituteAPIURL"
)}/institute/authority-contact-type-codes`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);

const districtContactTypeCodesResponse = await axios.get(
`${config.get(
"server:instituteAPIURL"
)}/institute/district-contact-type-codes`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);

const schoolContactTypeCodesResponse = await axios.get(
`${config.get(
"server:instituteAPIURL"
)}/institute/school-contact-type-codes`,
{
headers: { Authorization: `Bearer ${req.accessToken}` },
}
);

const codes = {
authorityContactTypeCodes: removeFieldsByCriteria(authorityContactTypeCodesResponse.data, [{ fieldToRemove: "publiclyAvailable", value: false }]),
districtContactTypeCodes: removeFieldsByCriteria(districtContactTypeCodesResponse.data,[{ fieldToRemove: "publiclyAvailable", value: false }]),
schoolContactTypeCodes: removeFieldsByCriteria(schoolContactTypeCodesResponse.data,[{ fieldToRemove: "publiclyAvailable", value: false }]),
};
res.json(codes);
listCache.set("codesList", { codesList: codes });
} catch (error) {
const statusCode = error.response ? error.response.status : 500;
log.error("getSchoolsList Error", statusCode, error.message);
res.status(statusCode).send(error.message);
}
listCache.set("codesList", codes);
}
res.status(200).json({ success: true });

}


async function getContactTypeCodes(req, res) {
if (await !listCache.has("codesList")) {
Expand Down
Loading

0 comments on commit 0bf853b

Please sign in to comment.