From 865b787214b100bef078976e242804a8bf53f91e Mon Sep 17 00:00:00 2001 From: hoeppner-dataport <106819770+hoeppner-dataport@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:04:07 +0100 Subject: [PATCH] BC-8182 - fix: make creating of schools work again for superheros (#5403) for superheros that create new schools * fix: handling of school counties * fix: validation of official schoolnumber --- src/services/school/hooks/index.js | 87 ++++++++++++++++++------------ 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/src/services/school/hooks/index.js b/src/services/school/hooks/index.js index d2904cf33c4..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( @@ -233,34 +237,47 @@ 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, - }, - }); - const currentSchool = schools.data[0]; - if (!currentSchool) { - throw new Error(`Internal error`); - } + 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 { 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 currentSchool = schools.data[0]; + if (!currentSchool) { + throw new Error(`Internal error`); + } + + 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`); + } + + // eslint-disable-next-line prefer-destructuring + federalState = currentSchool.federalState; } - /* 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`); + 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`); } - 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) {