Buchungskalender: Abschluss – S6 Belegung laedt nur Relevantes, bis-Indizes, 10-s-Speicher
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab838d07bc
commit
17400cac1f
7 files changed
+99
-17
No files matched your search
@@ -7,7 +7,7 @@ import { belegteBereiche } from '../services/unterkunft/belegung.js'
|
||||
import { istTag, naechte } from '../services/unterkunft/datum.js'
|
||||
import { UkFehler } from '../services/unterkunft/fehler.js'
|
||||
import { unterkunftPerSiteKey } from '../services/unterkunft/kontext.js'
|
||||
import { ID_RE, findeRechnung, ladeAnfrage, ladeRechnung, ladeUnterkunft, listeUnterkuenfte } from '../services/unterkunft/speicher.js'
|
||||
import { ID_RE, belegungsStand, findeRechnung, ladeAnfrage, ladeRechnung, ladeUnterkunft, listeUnterkuenfte } from '../services/unterkunft/speicher.js'
|
||||
import { checkoutFuer, schliesseAbWennBezahlt } from '../services/unterkunft/zahlung.js'
|
||||
import { formToken, ipHashVon, pruefeFormToken, tokenPasst } from '../services/unterkunft/token.js'
|
||||
import { darfAnfrage, darfAnfrageSite, darfBuchungsversuch, zaehle } from '../services/termine/limits.js'
|
||||
@@ -264,6 +264,12 @@ function verschmelze(bereiche) {
|
||||
return aus
|
||||
}
|
||||
|
||||
// Antwort von /belegung 10 s im Speicher je siteKey+unterkunft+von+bis; jedes Schreiben an Sperren/Anfragen (speicher.belegungsStand) macht sie ungueltig
|
||||
const BELEGUNG_TTL_MS = 10 * 1000
|
||||
const BELEGUNG_MAX = 2000
|
||||
const belegungSpeicher = new Map()
|
||||
export const leereBelegungSpeicherFuerTests = () => belegungSpeicher.clear()
|
||||
|
||||
router.get(
|
||||
'/:siteKey/belegung',
|
||||
route(async (req, now) => {
|
||||
@@ -277,13 +283,24 @@ router.get(
|
||||
throw ungueltig({ zeitraum: 'Ungültiger Zeitraum.' })
|
||||
}
|
||||
const tag = heute(now)
|
||||
const nowMs = now instanceof Date ? now.getTime() : Number(now)
|
||||
const speicherKey = `${req.params.siteKey}|${unterkunft}|${von}|${bis}`
|
||||
const gemerkt = belegungSpeicher.get(speicherKey)
|
||||
if (gemerkt && gemerkt.bis > nowMs && gemerkt.stand === belegungsStand() && gemerkt.tag === tag) return { body: gemerkt.body }
|
||||
const stand = belegungsStand()
|
||||
const bisMax = monateDazu(tag, config.unterkunft.horizontMonate)
|
||||
const bisBis = bis > bisMax ? bisMax : bis // Fenster des Widgets (UTC-Datum des Browsers) darf nie an einem Tag Abweichung scheitern
|
||||
const u = await ladeUnterkunft(unterkunft)
|
||||
if (!u || u.featureId !== ctx.featureId || u.aktiv !== true) throw nichtGefunden()
|
||||
const ab = von < tag ? tag : von
|
||||
const belegt = ab < bisBis ? verschmelze(await belegteBereiche(u.$id, ab, bisBis, { now })) : []
|
||||
return { body: { belegt, heute: tag, bisMax } }
|
||||
const body = { belegt, heute: tag, bisMax }
|
||||
if (belegungSpeicher.size >= BELEGUNG_MAX) {
|
||||
for (const [k, e] of belegungSpeicher) if (e.bis <= nowMs) belegungSpeicher.delete(k)
|
||||
if (belegungSpeicher.size >= BELEGUNG_MAX) belegungSpeicher.delete(belegungSpeicher.keys().next().value)
|
||||
}
|
||||
belegungSpeicher.set(speicherKey, { bis: nowMs + BELEGUNG_TTL_MS, stand, tag, body })
|
||||
return { body }
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user