|
@@ -8,6 +8,11 @@ import { ImportProgressServer } from './websocket-server';
|
|
|
import { ImportProgress, ImportResult } from './types';
|
|
import { ImportProgress, ImportResult } from './types';
|
|
|
import { FileDownloader } from './file-downloader';
|
|
import { FileDownloader } from './file-downloader';
|
|
|
|
|
|
|
|
|
|
+interface ProcessedSection {
|
|
|
|
|
+ sectionData: any;
|
|
|
|
|
+ insertedRows: number;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export class CintasImportProcessor {
|
|
export class CintasImportProcessor {
|
|
|
private prisma: PrismaClient;
|
|
private prisma: PrismaClient;
|
|
|
private reader: ExcelReaderService;
|
|
private reader: ExcelReaderService;
|
|
@@ -25,10 +30,10 @@ export class CintasImportProcessor {
|
|
|
|
|
|
|
|
async processCintasImport(importId: number): Promise<ImportResult> {
|
|
async processCintasImport(importId: number): Promise<ImportResult> {
|
|
|
let filePath: string | null = null;
|
|
let filePath: string | null = null;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Starting import processing for ID: ${importId}`);
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Starting import processing for ID: ${importId}`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Initialize the progress server if not already done
|
|
// Initialize the progress server if not already done
|
|
|
if (!this.progressServer.isServerInitialized()) {
|
|
if (!this.progressServer.isServerInitialized()) {
|
|
|
this.progressServer.initialize();
|
|
this.progressServer.initialize();
|
|
@@ -75,26 +80,31 @@ export class CintasImportProcessor {
|
|
|
// Save file to temporary location
|
|
// Save file to temporary location
|
|
|
const filename = `import_${importId}_${Date.now()}.xlsx`;
|
|
const filename = `import_${importId}_${Date.now()}.xlsx`;
|
|
|
filePath = path.join(this.fileDownloader.getTempDir(), filename);
|
|
filePath = path.join(this.fileDownloader.getTempDir(), filename);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let fileBuffer: Buffer;
|
|
let fileBuffer: Buffer;
|
|
|
if (Buffer.isBuffer(file.data)) {
|
|
if (Buffer.isBuffer(file.data)) {
|
|
|
fileBuffer = file.data;
|
|
fileBuffer = file.data;
|
|
|
} else if (file.data instanceof Uint8Array) {
|
|
} else if (file.data instanceof Uint8Array) {
|
|
|
fileBuffer = Buffer.from(file.data);
|
|
fileBuffer = Buffer.from(file.data);
|
|
|
|
|
+ } else if (typeof file.data === 'string') {
|
|
|
|
|
+ fileBuffer = Buffer.from(file.data, 'base64');
|
|
|
} else {
|
|
} else {
|
|
|
- fileBuffer = Buffer.from(file.data as any);
|
|
|
|
|
|
|
+ fileBuffer = Buffer.from(file.data as Buffer);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
fs.writeFileSync(filePath, fileBuffer);
|
|
fs.writeFileSync(filePath, fileBuffer);
|
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] File saved to: ${filePath}`);
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] File saved to: ${filePath}`);
|
|
|
|
|
|
|
|
// Read Excel file
|
|
// Read Excel file
|
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Starting Excel file reading...`);
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Starting Excel file reading...`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Read file content as buffer
|
|
|
|
|
+ const fileContent = fs.readFileSync(filePath);
|
|
|
|
|
+
|
|
|
const sections = await this.reader.readExcelFile(
|
|
const sections = await this.reader.readExcelFile(
|
|
|
- filePath,
|
|
|
|
|
|
|
+ fileContent,
|
|
|
importRecord.layout,
|
|
importRecord.layout,
|
|
|
- (sectionProgress) => {
|
|
|
|
|
|
|
+ (sectionProgress: ImportProgress) => {
|
|
|
this.progressServer.broadcastProgress(importId, sectionProgress);
|
|
this.progressServer.broadcastProgress(importId, sectionProgress);
|
|
|
}
|
|
}
|
|
|
);
|
|
);
|
|
@@ -102,13 +112,13 @@ export class CintasImportProcessor {
|
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Excel file read successfully. Found ${sections.length} sections`);
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Excel file read successfully. Found ${sections.length} sections`);
|
|
|
|
|
|
|
|
// Process each section
|
|
// Process each section
|
|
|
- const processedSections = [];
|
|
|
|
|
|
|
+ const processedSections: ProcessedSection[] = [];
|
|
|
let totalInserted = 0;
|
|
let totalInserted = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < sections.length; i++) {
|
|
for (let i = 0; i < sections.length; i++) {
|
|
|
const section = sections[i];
|
|
const section = sections[i];
|
|
|
-
|
|
|
|
|
- console.log(`[${new Date().toISOString()}] [CintasImport] Processing section ${i+1}/${sections.length}: ${section.name}`);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`[${new Date().toISOString()}] [CintasImport] Processing section ${i + 1}/${sections.length}: ${section.name}`);
|
|
|
progress.currentSection = section.name;
|
|
progress.currentSection = section.name;
|
|
|
progress.processedSections = i + 1;
|
|
progress.processedSections = i + 1;
|
|
|
this.progressServer.broadcastProgress(importId, progress);
|
|
this.progressServer.broadcastProgress(importId, progress);
|
|
@@ -117,11 +127,11 @@ export class CintasImportProcessor {
|
|
|
// Ensure table exists for this section
|
|
// Ensure table exists for this section
|
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Creating table ${section.tableName} for section ${section.name}`);
|
|
console.log(`[${new Date().toISOString()}] [CintasImport] Creating table ${section.tableName} for section ${section.name}`);
|
|
|
await this.inserter.createImportTable(section.tableName, section.fields);
|
|
await this.inserter.createImportTable(section.tableName, section.fields);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const insertedRows = await this.inserter.insertSectionData(
|
|
const insertedRows = await this.inserter.insertSectionData(
|
|
|
section,
|
|
section,
|
|
|
importId,
|
|
importId,
|
|
|
- (rows) => {
|
|
|
|
|
|
|
+ (rows: number) => {
|
|
|
progress.currentRow = rows;
|
|
progress.currentRow = rows;
|
|
|
this.progressServer.broadcastProgress(importId, progress);
|
|
this.progressServer.broadcastProgress(importId, progress);
|
|
|
}
|
|
}
|