|
@@ -1,4 +1,5 @@
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
+/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
import * as XLSX from 'xlsx';
|
|
import * as XLSX from 'xlsx';
|
|
|
import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
|
|
import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
|
|
|
import { prisma } from '@/lib/prisma';
|
|
import { prisma } from '@/lib/prisma';
|
|
@@ -6,16 +7,16 @@ import { prisma } from '@/lib/prisma';
|
|
|
// Simple logger utility for debugging
|
|
// Simple logger utility for debugging
|
|
|
const logger = {
|
|
const logger = {
|
|
|
debug: (message: string, ...args: any[]) => {
|
|
debug: (message: string, ...args: any[]) => {
|
|
|
- console.debug(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
|
|
|
|
+ //console.debug(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
},
|
|
},
|
|
|
info: (message: string, ...args: any[]) => {
|
|
info: (message: string, ...args: any[]) => {
|
|
|
- console.info(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
|
|
|
|
+ //console.info(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
},
|
|
},
|
|
|
warn: (message: string, ...args: any[]) => {
|
|
warn: (message: string, ...args: any[]) => {
|
|
|
- console.warn(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
|
|
|
|
+ //console.warn(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
},
|
|
},
|
|
|
error: (message: string, ...args: any[]) => {
|
|
error: (message: string, ...args: any[]) => {
|
|
|
- console.error(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
|
|
|
|
+ //console.error(`[DatabaseExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -31,7 +32,7 @@ export class DatabaseExcelReaderService {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
|
const startTime = Date.now();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
// Fetch file from database
|
|
// Fetch file from database
|
|
|
const fileRecord = await prisma.file.findUnique({
|
|
const fileRecord = await prisma.file.findUnique({
|
|
@@ -56,19 +57,19 @@ export class DatabaseExcelReaderService {
|
|
|
|
|
|
|
|
logger.debug('Loading Excel workbook from buffer...');
|
|
logger.debug('Loading Excel workbook from buffer...');
|
|
|
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
|
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info('Excel workbook loaded successfully from database', {
|
|
logger.info('Excel workbook loaded successfully from database', {
|
|
|
- worksheets: workbook.SheetNames.map(name => ({
|
|
|
|
|
- name,
|
|
|
|
|
- rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
|
|
|
|
|
|
|
+ worksheets: workbook.SheetNames.map(name => ({
|
|
|
|
|
+ name,
|
|
|
|
|
+ rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
|
|
|
}))
|
|
}))
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const results: ReadSectionData[] = [];
|
|
const results: ReadSectionData[] = [];
|
|
|
const totalSections = layoutConfig.sections?.length || 0;
|
|
const totalSections = layoutConfig.sections?.length || 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info('Processing Excel import from database', { totalSections });
|
|
logger.info('Processing Excel import from database', { totalSections });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Initialize progress
|
|
// Initialize progress
|
|
|
onProgress({
|
|
onProgress({
|
|
|
importId: 0,
|
|
importId: 0,
|
|
@@ -89,13 +90,13 @@ export class DatabaseExcelReaderService {
|
|
|
startingRow: section.startingRow,
|
|
startingRow: section.startingRow,
|
|
|
endingRow: section.endingRow
|
|
endingRow: section.endingRow
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const worksheet = workbook.Sheets[section.sheetName];
|
|
const worksheet = workbook.Sheets[section.sheetName];
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!worksheet) {
|
|
if (!worksheet) {
|
|
|
const error = `Worksheet '${section.sheetName}' not found`;
|
|
const error = `Worksheet '${section.sheetName}' not found`;
|
|
|
logger.warn(error, { availableWorksheets: workbook.SheetNames });
|
|
logger.warn(error, { availableWorksheets: workbook.SheetNames });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
onProgress({
|
|
onProgress({
|
|
|
importId: 0,
|
|
importId: 0,
|
|
|
status: 'processing',
|
|
status: 'processing',
|
|
@@ -110,14 +111,14 @@ export class DatabaseExcelReaderService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const sectionData = await this.processSectionFromWorksheet(
|
|
const sectionData = await this.processSectionFromWorksheet(
|
|
|
- worksheet,
|
|
|
|
|
- section,
|
|
|
|
|
- sectionIndex,
|
|
|
|
|
- totalSections,
|
|
|
|
|
|
|
+ worksheet,
|
|
|
|
|
+ section,
|
|
|
|
|
+ sectionIndex,
|
|
|
|
|
+ totalSections,
|
|
|
onProgress
|
|
onProgress
|
|
|
);
|
|
);
|
|
|
results.push(sectionData);
|
|
results.push(sectionData);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info(`Section ${section.name} processed successfully`, {
|
|
logger.info(`Section ${section.name} processed successfully`, {
|
|
|
rowsProcessed: sectionData.data.length,
|
|
rowsProcessed: sectionData.data.length,
|
|
|
fields: sectionData.fields.length
|
|
fields: sectionData.fields.length
|
|
@@ -152,7 +153,7 @@ export class DatabaseExcelReaderService {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
|
const startTime = Date.now();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
// Convert Buffer to ArrayBuffer for xlsx library
|
|
// Convert Buffer to ArrayBuffer for xlsx library
|
|
|
const arrayBuffer = buffer.buffer.slice(
|
|
const arrayBuffer = buffer.buffer.slice(
|
|
@@ -162,19 +163,19 @@ export class DatabaseExcelReaderService {
|
|
|
|
|
|
|
|
logger.debug('Loading Excel workbook from buffer...');
|
|
logger.debug('Loading Excel workbook from buffer...');
|
|
|
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
|
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info('Excel workbook loaded successfully from buffer', {
|
|
logger.info('Excel workbook loaded successfully from buffer', {
|
|
|
- worksheets: workbook.SheetNames.map(name => ({
|
|
|
|
|
- name,
|
|
|
|
|
- rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
|
|
|
|
|
|
|
+ worksheets: workbook.SheetNames.map(name => ({
|
|
|
|
|
+ name,
|
|
|
|
|
+ rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length
|
|
|
}))
|
|
}))
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const results: ReadSectionData[] = [];
|
|
const results: ReadSectionData[] = [];
|
|
|
const totalSections = layoutConfig.sections?.length || 0;
|
|
const totalSections = layoutConfig.sections?.length || 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info('Processing Excel import from buffer', { totalSections });
|
|
logger.info('Processing Excel import from buffer', { totalSections });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Initialize progress
|
|
// Initialize progress
|
|
|
onProgress({
|
|
onProgress({
|
|
|
importId: 0,
|
|
importId: 0,
|
|
@@ -195,13 +196,13 @@ export class DatabaseExcelReaderService {
|
|
|
startingRow: section.startingRow,
|
|
startingRow: section.startingRow,
|
|
|
endingRow: section.endingRow
|
|
endingRow: section.endingRow
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const worksheet = workbook.Sheets[section.sheetName];
|
|
const worksheet = workbook.Sheets[section.sheetName];
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!worksheet) {
|
|
if (!worksheet) {
|
|
|
const error = `Worksheet '${section.sheetName}' not found`;
|
|
const error = `Worksheet '${section.sheetName}' not found`;
|
|
|
logger.warn(error, { availableWorksheets: workbook.SheetNames });
|
|
logger.warn(error, { availableWorksheets: workbook.SheetNames });
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
onProgress({
|
|
onProgress({
|
|
|
importId: 0,
|
|
importId: 0,
|
|
|
status: 'processing',
|
|
status: 'processing',
|
|
@@ -216,14 +217,14 @@ export class DatabaseExcelReaderService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const sectionData = await this.processSectionFromWorksheet(
|
|
const sectionData = await this.processSectionFromWorksheet(
|
|
|
- worksheet,
|
|
|
|
|
- section,
|
|
|
|
|
- sectionIndex,
|
|
|
|
|
- totalSections,
|
|
|
|
|
|
|
+ worksheet,
|
|
|
|
|
+ section,
|
|
|
|
|
+ sectionIndex,
|
|
|
|
|
+ totalSections,
|
|
|
onProgress
|
|
onProgress
|
|
|
);
|
|
);
|
|
|
results.push(sectionData);
|
|
results.push(sectionData);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.info(`Section ${section.name} processed successfully`, {
|
|
logger.info(`Section ${section.name} processed successfully`, {
|
|
|
rowsProcessed: sectionData.data.length,
|
|
rowsProcessed: sectionData.data.length,
|
|
|
fields: sectionData.fields.length
|
|
fields: sectionData.fields.length
|
|
@@ -264,27 +265,27 @@ export class DatabaseExcelReaderService {
|
|
|
|
|
|
|
|
const startingRow = section.startingRow || 2; // Default to 2 to skip header
|
|
const startingRow = section.startingRow || 2; // Default to 2 to skip header
|
|
|
const endingRow = section.endingRow || Infinity;
|
|
const endingRow = section.endingRow || Infinity;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug('Section configuration', {
|
|
logger.debug('Section configuration', {
|
|
|
sectionName: section.name,
|
|
sectionName: section.name,
|
|
|
startingRow,
|
|
startingRow,
|
|
|
endingRow,
|
|
endingRow,
|
|
|
fieldsCount: section.fields?.length || 0
|
|
fieldsCount: section.fields?.length || 0
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Convert worksheet to JSON array
|
|
// Convert worksheet to JSON array
|
|
|
const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
|
|
const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Process data rows
|
|
// Process data rows
|
|
|
const data: Record<string, any>[] = [];
|
|
const data: Record<string, any>[] = [];
|
|
|
const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
|
|
const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let processedRows = 0;
|
|
let processedRows = 0;
|
|
|
let skippedRows = 0;
|
|
let skippedRows = 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
|
|
for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
|
|
|
const row = worksheetData[rowNum - 1]; // Convert to 0-based index
|
|
const row = worksheetData[rowNum - 1]; // Convert to 0-based index
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
|
|
if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
|
|
|
skippedRows++;
|
|
skippedRows++;
|
|
|
logger.debug(`Skipping empty row ${rowNum}`);
|
|
logger.debug(`Skipping empty row ${rowNum}`);
|
|
@@ -293,13 +294,13 @@ export class DatabaseExcelReaderService {
|
|
|
|
|
|
|
|
const rowData: Record<string, any> = {};
|
|
const rowData: Record<string, any> = {};
|
|
|
let fieldsProcessed = 0;
|
|
let fieldsProcessed = 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Map cell values based on field configuration
|
|
// Map cell values based on field configuration
|
|
|
for (const field of section.fields || []) {
|
|
for (const field of section.fields || []) {
|
|
|
try {
|
|
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
|
|
const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Processing field`, {
|
|
logger.debug(`Processing field`, {
|
|
|
fieldName: field.name,
|
|
fieldName: field.name,
|
|
|
cellPosition: field.cellPosition,
|
|
cellPosition: field.cellPosition,
|
|
@@ -307,20 +308,21 @@ export class DatabaseExcelReaderService {
|
|
|
rawValue: cellValue,
|
|
rawValue: cellValue,
|
|
|
rowNum
|
|
rowNum
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
|
|
if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
|
|
|
|
|
+ console.log(field.name, field.dataType, field.dataTypeFormat, cellValue, field.parsedType);
|
|
|
const value = this.convertCellValue(
|
|
const value = this.convertCellValue(
|
|
|
cellValue,
|
|
cellValue,
|
|
|
field.parsedType || FieldTypeEnum.String
|
|
field.parsedType || FieldTypeEnum.String
|
|
|
);
|
|
);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Value converted`, {
|
|
logger.debug(`Value converted`, {
|
|
|
fieldName: field.name,
|
|
fieldName: field.name,
|
|
|
originalValue: cellValue,
|
|
originalValue: cellValue,
|
|
|
convertedValue: value,
|
|
convertedValue: value,
|
|
|
fieldType: field.parsedType || FieldTypeEnum.String
|
|
fieldType: field.parsedType || FieldTypeEnum.String
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Map to the correct column name for Prisma model
|
|
// Map to the correct column name for Prisma model
|
|
|
const columnName = field.importTableColumnName;
|
|
const columnName = field.importTableColumnName;
|
|
|
rowData[columnName] = value;
|
|
rowData[columnName] = value;
|
|
@@ -334,12 +336,12 @@ export class DatabaseExcelReaderService {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Only add non-empty rows
|
|
// Only add non-empty rows
|
|
|
if (Object.keys(rowData).length > 0) {
|
|
if (Object.keys(rowData).length > 0) {
|
|
|
data.push(rowData);
|
|
data.push(rowData);
|
|
|
processedRows++;
|
|
processedRows++;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (processedRows <= 5 || processedRows % 100 === 0) {
|
|
if (processedRows <= 5 || processedRows % 100 === 0) {
|
|
|
logger.debug(`Row processed`, {
|
|
logger.debug(`Row processed`, {
|
|
|
rowNum,
|
|
rowNum,
|
|
@@ -351,7 +353,7 @@ export class DatabaseExcelReaderService {
|
|
|
} else {
|
|
} else {
|
|
|
logger.debug(`Skipping row with no valid data`, { rowNum });
|
|
logger.debug(`Skipping row with no valid data`, { rowNum });
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Update progress every 100 rows
|
|
// Update progress every 100 rows
|
|
|
if (rowNum % 100 === 0 || rowNum === Math.min(endingRow, worksheetData.length)) {
|
|
if (rowNum % 100 === 0 || rowNum === Math.min(endingRow, worksheetData.length)) {
|
|
|
onProgress({
|
|
onProgress({
|
|
@@ -404,30 +406,35 @@ export class DatabaseExcelReaderService {
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private parseCellAddress(cellPosition: string): { row: number; col: number } {
|
|
|
|
|
|
|
+ private parseCellAddress(cellPosition: string, rowNumber: number): { row: number; col: number } {
|
|
|
logger.debug(`Parsing cell address: ${cellPosition}`);
|
|
logger.debug(`Parsing cell address: ${cellPosition}`);
|
|
|
-
|
|
|
|
|
- const match = cellPosition.match(/([A-Z]+)(\d+)/);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ let match = cellPosition.match(/([A-Z]+)(\d+)/);
|
|
|
if (!match) {
|
|
if (!match) {
|
|
|
- logger.warn(`Invalid cell position format: ${cellPosition}, using default 1,1`);
|
|
|
|
|
- return { row: 1, col: 1 };
|
|
|
|
|
|
|
+ logger.warn(`Invalid cell position format: ${cellPosition}, trying to add row number ${rowNumber}`);
|
|
|
|
|
+ const appendedCellPosition = `${cellPosition}${rowNumber}`;
|
|
|
|
|
+ match = appendedCellPosition.match(/([A-Z]+)(\d+)/);
|
|
|
|
|
+ if (!match) {
|
|
|
|
|
+ logger.warn(`Invalid cell position format: ${appendedCellPosition}, using default 1,1`);
|
|
|
|
|
+ return { row: 1, col: 1 };
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
|
|
const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
|
|
|
const row = parseInt(match[2]);
|
|
const row = parseInt(match[2]);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Parsed cell address`, {
|
|
logger.debug(`Parsed cell address`, {
|
|
|
original: cellPosition,
|
|
original: cellPosition,
|
|
|
row,
|
|
row,
|
|
|
col
|
|
col
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return { row, col };
|
|
return { row, col };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private mapSectionType(type: string): SectionTypeEnum {
|
|
private mapSectionType(type: string): SectionTypeEnum {
|
|
|
logger.debug(`Mapping section type: ${type}`);
|
|
logger.debug(`Mapping section type: ${type}`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const mappedType = (() => {
|
|
const mappedType = (() => {
|
|
|
switch (type?.toLowerCase()) {
|
|
switch (type?.toLowerCase()) {
|
|
|
case 'grid':
|
|
case 'grid':
|
|
@@ -438,18 +445,18 @@ export class DatabaseExcelReaderService {
|
|
|
return SectionTypeEnum.Unknown;
|
|
return SectionTypeEnum.Unknown;
|
|
|
}
|
|
}
|
|
|
})();
|
|
})();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Section type mapped`, {
|
|
logger.debug(`Section type mapped`, {
|
|
|
originalType: type,
|
|
originalType: type,
|
|
|
mappedType: SectionTypeEnum[mappedType]
|
|
mappedType: SectionTypeEnum[mappedType]
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return mappedType;
|
|
return mappedType;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private mapFields(fields: any[]): LayoutSectionField[] {
|
|
private mapFields(fields: any[]): LayoutSectionField[] {
|
|
|
logger.debug(`Mapping ${fields.length} fields`);
|
|
logger.debug(`Mapping ${fields.length} fields`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const mappedFields = fields.map((field, index) => {
|
|
const mappedFields = fields.map((field, index) => {
|
|
|
const mappedField = {
|
|
const mappedField = {
|
|
|
id: field.id || index,
|
|
id: field.id || index,
|
|
@@ -461,7 +468,7 @@ export class DatabaseExcelReaderService {
|
|
|
importColumnOrderNumber: field.importColumnOrderNumber || index,
|
|
importColumnOrderNumber: field.importColumnOrderNumber || index,
|
|
|
parsedType: this.mapFieldType(field.dataType)
|
|
parsedType: this.mapFieldType(field.dataType)
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Field mapped`, {
|
|
logger.debug(`Field mapped`, {
|
|
|
index,
|
|
index,
|
|
|
originalName: field.name,
|
|
originalName: field.name,
|
|
@@ -469,16 +476,16 @@ export class DatabaseExcelReaderService {
|
|
|
cellPosition: mappedField.cellPosition,
|
|
cellPosition: mappedField.cellPosition,
|
|
|
parsedType: FieldTypeEnum[mappedField.parsedType]
|
|
parsedType: FieldTypeEnum[mappedField.parsedType]
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return mappedField;
|
|
return mappedField;
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return mappedFields;
|
|
return mappedFields;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private mapFieldType(dataType: string): FieldTypeEnum {
|
|
private mapFieldType(dataType: string): FieldTypeEnum {
|
|
|
const type = dataType?.toLowerCase();
|
|
const type = dataType?.toLowerCase();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const mappedType = (() => {
|
|
const mappedType = (() => {
|
|
|
switch (type) {
|
|
switch (type) {
|
|
|
case 'time':
|
|
case 'time':
|
|
@@ -497,12 +504,12 @@ export class DatabaseExcelReaderService {
|
|
|
return FieldTypeEnum.String;
|
|
return FieldTypeEnum.String;
|
|
|
}
|
|
}
|
|
|
})();
|
|
})();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Field type mapped`, {
|
|
logger.debug(`Field type mapped`, {
|
|
|
originalDataType: dataType,
|
|
originalDataType: dataType,
|
|
|
mappedType: FieldTypeEnum[mappedType]
|
|
mappedType: FieldTypeEnum[mappedType]
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return mappedType;
|
|
return mappedType;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -511,14 +518,16 @@ export class DatabaseExcelReaderService {
|
|
|
logger.debug(`Converting null/undefined value to null`, { fieldType: FieldTypeEnum[fieldType] });
|
|
logger.debug(`Converting null/undefined value to null`, { fieldType: FieldTypeEnum[fieldType] });
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
logger.debug(`Converting cell value`, {
|
|
logger.debug(`Converting cell value`, {
|
|
|
originalValue: value,
|
|
originalValue: value,
|
|
|
originalType: typeof value,
|
|
originalType: typeof value,
|
|
|
targetFieldType: FieldTypeEnum[fieldType]
|
|
targetFieldType: FieldTypeEnum[fieldType]
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const convertedValue = (() => {
|
|
const convertedValue = (() => {
|
|
|
|
|
+ if (fieldType === FieldTypeEnum.Date)
|
|
|
|
|
+ console.log(value, fieldType);
|
|
|
switch (fieldType) {
|
|
switch (fieldType) {
|
|
|
case FieldTypeEnum.Time:
|
|
case FieldTypeEnum.Time:
|
|
|
if (typeof value === 'number') {
|
|
if (typeof value === 'number') {
|
|
@@ -528,12 +537,12 @@ export class DatabaseExcelReaderService {
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
return value;
|
|
return value;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case FieldTypeEnum.Decimal:
|
|
case FieldTypeEnum.Decimal:
|
|
|
const decimalResult = parseFloat(value.toString()) || 0;
|
|
const decimalResult = parseFloat(value.toString()) || 0;
|
|
|
logger.debug(`Decimal conversion`, { original: value, converted: decimalResult });
|
|
logger.debug(`Decimal conversion`, { original: value, converted: decimalResult });
|
|
|
return decimalResult;
|
|
return decimalResult;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case FieldTypeEnum.Date:
|
|
case FieldTypeEnum.Date:
|
|
|
if (typeof value === 'number') {
|
|
if (typeof value === 'number') {
|
|
|
// Excel date is days since 1900-01-01
|
|
// Excel date is days since 1900-01-01
|
|
@@ -545,12 +554,12 @@ export class DatabaseExcelReaderService {
|
|
|
const dateResult = new Date(value);
|
|
const dateResult = new Date(value);
|
|
|
logger.debug(`Date conversion from string`, { original: value, converted: dateResult });
|
|
logger.debug(`Date conversion from string`, { original: value, converted: dateResult });
|
|
|
return dateResult;
|
|
return dateResult;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case FieldTypeEnum.Numeric:
|
|
case FieldTypeEnum.Numeric:
|
|
|
const numericResult = parseInt(value.toString()) || 0;
|
|
const numericResult = parseInt(value.toString()) || 0;
|
|
|
logger.debug(`Numeric conversion`, { original: value, converted: numericResult });
|
|
logger.debug(`Numeric conversion`, { original: value, converted: numericResult });
|
|
|
return numericResult;
|
|
return numericResult;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case FieldTypeEnum.String:
|
|
case FieldTypeEnum.String:
|
|
|
default:
|
|
default:
|
|
|
const stringResult = value.toString();
|
|
const stringResult = value.toString();
|
|
@@ -558,7 +567,7 @@ export class DatabaseExcelReaderService {
|
|
|
return stringResult;
|
|
return stringResult;
|
|
|
}
|
|
}
|
|
|
})();
|
|
})();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return convertedValue;
|
|
return convertedValue;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|