From f4517a93eb0a9075d99098c6e0f3d7aef24ba2b9 Mon Sep 17 00:00:00 2001 From: suzalflueck Date: Mon, 29 Apr 2024 13:39:41 -0700 Subject: [PATCH] add check --- frontend/src/utils/common.ts | 5 + frontend/src/views/DistrictView.vue | 182 ++++++++++++++-------------- 2 files changed, 98 insertions(+), 89 deletions(-) diff --git a/frontend/src/utils/common.ts b/frontend/src/utils/common.ts index a3459726..f00bbbfd 100644 --- a/frontend/src/utils/common.ts +++ b/frontend/src/utils/common.ts @@ -13,3 +13,8 @@ export function useSanitizeURL(input: string | String): String { input = input.toLowerCase().replace(/^a-zA-Z0-9 ]/g, '') return input } + +/** VALIDATION FUNCTIONS */ +export function isValidDistrictNumber(input: string | String | number): boolean { + return !!input && input.toString().length === 3 && /^\d+$/.test(input.toString()); +} diff --git a/frontend/src/views/DistrictView.vue b/frontend/src/views/DistrictView.vue index 88203020..833ce3da 100644 --- a/frontend/src/views/DistrictView.vue +++ b/frontend/src/views/DistrictView.vue @@ -4,7 +4,7 @@ import { ref, reactive, onMounted, computed, toValue } from 'vue' import { useAppStore } from '@/stores/app' import { useRoute } from 'vue-router' import router from '@/router' -import { formatPhoneNumber } from '@/utils/common' +import { formatPhoneNumber, isValidDistrictNumber } from '@/utils/common' import type { District, School, Grade, Address, Contact } from '@/types/types.d.ts' import jsonexport from 'jsonexport/dist' import { useSanitizeURL } from '@/composables/string' @@ -78,97 +78,101 @@ function downloadDistrictSchools() { onMounted(async () => { const route = useRoute() // Set the districtId inside the onMounted hook; null if districtId not found - districtId.value = - appStore.getDistrictByDistrictNumber(String(route.params.districtNumber))?.districtId ?? null + if (isValidDistrictNumber(String(route.params.districtNumber))) { + districtId.value = + appStore.getDistrictByDistrictNumber(String(route.params.districtNumber))?.districtId ?? null + } // get district data - try { - const response = await InstituteService.getDistrictView(districtId.value as string) - if (response.data?.districtData?.contacts) { - district.value = response.data - contacts.value = response.data?.districtData?.contacts - schools.value = district.value?.districtSchools + if (!!districtId.value) { + try { + const response = await InstituteService.getDistrictView(districtId.value as string) + if (response.data?.districtData?.contacts) { + district.value = response.data + contacts.value = response.data?.districtData?.contacts + schools.value = district.value?.districtSchools - //Change School date for DL - const transformedSchoolData = schools.value.map((school: School) => { - const { contacts, addresses, ...rest } = school - const transformedContacts = contacts.map(({ schoolContactTypeCode, ...contactRest }) => ({ - schoolContactTypeCode, - ...contactRest - })) - const physicalAddress = addresses.find( - (address: Address) => address?.addressTypeCode === 'PHYSICAL' - ) - const mailingAddress = addresses.find( - (address: Address) => address?.addressTypeCode === 'MAILING' - ) - return { - ...rest, - contacts: transformedContacts.reduce((acc, contact) => ({ ...acc, ...contact }), {}), - physicalAddress: physicalAddress, - mailingAddress: mailingAddress, - grades: [], - neighborhoodLearning: [], - schoolMove: [] - } - }) - //Change School data for DL - filteredSchools.value = transformedSchoolData.map((item: any) => { - return { - 'District Number': response.data.districtData.districtNumber, - Mincode: item.mincode, - 'Display Name': item.displayName, - 'Mailing Address': item.mailingAddress?.addressLine1, - 'Mailing Address Line2': item.mailingAddress?.addressLine2, - 'Mailing Address City': item.mailingAddress?.city, - 'Mailing Address Province': item.mailingAddress?.provinceCode, - 'Mailing Address PostalCode': item.mailingAddress?.postal, - 'Physical Address': item.physicalAddress?.addressLine1, - 'Physical Address Line2': item.physicalAddress?.addressLine2, - 'Physical Address City': item.physicalAddress?.city, - 'Physical Address Province': item.physicalAddress?.provinceCode, - 'Physical Address Postal Code': item.physicalAddress?.postal, - Role: item.contacts?.jobTitle, - 'Contact First Name': item.contacts?.firstName, - 'Contact Last Name': item.contacts?.lastName, - 'Contact Phone Extension': item.contacts?.phoneExtension, - 'Contact Phone Number': item.contacts?.phoneNumber, - 'Facility Type Code': appStore.getFacilityCodeLabel(item.facilityTypeCode), - 'School Category Code': appStore.getCategoryCodeLabel(item.schoolCategoryCode), - 'Phone Number': item.phoneNumber, - Fax: item.faxNumber, - Email: item.email, - Website: item.website, - 'Group Classification Primary K-3': item.primaryK3, - 'Group Classification Elementary 4-7 EU': item.elementary47, - 'Group Classification Junior Secondary 8-10 SU': item.juniorSecondary810, - 'Group Classification Senior Secondary 11-12': item.seniorSecondary1112 - } - }) - filteredContacts.value = contacts.value.map((item: any) => { - return { - 'District Number': response.data.districtData?.districtNumber, - 'District Name': response.data.districtData?.displayName, - 'Contact Type': item.label, - 'Job Title': item.jobTitle, - 'First Name': item.firstName, - 'Last Name': item.lastName, - 'Phone Number': item.phoneNumber, - 'Phone Extension': item.phoneExtension, - 'Alternate Phone Number': item.alternatePhoneNumber, - 'Alternate Phone Extension': item.alternatePhoneExtension, - Email: item.email, - 'Mailing Address': response.data.districtData?.addresses[0].addressLine1, - 'Mailing City': response.data.districtData?.addresses[0].city, - 'Mailing Province': response.data.districtData?.addresses[0].provinceCode, - 'Mailing Postal Code': response.data.districtData?.addresses[0].postal, - 'District Phone': response.data.districtData?.phoneNumber, - 'District Fax': response.data.districtData?.faxNumber, - Website: response.data.districtData?.website - } - }) + //Change School date for DL + const transformedSchoolData = schools.value.map((school: School) => { + const { contacts, addresses, ...rest } = school + const transformedContacts = contacts.map(({ schoolContactTypeCode, ...contactRest }) => ({ + schoolContactTypeCode, + ...contactRest + })) + const physicalAddress = addresses.find( + (address: Address) => address?.addressTypeCode === 'PHYSICAL' + ) + const mailingAddress = addresses.find( + (address: Address) => address?.addressTypeCode === 'MAILING' + ) + return { + ...rest, + contacts: transformedContacts.reduce((acc, contact) => ({ ...acc, ...contact }), {}), + physicalAddress: physicalAddress, + mailingAddress: mailingAddress, + grades: [], + neighborhoodLearning: [], + schoolMove: [] + } + }) + //Change School data for DL + filteredSchools.value = transformedSchoolData.map((item: any) => { + return { + 'District Number': response.data.districtData.districtNumber, + Mincode: item.mincode, + 'Display Name': item.displayName, + 'Mailing Address': item.mailingAddress?.addressLine1, + 'Mailing Address Line2': item.mailingAddress?.addressLine2, + 'Mailing Address City': item.mailingAddress?.city, + 'Mailing Address Province': item.mailingAddress?.provinceCode, + 'Mailing Address PostalCode': item.mailingAddress?.postal, + 'Physical Address': item.physicalAddress?.addressLine1, + 'Physical Address Line2': item.physicalAddress?.addressLine2, + 'Physical Address City': item.physicalAddress?.city, + 'Physical Address Province': item.physicalAddress?.provinceCode, + 'Physical Address Postal Code': item.physicalAddress?.postal, + Role: item.contacts?.jobTitle, + 'Contact First Name': item.contacts?.firstName, + 'Contact Last Name': item.contacts?.lastName, + 'Contact Phone Extension': item.contacts?.phoneExtension, + 'Contact Phone Number': item.contacts?.phoneNumber, + 'Facility Type Code': appStore.getFacilityCodeLabel(item.facilityTypeCode), + 'School Category Code': appStore.getCategoryCodeLabel(item.schoolCategoryCode), + 'Phone Number': item.phoneNumber, + Fax: item.faxNumber, + Email: item.email, + Website: item.website, + 'Group Classification Primary K-3': item.primaryK3, + 'Group Classification Elementary 4-7 EU': item.elementary47, + 'Group Classification Junior Secondary 8-10 SU': item.juniorSecondary810, + 'Group Classification Senior Secondary 11-12': item.seniorSecondary1112 + } + }) + filteredContacts.value = contacts.value.map((item: any) => { + return { + 'District Number': response.data.districtData?.districtNumber, + 'District Name': response.data.districtData?.displayName, + 'Contact Type': item.label, + 'Job Title': item.jobTitle, + 'First Name': item.firstName, + 'Last Name': item.lastName, + 'Phone Number': item.phoneNumber, + 'Phone Extension': item.phoneExtension, + 'Alternate Phone Number': item.alternatePhoneNumber, + 'Alternate Phone Extension': item.alternatePhoneExtension, + Email: item.email, + 'Mailing Address': response.data.districtData?.addresses[0].addressLine1, + 'Mailing City': response.data.districtData?.addresses[0].city, + 'Mailing Province': response.data.districtData?.addresses[0].provinceCode, + 'Mailing Postal Code': response.data.districtData?.addresses[0].postal, + 'District Phone': response.data.districtData?.phoneNumber, + 'District Fax': response.data.districtData?.faxNumber, + Website: response.data.districtData?.website + } + }) + } + } catch (error) { + console.error(error) } - } catch (error) { - console.error(error) } // get district contact type codes // try {