Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make creating of schools work again for superheros #5403

Merged
merged 6 commits into from
Dec 19, 2024
87 changes: 52 additions & 35 deletions src/services/school/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
Loading