Öffnungszeiten: Schalter, Collections und Felder
ZEITEN_ENABLED/ZEITEN_PILOT_EMAILS/ZEITEN_KEIN_NEUBAU als config.zeiten (neben config.termine), zeitenSetup (30 € einmalig), Collection-IDs oeffnungszeiten und zeitenErinnerungen, Schema der beiden Collections plus fünf neue portalFeatures-Felder, scripts/zeiten-setup.mjs (idempotent, --check), schemaKeys kennt die neuen Collections und Felder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
385f768d76
commit
4b4cd1058f
7 files changed
+649
-3
No files matched your search
@@ -0,0 +1,94 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
/**
|
||||||
|
* Oeffnungszeiten live: legt die Collections oeffnungszeiten und zeitenErinnerungen mit Attributen und Indizes in
|
||||||
|
* Appwrite an (nur Server-Key, keine Client-Rechte) und ergaenzt portalFeatures um die Felder unlockedBy,
|
||||||
|
* aboEndeAt, einbau, behaltenSessionId und k24At (Abo-Freischaltung, Behalten, Selbstheilung). Das Schema steht in
|
||||||
|
* server/services/zeiten/schema.js. Idempotent: Vorhandenes wird uebersprungen, fehlende Attribute und Indizes
|
||||||
|
* auch in schon vorhandenen Collections nachgezogen. portalFeatures selbst legt setup-abo-collections.mjs an
|
||||||
|
* (fehlt sie, bricht das Skript vor jeder Aenderung ab). Vor dem Einschalten (ZEITEN_ENABLED) und vor dem
|
||||||
|
* Code-Deploy ausfuehren: der Code schreibt die neuen Felder, ohne Schema antwortet Appwrite mit "Unknown attribute".
|
||||||
|
* Usage:
|
||||||
|
* node scripts/zeiten-setup.mjs --check (nur melden, nichts anlegen; Exit 1 bei Fehlendem)
|
||||||
|
* node scripts/zeiten-setup.mjs (Fehlendes anlegen)
|
||||||
|
*/
|
||||||
|
import { pathToFileURL } from 'node:url'
|
||||||
|
import { config } from '../server/config.js'
|
||||||
|
import { ZEITEN_COLLECTIONS, ZEITEN_FEATURE_ATTRIBUTES } from '../server/services/zeiten/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 = []
|
||||||
|
|
||||||
|
// Eigene Collections plus die vorhandene portalFeatures (nur zusaetzliche Attribute, keine Indizes)
|
||||||
|
const targets = [
|
||||||
|
...Object.entries(ZEITEN_COLLECTIONS).map(([name, { attributes, indexes }]) => ({ id: config.collections[name], attributes, indexes, own: true })),
|
||||||
|
{ id: config.collections.portalFeatures, attributes: ZEITEN_FEATURE_ATTRIBUTES, indexes: [], own: false },
|
||||||
|
]
|
||||||
|
|
||||||
|
// Erst alles pruefen, dann (ohne --check) anlegen, damit --check garantiert nichts veraendert
|
||||||
|
for (const { id, attributes, indexes, own } of targets) {
|
||||||
|
const exists = await api.collectionExists(id)
|
||||||
|
if (!exists && !own) throw new Error(`Collection ${id} (portalFeatures) fehlt – zuerst scripts/setup-abo-collections.mjs ausfuehren`)
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -30,6 +30,16 @@ function nonNegativeNumber(raw, fallback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const termineEnabled = (process.env.TERMINE_ENABLED || '').trim().toLowerCase()
|
const termineEnabled = (process.env.TERMINE_ENABLED || '').trim().toLowerCase()
|
||||||
|
const zeitenEnabled = (process.env.ZEITEN_ENABLED || '').trim().toLowerCase()
|
||||||
|
|
||||||
|
/** Komma-Liste aus einer Umgebungsvariablen: getrimmt, klein geschrieben, ohne Leereintraege; leer ergibt den Vorgabewert. */
|
||||||
|
function commaList(raw, fallback) {
|
||||||
|
const list = String(raw || '')
|
||||||
|
.split(',')
|
||||||
|
.map((s) => s.trim().toLowerCase())
|
||||||
|
.filter(Boolean)
|
||||||
|
return list.length ? list : fallback
|
||||||
|
}
|
||||||
|
|
||||||
// Abo-Produkte (Spec 2026-09-27 "Verträge, Hosting-Abo und E-Mail-Zusatzspeicher"). Preise in Cent,
|
// Abo-Produkte (Spec 2026-09-27 "Verträge, Hosting-Abo und E-Mail-Zusatzspeicher"). Preise in Cent,
|
||||||
// Endpreise gemäß § 19 UStG. Texte, die Kunden sehen, stimmen mit den Vertragsbedingungen überein.
|
// Endpreise gemäß § 19 UStG. Texte, die Kunden sehen, stimmen mit den Vertragsbedingungen überein.
|
||||||
@@ -116,6 +126,16 @@ const termineSetup = {
|
|||||||
priceNickname: 'Terminplaner-Einrichtung 149 €',
|
priceNickname: 'Terminplaner-Einrichtung 149 €',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Oeffnungszeiten live: einmalige Einrichtung (Einmalpreis, kein Abo); im Wartungs-Abo ist die Funktion enthalten
|
||||||
|
const zeitenSetup = {
|
||||||
|
key: 'zeitenSetup',
|
||||||
|
name: 'Öffnungszeiten live – Einrichtung',
|
||||||
|
amountCents: 3000,
|
||||||
|
lookupKey: 'webklar_zeiten_einrichtung_3000',
|
||||||
|
oneTime: true,
|
||||||
|
priceNickname: 'Öffnungszeiten-Einrichtung 30 €',
|
||||||
|
}
|
||||||
|
|
||||||
// Ticketsystem (WOMS); eigene Konstante, weil auch die Enable-Banking-Rueckkehradresse darauf aufbaut
|
// Ticketsystem (WOMS); eigene Konstante, weil auch die Enable-Banking-Rueckkehradresse darauf aufbaut
|
||||||
const womsBaseUrl = (process.env.WOMS_PUBLIC_URL || 'https://ticket.webklar.com').replace(/\/$/, '')
|
const womsBaseUrl = (process.env.WOMS_PUBLIC_URL || 'https://ticket.webklar.com').replace(/\/$/, '')
|
||||||
|
|
||||||
@@ -310,6 +330,9 @@ export const config = {
|
|||||||
termineBookings: process.env.APPWRITE_COLLECTION_TERMINE_BOOKINGS || 'termineBookings',
|
termineBookings: process.env.APPWRITE_COLLECTION_TERMINE_BOOKINGS || 'termineBookings',
|
||||||
termineClaims: process.env.APPWRITE_COLLECTION_TERMINE_CLAIMS || 'termineClaims',
|
termineClaims: process.env.APPWRITE_COLLECTION_TERMINE_CLAIMS || 'termineClaims',
|
||||||
termineOutbox: process.env.APPWRITE_COLLECTION_TERMINE_OUTBOX || 'termineOutbox',
|
termineOutbox: process.env.APPWRITE_COLLECTION_TERMINE_OUTBOX || 'termineOutbox',
|
||||||
|
// Oeffnungszeiten live (Einstellungen, Erinnerungsmerker; scripts/zeiten-setup.mjs)
|
||||||
|
oeffnungszeiten: process.env.APPWRITE_COLLECTION_OEFFNUNGSZEITEN || 'oeffnungszeiten',
|
||||||
|
zeitenErinnerungen: process.env.APPWRITE_COLLECTION_ZEITEN_ERINNERUNGEN || 'zeitenErinnerungen',
|
||||||
},
|
},
|
||||||
|
|
||||||
publicBaseUrl: (process.env.PORTAL_PUBLIC_URL || 'https://project.webklar.com').replace(/\/$/, ''),
|
publicBaseUrl: (process.env.PORTAL_PUBLIC_URL || 'https://project.webklar.com').replace(/\/$/, ''),
|
||||||
@@ -326,6 +349,7 @@ export const config = {
|
|||||||
mailStorage,
|
mailStorage,
|
||||||
newsSetup,
|
newsSetup,
|
||||||
termineSetup,
|
termineSetup,
|
||||||
|
zeitenSetup,
|
||||||
ustHinweis,
|
ustHinweis,
|
||||||
// Register der kaufbaren Funktionen (siehe oben)
|
// Register der kaufbaren Funktionen (siehe oben)
|
||||||
features,
|
features,
|
||||||
@@ -551,6 +575,18 @@ export const config = {
|
|||||||
minAntwortMs: nonNegativeNumber(process.env.TERMINE_MIN_ANTWORT_MS, 300),
|
minAntwortMs: nonNegativeNumber(process.env.TERMINE_MIN_ANTWORT_MS, 300),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Oeffnungszeiten live (Funktionsschluessel 'zeiten'); Geschwister von termine (termineSchalter.test.js pinnt
|
||||||
|
// config.termine). Wird bei jedem Aufruf gelesen; Schalter siehe server/services/zeiten/schalter.js
|
||||||
|
zeiten: {
|
||||||
|
// off (Vorgabe) | pilot (nur ZEITEN_PILOT_EMAILS) | on (alle Kunden)
|
||||||
|
mode: ['pilot', 'on'].includes(zeitenEnabled) ? zeitenEnabled : 'off',
|
||||||
|
// Kunden-E-Mails fuer den Pilotbetrieb; '*' = alle
|
||||||
|
pilotEmails: commaList(process.env.ZEITEN_PILOT_EMAILS, []),
|
||||||
|
// Subdomains, fuer die der Neubau nie angestossen wird (Einbau von Hand); ZEITEN_KEIN_NEUBAU ist eine
|
||||||
|
// Komma-Liste, die Vorgabe ist emmas-schatzkiste
|
||||||
|
keinNeubau: commaList(process.env.ZEITEN_KEIN_NEUBAU, ['emmas-schatzkiste']),
|
||||||
|
},
|
||||||
|
|
||||||
invoiceNinja: {
|
invoiceNinja: {
|
||||||
url: (process.env.INVOICE_NINJA_URL || 'https://invoice.webklar.com').replace(/\/$/, ''),
|
url: (process.env.INVOICE_NINJA_URL || 'https://invoice.webklar.com').replace(/\/$/, ''),
|
||||||
apiToken: process.env.INVOICE_NINJA_API_TOKEN || '',
|
apiToken: process.env.INVOICE_NINJA_API_TOKEN || '',
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { config } from '../../config.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oeffnungszeiten-Schalter (Funktionsschluessel 'zeiten'): ZEITEN_ENABLED = off (Vorgabe) | pilot | on.
|
||||||
|
* Liest config.zeiten bei jedem Aufruf. Bei off laufen keine Zeiten-Jobs und keine Zeiten-Abfragen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 'off' | 'pilot' | 'on'; alles andere (leer, unbekannt) gilt als off. */
|
||||||
|
export function zeitenModus() {
|
||||||
|
const mode = config.zeiten.mode
|
||||||
|
return mode === 'pilot' || mode === 'on' ? mode : 'off'
|
||||||
|
}
|
||||||
|
|
||||||
|
export function zeitenAus() {
|
||||||
|
return zeitenModus() === 'off'
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Darf dieser Kunde Oeffnungszeiten live nutzen? pilot = nur Adressen aus ZEITEN_PILOT_EMAILS ('*' = alle; eine
|
||||||
|
* leere Liste laesst niemanden durch); on = alle Kunden; off = niemand.
|
||||||
|
*/
|
||||||
|
export function zeitenFuerKunde(email) {
|
||||||
|
const modus = zeitenModus()
|
||||||
|
if (modus === 'off') return false
|
||||||
|
if (modus === 'on') return true
|
||||||
|
const erlaubt = config.zeiten.pilotEmails
|
||||||
|
if (erlaubt.includes('*')) return true
|
||||||
|
const adresse = String(email || '').trim().toLowerCase()
|
||||||
|
return adresse !== '' && erlaubt.includes(adresse)
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* Oeffnungszeiten live: Appwrite-Schema der beiden Collections und der neuen portalFeatures-Felder
|
||||||
|
* (Spezifikation 2026-10-04-oeffnungszeiten-live-design.md, Abschnitte 2.1 bis 2.3).
|
||||||
|
* Reine Daten, absichtlich ohne Import von server/config.js: test/support/schema.js liest sie vor useTestEnv(),
|
||||||
|
* ein frueh geladenes config.js wuerde den Endpunkt des Appwrite-Nachbaus verfehlen. Die Collection-IDs stehen in
|
||||||
|
* config.collections.<Name>; scripts/zeiten-setup.mjs legt alles an (auch die portalFeatures-Felder, bewusst nicht
|
||||||
|
* scripts/feature-setup.mjs: dessen Tests pinnen genau drei Attribute).
|
||||||
|
*
|
||||||
|
* Attribut: { key, type: 'string' | 'integer' | 'boolean' | 'datetime', size (nur Text), required: false }
|
||||||
|
* Index: { key, type: 'key', attributes: [...] }
|
||||||
|
* Datumswerte sind UTC (toISOString()); Appwrite liefert sie als "+00:00" zurueck, daher immer mit new Date() lesen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const str = (key, size) => ({ key, type: 'string', size, required: false })
|
||||||
|
const int = (key) => ({ key, type: 'integer', required: false })
|
||||||
|
const dt = (key) => ({ key, type: 'datetime', required: false })
|
||||||
|
const index = (key, ...attributes) => ({ key, type: 'key', attributes })
|
||||||
|
|
||||||
|
export const ZEITEN_COLLECTIONS = {
|
||||||
|
// Eine Einstellung je Funktion; Dokument-ID = ID des portalFeatures-Dokuments. Nur der Server-Key schreibt.
|
||||||
|
// daten = JSON der Kundeneingaben (hoechstens 32 KB, die Spalte ist groesser), team = JSON der Team-Einstellungen
|
||||||
|
// (nur ueber WOMS aenderbar), ersatzHash = Hash des Hauptplan-Wochenplans beim letzten Ersatz-HTML,
|
||||||
|
// updatedBy = 'kunde' | 'team:<staffId>'.
|
||||||
|
oeffnungszeiten: {
|
||||||
|
attributes: [
|
||||||
|
str('customerId', 64),
|
||||||
|
str('projectId', 64),
|
||||||
|
str('featureId', 64),
|
||||||
|
int('version'),
|
||||||
|
str('daten', 40000),
|
||||||
|
int('teamVersion'),
|
||||||
|
str('team', 2000),
|
||||||
|
str('ersatzHash', 64),
|
||||||
|
dt('updatedAt'),
|
||||||
|
str('updatedBy', 64),
|
||||||
|
],
|
||||||
|
// idx_customer: Loeschung beim Kunden-Loeschen
|
||||||
|
indexes: [index('idx_customer', 'customerId')],
|
||||||
|
},
|
||||||
|
|
||||||
|
// Erinnerungsmerker der Feiertags-Mail; Dokument-ID = sha1(featureId|datum) gekuerzt (anlegen mit 409 = schon
|
||||||
|
// erinnert). datum = Kalendertag 'YYYY-MM-DD' (Berliner Zeit); der Tagesjob loescht Merker, deren Datum > 30 Tage
|
||||||
|
// zurueckliegt (idx_datum).
|
||||||
|
zeitenErinnerungen: {
|
||||||
|
attributes: [str('featureId', 64), str('datum', 10), dt('createdAt')],
|
||||||
|
indexes: [index('idx_datum', 'datum')],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Neue Felder von portalFeatures (alle optional), angelegt von scripts/zeiten-setup.mjs:
|
||||||
|
* - unlockedBy: 'kauf' | 'abo' | 'team'
|
||||||
|
* - aboEndeAt: Zeitpunkt, an dem der Abgleich das Ende des Wartungs-Abos erkannt hat
|
||||||
|
* - einbau: 'angestossen' | 'manuell' (Ergebnis des Neubau-Anstosses)
|
||||||
|
* - behaltenSessionId: offene Behalten-Zahlung (Stripe-Sitzung)
|
||||||
|
* - k24At: Marker der Mail K24 (Selbstheilung)
|
||||||
|
*/
|
||||||
|
export const ZEITEN_FEATURE_ATTRIBUTES = [
|
||||||
|
str('unlockedBy', 8),
|
||||||
|
dt('aboEndeAt'),
|
||||||
|
str('einbau', 12),
|
||||||
|
str('behaltenSessionId', 128),
|
||||||
|
dt('k24At'),
|
||||||
|
]
|
||||||
|
|
||||||
|
/** Datumsfelder je Collection, damit der Appwrite-Nachbau der Tests sie wie Appwrite formatieren kann. */
|
||||||
|
export const ZEITEN_DATETIMES = Object.fromEntries(
|
||||||
|
Object.entries(ZEITEN_COLLECTIONS).map(([name, { attributes }]) => [
|
||||||
|
name,
|
||||||
|
attributes.filter((attribute) => attribute.type === 'datetime').map((attribute) => attribute.key),
|
||||||
|
]),
|
||||||
|
)
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync } from 'node:fs'
|
||||||
import { TERMINE_COLLECTIONS } from '../../server/services/termine/schema.js'
|
import { TERMINE_COLLECTIONS } from '../../server/services/termine/schema.js'
|
||||||
|
import { ZEITEN_COLLECTIONS, ZEITEN_FEATURE_ATTRIBUTES } from '../../server/services/zeiten/schema.js'
|
||||||
import { FEATURE_CONSENT_ATTRIBUTES, PORTAL_FEATURE_ATTRIBUTES } from '../../scripts/feature-setup.mjs'
|
import { FEATURE_CONSENT_ATTRIBUTES, PORTAL_FEATURE_ATTRIBUTES } from '../../scripts/feature-setup.mjs'
|
||||||
|
|
||||||
// Setup-Skripte, die Collections in Appwrite anlegen (Blockformat: 'id', ... \n ],)
|
// Setup-Skripte, die Collections in Appwrite anlegen (Blockformat: 'id', ... \n ],)
|
||||||
@@ -9,12 +10,15 @@ const SETUP_SCRIPTS = ['setup-abo-collections.mjs', 'setup-finanzen.mjs']
|
|||||||
* Attribut-Schlüssel einer Collection, wie sie scripts/setup-abo-collections.mjs bzw.
|
* Attribut-Schlüssel einer Collection, wie sie scripts/setup-abo-collections.mjs bzw.
|
||||||
* scripts/setup-finanzen.mjs in Appwrite anlegen. Tests geben sie dem Appwrite-Nachbau als Schema:
|
* scripts/setup-finanzen.mjs in Appwrite anlegen. Tests geben sie dem Appwrite-Nachbau als Schema:
|
||||||
* Schreibt der Code ein Feld, das das Setup-Skript nicht anlegt, scheitert der Test wie Appwrite in
|
* Schreibt der Code ein Feld, das das Setup-Skript nicht anlegt, scheitert der Test wie Appwrite in
|
||||||
* Produktion. Die Termine-Collections kommen aus server/services/termine/schema.js (scripts/termine-setup.mjs);
|
* Produktion. Die Termine-Collections kommen aus server/services/termine/schema.js (scripts/termine-setup.mjs),
|
||||||
* portalFeatures bekommt zusaetzlich die Attribute aus scripts/feature-setup.mjs (PORTAL_FEATURE_ATTRIBUTES);
|
* die Zeiten-Collections aus server/services/zeiten/schema.js (scripts/zeiten-setup.mjs);
|
||||||
|
* portalFeatures bekommt zusaetzlich die Attribute aus scripts/feature-setup.mjs (PORTAL_FEATURE_ATTRIBUTES) und die
|
||||||
|
* Zeiten-Felder aus server/services/zeiten/schema.js (ZEITEN_FEATURE_ATTRIBUTES, angelegt von scripts/zeiten-setup.mjs);
|
||||||
* featureConsents (Zustimmungsnachweis) steht ganz dort (FEATURE_CONSENT_ATTRIBUTES).
|
* featureConsents (Zustimmungsnachweis) steht ganz dort (FEATURE_CONSENT_ATTRIBUTES).
|
||||||
*/
|
*/
|
||||||
export function schemaKeys(collection) {
|
export function schemaKeys(collection) {
|
||||||
if (Object.hasOwn(TERMINE_COLLECTIONS, collection)) return TERMINE_COLLECTIONS[collection].attributes.map((a) => a.key)
|
if (Object.hasOwn(TERMINE_COLLECTIONS, collection)) return TERMINE_COLLECTIONS[collection].attributes.map((a) => a.key)
|
||||||
|
if (Object.hasOwn(ZEITEN_COLLECTIONS, collection)) return ZEITEN_COLLECTIONS[collection].attributes.map((a) => a.key)
|
||||||
if (collection === 'featureConsents') return FEATURE_CONSENT_ATTRIBUTES.map((a) => a.key)
|
if (collection === 'featureConsents') return FEATURE_CONSENT_ATTRIBUTES.map((a) => a.key)
|
||||||
for (const script of SETUP_SCRIPTS) {
|
for (const script of SETUP_SCRIPTS) {
|
||||||
const src = readFileSync(new URL(`../../scripts/${script}`, import.meta.url), 'utf8')
|
const src = readFileSync(new URL(`../../scripts/${script}`, import.meta.url), 'utf8')
|
||||||
@@ -22,7 +26,9 @@ export function schemaKeys(collection) {
|
|||||||
if (start < 0) continue
|
if (start < 0) continue
|
||||||
const block = src.slice(start, src.indexOf('\n ],', start))
|
const block = src.slice(start, src.indexOf('\n ],', start))
|
||||||
const keys = [...block.matchAll(/key: '(\w+)'/g)].map((m) => m[1])
|
const keys = [...block.matchAll(/key: '(\w+)'/g)].map((m) => m[1])
|
||||||
return collection === 'portalFeatures' ? [...keys, ...PORTAL_FEATURE_ATTRIBUTES.map((a) => a.key)] : keys
|
return collection === 'portalFeatures'
|
||||||
|
? [...keys, ...PORTAL_FEATURE_ATTRIBUTES.map((a) => a.key), ...ZEITEN_FEATURE_ATTRIBUTES.map((a) => a.key)]
|
||||||
|
: keys
|
||||||
}
|
}
|
||||||
throw new Error(`Collection ${collection} fehlt im Setup-Skript`)
|
throw new Error(`Collection ${collection} fehlt im Setup-Skript`)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
import { test, beforeEach, afterEach } from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { config } from '../server/config.js'
|
||||||
|
import { zeitenAus, zeitenFuerKunde, zeitenModus } from '../server/services/zeiten/schalter.js'
|
||||||
|
|
||||||
|
const ZEITEN_ENV = [
|
||||||
|
'ZEITEN_ENABLED',
|
||||||
|
'ZEITEN_PILOT_EMAILS',
|
||||||
|
'ZEITEN_KEIN_NEUBAU',
|
||||||
|
'APPWRITE_COLLECTION_OEFFNUNGSZEITEN',
|
||||||
|
'APPWRITE_COLLECTION_ZEITEN_ERINNERUNGEN',
|
||||||
|
]
|
||||||
|
|
||||||
|
let original
|
||||||
|
beforeEach(() => {
|
||||||
|
original = { ...config.zeiten }
|
||||||
|
})
|
||||||
|
afterEach(() => {
|
||||||
|
Object.assign(config.zeiten, original)
|
||||||
|
})
|
||||||
|
|
||||||
|
/** Frische config.js-Instanz mit genau diesen Zeiten-Umgebungsvariablen (alle anderen entfernt). */
|
||||||
|
let freshCounter = 0
|
||||||
|
async function freshConfig(env = {}) {
|
||||||
|
const saved = Object.fromEntries(ZEITEN_ENV.map((key) => [key, process.env[key]]))
|
||||||
|
for (const key of ZEITEN_ENV) delete process.env[key]
|
||||||
|
Object.assign(process.env, env)
|
||||||
|
try {
|
||||||
|
freshCounter += 1
|
||||||
|
return (await import(`../server/config.js?zeiten-test=${freshCounter}`)).config
|
||||||
|
} finally {
|
||||||
|
for (const key of ZEITEN_ENV) {
|
||||||
|
if (saved[key] === undefined) delete process.env[key]
|
||||||
|
else process.env[key] = saved[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test('Vorgabe ist aus', async () => {
|
||||||
|
const fresh = await freshConfig()
|
||||||
|
assert.deepEqual(fresh.zeiten, { mode: 'off', pilotEmails: [], keinNeubau: ['emmas-schatzkiste'] })
|
||||||
|
// ZEITEN_ENABLED leer ⇒ aus; der Schalter liest config.zeiten bei jedem Aufruf
|
||||||
|
config.zeiten.mode = fresh.zeiten.mode
|
||||||
|
assert.equal(zeitenModus(), 'off')
|
||||||
|
assert.equal(zeitenAus(), true)
|
||||||
|
assert.equal(zeitenFuerKunde('kenso.gri@gmail.com'), false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Konfiguration: leerer oder unbekannter Schalter ist off, Umgebungsvariablen werden bereinigt', async () => {
|
||||||
|
for (const value of ['', ' ', 'banane', 'true', '1']) {
|
||||||
|
assert.equal((await freshConfig({ ZEITEN_ENABLED: value })).zeiten.mode, 'off', `ZEITEN_ENABLED=${JSON.stringify(value)}`)
|
||||||
|
}
|
||||||
|
assert.equal((await freshConfig({ ZEITEN_ENABLED: ' ON ' })).zeiten.mode, 'on')
|
||||||
|
const fresh = await freshConfig({
|
||||||
|
ZEITEN_ENABLED: ' Pilot ',
|
||||||
|
ZEITEN_PILOT_EMAILS: ' Kenso.Gri@Gmail.com, ,justin@webklar.com ',
|
||||||
|
})
|
||||||
|
assert.equal(fresh.zeiten.mode, 'pilot')
|
||||||
|
assert.deepEqual(fresh.zeiten.pilotEmails, ['kenso.gri@gmail.com', 'justin@webklar.com'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('zeitenModus: leer und unbekannt sind off, pilot und on bleiben', () => {
|
||||||
|
for (const value of [undefined, null, '', 'banane', 'PILOT', 'true']) {
|
||||||
|
config.zeiten.mode = value
|
||||||
|
assert.equal(zeitenModus(), 'off', `mode=${JSON.stringify(value)}`)
|
||||||
|
assert.equal(zeitenAus(), true)
|
||||||
|
}
|
||||||
|
config.zeiten.mode = 'pilot'
|
||||||
|
assert.equal(zeitenModus(), 'pilot')
|
||||||
|
assert.equal(zeitenAus(), false)
|
||||||
|
config.zeiten.mode = 'on'
|
||||||
|
assert.equal(zeitenModus(), 'on')
|
||||||
|
assert.equal(zeitenAus(), false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('off sperrt alle, auch mit Stern in der Liste', () => {
|
||||||
|
config.zeiten.mode = 'off'
|
||||||
|
config.zeiten.pilotEmails = ['*', 'kenso.gri@gmail.com']
|
||||||
|
assert.equal(zeitenFuerKunde('kenso.gri@gmail.com'), false)
|
||||||
|
assert.equal(zeitenFuerKunde('jemand@example.com'), false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('pilot gilt nur für die Pilotliste', () => {
|
||||||
|
config.zeiten.mode = 'pilot'
|
||||||
|
config.zeiten.pilotEmails = ['a@b.de']
|
||||||
|
assert.equal(zeitenFuerKunde('A@b.de'), true)
|
||||||
|
assert.equal(zeitenFuerKunde(' a@b.de '), true)
|
||||||
|
assert.equal(zeitenFuerKunde('x@y.de'), false)
|
||||||
|
assert.equal(zeitenFuerKunde('a@b.de.evil.de'), false)
|
||||||
|
// Stern = alle, leere Liste = niemand, ohne Adresse = niemand
|
||||||
|
config.zeiten.pilotEmails = ['*']
|
||||||
|
assert.equal(zeitenFuerKunde('x@y.de'), true)
|
||||||
|
config.zeiten.pilotEmails = []
|
||||||
|
assert.equal(zeitenFuerKunde('a@b.de'), false)
|
||||||
|
config.zeiten.pilotEmails = ['a@b.de']
|
||||||
|
for (const email of [undefined, null, '', ' ']) assert.equal(zeitenFuerKunde(email), false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('on gilt für alle', () => {
|
||||||
|
config.zeiten.mode = 'on'
|
||||||
|
config.zeiten.pilotEmails = ['a@b.de']
|
||||||
|
assert.equal(zeitenFuerKunde('x@y.de'), true)
|
||||||
|
assert.equal(zeitenFuerKunde(undefined), true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('keinNeubau kommt aus ZEITEN_KEIN_NEUBAU, Vorgabe emmas-schatzkiste', async () => {
|
||||||
|
assert.deepEqual((await freshConfig()).zeiten.keinNeubau, ['emmas-schatzkiste'])
|
||||||
|
assert.deepEqual((await freshConfig({ ZEITEN_KEIN_NEUBAU: '' })).zeiten.keinNeubau, ['emmas-schatzkiste'])
|
||||||
|
assert.deepEqual(
|
||||||
|
(await freshConfig({ ZEITEN_KEIN_NEUBAU: ' Emmas-Schatzkiste, , Andere-Site ' })).zeiten.keinNeubau,
|
||||||
|
['emmas-schatzkiste', 'andere-site'],
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('zeiten steht neben config.termine, nicht darin', async () => {
|
||||||
|
const fresh = await freshConfig()
|
||||||
|
assert.equal('keinNeubau' in fresh.termine, false)
|
||||||
|
assert.equal('zeiten' in fresh.termine, false)
|
||||||
|
assert.notEqual(fresh.zeiten, fresh.termine)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('zeitenSetup: 3000 Cent einmalig', async () => {
|
||||||
|
const fresh = await freshConfig()
|
||||||
|
assert.deepEqual(fresh.zeitenSetup, {
|
||||||
|
key: 'zeitenSetup',
|
||||||
|
name: 'Öffnungszeiten live – Einrichtung',
|
||||||
|
amountCents: 3000,
|
||||||
|
lookupKey: 'webklar_zeiten_einrichtung_3000',
|
||||||
|
oneTime: true,
|
||||||
|
priceNickname: 'Öffnungszeiten-Einrichtung 30 €',
|
||||||
|
})
|
||||||
|
assert.equal(config.zeitenSetup.amountCents, 3000)
|
||||||
|
assert.equal(config.zeitenSetup.oneTime, true)
|
||||||
|
assert.equal(config.zeitenSetup.lookupKey, 'webklar_zeiten_einrichtung_3000')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Konfiguration: Collection-IDs der Zeiten-Collections', async () => {
|
||||||
|
const fresh = await freshConfig()
|
||||||
|
assert.equal(fresh.collections.oeffnungszeiten, 'oeffnungszeiten')
|
||||||
|
assert.equal(fresh.collections.zeitenErinnerungen, 'zeitenErinnerungen')
|
||||||
|
const anders = await freshConfig({
|
||||||
|
APPWRITE_COLLECTION_OEFFNUNGSZEITEN: 'zDaten',
|
||||||
|
APPWRITE_COLLECTION_ZEITEN_ERINNERUNGEN: 'zMerker',
|
||||||
|
})
|
||||||
|
assert.deepEqual([anders.collections.oeffnungszeiten, anders.collections.zeitenErinnerungen], ['zDaten', 'zMerker'])
|
||||||
|
})
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
import { test, after } from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { startAppwriteMock } from './support/appwriteMock.js'
|
||||||
|
import { startFakeStripe } from './support/fakeStripe.js'
|
||||||
|
import { useTestEnv } from './support/testEnv.js'
|
||||||
|
import { schemaKeys } from './support/schema.js'
|
||||||
|
import { ZEITEN_COLLECTIONS, ZEITEN_DATETIMES, ZEITEN_FEATURE_ATTRIBUTES } from '../server/services/zeiten/schema.js'
|
||||||
|
|
||||||
|
// Das Skript und die Konfiguration laden erst nach useTestEnv (Endpunkt des Nachbaus), daher dynamische Imports
|
||||||
|
const aw = await startAppwriteMock({ schema: { oeffnungszeiten: schemaKeys('oeffnungszeiten') }, datetimes: ZEITEN_DATETIMES })
|
||||||
|
const st = await startFakeStripe()
|
||||||
|
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
|
||||||
|
const { config } = await import('../server/config.js')
|
||||||
|
const { main } = await import('../scripts/zeiten-setup.mjs')
|
||||||
|
const { createDocument, updateDocument, getDocument } = await import('../server/services/appwriteAdmin.js')
|
||||||
|
after(() => Promise.all([aw.close(), st.close()]))
|
||||||
|
|
||||||
|
const NAMES = ['oeffnungszeiten', 'zeitenErinnerungen']
|
||||||
|
const FEATURE_KEYS = ['unlockedBy', 'aboEndeAt', 'einbau', 'behaltenSessionId', 'k24At']
|
||||||
|
const keysOf = (name) => ZEITEN_COLLECTIONS[name].attributes.map((a) => a.key)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appwrite-Client mit Zustand (wie in termineSetup.test.js): Attribute gibt es nur in vorhandenen Collections,
|
||||||
|
* Indizes nur auf vorhandenen Attributen. `initial` = { collectionId: { attrs: [...], indexes: [...] } }.
|
||||||
|
* portalFeatures gibt es immer (legt setup-abo-collections.mjs an), mit den Feldern, die schon da sind.
|
||||||
|
*/
|
||||||
|
function fakeClient(initial = {}) {
|
||||||
|
const start = { portalFeatures: { attrs: ['customerId', 'projectId', 'featureKey', 'state'] }, ...initial }
|
||||||
|
const state = new Map(
|
||||||
|
Object.entries(start).map(([id, c]) => [id, { attrs: new Map((c.attrs || []).map((key) => [key, null])), indexes: new Set(c.indexes || []) }]),
|
||||||
|
)
|
||||||
|
const calls = { collections: [], attrs: [], indexes: [] }
|
||||||
|
const need = (id) => {
|
||||||
|
if (!state.has(id)) throw Object.assign(new Error(`Collection ${id} fehlt`), { status: 404 })
|
||||||
|
return state.get(id)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
calls,
|
||||||
|
async collectionExists(id) {
|
||||||
|
return state.has(id)
|
||||||
|
},
|
||||||
|
async ensureCollection(id, name) {
|
||||||
|
if (state.has(id)) return false
|
||||||
|
state.set(id, { attrs: new Map(), indexes: new Set() })
|
||||||
|
calls.collections.push({ id, name })
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
async listAttributes(id) {
|
||||||
|
return [...need(id).attrs.keys()].map((key) => ({ key }))
|
||||||
|
},
|
||||||
|
async createAttribute(id, attr) {
|
||||||
|
const collection = need(id)
|
||||||
|
if (collection.attrs.has(attr.key)) throw new Error(`Attribut ${id}.${attr.key} existiert schon`)
|
||||||
|
collection.attrs.set(attr.key, attr)
|
||||||
|
calls.attrs.push(`${id}.${attr.key}`)
|
||||||
|
},
|
||||||
|
async waitForAttribute(id, key) {
|
||||||
|
assert.ok(need(id).attrs.has(key), `wartet auf nie angelegtes Attribut ${id}.${key}`)
|
||||||
|
},
|
||||||
|
async listIndexes(id) {
|
||||||
|
return [...need(id).indexes].map((key) => ({ key }))
|
||||||
|
},
|
||||||
|
async createIndex(id, index) {
|
||||||
|
const collection = need(id)
|
||||||
|
for (const key of index.attributes) {
|
||||||
|
if (!collection.attrs.has(key)) throw new Error(`Index ${index.key}: Attribut ${key} fehlt in ${id}`)
|
||||||
|
}
|
||||||
|
collection.indexes.add(index.key)
|
||||||
|
calls.indexes.push(`${id}.${index.key}`)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const quiet = () => {}
|
||||||
|
const featuresId = config.collections.portalFeatures
|
||||||
|
const expectedAttrs = [
|
||||||
|
...NAMES.flatMap((name) => keysOf(name).map((key) => `${config.collections[name]}.${key}`)),
|
||||||
|
...FEATURE_KEYS.map((key) => `${featuresId}.${key}`),
|
||||||
|
]
|
||||||
|
const expectedIndexes = NAMES.flatMap((name) => ZEITEN_COLLECTIONS[name].indexes.map((i) => `${config.collections[name]}.${i.key}`))
|
||||||
|
|
||||||
|
test('oeffnungszeiten hat die Felder aus Spec 2.1', () => {
|
||||||
|
assert.deepEqual(Object.keys(ZEITEN_COLLECTIONS), NAMES)
|
||||||
|
const keys = keysOf('oeffnungszeiten')
|
||||||
|
for (const key of ['customerId', 'projectId', 'featureId', 'version', 'daten', 'teamVersion', 'team', 'ersatzHash', 'updatedAt', 'updatedBy']) {
|
||||||
|
assert.ok(keys.includes(key), key)
|
||||||
|
}
|
||||||
|
assert.equal(keys.length, 10)
|
||||||
|
|
||||||
|
const byKey = Object.fromEntries(ZEITEN_COLLECTIONS.oeffnungszeiten.attributes.map((a) => [a.key, a]))
|
||||||
|
assert.deepEqual(byKey.daten, { key: 'daten', type: 'string', size: 40000, required: false })
|
||||||
|
assert.deepEqual(byKey.team, { key: 'team', type: 'string', size: 2000, required: false })
|
||||||
|
assert.deepEqual(byKey.ersatzHash, { key: 'ersatzHash', type: 'string', size: 64, required: false })
|
||||||
|
assert.deepEqual(byKey.updatedBy, { key: 'updatedBy', type: 'string', size: 64, required: false })
|
||||||
|
for (const key of ['customerId', 'projectId', 'featureId']) assert.deepEqual([byKey[key].type, byKey[key].size], ['string', 64], key)
|
||||||
|
for (const key of ['version', 'teamVersion']) assert.equal(byKey[key].type, 'integer', key)
|
||||||
|
assert.equal(byKey.updatedAt.type, 'datetime')
|
||||||
|
// Löschung beim Kunden-Löschen liest über customerId
|
||||||
|
assert.deepEqual(ZEITEN_COLLECTIONS.oeffnungszeiten.indexes, [{ key: 'idx_customer', type: 'key', attributes: ['customerId'] }])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('daten fasst 32 KB JSON', () => {
|
||||||
|
const daten = ZEITEN_COLLECTIONS.oeffnungszeiten.attributes.find((a) => a.key === 'daten')
|
||||||
|
assert.ok(daten.size >= 40000, `daten.size ${daten.size}`)
|
||||||
|
// 32 KB Spielraum auch bei JSON-Maskierung von Umlauten und Anführungszeichen
|
||||||
|
assert.ok(daten.size >= 32 * 1024)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('zeitenErinnerungen: featureId, datum, createdAt', () => {
|
||||||
|
assert.deepEqual(keysOf('zeitenErinnerungen'), ['featureId', 'datum', 'createdAt'])
|
||||||
|
const byKey = Object.fromEntries(ZEITEN_COLLECTIONS.zeitenErinnerungen.attributes.map((a) => [a.key, a]))
|
||||||
|
assert.deepEqual(byKey.featureId, { key: 'featureId', type: 'string', size: 64, required: false })
|
||||||
|
assert.deepEqual(byKey.datum, { key: 'datum', type: 'string', size: 10, required: false })
|
||||||
|
assert.deepEqual(byKey.createdAt, { key: 'createdAt', type: 'datetime', required: false })
|
||||||
|
assert.deepEqual(ZEITEN_COLLECTIONS.zeitenErinnerungen.indexes, [{ key: 'idx_datum', type: 'key', attributes: ['datum'] }])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('portalFeatures bekommt unlockedBy(8), aboEndeAt, einbau(12), behaltenSessionId(128), k24At', () => {
|
||||||
|
assert.deepEqual(ZEITEN_FEATURE_ATTRIBUTES, [
|
||||||
|
{ key: 'unlockedBy', type: 'string', size: 8, required: false },
|
||||||
|
{ key: 'aboEndeAt', type: 'datetime', required: false },
|
||||||
|
{ key: 'einbau', type: 'string', size: 12, required: false },
|
||||||
|
{ key: 'behaltenSessionId', type: 'string', size: 128, required: false },
|
||||||
|
{ key: 'k24At', type: 'datetime', required: false },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Schema: jedes Attribut ist optional, hat eine gültige Form; Indizes nur auf vorhandenen Attributen', () => {
|
||||||
|
for (const name of NAMES) {
|
||||||
|
const { attributes, indexes } = ZEITEN_COLLECTIONS[name]
|
||||||
|
assert.equal(new Set(attributes.map((a) => a.key)).size, attributes.length, `${name}: doppelte Schlüssel`)
|
||||||
|
for (const attr of attributes) {
|
||||||
|
assert.match(attr.key, /^[A-Za-z][A-Za-z0-9]{0,35}$/, `${name}.${attr.key}`)
|
||||||
|
assert.equal(attr.required, false, `${name}.${attr.key}`)
|
||||||
|
assert.ok(['string', 'integer', 'boolean', 'datetime'].includes(attr.type), `${name}.${attr.key}: Typ ${attr.type}`)
|
||||||
|
if (attr.type === 'string') assert.ok(Number.isInteger(attr.size) && attr.size > 0, `${name}.${attr.key}: Größe`)
|
||||||
|
else assert.equal('size' in attr, false, `${name}.${attr.key}: Größe nur bei Text`)
|
||||||
|
}
|
||||||
|
for (const index of indexes) {
|
||||||
|
assert.equal(index.type, 'key')
|
||||||
|
assert.ok(index.attributes.every((key) => keysOf(name).includes(key)), `${name}.${index.key}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const attr of ZEITEN_FEATURE_ATTRIBUTES) {
|
||||||
|
assert.match(attr.key, /^[A-Za-z][A-Za-z0-9]{0,35}$/, attr.key)
|
||||||
|
assert.equal(attr.required, false, attr.key)
|
||||||
|
if (attr.type === 'string') assert.ok(Number.isInteger(attr.size) && attr.size > 0, `${attr.key}: Größe`)
|
||||||
|
else assert.equal('size' in attr, false, `${attr.key}: Größe nur bei Text`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test('ZEITEN_DATETIMES nennt je Collection genau die datetime-Attribute', () => {
|
||||||
|
assert.deepEqual(ZEITEN_DATETIMES, { oeffnungszeiten: ['updatedAt'], zeitenErinnerungen: ['createdAt'] })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('schemaKeys liefert die Zeiten-Collections und die neuen portalFeatures-Felder', () => {
|
||||||
|
assert.deepEqual(schemaKeys('oeffnungszeiten'), keysOf('oeffnungszeiten'))
|
||||||
|
assert.deepEqual(schemaKeys('zeitenErinnerungen'), ['featureId', 'datum', 'createdAt'])
|
||||||
|
const features = schemaKeys('portalFeatures')
|
||||||
|
for (const key of FEATURE_KEYS) assert.ok(features.includes(key), `portalFeatures.${key}`)
|
||||||
|
// bestehende Felder und Setup-Skripte bleiben
|
||||||
|
for (const key of ['disabledReason', 'aboEndeMailAt', 'zahlungMeldung', 'unlockedByPurchase']) assert.ok(features.includes(key), key)
|
||||||
|
assert.ok(schemaKeys('termineConfig').includes('einstellung'))
|
||||||
|
assert.ok(schemaKeys('subscriptions').length > 0)
|
||||||
|
assert.throws(() => schemaKeys('gibtEsNicht'), /fehlt im Setup-Skript/)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Rundreise: Öffnungszeiten anlegen, ändern und zurücklesen; unbekanntes Feld lehnt der Nachbau ab', async () => {
|
||||||
|
const collection = config.collections.oeffnungszeiten
|
||||||
|
const daten = JSON.stringify({ land: 'RP', plaene: [], feiertage: {}, ausnahmen: [], hinweise: [], kontakt: { an: false } })
|
||||||
|
const created = await createDocument(
|
||||||
|
collection,
|
||||||
|
{ customerId: 'c1', projectId: 'p1', featureId: 'f1', version: 1, daten, teamVersion: 0, updatedAt: '2026-10-04T10:00:00.000Z', updatedBy: 'kunde' },
|
||||||
|
'f1',
|
||||||
|
)
|
||||||
|
assert.equal(created.$id, 'f1')
|
||||||
|
await updateDocument(collection, 'f1', { version: 2, updatedBy: 'team:s1', ersatzHash: 'abc' })
|
||||||
|
const doc = await getDocument(collection, 'f1')
|
||||||
|
assert.deepEqual([doc.version, doc.updatedBy, doc.ersatzHash, doc.daten], [2, 'team:s1', 'abc', daten])
|
||||||
|
await assert.rejects(() => createDocument(collection, { customerId: 'c1', fremd: 'x' }, 'f2'), /Unknown attribute/)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Konfiguration: Collection-IDs der Zeiten-Collections', () => {
|
||||||
|
assert.deepEqual(NAMES.map((name) => config.collections[name]), NAMES)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('main legt auf leerer Datenbank 2 Collections, alle Attribute und Indizes an und ergänzt portalFeatures', async () => {
|
||||||
|
const client = fakeClient()
|
||||||
|
const result = await main({ client, log: quiet })
|
||||||
|
assert.deepEqual(client.calls.collections, NAMES.map((id) => ({ id, name: id })))
|
||||||
|
assert.deepEqual([...client.calls.attrs].sort(), [...expectedAttrs].sort())
|
||||||
|
assert.deepEqual([...client.calls.indexes].sort(), [...expectedIndexes].sort())
|
||||||
|
assert.ok(client.calls.indexes.includes('oeffnungszeiten.idx_customer'))
|
||||||
|
assert.ok(client.calls.indexes.includes('zeitenErinnerungen.idx_datum'))
|
||||||
|
assert.equal(result.missing.length, expectedAttrs.length)
|
||||||
|
assert.deepEqual(result.missingCollections, NAMES)
|
||||||
|
assert.equal(result.missingIndexes.length, expectedIndexes.length)
|
||||||
|
// portalFeatures wird nicht neu angelegt, bekommt aber genau die fünf Felder
|
||||||
|
assert.ok(!client.calls.collections.some((c) => c.id === featuresId))
|
||||||
|
assert.deepEqual(client.calls.attrs.filter((k) => k.startsWith(`${featuresId}.`)), FEATURE_KEYS.map((key) => `${featuresId}.${key}`))
|
||||||
|
const features = client.state.get(featuresId).attrs
|
||||||
|
assert.deepEqual(features.get('unlockedBy'), { key: 'unlockedBy', type: 'string', size: 8, required: false })
|
||||||
|
assert.deepEqual(features.get('k24At'), { key: 'k24At', type: 'datetime', required: false })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('main: zweiter Lauf legt nichts mehr an', async () => {
|
||||||
|
const client = fakeClient()
|
||||||
|
await main({ client, log: quiet })
|
||||||
|
client.calls.collections.length = client.calls.attrs.length = client.calls.indexes.length = 0
|
||||||
|
const result = await main({ client, log: quiet })
|
||||||
|
assert.deepEqual(client.calls, { collections: [], attrs: [], indexes: [] })
|
||||||
|
assert.deepEqual(result, { missing: [], missingCollections: [], missingIndexes: [] })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('main legt nur fehlende Attribute und Indizes einer vorhandenen Collection an', async () => {
|
||||||
|
const client = fakeClient({
|
||||||
|
zeitenErinnerungen: { attrs: ['featureId', 'datum'], indexes: [] },
|
||||||
|
portalFeatures: { attrs: ['customerId', 'unlockedBy', 'einbau'] },
|
||||||
|
})
|
||||||
|
await main({ client, log: quiet })
|
||||||
|
assert.ok(!client.calls.collections.some((c) => c.id === 'zeitenErinnerungen'))
|
||||||
|
assert.deepEqual(client.calls.attrs.filter((k) => k.startsWith('zeitenErinnerungen.')), ['zeitenErinnerungen.createdAt'])
|
||||||
|
assert.deepEqual(client.calls.indexes.filter((k) => k.startsWith('zeitenErinnerungen.')), ['zeitenErinnerungen.idx_datum'])
|
||||||
|
assert.deepEqual(
|
||||||
|
client.calls.attrs.filter((k) => k.startsWith(`${featuresId}.`)),
|
||||||
|
[`${featuresId}.aboEndeAt`, `${featuresId}.behaltenSessionId`, `${featuresId}.k24At`],
|
||||||
|
)
|
||||||
|
assert.equal(client.calls.collections.length, 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('--check meldet fehlende Collections und Felder, ohne zu schreiben', async () => {
|
||||||
|
const client = fakeClient()
|
||||||
|
const lines = []
|
||||||
|
const result = await main({ check: true, client, log: (line) => lines.push(line) })
|
||||||
|
assert.deepEqual(client.calls, { collections: [], attrs: [], indexes: [] })
|
||||||
|
assert.equal(client.state.size, 1, 'nur portalFeatures, nichts angelegt')
|
||||||
|
assert.deepEqual([...client.state.get(featuresId).attrs.keys()], ['customerId', 'projectId', 'featureKey', 'state'])
|
||||||
|
assert.deepEqual(result.missingCollections, NAMES)
|
||||||
|
assert.equal(result.missing.length, expectedAttrs.length)
|
||||||
|
assert.equal(result.missingIndexes.length, expectedIndexes.length)
|
||||||
|
assert.ok(lines.some((line) => line.includes('oeffnungszeiten') && line.includes('FEHLT')))
|
||||||
|
assert.ok(lines.some((line) => line.includes(`${featuresId}.unlockedBy FEHLT`)), lines.join('\n'))
|
||||||
|
assert.ok(lines.some((line) => line.includes('Index oeffnungszeiten.idx_customer FEHLT')))
|
||||||
|
})
|
||||||
|
|
||||||
|
test('--check meldet bei vollständigem Stand nichts und ein einzelnes fehlendes portalFeatures-Feld genau dieses', async () => {
|
||||||
|
const client = fakeClient()
|
||||||
|
await main({ client, log: quiet })
|
||||||
|
assert.deepEqual(await main({ check: true, client, log: quiet }), { missing: [], missingCollections: [], missingIndexes: [] })
|
||||||
|
client.state.get(featuresId).attrs.delete('k24At')
|
||||||
|
const result = await main({ check: true, client, log: quiet })
|
||||||
|
assert.deepEqual(result.missing.map((a) => `${a.collection}.${a.key}`), [`${featuresId}.k24At`])
|
||||||
|
assert.deepEqual(result.missingCollections, [])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('main: fehlt portalFeatures, bricht es mit klarer Meldung ab, ohne etwas anzulegen', async () => {
|
||||||
|
const client = fakeClient()
|
||||||
|
client.state.delete(featuresId)
|
||||||
|
await assert.rejects(() => main({ client, log: quiet }), /portalFeatures.*fehlt/)
|
||||||
|
assert.deepEqual(client.calls, { collections: [], attrs: [], indexes: [] })
|
||||||
|
})
|
||||||
Reference in new issue
Block a user