From 6cf82eb6950380ec7d7c6da1a0d38d9aade04fdb Mon Sep 17 00:00:00 2001 From: Tang Date: Fri, 10 Nov 2023 15:03:03 -0800 Subject: [PATCH] added the publish contact check to school --- backend/src/components/utils.js | 464 +++++++++++++++----------- backend/src/routes/district-router.js | 72 +--- backend/src/routes/school-router.js | 255 +++++++++++--- 3 files changed, 485 insertions(+), 306 deletions(-) diff --git a/backend/src/components/utils.js b/backend/src/components/utils.js index b7fcc97f..214b0cf4 100644 --- a/backend/src/components/utils.js +++ b/backend/src/components/utils.js @@ -1,34 +1,43 @@ +// const express = require("express"); +// const NodeCache = require("node-cache"); +const axios = require("axios"); +// const fs = require("fs"); +// const path = require("path"); +// const { checkToken } = require("../components/auth"); +const log = require("../components/logger"); +const { listCache } = require("../components/cache"); +const config = require("../config/index"); const ALLOWED_FILENAMES = new Set([ - 'trans', - 'independent-authority-rep', - 'indigenous', - 'continuing-custody-order', - 'distributed-learning', - 'online-learning-contact', - 'early-learning-child-care', - 'planning-officer', - 'early-learning', - 'facilities', - 'financial', - 'french', - 'international-education', - 'literacy', - 'myed-bc', - 'inclusive-education', - 'transportation', - 'superintendent', - 'chairperson', - 'secretary-treasurer', - 'executive-admin-assistant', - 'exceldistrictcontacts', - 'publicschoolcontacts', - 'independentschoolcontacts', - 'allschoolcontacts' + "trans", + "independent-authority-rep", + "indigenous", + "continuing-custody-order", + "distributed-learning", + "online-learning-contact", + "early-learning-child-care", + "planning-officer", + "early-learning", + "facilities", + "financial", + "french", + "international-education", + "literacy", + "myed-bc", + "inclusive-education", + "transportation", + "superintendent", + "chairperson", + "secretary-treasurer", + "executive-admin-assistant", + "exceldistrictcontacts", + "publicschoolcontacts", + "independentschoolcontacts", + "allschoolcontacts", // Add more allowed filepaths as needed ]); const ALLOWED_SCHOOLCATEGORYCODES = new Set([ - 'PUBLIC', - 'INDEPEND' + "PUBLIC", + "INDEPEND", // Add more allowed filepaths as needed ]); function isAllowedSchoolCategory(category) { @@ -44,17 +53,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; @@ -71,9 +80,55 @@ function createList(list, options = {}) { }); } - return filteredList; } +function getNonPublicContactTypeCodes(contactTypes) { + const nonPublicContactTypeCodes = []; + for (const contactType of contactTypes) { + if (!contactType.publiclyAvailable) { + nonPublicContactTypeCodes.push(contactType.districtContactTypeCode); + } + } + return nonPublicContactTypeCodes; +} +async function getDistrictCodes(req) { + if (!listCache.has("districtCodesList")) { + const url = `${config.get( + "server:instituteAPIURL" + )}/institute/district-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; + } +} +function removeContacts(dataResponse, nonPublicContactTypeCodes) { + const updatedData = { ...dataResponse }; + if (updatedData.contacts && Array.isArray(updatedData.contacts)) { + updatedData.contacts = updatedData.contacts.filter((contact) => { + return !nonPublicContactTypeCodes.includes( + contact.districtContactTypeCode + ); + }); + updatedData.contacts = updatedData.contacts.filter((contact) => { + return !nonPublicContactTypeCodes.includes(contact.schoolContactTypeCode); + }); + } + return updatedData; +} + function removeFieldsByCriteria(inputData, criteria) { if (!Array.isArray(criteria) || criteria.length === 0) { return inputData; // Return the original data if the criteria is empty or not an array. @@ -91,182 +146,207 @@ function removeFieldsByCriteria(inputData, criteria) { return inputData; } 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.displayName = district.displayName; - } - }); - } - return jsonData + 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.displayName = district.displayName; + } + }); } + 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; - } - if (numA > numB) { - return 1; - } - return 0; +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; } - function formatGrades(grades, schoolGrades) { - const result = {}; + if (numA > numB) { + return 1; + } + 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"; - } - - // 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"; - } - } - - return 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 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; + + // 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 createSchoolCache(schoolData, schoolGrades) { - // Preload convertedGrades with schoolGrades.schoolGradeCode and set the value to "N" + return result; +} +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 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"; - }); + // Map over each school object + return schoolData.map((school) => { + const convertedGrades = {}; + schoolGrades.forEach((grade) => { + convertedGrades[grade.schoolGradeCode] = "N"; + }); - const addressFields = { - mailing: {}, - physical: {}, - }; + 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"; - }); + // 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; - school.email = principalContact.email; - school.phoneNumber = principalContact.phoneNumber; - } + // 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; + school.email = principalContact.email; + school.phoneNumber = principalContact.phoneNumber; + } - // 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]; - } - }); - } + // 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(' | '); + // 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 }); + // 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 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; - const propertyOrder = [ - { property: "districtNumber", label: "District Number" }, - { property: "mincode", label: "School Code" }, - { property: "displayName", label: "School 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: "fax", 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; - }); + // Remove the contacts property + delete school.contacts; + const propertyOrder = [ + { property: "districtNumber", label: "District Number" }, + { property: "mincode", label: "School Code" }, + { property: "displayName", label: "School 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: "fax", 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; + }); } - module.exports = {removeFieldsByCriteria, createList, isSafeFilePath,isAllowedSchoolCategory, addDistrictLabels, districtNumberSort, createSchoolCache, formatGrades}; \ No newline at end of file +module.exports = { + removeFieldsByCriteria, + createList, + isSafeFilePath, + isAllowedSchoolCategory, + addDistrictLabels, + districtNumberSort, + createSchoolCache, + formatGrades, + removeContacts, + getDistrictCodes, + getNonPublicContactTypeCodes, +}; diff --git a/backend/src/routes/district-router.js b/backend/src/routes/district-router.js index 936a40f9..1f8d0401 100644 --- a/backend/src/routes/district-router.js +++ b/backend/src/routes/district-router.js @@ -8,7 +8,11 @@ const fs = require("fs"); const path = require("path"); const { checkToken } = require("../components/auth"); const { listCache } = require("../components/cache"); - +const { + getDistrictCodes, + getNonPublicContactTypeCodes, + removeContacts, +} = require("../components/utils"); //Batch Routes router.get("/:id", checkToken, getDistrict); @@ -28,81 +32,28 @@ async function removeItemsFromDistrictDataResponse(response, itemsToRemove) { } } -async function getDistrictCodes(req) { - if (!listCache.has("districtCodesList")) { - const url = `${config.get( - "server:instituteAPIURL" - )}/institute/district-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; - } -} -function getNonPublicContactTypeCodes(contactTypes) { - const nonPublicContactTypeCodes = []; - - for (const contactType of contactTypes) { - if (!contactType.publiclyAvailable) { - nonPublicContactTypeCodes.push(contactType.districtContactTypeCode); - } - } - - return nonPublicContactTypeCodes; -} function addContactTypeLabels(districtDataResponse, nonPublicContactTypeCodes) { - const updatedDistrictData = { ...districtDataResponse }; if ( updatedDistrictData.contacts && Array.isArray(updatedDistrictData.contacts) ) { - updatedDistrictData.contacts.forEach(contact => { + updatedDistrictData.contacts.forEach((contact) => { const matchingType = nonPublicContactTypeCodes.find( - codeObj => codeObj.districtContactTypeCode === contact.districtContactTypeCode + (codeObj) => + codeObj.districtContactTypeCode === contact.districtContactTypeCode ); if (matchingType) { contact.label = matchingType.label; } else { - } }); } return updatedDistrictData; } -function removeContacts(districtDataResponse, nonPublicContactTypeCodes) { - const updatedDistrictData = { ...districtDataResponse }; - if ( - updatedDistrictData.contacts && - Array.isArray(updatedDistrictData.contacts) - ) { - updatedDistrictData.contacts = updatedDistrictData.contacts.filter( - (contact) => { - return !nonPublicContactTypeCodes.includes( - contact.districtContactTypeCode - ); - } - ); - } - - return updatedDistrictData; -} -//api/v1/institute/district/12342525 async function getDistrict(req, res) { const { id } = req.params; @@ -138,8 +89,11 @@ async function getDistrict(req, res) { const districtSchoolsResponse = await axios.get(districtSchoolsUrl, { headers: { Authorization: `Bearer ${req.accessToken}` }, }); + const contactTypeCodes = await getDistrictCodes(req); - const nonPublicContactTypeCodes = getNonPublicContactTypeCodes(contactTypeCodes); + const nonPublicContactTypeCodes = + getNonPublicContactTypeCodes(contactTypeCodes); + const districtDataPublic = removeContacts( districtDataResponse.data, nonPublicContactTypeCodes @@ -148,7 +102,7 @@ async function getDistrict(req, res) { districtDataPublic, contactTypeCodes ); - + // console.log(districtDataPublic); const districtJSON = { districtData: districtDataPublicWithLabels, districtSchools: districtSchoolsResponse.data.content, diff --git a/backend/src/routes/school-router.js b/backend/src/routes/school-router.js index b47031b6..dd40081b 100644 --- a/backend/src/routes/school-router.js +++ b/backend/src/routes/school-router.js @@ -7,7 +7,14 @@ const axios = require("axios"); const fs = require("fs"); const path = require("path"); -const { createSchoolCache, addDistrictLabels, formatGrades } = require("../components/utils"); +const { + createSchoolCache, + addDistrictLabels, + formatGrades, + getDistrictCodes, + getNonPublicContactTypeCodes, + removeContacts, +} = require("../components/utils"); const { checkToken } = require("../components/auth"); const { schoolCache, listCache, codeCache } = require("../components/cache"); @@ -16,57 +23,187 @@ router.get("/all/:schoolCategory", checkToken, getAllSchools); router.get("/:schoolId", checkToken, getSchool); async function getSchool(req, res) { - const {schoolId} = req.params; - const url = `${config.get( - "server:instituteAPIURL" - )}/institute/school/` + schoolId; - axios - .get(url, { headers: { Authorization: `Bearer ${req.accessToken}` } }) - .then((response) => { - //const openSchoolList = createList(response.data, openSchoolListOptions); - const schoolGrades = [{"schoolGradeCode":"KINDHALF","label":"Kindergarten Half","description":"Kindergarten half","displayOrder":1,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"KINDFULL","label":"Kindergarten Full","description":"Kindergarten full","displayOrder":2,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE01","label":"Grade 1","description":"First grade","displayOrder":3,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE02","label":"Grade 2","description":"Second grade","displayOrder":4,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE03","label":"Grade 3","description":"Third grade","displayOrder":5,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE04","label":"Grade 4","description":"Fourth grade","displayOrder":6,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE05","label":"Grade 5","description":"Fifth grade","displayOrder":7,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE06","label":"Grade 6","description":"Sixth grade","displayOrder":8,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE07","label":"Grade 7","description":"Seventh grade","displayOrder":9,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"ELEMUNGR","label":"Elementary Ungraded","description":"Elementary ungraded","displayOrder":10,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE08","label":"Grade 8","description":"Eighth grade","displayOrder":11,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE09","label":"Grade 9","description":"Ninth grade","displayOrder":12,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE10","label":"Grade 10","description":"Tenth grade","displayOrder":13,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE11","label":"Grade 11","description":"Eleventh grade","displayOrder":14,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"GRADE12","label":"Grade 12","description":"Twelfth grade","displayOrder":15,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"},{"schoolGradeCode":"SECUNGR","label":"Secondary Ungraded","description":"Secondary ungraded","displayOrder":16,"effectiveDate":"2020-01-01T00:00:00","expiryDate":"2099-12-31T00:00:00"}] - const schoolData = response.data; - const formattedGrades = formatGrades(schoolData.grades, schoolGrades); - const schoolWithFormattedGrades = { ...schoolData, ...formattedGrades }; - res.json(schoolWithFormattedGrades); - }) - .catch((e) => { - log.error( - "getSchools Error", - e.response ? e.response.status : e.message - ); - }); - + const { schoolId } = req.params; + const url = + `${config.get("server:instituteAPIURL")}/institute/school/` + schoolId; + const contactTypeCodes = await getDistrictCodes(req); + const nonPublicContactTypeCodes = + getNonPublicContactTypeCodes(contactTypeCodes); + axios + .get(url, { headers: { Authorization: `Bearer ${req.accessToken}` } }) + .then((response) => { + //const openSchoolList = createList(response.data, openSchoolListOptions); + const schoolGrades = [ + { + schoolGradeCode: "KINDHALF", + label: "Kindergarten Half", + description: "Kindergarten half", + displayOrder: 1, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "KINDFULL", + label: "Kindergarten Full", + description: "Kindergarten full", + displayOrder: 2, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE01", + label: "Grade 1", + description: "First grade", + displayOrder: 3, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE02", + label: "Grade 2", + description: "Second grade", + displayOrder: 4, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE03", + label: "Grade 3", + description: "Third grade", + displayOrder: 5, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE04", + label: "Grade 4", + description: "Fourth grade", + displayOrder: 6, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE05", + label: "Grade 5", + description: "Fifth grade", + displayOrder: 7, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE06", + label: "Grade 6", + description: "Sixth grade", + displayOrder: 8, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE07", + label: "Grade 7", + description: "Seventh grade", + displayOrder: 9, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "ELEMUNGR", + label: "Elementary Ungraded", + description: "Elementary ungraded", + displayOrder: 10, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE08", + label: "Grade 8", + description: "Eighth grade", + displayOrder: 11, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE09", + label: "Grade 9", + description: "Ninth grade", + displayOrder: 12, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE10", + label: "Grade 10", + description: "Tenth grade", + displayOrder: 13, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE11", + label: "Grade 11", + description: "Eleventh grade", + displayOrder: 14, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "GRADE12", + label: "Grade 12", + description: "Twelfth grade", + displayOrder: 15, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + { + schoolGradeCode: "SECUNGR", + label: "Secondary Ungraded", + description: "Secondary ungraded", + displayOrder: 16, + effectiveDate: "2020-01-01T00:00:00", + expiryDate: "2099-12-31T00:00:00", + }, + ]; + const schoolData = response.data; + const formattedGrades = formatGrades(schoolData.grades, schoolGrades); + var schoolWithFormattedGrades = { ...schoolData, ...formattedGrades }; + const schoolDataPublic = removeContacts( + schoolWithFormattedGrades, + nonPublicContactTypeCodes + ); + res.json(schoolDataPublic); + }) + .catch((e) => { + log.error("getSchools Error", e.response ? e.response.status : e.message); + }); } async function getAllSchools(req, res) { - - const {schoolCategory} = req.params + const { schoolCategory } = req.params; let params = []; if (await !schoolCache.has("openschoollist" + schoolCategory)) { - if(schoolCategory!='ALL'){ - params = [ - { - condition: null, - searchCriteriaList: [ - { - key: "closedDate", - operation: "eq", - value: null, - valueType: "STRING", - condition: "AND", - }, - { + if (schoolCategory != "ALL") { + params = [ + { + condition: null, + searchCriteriaList: [ + { + key: "closedDate", + operation: "eq", + value: null, + valueType: "STRING", + condition: "AND", + }, + { key: "schoolCategoryCode", operation: "eq", value: schoolCategory, valueType: "STRING", condition: "AND", - } - ], - } - ]; - }else { + }, + ], + }, + ]; + } else { params = [ { condition: null, @@ -77,17 +214,17 @@ async function getAllSchools(req, res) { value: null, valueType: "STRING", condition: "AND", - } - ] - }] - } - - + }, + ], + }, + ]; + } + const jsonString = JSON.stringify(params); const encodedParams = encodeURIComponent(jsonString); - const districtList = await listCache.get("districtlist") - const schoolGrades = await codeCache.get("gradelist"); - + const districtList = await listCache.get("districtlist"); + const schoolGrades = await codeCache.get("gradelist"); + const url = `${config.get( "server:instituteAPIURL" )}/institute/school/paginated?pageSize=3000&searchCriteriaList=${encodedParams}`; @@ -95,9 +232,15 @@ async function getAllSchools(req, res) { .get(url, { headers: { Authorization: `Bearer ${req.accessToken}` } }) .then((response) => { //const openSchoolList = createList(response.data, openSchoolListOptions); - - const openSchoolListWithDistrictLabels = addDistrictLabels(response.data, districtList) - const openSchoolList = createSchoolCache(openSchoolListWithDistrictLabels.content, schoolGrades); + + const openSchoolListWithDistrictLabels = addDistrictLabels( + response.data, + districtList + ); + const openSchoolList = createSchoolCache( + openSchoolListWithDistrictLabels.content, + schoolGrades + ); res.json(openSchoolList); schoolCache.set("openschoollist" + schoolCategory, openSchoolList); log.info(req.url); @@ -109,7 +252,9 @@ async function getAllSchools(req, res) { ); }); } else { - const openSchoolList = await schoolCache.get("openschoollist" + schoolCategory); + const openSchoolList = await schoolCache.get( + "openschoollist" + schoolCategory + ); res.json(openSchoolList); } }