From a8ff0ab4afd6469d58605035b4c03decb89ed93e Mon Sep 17 00:00:00 2001 From: suzalflueck Date: Wed, 31 Jul 2024 13:32:41 -0700 Subject: [PATCH 1/2] Updated filtering on school, authority, and district routers --- backend/src/components/utils.js | 764 ++++++++++++++----------- backend/src/routes/authority-router.js | 69 ++- backend/src/routes/district-router.js | 126 ++-- backend/src/routes/school-router.js | 49 +- backend/src/util/constants.js | 0 5 files changed, 589 insertions(+), 419 deletions(-) delete mode 100644 backend/src/util/constants.js diff --git a/backend/src/components/utils.js b/backend/src/components/utils.js index ea0f424f..87e09f86 100644 --- a/backend/src/components/utils.js +++ b/backend/src/components/utils.js @@ -1,21 +1,20 @@ const ALLOWED_FILENAMES = new Set([ - - 'districtcontacts', - 'districtmailing', - 'publicschoolcontacts', - 'independentschoolcontacts', - 'allschoolcontacts', - 'allschoolmailing', - 'authoritycontacts', - 'authoritymailing', - 'offshoreschoolrepresentatives' + "districtcontacts", + "districtmailing", + "publicschoolcontacts", + "independentschoolcontacts", + "allschoolcontacts", + "allschoolmailing", + "authoritycontacts", + "authoritymailing", + "offshoreschoolrepresentatives", // Add more allowed filepaths as needed ]); const ALLOWED_SCHOOLCATEGORYCODES = new Set([ - 'PUBLIC', - 'INDEPEND', - 'OFFSHORE' + "PUBLIC", + "INDEPEND", + "OFFSHORE", // Add more allowed filepaths as needed ]); function isAllowedSchoolCategory(category) { @@ -31,17 +30,17 @@ function createList(list, options = {}) { fieldToInclude = null, // Updated option name valueToInclude = null, // Updated option name sortFunction = null, - sortField = null + sortField = null, } = options; const filteredList = list .filter(function (item) { // Change the condition from removal to inclusion - return (!fieldToInclude || item[fieldToInclude] === valueToInclude); + return !fieldToInclude || item[fieldToInclude] === valueToInclude; }) .map(function (item) { const itemData = {}; - fields.forEach(field => { + fields.forEach((field) => { itemData[field] = item[field]; }); return itemData; @@ -58,7 +57,6 @@ function createList(list, options = {}) { }); } - return filteredList; } function removeFieldsByCriteria(inputData, criteria) { @@ -79,355 +77,441 @@ function removeFieldsByCriteria(inputData, criteria) { } function appendMailingAddressDetailsAndRemoveAddresses(data) { if (data && data.addresses && data.addresses.length > 0) { - const physicalAddress = data.addresses.find(address => address.addressTypeCode === 'PHYSICAL'); - if (physicalAddress) { - // Extract specific name-value pairs from the mailing address - const { addressLine1, addressLine2, city, postal, provinceCode, countryCode } = physicalAddress; - - // Add these name-value pairs to the original district object - data.physicalAddressLine1 = addressLine1; - data.physicalAddressLine2 = addressLine2; - data.physicalCity = city; - data.physicalPostal = postal; - data.physicalProvinceCode = provinceCode; - data.physicalCountryCode = countryCode; - - // Remove the "addresses" property - - } - const courierAddress = data.addresses.find(address => address.addressTypeCode === 'MAILING'); - if (courierAddress) { - // Extract specific name-value pairs from the mailing address - const { addressLine1, addressLine2, city, postal, provinceCode, countryCode } = courierAddress; - - // Add these name-value pairs to the original district object - data.mailingAddressLine1 = addressLine1; - data.mailingAddressLine2 = addressLine2; - data.mailingCity = city; - data.mailingPostal = postal; - data.mailingProvinceCode = provinceCode; - data.mailingCountryCode = countryCode; - - // Remove the "addresses" property - } - delete data.addresses; - delete data.contacts; - } -} -function addDistrictLabels(jsonData, districtList) { - if (jsonData.content && Array.isArray(jsonData.content)) { - jsonData.content.forEach(dataItem => { - const district = districtList.find(item => item.districtId === dataItem.districtId); - if (district) { - dataItem.districtNumber = district.districtNumber; - dataItem.districtName = district.displayName; - } - }); + const physicalAddress = data.addresses.find( + (address) => address.addressTypeCode === "PHYSICAL" + ); + if (physicalAddress) { + // Extract specific name-value pairs from the mailing address + const { + addressLine1, + addressLine2, + city, + postal, + provinceCode, + countryCode, + } = physicalAddress; + + // Add these name-value pairs to the original district object + data.physicalAddressLine1 = addressLine1; + data.physicalAddressLine2 = addressLine2; + data.physicalCity = city; + data.physicalPostal = postal; + data.physicalProvinceCode = provinceCode; + data.physicalCountryCode = countryCode; + + // Remove the "addresses" property } - return jsonData - } - - function districtNumberSort(a, b) { - // Convert the strings to numbers for comparison - const numA = parseInt(a, 10); - const numB = parseInt(b, 10); - - if (numA < numB) { - return -1; + const courierAddress = data.addresses.find( + (address) => address.addressTypeCode === "MAILING" + ); + if (courierAddress) { + // Extract specific name-value pairs from the mailing address + const { + addressLine1, + addressLine2, + city, + postal, + provinceCode, + countryCode, + } = courierAddress; + + // Add these name-value pairs to the original district object + data.mailingAddressLine1 = addressLine1; + data.mailingAddressLine2 = addressLine2; + data.mailingCity = city; + data.mailingPostal = postal; + data.mailingProvinceCode = provinceCode; + data.mailingCountryCode = countryCode; + + // Remove the "addresses" property } - if (numA > numB) { - return 1; - } - return 0; + delete data.addresses; + delete data.contacts; } - function formatGrades(grades, schoolGrades) { - const result = {}; - - // Create a set of all school grade codes from the provided grades - const gradeCodesSet = new Set(grades.map(grade => grade.schoolGradeCode)); - - // Include all school grade codes in the result object - for (const grade of grades) { - result[grade.schoolGradeCode] = "Y"; - } - - // Set the value to "N" for school grade codes not in the provided grades - for (const grade of schoolGrades) { - if (!gradeCodesSet.has(grade.schoolGradeCode)) { - result[grade.schoolGradeCode] = "N"; +} +function addDistrictLabels(jsonData, districtList) { + if (jsonData.content && Array.isArray(jsonData.content)) { + jsonData.content.forEach((dataItem) => { + const district = districtList.find( + (item) => item.districtId === dataItem.districtId + ); + if (district) { + dataItem.districtNumber = district.districtNumber; + dataItem.districtName = district.displayName; } - } - - return result; - } - - function sortJSONByDistrictNumber(districts) { - return districts.slice().sort((a, b) => { - const districtNumberA = a['District Number'] || ''; - const districtNumberB = b['District Number'] || ''; - return districtNumberA.localeCompare(districtNumberB, undefined, { numeric: true, sensitivity: 'base' }); }); } - function sortJSONBySchoolCode(schools) { - return schools.slice().sort((a, b) => { - const schoolCodeA = a.mincode || ''; - const schoolCodeB = b.mincode || ''; - return schoolCodeA.localeCompare(schoolCodeB, undefined, { numeric: true, sensitivity: 'base' }); - }); - } + return jsonData; +} - 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 = {}; - propertyList.forEach((propertyInfo) => { - const prop = propertyInfo.property; - const label = propertyInfo.label; - reorderedObject[label] = object.hasOwnProperty(prop) ? object[prop] : ""; - }); - return reorderedObject; - } +function districtNumberSort(a, b) { + // Convert the strings to numbers for comparison + const numA = parseInt(a, 10); + const numB = parseInt(b, 10); - 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[matchKey + "_" + field] = matchingItem[field]; - return result; - }, {}), - }; - } - return item; - }); + if (numA < numB) { + return -1; } - function filterRemoveByField(data, field, valuesToExclude) { - return data.filter(item => !valuesToExclude.includes(item[field])); + if (numA > numB) { + return 1; } - function filterIncludeByField(data, field, valuesToInclude) { - return data.filter(item => valuesToInclude.includes(item[field])); - } - - function filterByPubliclyAvailableCodes(jsonArray, fieldName, publicCodes) { - // 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 publicCodes.includes(fieldValue); - }); - - return filteredArray; + return 0; +} +function formatGrades(grades, schoolGrades) { + const result = {}; + + // Create a set of all school grade codes from the provided grades + const gradeCodesSet = new Set(grades.map((grade) => grade.schoolGradeCode)); + + // Include all school grade codes in the result object + for (const grade of grades) { + result[grade.schoolGradeCode] = "Y"; } - 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; + + // Set the value to "N" for school grade codes not in the provided grades + for (const grade of schoolGrades) { + if (!gradeCodesSet.has(grade.schoolGradeCode)) { + result[grade.schoolGradeCode] = "N"; + } } - function filterByOpenedAndClosedDate(data){ - const currentDate = new Date(); - - return data.filter(item => { - const closedDate = item.closedDate ? new Date(item.closedDate) : null; - const openedDate = item.openedDate ? new Date(item.openedDate) : null; - - return (closedDate === null && currentDate > openedDate) || (currentDate < closedDate && currentDate > openedDate) ; + + return result; +} + +function sortJSONByDistrictNumber(districts) { + return districts.slice().sort((a, b) => { + const districtNumberA = a["District Number"] || ""; + const districtNumberB = b["District Number"] || ""; + return districtNumberA.localeCompare(districtNumberB, undefined, { + numeric: true, + sensitivity: "base", }); - } - function filterByExpiryDate(data) { - const currentDate = new Date(); - - return data.filter(item => { - const expiryDate = item.expiryDate ? new Date(item.expiryDate) : null; - const effectiveDate = item.effectiveDate ? new Date(item.effectiveDate) : null; - - return (expiryDate === null && currentDate > effectiveDate) || (currentDate < expiryDate && currentDate > effectiveDate) ; + }); +} +function sortJSONBySchoolCode(schools) { + return schools.slice().sort((a, b) => { + const schoolCodeA = a.mincode || ""; + const schoolCodeB = b.mincode || ""; + return schoolCodeA.localeCompare(schoolCodeB, undefined, { + numeric: true, + sensitivity: "base", }); - } - 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 addFundingGroups(schools, fundingGroups) { - try { - // Process each school in the array - const schoolsWithFunding = schools.map(school => { - // Find all matching funding groups by mincode - const matchingFundingGroups = fundingGroups.filter(fundingGroup => - fundingGroup.mincode === school.mincode - ); - - const schoolWithFunding = { - ...school, - primaryK3: "", // Replace with an appropriate default value - elementary47: "", // Replace with an appropriate default value - juniorSecondary810: "", // Replace with an appropriate default value - seniorSecondary1112: "" // Replace with an appropriate default value - }; - - // Iterate through the matching funding groups - matchingFundingGroups.forEach(matchingFundingGroup => { - // Access the fundingGroupCode and fundingSubCode properties - const fundingGroupCode = matchingFundingGroup.fundingGroupCode; - const fundingSubCode = matchingFundingGroup.fundingGroupSubCode; - - // Check the fundingSubCode and update the school information - switch (fundingSubCode) { - case "01": - schoolWithFunding.primaryK3 = fundingGroupCode; - break; - case "04": - schoolWithFunding.elementary47 = fundingGroupCode; - break; - case "08": - schoolWithFunding.juniorSecondary810 = fundingGroupCode; - break; - case "11": - schoolWithFunding.seniorSecondary1112 = fundingGroupCode; - break; - default: - break; - } - }); - - return schoolWithFunding; - }); + }); +} - 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 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 = {}; + propertyList.forEach((propertyInfo) => { + const prop = propertyInfo.property; + const label = propertyInfo.label; + reorderedObject[label] = object.hasOwnProperty(prop) ? object[prop] : ""; + }); + return reorderedObject; } - function getArrayofPubliclyAvailableCodes(codes, field) { - if (!Array.isArray(codes)) { - throw new Error('Invalid input. Expecting an array of objects.'); + +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[matchKey + "_" + field] = matchingItem[field]; + return result; + }, {}), + }; } - - // Filter out objects where "publiclyAvailable" is true - const publiclyAvailableCodes = codes - .filter(item => item && item.publiclyAvailable === true) - .map(item => item[field]); - - return publiclyAvailableCodes; + return item; + }); +} +function filterRemoveByField(data, field, valuesToExclude) { + return data.filter((item) => !valuesToExclude.includes(item[field])); +} +function filterIncludeByField(data, field, valuesToInclude) { + return data.filter((item) => valuesToInclude.includes(item[field])); +} + +function filterByPubliclyAvailableCodes(jsonArray, fieldName, publicCodes) { + // 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 publicCodes.includes(fieldValue); + }); + + return filteredArray; +} +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 filterByOpenedAndClosedDate(data) { + const currentDate = new Date(); + + return data.filter((item) => { + const closedDate = item.closedDate ? new Date(item.closedDate) : null; + const openedDate = item.openedDate ? new Date(item.openedDate) : null; + + return ( + (closedDate === null && currentDate > openedDate) || + (currentDate < closedDate && currentDate > openedDate) + ); + }); +} +function filterByExpiryDate(data) { + const currentDate = new Date(); + + return data.filter((item) => { + const expiryDate = item.expiryDate ? new Date(item.expiryDate) : null; + const effectiveDate = item.effectiveDate + ? new Date(item.effectiveDate) + : null; + + return ( + (expiryDate === null && currentDate > effectiveDate) || + (currentDate < expiryDate && currentDate > effectiveDate) + ); + }); +} +function getArrayofNonPubliclyAvailableCodes(codes, field) { + if (!Array.isArray(codes)) { + throw new Error("Invalid input. Expecting an array of objects."); } - function createSchoolCache(schoolData, schoolGrades) { - // Preload convertedGrades with schoolGrades.schoolGradeCode and set the value to "N" + // Filter out objects where "publiclyAvailable" is false + const nonPubliclyAvailableCodes = codes + .filter((item) => item && item.publiclyAvailable !== true) + .map((item) => item[field]); - // Map over each school object - return schoolData.map((school) => { - const convertedGrades = {}; - schoolGrades.forEach((grade) => { - convertedGrades[grade.schoolGradeCode] = "N"; - }); + return nonPubliclyAvailableCodes; +} +function addFundingGroups(schools, fundingGroups) { + try { + // Process each school in the array + const schoolsWithFunding = schools.map((school) => { + // Find all matching funding groups by mincode + const matchingFundingGroups = fundingGroups.filter( + (fundingGroup) => fundingGroup.mincode === school.mincode + ); - const addressFields = { - mailing: {}, - physical: {}, + const schoolWithFunding = { + ...school, + primaryK3: "", // Replace with an appropriate default value + elementary47: "", // Replace with an appropriate default value + juniorSecondary810: "", // Replace with an appropriate default value + seniorSecondary1112: "", // Replace with an appropriate default value }; - // Loop through the grades and set the value to "Y" for each grade - school.grades.forEach((grade) => { - convertedGrades[grade.schoolGradeCode] = "Y"; + // Iterate through the matching funding groups + matchingFundingGroups.forEach((matchingFundingGroup) => { + // Access the fundingGroupCode and fundingSubCode properties + const fundingGroupCode = matchingFundingGroup.fundingGroupCode; + const fundingSubCode = matchingFundingGroup.fundingGroupSubCode; + + // Check the fundingSubCode and update the school information + switch (fundingSubCode) { + case "01": + schoolWithFunding.primaryK3 = fundingGroupCode; + break; + case "04": + schoolWithFunding.elementary47 = fundingGroupCode; + break; + case "08": + schoolWithFunding.juniorSecondary810 = fundingGroupCode; + break; + case "11": + schoolWithFunding.seniorSecondary1112 = fundingGroupCode; + break; + default: + break; + } }); - // Extract and format principal contact information if it exists - const principalContact = school.contacts.find((contact) => contact.schoolContactTypeCode === "PRINCIPAL"); - if (principalContact) { - school.firstName = principalContact.firstName; - school.lastName = principalContact.lastName; - - - } + return schoolWithFunding; + }); - // Loop through addresses and update the fields based on addressTypeCode - school.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)) { - addressFields.mailing[`mailing_${field}`] = address[field]; - } - }); - } else if (address.addressTypeCode === "PHYSICAL") { - Object.keys(address).forEach((field) => { - if (![ - "createUser", - "updateUser", - "createDate", - "updateDate", - "schoolAddressId", - "schoolId", - "addressTypeCode" - ].includes(field)) { - addressFields.mailing[`physical_${field}`] = address[field]; - } - }); - } - }); + 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."); + } + + // Filter out objects where "publiclyAvailable" is true + const publiclyAvailableCodes = codes + .filter((item) => item && item.publiclyAvailable === true) + .map((item) => item[field]); - // Concatenate neighborhoodLearningTypeCode into a single string - const nlc = school.neighborhoodLearning.map(learning => learning.neighborhoodLearningTypeCode).join(' | '); - - // Merge the address fields and nlc into the school object - Object.assign(school, convertedGrades, addressFields.mailing, addressFields.physical, { nlc }); - - // Remove the original grades property and the updated address object - delete school.grades; - delete school.addresses; - delete school.neighborhoodLearning; - delete school.createUser; - delete school.updateUser; - delete school.updateDate; - delete school.createDate; - delete school.schoolId; - delete school.openedDate; - delete school.closedDate; - delete school.notes; - delete school.schoolMove.createUser; - delete school.schoolMove; - - // Remove the contacts property - delete school.contacts; - - return school; + return publiclyAvailableCodes; +} +function createSchoolCache(schoolData, schoolGrades) { + // Preload convertedGrades with schoolGrades.schoolGradeCode and set the value to "N" + + // Map over each school object + return schoolData.map((school) => { + const convertedGrades = {}; + schoolGrades.forEach((grade) => { + convertedGrades[grade.schoolGradeCode] = "N"; }); + + const addressFields = { + mailing: {}, + physical: {}, + }; + + // Loop through the grades and set the value to "Y" for each grade + school.grades.forEach((grade) => { + convertedGrades[grade.schoolGradeCode] = "Y"; + }); + + // Extract and format principal contact information if it exists + const principalContact = school.contacts.find( + (contact) => contact.schoolContactTypeCode === "PRINCIPAL" + ); + if (principalContact) { + school.firstName = principalContact.firstName; + school.lastName = principalContact.lastName; + } + + // Loop through addresses and update the fields based on addressTypeCode + school.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) + ) { + addressFields.mailing[`mailing_${field}`] = address[field]; + } + }); + } else if (address.addressTypeCode === "PHYSICAL") { + Object.keys(address).forEach((field) => { + if ( + ![ + "createUser", + "updateUser", + "createDate", + "updateDate", + "schoolAddressId", + "schoolId", + "addressTypeCode", + ].includes(field) + ) { + addressFields.mailing[`physical_${field}`] = address[field]; + } + }); + } + }); + + // Concatenate neighborhoodLearningTypeCode into a single string + const nlc = school.neighborhoodLearning + .map((learning) => learning.neighborhoodLearningTypeCode) + .join(" | "); + + // Merge the address fields and nlc into the school object + Object.assign( + school, + convertedGrades, + addressFields.mailing, + addressFields.physical, + { nlc } + ); + + // Remove the original grades property and the updated address object + delete school.grades; + delete school.addresses; + delete school.neighborhoodLearning; + delete school.createUser; + delete school.updateUser; + delete school.updateDate; + delete school.createDate; + delete school.schoolId; + delete school.openedDate; + delete school.closedDate; + delete school.notes; + delete school.schoolMove.createUser; + delete school.schoolMove; + + // Remove the contacts property + delete school.contacts; + + return school; + }); } - 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}; \ No newline at end of file + +function isActiveEntity(effective, expiry) { + let today = new Date(); + return today > new Date(effective) && (!expiry || today < new Date(expiry)); +} + +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, + isActiveEntity, +}; diff --git a/backend/src/routes/authority-router.js b/backend/src/routes/authority-router.js index 58fc2a03..41593888 100644 --- a/backend/src/routes/authority-router.js +++ b/backend/src/routes/authority-router.js @@ -12,6 +12,7 @@ const { appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, sortByProperty, + isActiveEntity, } = require("../components/utils.js"); //Batch Routes router.get("/all-mailing/:type", checkToken, getAllAuthorityMailing); @@ -87,34 +88,13 @@ async function getAllAuthorityMailing(req, res) { log.error("getData Error", e.response ? e.response.status : e.message); } } -async function getDistrictCodes(req) { - if (!listCache.has("districtCodesList")) { - const url = `${config.get( - "server:instituteAPIURL" - )}/institute/authority-contact-type-codes`; // Update the URL according to your API endpoint - try { - const response = await axios.get(url, { - headers: { Authorization: `Bearer ${req.accessToken}` }, - }); - const districtCodeList = response.data; - listCache.set("districtCodesList", districtCodeList); - return districtCodeList; - } catch (e) { - log.error( - "getDistrictList Error", - e.response ? e.response.status : e.message - ); - } - } else { - const districtCodeList = await listCache.get("districtCodesList"); - return districtCodeList; - } -} + async function getAuthority(req, res) { const { id } = req.params; + let currentDate = new Date().toISOString().substring(0, 19); const params = [ { - condition: null, + condition: "AND", searchCriteriaList: [ { key: "independentAuthorityId", @@ -125,6 +105,37 @@ async function getAuthority(req, res) { }, ], }, + { + condition: "AND", + searchCriteriaList: [ + { + key: "closedDate", + operation: "eq", + value: null, + valueType: "STRING", + condition: "OR", + }, + { + key: "closedDate", + operation: "gte", + value: currentDate, + valueType: "DATE_TIME", + condition: "OR", + }, + ], + }, + { + condition: "AND", + searchCriteriaList: [ + { + key: "openedDate", + operation: "lte", + value: currentDate, + valueType: "DATE_TIME", + condition: "AND", + }, + ], + }, ]; const jsonString = JSON.stringify(params); @@ -142,6 +153,14 @@ async function getAuthority(req, res) { headers: { Authorization: `Bearer ${req.accessToken}` }, }); + // Filter Authority Contacts + const filteredAuthorityContacts = + authorityDataResponse?.data?.contacts?.filter((contact) => + isActiveEntity(contact.effectiveDate, contact.expiryDate) + ); + // Overwrite Authority Contacts + authorityDataResponse.data.contacts = filteredAuthorityContacts; + const authoritySchoolsResponse = await axios.get(authoritySchoolsUrl, { headers: { Authorization: `Bearer ${req.accessToken}` }, }); @@ -162,6 +181,8 @@ async function getAuthority(req, res) { openedDate < today ); }); + + console.log(authorityDataResponse.data); const authorityJSON = { authorityData: authorityDataResponse.data, authoritySchools: filteredSchoolsResponse, diff --git a/backend/src/routes/district-router.js b/backend/src/routes/district-router.js index 8df54a90..5a26a2e7 100644 --- a/backend/src/routes/district-router.js +++ b/backend/src/routes/district-router.js @@ -7,8 +7,22 @@ const axios = require("axios"); const fs = require("fs"); const path = require("path"); const { checkToken } = require("../components/auth"); -const { listCache} = require("../components/cache"); -const {addFundingGroups, 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); @@ -319,9 +333,10 @@ async function getAllDistrictMailing(req, res) { async function getDistrict(req, res) { const { id } = req.params; + let currentDate = new Date().toISOString().substring(0, 19); const params = [ { - condition: null, + condition: "AND", searchCriteriaList: [ { key: "districtID", @@ -330,13 +345,6 @@ async function getDistrict(req, res) { valueType: "UUID", condition: "AND", }, - { - key: "closedDate", - operation: "eq", - value: null, - valueType: "STRING", - condition: "AND", - }, { key: "schoolCategoryCode", operation: "neq", @@ -360,6 +368,37 @@ async function getDistrict(req, res) { }, ], }, + { + condition: "AND", + searchCriteriaList: [ + { + key: "closedDate", + operation: "eq", + value: null, + valueType: "STRING", + condition: "OR", + }, + { + key: "closedDate", + operation: "gte", + value: currentDate, + valueType: "DATE_TIME", + condition: "OR", + }, + ], + }, + { + condition: "AND", + searchCriteriaList: [ + { + key: "openedDate", + operation: "lte", + value: currentDate, + valueType: "DATE_TIME", + condition: "AND", + }, + ], + }, ]; const jsonString = JSON.stringify(params); @@ -381,13 +420,13 @@ async function getDistrict(req, res) { }); const contactTypeCodes = await getDistrictCodes(req); - 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 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 @@ -408,28 +447,43 @@ 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"]); - districtSchoolsResponse.data.content = addFundingGroups(districtSchoolsResponse.data.content, fundingGroups) + 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 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); + 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); + // 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 - ); - } - ); + // 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 + ); + } + ); const districtJSON = { districtData: districtDataPublicWithLabels, districtSchools: filteredSchoolsResponse, diff --git a/backend/src/routes/school-router.js b/backend/src/routes/school-router.js index 649f17b1..ee8f1acf 100644 --- a/backend/src/routes/school-router.js +++ b/backend/src/routes/school-router.js @@ -17,7 +17,8 @@ const { sortJSONBySchoolCode, rearrangeAndRelabelObjectProperties, filterByPubliclyAvailableCodes, - addFundingGroups + addFundingGroups, + isActiveEntity, } = require("../components/utils"); const { checkToken } = require("../components/auth"); const { schoolCache, listCache, codeCache } = require("../components/cache"); @@ -167,6 +168,7 @@ async function getSchool(req, res) { expiryDate: "2099-12-31T00:00:00", }, ]; + const schoolData = response.data; const includedFields = ["label", "description"]; @@ -177,7 +179,6 @@ async function getSchool(req, res) { (info) => info.publiclyAvailable === true, includedFields ); - schoolData.contacts = filterByPubliclyAvailableCodes( schoolData.contacts, @@ -186,26 +187,27 @@ async function getSchool(req, res) { contactTypeCodes.codesList.schoolContactTypeCodes, "schoolContactTypeCode" ) - ); + ).filter((contact) => + isActiveEntity(contact.effectiveDate, contact.expiryDate) + ); // Filter out inactive contacts schoolData.contacts = filterByExpiryDate(schoolData.contacts); const formattedGrades = formatGrades(schoolData.grades, schoolGrades); const schoolWithFormattedGrades = [{ ...schoolData, ...formattedGrades }]; - - const schoolsWithFundingGroups = addFundingGroups(schoolWithFormattedGrades, fundingGroups) + + const schoolsWithFundingGroups = addFundingGroups( + schoolWithFormattedGrades, + fundingGroups + ); res.json(schoolsWithFundingGroups[0]); - }) .catch((e) => { log.error("getSchools Error", e.response ? e.response.status : e.message); }); } -async function getAllSchoolMailing(req, res) { - const allSchools = await getAllSchools(req, res); - res.json(allSchools); -} + async function getAllSchools(req, res) { const { schoolCategory } = req.params; - const contactTypeCodes = await listCache.get("codesList"); + //const contactTypeCodes = await listCache.get("codesList"); let params = []; if (await !schoolCache.has("openschoollist" + schoolCategory)) { @@ -286,7 +288,7 @@ async function getAllSchools(req, res) { { property: "physical_addressLine1", label: "Physical Address" }, { property: "physical_city", label: "Physical City" }, { property: "physical_provinceCode", label: "Physical Province" }, - { property: "physical_postal", label: "Physical Postal Code" }, + { property: "physical_postal", label: "Physical Postal Code" }, { property: "firstName", label: "Principal First Name" }, { property: "lastName", label: "Principal Last Name" }, { property: "facilityTypeCode", label: "Type" }, @@ -315,9 +317,18 @@ async function getAllSchools(req, res) { { property: "GRADE11", label: "Grade 11 Enrollment" }, { property: "GRADE12", label: "Grade 12 Enrollment" }, { property: "primaryK3", label: "Group Classification Primary K-3" }, - { property: "elementary47", label: "Group Classification Elementary 4-7 EU" }, - { property: "juniorSecondary810", label: "Group Classification Junior Secondary 8-10 SU" }, - { property: "seniorSecondary1112", label: "Group Classification Senior Secondary 11-12" }, + { + property: "elementary47", + label: "Group Classification Elementary 4-7 EU", + }, + { + property: "juniorSecondary810", + label: "Group Classification Junior Secondary 8-10 SU", + }, + { + property: "seniorSecondary1112", + label: "Group Classification Senior Secondary 11-12", + }, ]; const mailingListpropertyOrder = [ { property: "districtNumber", label: "District Number" }, @@ -343,8 +354,8 @@ async function getAllSchools(req, res) { schoolGrades ) ); - - openSchoolList = addFundingGroups(openSchoolListSorted, fundingGroups) + + openSchoolList = addFundingGroups(openSchoolListSorted, fundingGroups); let openSchoolMailingList = [...openSchoolList]; openSchoolList = normalizeJsonObject( @@ -354,7 +365,7 @@ async function getAllSchools(req, res) { null, ["label", "description"] ); - + openSchoolList = normalizeJsonObject( openSchoolList, facilityCodes, @@ -430,4 +441,4 @@ async function getAllSchools(req, res) { } } -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/backend/src/util/constants.js b/backend/src/util/constants.js deleted file mode 100644 index e69de29b..00000000 From 49a10f26560f4a4c9421cbf71358fc96ac424c2a Mon Sep 17 00:00:00 2001 From: suzalflueck Date: Thu, 1 Aug 2024 14:05:18 -0700 Subject: [PATCH 2/2] remove console log --- backend/src/routes/authority-router.js | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/routes/authority-router.js b/backend/src/routes/authority-router.js index 41593888..a1a73f46 100644 --- a/backend/src/routes/authority-router.js +++ b/backend/src/routes/authority-router.js @@ -182,7 +182,6 @@ async function getAuthority(req, res) { ); }); - console.log(authorityDataResponse.data); const authorityJSON = { authorityData: authorityDataResponse.data, authoritySchools: filteredSchoolsResponse,