From 459da008200f6fcddacc7154f2a6a351a31c234c Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 20 May 2026 18:12:32 +0200 Subject: [PATCH] fix(import): publish season spielplan json after import --- scripts/import-spielplan.js | 4 ++ server/plugins/spielplan-import-scheduler.js | 7 ++++ server/utils/spielplan-publish.js | 42 ++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 server/utils/spielplan-publish.js diff --git a/scripts/import-spielplan.js b/scripts/import-spielplan.js index 592af77..55bf3e0 100644 --- a/scripts/import-spielplan.js +++ b/scripts/import-spielplan.js @@ -2,13 +2,17 @@ import { importSpielplan } from '../server/utils/spielplan-import.js' import { importLeagueTables } from '../server/utils/spielklassen-tables-import.js' +import { publishImportedSpielplan } from '../server/utils/spielplan-publish.js' const result = await importSpielplan() +const published = await publishImportedSpielplan({ inputPath: result.jsonFile }) const tables = await importLeagueTables() console.log(`Spielplan gespeichert: ${result.jsonFile}`) console.log(`Roh-HTML gespeichert: ${result.htmlFile}`) console.log(`Spiele: ${result.matchCount}`) console.log(`Zeitraum: ${result.source.season.dateStart} bis ${result.source.season.dateEnd}`) +console.log(`Spielplan publiziert: ${published.internalSeasonPath}`) +console.log(`Spielplan publiziert (public): ${published.publicSeasonPath}`) console.log(`Tabellen gespeichert: ${tables.outputFile}`) console.log(`Tabellen importiert: ${tables.importedCount}/${tables.teamCount} (Fehler: ${tables.errorCount})`) diff --git a/server/plugins/spielplan-import-scheduler.js b/server/plugins/spielplan-import-scheduler.js index de95ea7..b90c867 100644 --- a/server/plugins/spielplan-import-scheduler.js +++ b/server/plugins/spielplan-import-scheduler.js @@ -1,5 +1,6 @@ import { importSpielplan } from '../utils/spielplan-import.js' import { importLeagueTables } from '../utils/spielklassen-tables-import.js' +import { publishImportedSpielplan } from '../utils/spielplan-publish.js' import { info as loggerInfo, error as loggerError } from '../utils/logger.js' const TIME_ZONE = 'Europe/Berlin' @@ -72,6 +73,12 @@ async function runImport(reason) { const spielplan = await importSpielplan() loggerInfo(`[spielplan-import] ${reason}: ${spielplan.matchCount} Spiele importiert`, { range: `${spielplan.source.season.dateStart} - ${spielplan.source.season.dateEnd}` }) + const published = await publishImportedSpielplan({ inputPath: spielplan.jsonFile }) + loggerInfo(`[spielplan-import] ${reason}: Spielplan publiziert`, { + season: published.seasonSlug, + internalPath: published.internalSeasonPath + }) + try { const tables = await importLeagueTables() loggerInfo(`[spielplan-import] ${reason}: ${tables.importedCount}/${tables.teamCount} Tabellen importiert`, { diff --git a/server/utils/spielplan-publish.js b/server/utils/spielplan-publish.js new file mode 100644 index 0000000..2633a17 --- /dev/null +++ b/server/utils/spielplan-publish.js @@ -0,0 +1,42 @@ +import { promises as fs } from 'fs' +import { convertImportedSpielplanToJson, validateImportedSpielplan } from './spielplan-data.js' +import { getProjectPath, getServerDataPath } from './paths.js' + +const DEFAULT_INPUT_PATH = getServerDataPath('spielplan-import', 'harheimer_tc_spielplan.json') + +async function writeJsonAtomic(filePath, data) { + await fs.mkdir(filePath.substring(0, filePath.lastIndexOf('/')), { recursive: true }) + const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now()}` + const content = `${JSON.stringify(data, null, 2)}\n` + + await fs.writeFile(tmpPath, content, 'utf8') + await fs.rename(tmpPath, filePath) +} + +export async function publishImportedSpielplan(options = {}) { + const inputPath = options.inputPath || DEFAULT_INPUT_PATH + const raw = await fs.readFile(inputPath, 'utf8') + const imported = JSON.parse(raw) + + validateImportedSpielplan(imported) + + const spielplan = convertImportedSpielplanToJson(imported) + const seasonSlug = imported.source?.season?.seasonSlug + if (!seasonSlug) { + throw new Error('Saison-Slug fehlt in der Import-Datei') + } + + const internalSeasonPath = getServerDataPath('public-data', 'spielplaene', `spielplan-${seasonSlug}.json`) + const publicSeasonPath = getProjectPath('public', 'data', 'spielplaene', `spielplan-${seasonSlug}.json`) + + await writeJsonAtomic(internalSeasonPath, spielplan) + await writeJsonAtomic(publicSeasonPath, spielplan) + + return { + seasonSlug, + matchCount: imported.matchCount, + inputPath, + internalSeasonPath, + publicSeasonPath + } +} \ No newline at end of file