Buchungskalender: Speicher und Belegung
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c38c8e7c13
commit
bdf191d945
3 files changed
+330
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { listeAnfragenNachStatus, listeSperren } from './speicher.js'
|
||||
|
||||
/** Status, die die Unterkunft endgueltig belegen. 'freigegeben' blockiert nur bis fristAt. */
|
||||
export const AKTIVE_STATUS = ['gebucht', 'bezahlt']
|
||||
|
||||
/** Halboffene Intervalle [von,bis) auf Tagesstrings: Abreisetag gleich Anreisetag ueberschneidet nicht. */
|
||||
export const ueberschneidet = (a, b) => a.von < b.bis && b.von < a.bis
|
||||
|
||||
/**
|
||||
* Belegte Bereiche einer Unterkunft im Fenster [von,bis), auf das Fenster zugeschnitten, nach von sortiert.
|
||||
* Quellen: manuell, ical (Sperre mit quelle 'ical:<id>'), gebucht (gebucht|bezahlt), reserviert (freigegeben mit fristAt in der Zukunft).
|
||||
* ohneAnfrage: ID einer Anfrage, die nicht zaehlt (z. B. beim Freigeben der Anfrage selbst). now: Date oder ms.
|
||||
*/
|
||||
export async function belegteBereiche(unterkunftId, von, bis, { ohneAnfrage, now = Date.now() } = {}) {
|
||||
const jetzt = now instanceof Date ? now.getTime() : now
|
||||
const fenster = { von, bis }
|
||||
const roh = []
|
||||
for (const s of await listeSperren(unterkunftId)) {
|
||||
roh.push({ von: s.von, bis: s.bis, quelle: String(s.quelle ?? '').startsWith('ical') ? 'ical' : 'manuell' })
|
||||
}
|
||||
for (const a of await listeAnfragenNachStatus(unterkunftId, [...AKTIVE_STATUS, 'freigegeben'])) {
|
||||
if (ohneAnfrage && a.$id === ohneAnfrage) continue
|
||||
if (a.status === 'freigegeben') {
|
||||
const frist = Date.parse(a.fristAt)
|
||||
if (!(frist > jetzt)) continue
|
||||
roh.push({ von: a.von, bis: a.bis, quelle: 'reserviert' })
|
||||
} else {
|
||||
roh.push({ von: a.von, bis: a.bis, quelle: 'gebucht' })
|
||||
}
|
||||
}
|
||||
return roh
|
||||
.filter((r) => r.von && r.bis && ueberschneidet(r, fenster))
|
||||
.map((r) => ({ von: r.von > von ? r.von : von, bis: r.bis < bis ? r.bis : bis, quelle: r.quelle }))
|
||||
.sort((a, b) => (a.von < b.von ? -1 : a.von > b.von ? 1 : 0))
|
||||
}
|
||||
|
||||
export async function istFrei(unterkunftId, von, bis, opts = {}) {
|
||||
return (await belegteBereiche(unterkunftId, von, bis, opts)).length === 0
|
||||
}
|
||||
Reference in new issue
Block a user