fix(falukant): validate office term length and adjust term end calculation
All checks were successful
Deploy to production / deploy (push) Successful in 2m51s

- Added validation for office term length to ensure it is a positive finite number, throwing an error for invalid values.
- Updated the calculation of the term end date to use the term length in days instead of years, aligning with the expected data format.
This commit is contained in:
Torsten Schulz (local)
2026-04-10 16:08:50 +02:00
parent c6419c6c34
commit 60ef98283f
2 changed files with 11 additions and 4 deletions

View File

@@ -457,8 +457,14 @@ class FalukantPoliticalPowersService {
}
const start = new Date();
const termDays = Number(officeType.termLength);
if (!Number.isFinite(termDays) || termDays <= 0) {
const err = new Error('invalid_office_term_length');
err.status = 500;
throw err;
}
const end = new Date(start);
end.setFullYear(end.getFullYear() + (officeType.termLength || 4));
end.setDate(end.getDate() + termDays);
let newOffice;
await sequelize.transaction(async (t) => {

View File

@@ -6388,13 +6388,14 @@ class FalukantService extends BaseService {
return offices.map((office) => {
const o = office.get({ plain: true });
// Enddatum der Amtszeit berechnen: Start = createdAt, Dauer = termLength Jahre
// Enddatum der Amtszeit: wie DB/C++-Worker — Start + termLength Kalendertage
// (falukant_type.political_office_type.term_length ist in Tagen, vgl. politics_worker.h)
let termEnds = null;
if (o.createdAt && o.type && typeof o.type.termLength === 'number') {
if (o.createdAt && o.type && typeof o.type.termLength === 'number' && o.type.termLength > 0) {
const start = new Date(o.createdAt);
if (!Number.isNaN(start.getTime())) {
const end = new Date(start);
end.setFullYear(end.getFullYear() + o.type.termLength);
end.setDate(end.getDate() + o.type.termLength);
termEnds = end;
}
}