feat: enhance date parsing in import processor to handle European format and improve error handling

This commit is contained in:
Max Kowalski
2025-06-27 19:41:54 +02:00
parent f3f63943a8
commit 9a3741cd01

View File

@ -6,19 +6,48 @@ import cron from "node-cron";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
/**
* Parse European date format (DD.MM.YYYY HH:mm:ss) to JavaScript Date
*/
function parseEuropeanDate(dateStr: string): Date {
if (!dateStr || typeof dateStr !== 'string') {
throw new Error(`Invalid date string: ${dateStr}`);
}
// Handle format: "DD.MM.YYYY HH:mm:ss"
const [datePart, timePart] = dateStr.trim().split(' ');
if (!datePart || !timePart) {
throw new Error(`Invalid date format: ${dateStr}. Expected format: DD.MM.YYYY HH:mm:ss`);
}
const [day, month, year] = datePart.split('.');
if (!day || !month || !year) {
throw new Error(`Invalid date part: ${datePart}. Expected format: DD.MM.YYYY`);
}
// Convert to ISO format: YYYY-MM-DD HH:mm:ss
const isoDateStr = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')} ${timePart}`;
const date = new Date(isoDateStr);
if (isNaN(date.getTime())) {
throw new Error(`Failed to parse date: ${dateStr} -> ${isoDateStr}`);
}
return date;
}
/** /**
* Process a single SessionImport record into a Session record * Process a single SessionImport record into a Session record
*/ */
async function processSingleImport(importRecord: any): Promise<{ success: boolean; error?: string }> { async function processSingleImport(importRecord: any): Promise<{ success: boolean; error?: string }> {
try { try {
// Parse dates // Parse dates using European format parser
const startTime = new Date(importRecord.startTimeRaw); const startTime = parseEuropeanDate(importRecord.startTimeRaw);
const endTime = new Date(importRecord.endTimeRaw); const endTime = parseEuropeanDate(importRecord.endTimeRaw);
// Validate dates console.log(`[Import Processor] Parsed dates for ${importRecord.externalSessionId}: ${startTime.toISOString()} - ${endTime.toISOString()}`);
if (isNaN(startTime.getTime()) || isNaN(endTime.getTime())) {
throw new Error(`Invalid date format: start=${importRecord.startTimeRaw}, end=${importRecord.endTimeRaw}`);
}
// Process sentiment // Process sentiment
let sentiment: number | null = null; let sentiment: number | null = null;