Skip to content

Commit

Permalink
Merge pull request #97 from bcgov/extractfixes
Browse files Browse the repository at this point in the history
Extractfixes
  • Loading branch information
michaeltangbcgov authored Nov 16, 2023
2 parents 0785025 + 3c83cb8 commit 095b857
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 73 deletions.
80 changes: 38 additions & 42 deletions backend/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ function addDistrictLabels(jsonData, districtList) {

function sortJSONByDistrictNumber(districts) {
return districts.slice().sort((a, b) => {
const districtNumberA = a.districtNumber || '';
const districtNumberB = b.districtNumber || '';
const districtNumberA = a['District Number'] || '';
const districtNumberB = b['District Number'] || '';
return districtNumberA.localeCompare(districtNumberB, undefined, { numeric: true, sensitivity: 'base' });
});
}
Expand All @@ -197,7 +197,15 @@ function addDistrictLabels(jsonData, districtList) {
const schoolCodeB = b.mincode || '';
return schoolCodeA.localeCompare(schoolCodeB, undefined, { numeric: true, sensitivity: 'base' });
});
}
}

function sortByProperty(arr, propertyName, options = { numeric: true, sensitivity: 'base' }) {
return arr.slice().sort((a, b) => {
const valueA = a[propertyName] || '';
const valueB = b[propertyName] || '';
return valueA.localeCompare(valueB, undefined, options);
});
}

function rearrangeAndRelabelObjectProperties(object, propertyList) {
const reorderedObject = {};
Expand Down Expand Up @@ -227,6 +235,30 @@ function addDistrictLabels(jsonData, districtList) {
return item;
});
}
function filterByField(jsonArray, fieldName, stringsToRemove) {
// Filter the array based on the condition
const filteredArray = jsonArray.filter(item => {
// Extract the field value (or use an empty string if the field is not present)
const fieldValue = item[fieldName] || '';

// Check if the fieldValue exactly matches any string from the stringsToRemove array
return !stringsToRemove.includes(fieldValue);
});

return filteredArray;
}
function getArrayofNonPubliclyAvailableCodes(codes, field) {
if (!Array.isArray(codes)) {
throw new Error('Invalid input. Expecting an array of objects.');
}

// Filter out objects where "publiclyAvailable" is false
const nonPubliclyAvailableCodes = codes
.filter(item => item && item.publiclyAvailable !== true)
.map(item => item[field]);

return nonPubliclyAvailableCodes;
}
function createSchoolCache(schoolData, schoolGrades) {
// Preload convertedGrades with schoolGrades.schoolGradeCode and set the value to "N"

Expand Down Expand Up @@ -314,44 +346,8 @@ function addDistrictLabels(jsonData, districtList) {

// Remove the contacts property
delete school.contacts;
const propertyOrder = [

{ property: "displayName", label: "School Name" },
{ property: "mincode", label: "mincode" },
{ property: "districtNumber", label: "District Number" },
{ property: "districtName", label: "District Name" },

{ property: "mailing_addressLine1", label: "Address" },
{ property: "mailing_city", label: "City" },
{ property: "mailing_provinceCode", label: "Province" },
{ property: "mailing_postal", label: "Postal Code" },
// { property: "principalTitle", label: "Principal Title" },
{ property: "firstName", label: "Principal First Name" },
{ property: "lastName", label: "Principal Last Name" },
{ property: "schoolCategoryCode", label: "Type" },
// { property: "gradeRange", label: "Grade Range" },
// { property: "schoolCategory", label: "School Category" },
// { property: "fundingGroups", label: "Funding Group(s)" },
{ property: "phoneNumber", label: "Phone" },
{ property: "faxNumber", label: "Fax" },
{ property: "email", label: "Email" },
{ property: "GRADE01", label: "Grade 1 Enrollment" },
{ property: "GRADE02", label: "Grade 2 Enrollment" },
{ property: "GRADE03", label: "Grade 3 Enrollment" },
{ property: "GRADE04", label: "Grade 4 Enrollment" },
{ property: "GRADE05", label: "Grade 5 Enrollment" },
{ property: "GRADE06", label: "Grade 6 Enrollment" },
{ property: "GRADE07", label: "Grade 7 Enrollment" },
{ property: "GRADE08", label: "Grade 8 Enrollment" },
{ property: "GRADE09", label: "Grade 9 Enrollment" },
{ property: "GRADE10", label: "Grade 10 Enrollment" },
{ property: "GRADE11", label: "Grade 11 Enrollment" },
{ property: "GRADE12", label: "Grade 12 Enrollment" }


];
const schools = rearrangeAndRelabelObjectProperties(school,propertyOrder)
return schools;

return school;
});
}
module.exports = {appendMailingAddressDetailsAndRemoveAddresses,sortJSONBySchoolCode,sortJSONByDistrictNumber,normalizeJsonObject, removeFieldsByCriteria, createList, isSafeFilePath,isAllowedSchoolCategory, addDistrictLabels, districtNumberSort, createSchoolCache, formatGrades, rearrangeAndRelabelObjectProperties};
module.exports = {sortByProperty,getArrayofNonPubliclyAvailableCodes,filterByField,appendMailingAddressDetailsAndRemoveAddresses,sortJSONBySchoolCode,sortJSONByDistrictNumber,normalizeJsonObject, removeFieldsByCriteria, createList, isSafeFilePath,isAllowedSchoolCategory, addDistrictLabels, districtNumberSort, createSchoolCache, formatGrades, rearrangeAndRelabelObjectProperties};
24 changes: 11 additions & 13 deletions backend/src/routes/authority-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require("fs");
const path = require("path");
const { checkToken } = require("../components/auth");
const { listCache } = require("../components/cache");
const {appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties} = require("../components/utils.js")
const {appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, sortByProperty} = require("../components/utils.js")
//Batch Routes
router.get("/all-mailing/:type", checkToken, getAllAuthorityMailing);
router.get("/:id", checkToken, getAuthority);
Expand All @@ -32,6 +32,7 @@ router.get("/:id", checkToken, getAuthority);
// }

