fix(import): publish season spielplan json after import
This commit is contained in:
@@ -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})`)
|
||||
|
||||
@@ -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`, {
|
||||
|
||||
42
server/utils/spielplan-publish.js
Normal file
42
server/utils/spielplan-publish.js
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user