From ff06bb8b74dd03e5af08a446ef7a4424cb269cea Mon Sep 17 00:00:00 2001 From: "hoeppner.dataport" Date: Tue, 17 Dec 2024 17:47:13 +0100 Subject: [PATCH 1/3] fix: handling of school counties for shds --- src/services/school/hooks/index.js | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/services/school/hooks/index.js b/src/services/school/hooks/index.js index d2904cf33c4..0a392675381 100644 --- a/src/services/school/hooks/index.js +++ b/src/services/school/hooks/index.js @@ -240,27 +240,33 @@ const validateCounty = async (context) => { $limit: 1, }, }); - const currentSchool = schools.data[0]; - if (!currentSchool) { - throw new Error(`Internal error`); - } const isSuperHero = await globalHooks.hasRole(context, context.params.account.userId, 'superhero'); + if (!isSuperHero) { + const currentSchool = schools.data[0]; + if (!currentSchool) { + throw new Error(`Internal error`); + } - const { county } = context.data; - if ( - !isSuperHero && - (!currentSchool.federalState.counties.length || - !currentSchool.federalState.counties.some((c) => c._id.toString() === county.toString())) - ) { - throw new Error(`The state doesn't not have a matching county`); + const { county } = context.data; + if ( + !currentSchool.federalState.counties.length || + !currentSchool.federalState.counties.some((c) => c._id.toString() === county.toString()) + ) { + throw new Error(`The state doesn't not have a matching county`); + } + + /* Tries to replace the existing county with a new one */ + if (currentSchool.county && JSON.stringify(currentSchool.county) !== JSON.stringify(county)) { + throw new Error(`This school already have a county`); + } } - /* Tries to replace the existing county with a new one */ - if (!isSuperHero && currentSchool.county && JSON.stringify(currentSchool.county) !== JSON.stringify(county)) { - throw new Error(`This school already have a county`); + const federalState = await context.app.service('federalStates').get(context.data.federalState); + if (!federalState) { + throw new Error(`Unknown federal state was provided for the school`); } - context.data.county = currentSchool.federalState.counties.find((c) => c._id.toString() === county.toString()); + context.data.county = federalState.counties.find((c) => c._id.toString() === context.data.county.toString()); } // checks for empty value and deletes it from context if (context && context.data && Object.keys(context.data).includes('county') && !context.data.county) { From 993a76649df20970bd2dd829edbec049686cc3b1 Mon Sep 17 00:00:00 2001 From: "hoeppner.dataport" Date: Wed, 18 Dec 2024 13:33:05 +0100 Subject: [PATCH 2/3] chore: some changes for the fix --- src/services/school/hooks/index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/services/school/hooks/index.js b/src/services/school/hooks/index.js index 0a392675381..df2ae72de16 100644 --- a/src/services/school/hooks/index.js +++ b/src/services/school/hooks/index.js @@ -233,16 +233,18 @@ const validateOfficialSchoolNumber = async (context) => { // school County const validateCounty = async (context) => { if (context && context.data && context.data.county) { - const schools = await context.app.service('schools').find({ - query: { - _id: context.id, - $populate: 'federalState', - $limit: 1, - }, - }); + let federalState; const isSuperHero = await globalHooks.hasRole(context, context.params.account.userId, 'superhero'); if (!isSuperHero) { + const schools = await context.app.service('schools').find({ + query: { + _id: context.id, + $populate: 'federalState', + $limit: 1, + }, + }); + const currentSchool = schools.data[0]; if (!currentSchool) { throw new Error(`Internal error`); @@ -260,9 +262,14 @@ const validateCounty = async (context) => { if (currentSchool.county && JSON.stringify(currentSchool.county) !== JSON.stringify(county)) { throw new Error(`This school already have a county`); } + + // eslint-disable-next-line prefer-destructuring + federalState = currentSchool.federalState; } - const federalState = await context.app.service('federalStates').get(context.data.federalState); + if (!federalState) { + federalState = await context.app.service('federalStates').get(context.data.federalState); + } if (!federalState) { throw new Error(`Unknown federal state was provided for the school`); } From d95338e35c5e847e354f8c552c813b60258323c9 Mon Sep 17 00:00:00 2001 From: "hoeppner.dataport" Date: Wed, 18 Dec 2024 14:25:32 +0100 Subject: [PATCH 3/3] fix: validation of official schoolnumber for new schools --- src/services/school/hooks/index.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/services/school/hooks/index.js b/src/services/school/hooks/index.js index df2ae72de16..7bda1e83f0d 100644 --- a/src/services/school/hooks/index.js +++ b/src/services/school/hooks/index.js @@ -205,21 +205,25 @@ const isNotAuthenticated = async (context) => { const validateOfficialSchoolNumber = async (context) => { if (context && context.data && context.data.officialSchoolNumber) { const { officialSchoolNumber } = context.data; - const schools = await context.app.service('schools').find({ - query: { - _id: context.id, - $populate: 'federalState', - $limit: 1, - }, - }); - const currentSchool = schools.data[0]; - if (!currentSchool) { - throw new Error(`Internal error`); - } const isSuperHero = await globalHooks.hasRole(context, context.params.account.userId, 'superhero'); - if (!isSuperHero && currentSchool.officialSchoolNumber) { - throw new Error(`This school already have an officialSchoolNumber`); + if (!isSuperHero) { + const schools = await context.app.service('schools').find({ + query: { + _id: context.id, + $populate: 'federalState', + $limit: 1, + }, + }); + + const currentSchool = schools.data[0]; + if (!currentSchool) { + throw new Error(`Internal error`); + } + if (currentSchool.officialSchoolNumber) { + throw new Error(`This school already have an officialSchoolNumber`); + } } + const officialSchoolNumberFormat = /^[a-zA-Z0-9-]+$/; if (!officialSchoolNumberFormat.test(officialSchoolNumber)) { throw new Error(