Terminplaner: Schalter, Konfiguration, Schema
Schalter TERMINE_ENABLED (off|pilot|on) mit TERMINE_PILOT_EMAILS, config.termine und die vier Collection-IDs, Schema der Termine-Collections als reine Daten (server/services/termine/schema.js), Setup-Skript scripts/termine-setup.mjs mit --check und schemaKeys() fuer den Appwrite-Nachbau der Tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f63165c158
commit
334dc1e669
7 files changed
+714
-1
No files matched your search
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Terminplaner: legt die vier Collections termineConfig, termineBookings, termineClaims und termineOutbox mit
|
||||
* Attributen und Indizes in Appwrite an (nur Server-Key, keine Client-Rechte). Das Schema steht in
|
||||
* server/services/termine/schema.js. Idempotent: Vorhandenes wird uebersprungen, fehlende Attribute und Indizes
|
||||
* auch in schon vorhandenen Collections nachgezogen. Vor dem Einschalten (TERMINE_ENABLED) ausfuehren.
|
||||
* Usage:
|
||||
* node scripts/termine-setup.mjs --check (nur melden, nichts anlegen; Exit 1 bei Fehlendem)
|
||||
* node scripts/termine-setup.mjs (Fehlendes anlegen)
|
||||
*/
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import { config } from '../server/config.js'
|
||||
import { TERMINE_COLLECTIONS } from '../server/services/termine/schema.js'
|
||||
import { createAppwriteClient } from './analyse-setup.mjs'
|
||||
|
||||
export async function main({ check = false, client, log = console.log } = {}) {
|
||||
const api = client || createAppwriteClient()
|
||||
const missing = []
|
||||
const missingCollections = []
|
||||
const missingIndexes = []
|
||||
const plan = []
|
||||
|
||||
// Erst alles pruefen, dann (ohne --check) anlegen, damit --check garantiert nichts veraendert
|
||||
for (const [name, { attributes, indexes }] of Object.entries(TERMINE_COLLECTIONS)) {
|
||||
const id = config.collections[name]
|
||||
const exists = await api.collectionExists(id)
|
||||
const haveAttributes = exists ? new Set((await api.listAttributes(id)).map((a) => a.key)) : new Set()
|
||||
const haveIndexes = exists ? new Set((await api.listIndexes(id)).map((i) => i.key)) : new Set()
|
||||
const absentAttributes = attributes.filter((a) => !haveAttributes.has(a.key))
|
||||
const absentIndexes = indexes.filter((i) => !haveIndexes.has(i.key))
|
||||
|
||||
if (!exists) {
|
||||
log(`${check ? '-' : '+'} Collection ${id}${check ? ' FEHLT' : ''}`)
|
||||
missingCollections.push(id)
|
||||
} else {
|
||||
log(`= ${id}: ${haveAttributes.size} Attribute, ${haveIndexes.size} Indizes vorhanden`)
|
||||
}
|
||||
if (check) {
|
||||
absentAttributes.forEach((a) => log(`- ${id}.${a.key} FEHLT`))
|
||||
absentIndexes.forEach((i) => log(`- Index ${id}.${i.key} FEHLT`))
|
||||
}
|
||||
absentAttributes.forEach((a) => missing.push({ ...a, collection: id }))
|
||||
absentIndexes.forEach((i) => missingIndexes.push({ ...i, collection: id }))
|
||||
plan.push({ id, exists, absentAttributes, absentIndexes })
|
||||
}
|
||||
|
||||
const result = { missing, missingCollections, missingIndexes }
|
||||
if (check) {
|
||||
const open = missing.length + missingCollections.length + missingIndexes.length
|
||||
log(open ? `${open} Eintrag/Eintraege fehlen.` : 'Alles vorhanden.')
|
||||
return result
|
||||
}
|
||||
|
||||
for (const { id, exists, absentAttributes } of plan) {
|
||||
if (!exists) await api.ensureCollection(id, id)
|
||||
for (const attr of absentAttributes) {
|
||||
await api.createAttribute(id, attr)
|
||||
log(`+ Attribut ${id}.${attr.key}`)
|
||||
}
|
||||
}
|
||||
// Indizes brauchen verfuegbare Attribute: erst warten, dann anlegen
|
||||
for (const { id, absentAttributes } of plan) {
|
||||
for (const attr of absentAttributes) await api.waitForAttribute(id, attr.key)
|
||||
}
|
||||
for (const { id, absentIndexes } of plan) {
|
||||
for (const index of absentIndexes) {
|
||||
await api.createIndex(id, index)
|
||||
log(`+ Index ${id}.${index.key}`)
|
||||
}
|
||||
}
|
||||
log('Fertig.')
|
||||
return result
|
||||
}
|
||||
|
||||
const invokedDirectly = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href
|
||||
if (invokedDirectly) {
|
||||
const check = process.argv.includes('--check')
|
||||
main({ check })
|
||||
.then(({ missing, missingCollections, missingIndexes }) =>
|
||||
process.exit(check && (missing.length || missingCollections.length || missingIndexes.length) ? 1 : 0))
|
||||
.catch((e) => {
|
||||
console.error(e.message)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user