Skip to content

Commit

Permalink
fix feature checkbox handling when creating schools
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeppner-dataport committed Dec 19, 2024
1 parent 636d1d1 commit 07a75c9
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions controllers/schools.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,33 @@ const sortSchools = (schools, sortCriteria) => {
});
};

const collectSchoolFeatures = (data) => {
const features = [];
for (let feature of SCHOOL_FEATURES) {
let key = "hasFeature_" + feature;
if (data[key]) {
features.push(feature);
}
}
return features;
}

const separateSchoolFeatures = (data) => {
for (let feature of SCHOOL_FEATURES) {
let key = 'hasFeature_' + feature;
if (data.features) {
data[key] = data.features.indexOf(feature) !== -1;
} else {
data[key] = false;
}
}
return data;
}

const getCreateHandler = (service) => {
return function (req, res, next) {
req.body.features = collectSchoolFeatures(req.body);

api(req)
.post('/' + service + '/', {
// TODO: sanitize
Expand All @@ -165,6 +190,8 @@ const getUpdateHandler = (service) => {
try {
const configuration = await api(req, {version: 'v3'}).get(`/config/public`);



if (configuration.TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE) {
await api(req, {version: 'v3'}).patch(`/school/${req.params.id}`, {
json: {
Expand All @@ -176,15 +203,8 @@ const getUpdateHandler = (service) => {
},
})
}

// parse school features
req.body.features = [];
for (let feature of SCHOOL_FEATURES) {
let key = 'hasFeature_' + feature;
if (req.body[key]) {
req.body.features.push(feature);
}
}

req.body.features = collectSchoolFeatures(req.body);

await api(req).patch('/' + service + '/' + req.params.id, {
// TODO: sanitize
Expand All @@ -205,14 +225,7 @@ const getDetailHandler = (service) => {
const data = await api(req).get('/' + service + '/' + req.params.id)

// parse school features
for (let feature of SCHOOL_FEATURES) {
let key = 'hasFeature_' + feature;
if (data.features) {
data[key] = data.features.indexOf(feature) !== -1;
} else {
data[key] = false;
}
}
separateSchoolFeatures(data);

if (!configuration.TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE) {
data.hasFeature_studentVisibility_disabled = true;
Expand Down

0 comments on commit 07a75c9

Please sign in to comment.