async function getAllAuthorityMailing(req, res) {
//type = OFFSHORE or INDEPENDNT
const {type} = req.params

const params = [
Expand Down Expand Up @@ -66,29 +67,26 @@ async function getAllAuthorityMailing(req, res) {
const propertyOrder = [
{ property: "authorityNumber", label: "Number" },
{ property: "displayName", label: "Name" },
{ property: "physicalAddressLine1", label: "Address" },
{ property: "physicalAddressLine2", label: "Address Line 2" },
{ property: "physicalCity", label: "City" },
{ property: "physicalProvinceCode", label: "Province" },
{ property: "physicalPostal", label: "Postal Code" },
{ property: "mailingAddressLine1", label: "Address" },
{ property: "mailingAddressLine2", label: "Address Line 2" },
{ property: "mailingCity", label: "City" },
{ property: "mailingProvinceCode", label: "Province" },
{ property: "mailingPostal", label: "Postal Code" },
{ property: "phoneNumber", label: "Phone Number" },
{ property: "faxNumber", label: "Fax" },
{ property: "email", label: "Email" },

];

authorityResponse.data.content.forEach(appendMailingAddressDetailsAndRemoveAddresses);
// const includedFields = ['createUser', 'updateUser', '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 = sortJSONByDistrictNumber(content)
authorityResponse.data.content.forEach((currentElement, index, array) => {

authorityResponse.data.content.forEach((currentElement, index, array) => {
const rearrangedElement = rearrangeAndRelabelObjectProperties(currentElement, propertyOrder);
array[index] = rearrangedElement;
});

const authorityResponseSorted = sortByProperty(authorityResponse.data.content, 'Number')

res.json(authorityResponse.data.content);
res.json(authorityResponseSorted);
//res.json(districtContactsReorderedAndRelabeled );
} catch (e) {
log.error("getData Error", e.response ? e.response.status : e.message);
Expand Down
24 changes: 19 additions & 5 deletions backend/src/routes/district-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require("fs");
const path = require("path");
const { checkToken } = require("../components/auth");
const { listCache } = require("../components/cache");
const {appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, addDistrictLabels, normalizeJsonObject, sortJSONByDistrictNumber} = require("../components/utils.js")
const {getArrayofNonPubliclyAvailableCodes, filterByField,appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, addDistrictLabels, normalizeJsonObject, sortJSONByDistrictNumber} = require("../components/utils.js")

//Batch Routes
router.get("/all-contacts", checkToken, getAllDistrictContacts);
Expand Down Expand Up @@ -133,13 +133,20 @@ async function getAllDistrictContacts(req, res) {

const includedFields = ['createUser', 'updateUser', '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 = sortJSONByDistrictNumber(content)
content.forEach((currentElement, index, array) => {
content = normalizeJsonObject(content, districtList, 'districtId', null, ['displayName', 'districtNumber']);
//console.log(contactTypeCodes.codesList.districtContactTypeCodes)
//console.log(getArrayofNonPubliclyAvailableCodes(contactTypeCodes.codesList.districtContactTypeCodes, "districtContactTypeCode"))
//let filteredData = filterByField(content, 'districtContactTypeCode', getArrayofNonPubliclyAvailableCodes(contactTypeCodes.codesList.districtContactTypeCodes, "districtContactTypeCode"));
let filteredData = filterByField(content, 'districtNumber', ['']);
filteredData.forEach((currentElement, index, array) => {
const rearrangedElement = rearrangeAndRelabelObjectProperties(currentElement, propertyOrder);
array[index] = rearrangedElement;
});
res.json(content);
let sortedData = sortJSONByDistrictNumber(filteredData)



res.json(sortedData);
//res.json(districtContactsReorderedAndRelabeled );
} catch (e) {
log.error("getData Error", e.response ? e.response.status : e.message);
Expand Down Expand Up @@ -230,6 +237,13 @@ async function getDistrict(req, res) {
valueType: "UUID",
condition: "AND",
},
{
key: "closedDate",
operation: "eq",
value: null,
valueType: "STRING",
condition: "AND",
},
],
},
];
Expand Down
51 changes: 49 additions & 2 deletions backend/src/routes/school-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const log = require("../components/logger");
const config = require("../config/index");
const axios = require("axios");

const { createSchoolCache, addDistrictLabels, formatGrades, sortJSONBySchoolCode} = require("../components/utils");
const { createSchoolCache, addDistrictLabels, formatGrades, sortJSONBySchoolCode, rearrangeAndRelabelObjectProperties} = require("../components/utils");
const { checkToken } = require("../components/auth");
const { schoolCache, listCache, codeCache } = require("../components/cache");

//Batch Routes
router.get("/all/mailing", checkToken, getAllSchoolMailing);
router.get("/all/:schoolCategory", checkToken, getAllSchools);
router.get("/all-mailing", checkToken, getAllSchools);
router.get("/:schoolId", checkToken, getSchool);

async function getSchool(req, res) {
Expand All @@ -36,7 +36,12 @@ async function getSchool(req, res) {
});

}
async function getAllSchoolMailing(req, res) {
const allSchools = await getAllSchools(req,res)
console.log(allSchools)
res.json(allSchools)

}
async function getAllSchools(req, res) {

const {schoolCategory} = req.params
Expand Down Expand Up @@ -96,6 +101,48 @@ async function getAllSchools(req, res) {

const openSchoolListWithDistrictLabels = addDistrictLabels(response.data, districtList)
const openSchoolList = sortJSONBySchoolCode(createSchoolCache(openSchoolListWithDistrictLabels.content, schoolGrades));
const propertyOrder = [

{ property: "displayName", label: "School Name" },
{ property: "mincode", label: "School Code" },
{ property: "districtName", label: "District Name" },
{ property: "districtNumber", label: "District Number" },

{ property: "mailing_addressLine1", label: "Address" },
{ property: "mailing_city", label: "City" },
{ property: "mailing_provinceCode", label: "Province" },
{ property: "mailing_postal", label: "Postal Code" },
// { property: "principalTitle", label: "Principal Title" },
{ property: "firstName", label: "Principal First Name" },
{ property: "lastName", label: "Principal Last Name" },
{ property: "schoolCategoryCode", label: "School Category" },
// { property: "gradeRange", label: "Grade Range" },
// { property: "fundingGroups", label: "Funding Group(s)" },
{ property: "phoneNumber", label: "Phone" },
{ property: "faxNumber", label: "Fax" },
{ property: "email", label: "Email" },
{ property: "KINDHALF", label: "Kindergarten Half Enrollment" },
{ property: "KINDFULL", label: "Kindergarten Full Enrollment" },
{ property: "GRADE01", label: "Grade 1 Enrollment" },
{ property: "GRADE02", label: "Grade 2 Enrollment" },
{ property: "GRADE03", label: "Grade 3 Enrollment" },
{ property: "GRADE04", label: "Grade 4 Enrollment" },
{ property: "GRADE05", label: "Grade 5 Enrollment" },
{ property: "GRADE06", label: "Grade 6 Enrollment" },
{ property: "GRADE07", label: "Grade 7 Enrollment" },
{ property: "GRADE08", label: "Grade 8 Enrollment" },
{ property: "GRADE09", label: "Grade 9 Enrollment" },
{ property: "GRADE10", label: "Grade 10 Enrollment" },
{ property: "GRADE11", label: "Grade 11 Enrollment" },
{ property: "GRADE12", label: "Grade 12 Enrollment" }


];

openSchoolList.forEach((currentElement, index, array) => {
const rearrangedElement = rearrangeAndRelabelObjectProperties(currentElement, propertyOrder);
array[index] = rearrangedElement;
});
res.json(openSchoolList);
schoolCache.set("openschoollist" + schoolCategory, openSchoolList);
log.info(req.url);
Expand Down
12 changes: 2 additions & 10 deletions frontend/src/components/AuthoritySelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,8 @@ function downloadAuthorityContacts() {
href="/api/v1/download/csv/authority/all-mailing/INDEPENDNT?filepath=authoritymailing"
variant="text"
class="v-btn-align-left text-none text-subtitle-1 my-1"
><template v-slot:prepend> <v-icon icon="mdi-download" /> </template> Mailing for All
Authorities (CSV)</v-btn
>
<v-btn
href="/api/v1/download/csv/institute/authority?filepath=authoritycontacts"
variant="text"
class="v-btn-align-left text-none text-subtitle-1 my-1"
><template v-slot:prepend> <v-icon icon="mdi-download" /> </template>

Contacts for All Authorities (CSV)</v-btn
><template v-slot:prepend> <v-icon icon="mdi-download" /> </template> Contact
information for all Authorities (CSV)</v-btn
>
</v-row>
</v-col>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ContactTypeModel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const searchContact = async () => {
</v-card-actions>
</v-card-text>
<v-row>
<v-col class="ma-2">TOTAL: {{ results }}</v-col>
<v-col class="ma-2">Total: {{ results }}</v-col>
<v-col></v-col>
<v-col>
<v-btn
Expand Down

0 comments on commit 095b857

Please sign in to comment.