|
|
@@ -29,13 +29,25 @@ export class ImportProcessor {
|
|
|
include: { fields: true }
|
|
|
}
|
|
|
}
|
|
|
- },
|
|
|
- file: true
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- if (!importRecord || !importRecord.file) {
|
|
|
- throw new Error('Import not found or no file attached');
|
|
|
+ if (!importRecord) {
|
|
|
+ throw new Error('Import not found');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!importRecord.fileId) {
|
|
|
+ throw new Error('No file attached to import');
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get the actual file
|
|
|
+ const fileRecord = await this.prisma.file.findUnique({
|
|
|
+ where: { id: importRecord.fileId }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!fileRecord) {
|
|
|
+ throw new Error('File not found');
|
|
|
}
|
|
|
|
|
|
// Initialize progress tracking
|
|
|
@@ -47,14 +59,14 @@ export class ImportProcessor {
|
|
|
totalRows: 0,
|
|
|
errors: [],
|
|
|
processedSections: 0,
|
|
|
- totalSections: importRecord.layout?.sections?.length || 0
|
|
|
+ totalSections: importRecord.layout?.sections?.length ?? 0
|
|
|
};
|
|
|
|
|
|
// Read Excel file
|
|
|
const sections = await this.reader.readExcelFile(
|
|
|
- Buffer.from(importRecord.file.data),
|
|
|
+ fileRecord.data,
|
|
|
importRecord.layout,
|
|
|
- (sectionProgress) => {
|
|
|
+ (sectionProgress: ImportProgress) => {
|
|
|
this.progressServer.broadcastProgress(importId, sectionProgress);
|
|
|
}
|
|
|
);
|
|
|
@@ -63,39 +75,40 @@ export class ImportProcessor {
|
|
|
const processedSections: ProcessedSection[] = [];
|
|
|
let totalInserted = 0;
|
|
|
|
|
|
- for (let i = 0; i < sections.length; i++) {
|
|
|
+ for (let i = 0; i < sections.length; i += 1) {
|
|
|
const section = sections[i];
|
|
|
|
|
|
- progress.currentSection = section.name;
|
|
|
- progress.processedSections = i + 1;
|
|
|
- this.progressServer.broadcastProgress(importId, progress);
|
|
|
-
|
|
|
- try {
|
|
|
- const insertedRows = await this.inserter.insertSectionData(
|
|
|
- section,
|
|
|
- importId,
|
|
|
- (rows) => {
|
|
|
- progress.currentRow = rows;
|
|
|
- this.progressServer.broadcastProgress(importId, progress);
|
|
|
- }
|
|
|
- );
|
|
|
+ if (section) {
|
|
|
+ progress.currentSection = section.name ?? '';
|
|
|
+ progress.processedSections = i + 1;
|
|
|
+ this.progressServer.broadcastProgress(importId, progress);
|
|
|
|
|
|
- processedSections.push({
|
|
|
- sectionData: section,
|
|
|
- insertedRows
|
|
|
- });
|
|
|
+ try {
|
|
|
+ const insertedRows = await this.inserter.insertSectionData(
|
|
|
+ section,
|
|
|
+ importId,
|
|
|
+ (rows: number) => {
|
|
|
+ progress.currentRow = rows;
|
|
|
+ this.progressServer.broadcastProgress(importId, progress);
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
- totalInserted += insertedRows;
|
|
|
+ processedSections.push({
|
|
|
+ sectionData: section,
|
|
|
+ insertedRows
|
|
|
+ });
|
|
|
|
|
|
- } catch (error) {
|
|
|
- const errorMessage = `Error processing section ${section.name}: ${error instanceof Error ? error.message : 'Unknown error'}`;
|
|
|
- progress.errors.push(errorMessage);
|
|
|
- this.progressServer.broadcastProgress(importId, progress);
|
|
|
+ totalInserted += insertedRows;
|
|
|
+
|
|
|
+ } catch (error: unknown) {
|
|
|
+ const errorMessage = `Error processing section ${section.name ?? 'unknown'}: ${error instanceof Error ? error.message : 'Unknown error'}`;
|
|
|
+ progress.errors.push(errorMessage);
|
|
|
+ this.progressServer.broadcastProgress(importId, progress);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Import processing completed - no status field to update
|
|
|
-
|
|
|
+ // Import processing completed
|
|
|
progress.status = 'completed';
|
|
|
this.progressServer.broadcastProgress(importId, progress);
|
|
|
|
|
|
@@ -104,10 +117,8 @@ export class ImportProcessor {
|
|
|
totalInserted,
|
|
|
sections: processedSections
|
|
|
};
|
|
|
-
|
|
|
} catch (error) {
|
|
|
- // Import processing failed - no status field to update
|
|
|
-
|
|
|
+ // Import processing failed
|
|
|
const progress: ImportProgress = {
|
|
|
importId,
|
|
|
status: 'failed',
|
|
|
@@ -136,7 +147,9 @@ export class ImportProcessor {
|
|
|
try {
|
|
|
const importRecord = await this.prisma.import.findUnique({
|
|
|
where: { id: importId },
|
|
|
- include: { file: true, layout: true }
|
|
|
+ include: {
|
|
|
+ layout: true
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
if (!importRecord) {
|
|
|
@@ -144,7 +157,7 @@ export class ImportProcessor {
|
|
|
return { valid: false, errors };
|
|
|
}
|
|
|
|
|
|
- if (!importRecord.file) {
|
|
|
+ if (!importRecord.fileId) {
|
|
|
errors.push('No file attached to import');
|
|
|
}
|
|
|
|
|
|
@@ -153,7 +166,6 @@ export class ImportProcessor {
|
|
|
}
|
|
|
|
|
|
return { valid: errors.length === 0, errors };
|
|
|
-
|
|
|
} catch (error) {
|
|
|
errors.push(`Validation error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
|
return { valid: false, errors };
|