cascades of time bounds are now checked on time unit level

This commit is contained in:
Eero Antikainen
2024-11-02 12:21:58 +01:00
committed by Aaro Koivukangas
parent 0f9e9874f1
commit 08891aee19
2 changed files with 61 additions and 1 deletions
+2
View File
@@ -4,6 +4,7 @@ import { WriteHandler } from './writeOperations/writeHandler'
import { getFieldsOfTables } from '../../utils/db'
import { ActionType } from './writeOperations/types'
import { getTimeBoundDetails } from '../timeBound'
import { checkTimeBoundCascade } from '../../utils/cascadeHandler'
const getTimeBoundWriteHandler = (type: ActionType) => {
return new WriteHandler({
@@ -27,6 +28,7 @@ export const writeTimeBound = async (
try {
await writeHandler.start()
const result = await writeHandler.upsertObject('now_tu_bound', timeBound, ['bid'])
const { cascadeErrors, calculatorErrors, localitiesToUpdate } = await checkTimeBoundCascade(timeBound)
writeHandler.idValue = result ? (result.bid as number) : (timeBound.bid as number)
await writeHandler.logUpdatesAndComplete(authorizer, comment ?? '', references ?? [])
return { result: writeHandler.idValue, errorObject: undefined }
+59 -1
View File
@@ -1,4 +1,4 @@
import { EditDataType, TimeUnitDetailsType } from '../../../frontend/src/backendTypes'
import { EditDataType, TimeUnitDetailsType, TimeBoundDetailsType } from '../../../frontend/src/backendTypes'
import { nowDb } from './db'
import { calculateLocalityMaxAge, calculateLocalityMinAge } from './ageCalculator'
@@ -49,3 +49,61 @@ export const checkTimeUnitCascade = async (timeUnit: EditDataType<TimeUnitDetail
}
return { cascadeErrors, calculatorErrors, localitiesToUpdate }
}
type Bounds = {
bid: number
age: number
}
type Unit = {
up_bnd: number
low_bnd: number
}
export const checkTimeBoundCascade = async (timeBound: EditDataType<TimeBoundDetailsType>) => {
const timeUnits = await nowDb.now_time_unit.findMany({
select: {
up_bnd: true,
low_bnd: true,
},
where: {
OR: [{ up_bnd: timeBound.bid }, { low_bnd: timeBound.bid }],
},
})
for (const timeUnit of timeUnits) {
const bounds = await nowDb.now_tu_bound.findMany({
select: {
bid: true,
age: true,
},
where: {
OR: [{ bid: timeUnit.up_bnd }, { bid: timeUnit.low_bnd }],
},
})
if (bounds.length === 2) {
console.log(checkAges(timeUnit, bounds as Bounds[], timeBound.age as number, timeBound.bid as number))
} else {
console.log("Not enough bounds found for time unit. Bad")
}
}
return { cascadeErrors: [], calculatorErrors: [], timeUnitsToUpdate: [] }
}
function checkAges(
unit: Unit,
list: Bounds[],
editedAge: number,
bid: number
): boolean | undefined {
const upBoundEntry = list.find(item => item.bid === unit.up_bnd);
const lowBoundEntry = list.find(item => item.bid === unit.low_bnd);
if (upBoundEntry && lowBoundEntry) {
const upBoundAge = unit.up_bnd === bid ? editedAge : upBoundEntry.age;
const lowBoundAge = unit.low_bnd === bid ? editedAge : lowBoundEntry.age;
return upBoundAge < lowBoundAge;
} else {
console.error("One or both bounds not found in the list.");
return undefined;
}
}