Files

100 lines
4.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Buchungskalender: legt die sechs Collections ukUnterkuenfte, ukPreise, ukSperren, ukAnfragen, ukRechnungen und ukEinstellungen mit
* Attributen und Indizes in Appwrite an (nur Server-Key, keine Client-Rechte). Das Schema steht in
* server/services/unterkunft/schema.js. Idempotent: Vorhandenes wird uebersprungen, fehlende Attribute und Indizes
* auch in schon vorhandenen Collections nachgezogen. Vor dem Einschalten (UNTERKUNFT_ENABLED) ausfuehren.
* Usage:
* node scripts/unterkunft-setup.mjs --check (nur melden, nichts anlegen; Exit 1 bei Fehlendem)
* node scripts/unterkunft-setup.mjs (Fehlendes anlegen)
*/
import { pathToFileURL } from 'node:url'
import { config } from '../server/config.js'
import { UNTERKUNFT_COLLECTIONS } from '../server/services/unterkunft/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(UNTERKUNFT_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 })
}
// Rechnungs-PDF an Gastmails: termineOutbox braucht das Attribut `anhang` (Schema des Terminplaners). Fehlt es, meldet --check das (Exit 1) und
// der normale Lauf legt es an. Fehlt die ganze Collection, ist das ein Fall fuer scripts/termine-setup.mjs (nur Hinweis).
const outboxId = config.collections.termineOutbox
const anhang = { key: 'anhang', type: 'string', size: 500, required: false }
if (await api.collectionExists(outboxId)) {
if (!(await api.listAttributes(outboxId)).some((a) => a.key === 'anhang')) {
if (check) log(`- ${outboxId}.anhang FEHLT`)
missing.push({ ...anhang, collection: outboxId })
plan.push({ id: outboxId, exists: true, absentAttributes: [anhang], absentIndexes: [] })
}
} else {
log(`! ${outboxId} fehlt: zuerst scripts/termine-setup.mjs ausfuehren (Gastmails mit Rechnungs-PDF brauchen termineOutbox.anhang)`)
}
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)
})
}