Explorar o código

Fixed original excel reader by copying homework of the database one.

vtugulan hai 3 días
pai
achega
13b38025ee

+ 1 - 1
.vscode/launch.json

@@ -33,7 +33,7 @@
       },
       "sourceMaps": true,
       "resolveSourceMapLocations": [
-        "${workspaceFolder}/out/**/*.js",
+        "${workspaceFolder}/out/**/*.ts",
         "!/node_modules/**"
       ]
     },

+ 5 - 52
app/lib/excel-import/bulk-inserter.ts

@@ -29,29 +29,28 @@ export class BulkInserter {
         // Prepare data for insertion with proper field mapping
         const values = batch.map(row => {
           const mappedRow: any = {
-            importId: importId,
-            createdAt: new Date(),
-            updatedAt: new Date()
+            importId: importId
           };
 
           // Map the row data to match Prisma model field names
           Object.keys(row).forEach(key => {
             // Convert snake_case to camelCase for Prisma model compatibility
-            // Also handle digits (e.g., corp_id_2 -> corpId2)
-            const camelKey = key.replace(/_([a-z0-9])/g, (g) => g[1].toUpperCase());
+            const camelKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
             mappedRow[camelKey] = row[key];
           });
 
           return mappedRow;
         });
 
+        console.log(batch)
+        console.log(values);
         // Use appropriate Prisma model based on table name
         if (tableName === 'cintas_install_calendar') {
           await this.prisma.cintasInstallCalendar.createMany({
             data: values,
             skipDuplicates: false
           });
-        } else if (tableName === 'cintas_install_calendar_summary' || tableName === 'cintas_intall_calendar_summary') {
+        } else if (tableName === 'cintas_install_calendar_summary') {
           await this.prisma.cintasSummary.createMany({
             data: values,
             skipDuplicates: false
@@ -112,50 +111,4 @@ export class BulkInserter {
 
     return `INSERT INTO "${tableName}" (${columns}) VALUES ${placeholders}`;
   }
-
-  async createImportTable(tableName: string, fields: any[]): Promise<void> {
-    try {
-      // Create table if it doesn't exist
-      const columns = fields.map(field => {
-        const dataType = this.mapDataType(field.dataType);
-        return `"${field.importTableColumnName}" ${dataType}`;
-      }).join(', ');
-
-      const createTableQuery = `
-        CREATE TABLE IF NOT EXISTS "${tableName}" (
-          id SERIAL PRIMARY KEY,
-          "importId" INTEGER NOT NULL,
-          ${columns},
-          "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-        )
-      `;
-
-      await this.prisma.$executeRawUnsafe(createTableQuery);
-    } catch (error) {
-      console.error('Error creating import table:', error);
-      throw error;
-    }
-  }
-
-  private mapDataType(dataType: string): string {
-    switch (dataType?.toLowerCase()) {
-      case 'string':
-      case 'text':
-        return 'TEXT';
-      case 'number':
-      case 'integer':
-        return 'INTEGER';
-      case 'decimal':
-      case 'float':
-        return 'DECIMAL';
-      case 'boolean':
-        return 'BOOLEAN';
-      case 'date':
-        return 'DATE';
-      case 'datetime':
-        return 'TIMESTAMP';
-      default:
-        return 'TEXT';
-    }
-  }
 }

+ 0 - 4
app/lib/excel-import/cintas-import-processor.ts

@@ -123,10 +123,6 @@ export class CintasImportProcessor {
         this.progressServer.broadcastProgress(importId, progress);
 
         try {
-          // Ensure table exists for this section
-          console.log(`[${new Date().toISOString()}] [CintasImport] Creating table ${section.tableName} for section ${section.name}`);
-          await this.inserter.createImportTable(section.tableName, section.fields);
-
           const insertedRows = await this.inserter.insertSectionData(
             section,
             importId,

+ 0 - 8
app/lib/excel-import/database-cintas-import-processor.ts

@@ -84,10 +84,6 @@ export class DatabaseCintasImportProcessor {
         this.progressServer.broadcastProgress(importId, progress);
 
         try {
-          // Ensure table exists for this section
-          console.log(`[${new Date().toISOString()}] [DatabaseCintasImport] Creating table ${section.tableName} for section ${section.name}`);
-          await this.inserter.createImportTable(section.tableName, section.fields);
-
           const insertedRows = await this.inserter.insertSectionData(
             section,
             importId,
@@ -193,10 +189,6 @@ export class DatabaseCintasImportProcessor {
         onProgress(progress);
 
         try {
-          // Ensure table exists for this section
-          console.log(`[${new Date().toISOString()}] [DatabaseCintasImport] Creating table ${section.tableName} for section ${section.name}`);
-          await this.inserter.createImportTable(section.tableName, section.fields);
-
           const insertedRows = await this.inserter.insertSectionData(
             section,
             0, // Use 0 for buffer-based imports

+ 20 - 7
app/lib/excel-import/excel-reader.ts

@@ -86,11 +86,13 @@ export class ExcelReaderService {
       // Map cell values based on field configuration
       for (const field of section.fields || []) {
         try {
-          const cellAddress = this.parseCellAddress(field.cellPosition);
+          const cellAddress = this.parseCellAddress(field.cellPosition, rowNum);
           const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
 
           if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
             const value = this.convertCellValue(
+              field.dataType,
+              field.dataTypeFormat,
               cellValue,
               field.parsedType || FieldTypeEnum.String
             );
@@ -99,8 +101,12 @@ export class ExcelReaderService {
             const columnName = field.importTableColumnName;
             rowData[columnName] = value;
           }
-        } catch {
-          // Error processing field, skip this field
+        } catch (error) {
+          console.log(`Error processing field ${field.name} at row ${rowNum}`, {
+            error: error instanceof Error ? error.message : String(error),
+            field,
+            rowNum
+          });
         }
       }
 
@@ -140,10 +146,14 @@ export class ExcelReaderService {
     return result;
   }
 
-  private parseCellAddress(cellPosition: string): { row: number; col: number } {
-    const match = cellPosition.match(/([A-Z]+)(\d+)/);
+  private parseCellAddress(cellPosition: string, rowNumber: number): { row: number; col: number } {
+    let match = cellPosition.match(/([A-Z]+)(\d+)/);
     if (!match) {
-      return { row: 1, col: 1 };
+      const appendedCellPosition = `${cellPosition}${rowNumber}`;
+      match = appendedCellPosition.match(/([A-Z]+)(\d+)/);
+      if (!match) {
+        return { row: 1, col: 1 };
+      }
     }
 
     const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
@@ -211,12 +221,15 @@ export class ExcelReaderService {
     return mappedType;
   }
 
-  private convertCellValue(value: any, fieldType: FieldTypeEnum): any {
+  private convertCellValue(dataType: string, dataTypeFormat: string | undefined, value: any, fieldType: FieldTypeEnum): any {
     if (value === null || value === undefined) {
       return null;
     }
 
     const convertedValue = (() => {
+      if (dataType === "DATE" && (typeof value === 'number' || value instanceof Date))
+        return XLSX.SSF.format(dataTypeFormat || 'yyyy-mm-dd', value);
+
       switch (fieldType) {
         case FieldTypeEnum.Time:
           if (typeof value === 'number') {

+ 1 - 1
app/lib/excel-import/import-processor.ts

@@ -75,7 +75,7 @@ export class ImportProcessor {
       const processedSections: ProcessedSection[] = [];
       let totalInserted = 0;
 
-      for (let i = 0; i < sections.length; i += 1) {
+      for (let i = 0; i < sections.length; i++) {
         const section = sections[i];
 
         if (section) {