excel-reader.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. /* eslint-disable @typescript-eslint/no-require-imports */
  3. /* eslint-disable @typescript-eslint/no-unused-vars */
  4. import * as XLSX from 'xlsx';
  5. import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
  6. // Simple logger utility for debugging
  7. const logger = {
  8. debug: (message: string, ...args: any[]) => {
  9. //console.debug(`[ExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
  10. },
  11. info: (message: string, ...args: any[]) => {
  12. //console.info(`[ExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
  13. },
  14. warn: (message: string, ...args: any[]) => {
  15. //console.warn(`[ExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
  16. },
  17. error: (message: string, ...args: any[]) => {
  18. //console.error(`[ExcelReaderService] ${new Date().toISOString()} - ${message}`, ...args);
  19. }
  20. };
  21. export class ExcelReaderService {
  22. async readExcelFile(
  23. fileData: Buffer | Uint8Array,
  24. layoutConfig: any,
  25. onProgress: (progress: ImportProgress) => void
  26. ): Promise<ReadSectionData[]> {
  27. logger.info('Starting Excel file import', {
  28. fileSize: fileData.length,
  29. layoutConfigSections: layoutConfig.sections?.length || 0
  30. });
  31. const startTime = Date.now();
  32. try {
  33. logger.debug('Loading Excel workbook from buffer...');
  34. const workbook = XLSX.read(fileData, { type: 'buffer' });
  35. logger.info('Excel workbook loaded successfully', {
  36. worksheets: workbook.SheetNames.map(name => ({ name, rowCount: XLSX.utils.sheet_to_json(workbook.Sheets[name]).length }))
  37. });
  38. const results: ReadSectionData[] = [];
  39. const totalSections = layoutConfig.sections?.length || 0;
  40. logger.info('Processing Excel import', { totalSections });
  41. // Initialize progress
  42. onProgress({
  43. importId: 0, // Will be set by caller
  44. status: 'processing',
  45. currentSection: '',
  46. currentRow: 0,
  47. totalRows: 0,
  48. errors: [],
  49. processedSections: 0,
  50. totalSections
  51. });
  52. for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
  53. const section = layoutConfig.sections[sectionIndex];
  54. logger.debug(`Processing section ${sectionIndex + 1}/${totalSections}`, {
  55. sectionName: section.name,
  56. sheetName: section.sheetName,
  57. startingRow: section.startingRow,
  58. endingRow: section.endingRow
  59. });
  60. const worksheet = workbook.Sheets[section.sheetName];
  61. if (!worksheet) {
  62. const error = `Worksheet '${section.sheetName}' not found`;
  63. logger.warn(error, { availableWorksheets: workbook.SheetNames });
  64. onProgress({
  65. importId: 0,
  66. status: 'processing',
  67. currentSection: section.name,
  68. currentRow: 0,
  69. totalRows: 0,
  70. errors: [error],
  71. processedSections: sectionIndex + 1,
  72. totalSections
  73. });
  74. continue;
  75. }
  76. const sectionData = await this.processSection(worksheet, section, sectionIndex, totalSections, onProgress);
  77. results.push(sectionData);
  78. logger.info(`Section ${section.name} processed successfully`, {
  79. rowsProcessed: sectionData.data.length,
  80. fields: sectionData.fields.length
  81. });
  82. }
  83. const totalTime = Date.now() - startTime;
  84. logger.info('Excel file import completed', {
  85. totalSections: results.length,
  86. totalRows: results.reduce((sum, section) => sum + section.data.length, 0),
  87. totalTimeMs: totalTime
  88. });
  89. return results;
  90. } catch (error) {
  91. logger.error('Error reading Excel file', {
  92. error: error instanceof Error ? error.message : String(error),
  93. stack: error instanceof Error ? error.stack : undefined
  94. });
  95. throw error;
  96. }
  97. }
  98. private async processSection(
  99. worksheet: XLSX.WorkSheet,
  100. section: any,
  101. sectionIndex: number,
  102. totalSections: number,
  103. onProgress: (progress: ImportProgress) => void
  104. ): Promise<ReadSectionData> {
  105. const sectionStartTime = Date.now();
  106. logger.info(`Starting section processing`, {
  107. sectionName: section.name,
  108. sheetName: section.sheetName,
  109. sectionIndex: sectionIndex + 1,
  110. totalSections
  111. });
  112. const startingRow = section.startingRow || 2; // Default to 2 to skip header
  113. const endingRow = section.endingRow || Infinity;
  114. logger.debug('Section configuration', {
  115. sectionName: section.name,
  116. startingRow,
  117. endingRow,
  118. fieldsCount: section.fields?.length || 0
  119. });
  120. // Convert worksheet to JSON array
  121. const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
  122. // Process data rows
  123. const data: Record<string, any>[] = [];
  124. const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
  125. let processedRows = 0;
  126. let skippedRows = 0;
  127. for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
  128. const row = worksheetData[rowNum - 1]; // Convert to 0-based index
  129. if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
  130. skippedRows++;
  131. logger.debug(`Skipping empty row ${rowNum}`);
  132. continue;
  133. }
  134. const rowData: Record<string, any> = {};
  135. let fieldsProcessed = 0;
  136. // Map cell values based on field configuration
  137. for (const field of section.fields || []) {
  138. try {
  139. const cellAddress = this.parseCellAddress(field.cellPosition);
  140. const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
  141. logger.debug(`Processing field`, {
  142. fieldName: field.name,
  143. cellPosition: field.cellPosition,
  144. cellAddress,
  145. rawValue: cellValue,
  146. rowNum
  147. });
  148. if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
  149. const value = this.convertCellValue(
  150. cellValue,
  151. field.parsedType || FieldTypeEnum.String
  152. );
  153. logger.debug(`Value converted`, {
  154. fieldName: field.name,
  155. originalValue: cellValue,
  156. convertedValue: value,
  157. fieldType: field.parsedType || FieldTypeEnum.String
  158. });
  159. // Map to the correct column name for Prisma model
  160. const columnName = field.importTableColumnName;
  161. rowData[columnName] = value;
  162. fieldsProcessed++;
  163. }
  164. } catch (error) {
  165. logger.error(`Error processing field ${field.name} at row ${rowNum}`, {
  166. error: error instanceof Error ? error.message : String(error),
  167. field,
  168. rowNum
  169. });
  170. }
  171. }
  172. // Only add non-empty rows
  173. if (Object.keys(rowData).length > 0) {
  174. data.push(rowData);
  175. processedRows++;
  176. if (processedRows <= 5 || processedRows % 100 === 0) {
  177. logger.debug(`Row processed`, {
  178. rowNum,
  179. fieldsProcessed,
  180. rowDataKeys: Object.keys(rowData),
  181. dataLength: data.length
  182. });
  183. }
  184. } else {
  185. logger.debug(`Skipping row with no valid data`, { rowNum });
  186. }
  187. // Update progress every 100 rows
  188. if (rowNum % 100 === 0 || rowNum === Math.min(endingRow, worksheetData.length)) {
  189. onProgress({
  190. importId: 0,
  191. status: 'processing',
  192. currentSection: section.name,
  193. currentRow: rowNum - startingRow + 1,
  194. totalRows,
  195. errors: [],
  196. processedSections: sectionIndex,
  197. totalSections
  198. });
  199. }
  200. }
  201. const sectionTime = Date.now() - sectionStartTime;
  202. logger.info(`Section processing completed`, {
  203. sectionName: section.name,
  204. processedRows,
  205. skippedRows,
  206. totalRows,
  207. dataRows: data.length,
  208. processingTimeMs: sectionTime
  209. });
  210. const result = {
  211. id: section.id || 0,
  212. name: section.name || '',
  213. tableName: section.tableName || '',
  214. sheet: section.sheetName || '',
  215. type: section.type || '',
  216. startingRow,
  217. endingRow,
  218. parsedType: this.mapSectionType(section.type),
  219. fields: this.mapFields(section.fields || []),
  220. data
  221. };
  222. logger.debug('Section result', {
  223. sectionName: section.name,
  224. resultSummary: {
  225. id: result.id,
  226. name: result.name,
  227. tableName: result.tableName,
  228. dataRows: result.data.length,
  229. fields: result.fields.length
  230. }
  231. });
  232. return result;
  233. }
  234. private parseCellAddress(cellPosition: string): { row: number; col: number } {
  235. logger.debug(`Parsing cell address: ${cellPosition}`);
  236. const match = cellPosition.match(/([A-Z]+)(\d+)/);
  237. if (!match) {
  238. logger.warn(`Invalid cell position format: ${cellPosition}, using default 1,1`);
  239. return { row: 1, col: 1 };
  240. }
  241. const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
  242. const row = parseInt(match[2]);
  243. logger.debug(`Parsed cell address`, {
  244. original: cellPosition,
  245. row,
  246. col
  247. });
  248. return { row, col };
  249. }
  250. private mapSectionType(type: string): SectionTypeEnum {
  251. logger.debug(`Mapping section type: ${type}`);
  252. const mappedType = (() => {
  253. switch (type?.toLowerCase()) {
  254. case 'grid':
  255. return SectionTypeEnum.Grid;
  256. case 'properties':
  257. return SectionTypeEnum.Properties;
  258. default:
  259. return SectionTypeEnum.Unknown;
  260. }
  261. })();
  262. logger.debug(`Section type mapped`, {
  263. originalType: type,
  264. mappedType: SectionTypeEnum[mappedType]
  265. });
  266. return mappedType;
  267. }
  268. private mapFields(fields: any[]): LayoutSectionField[] {
  269. logger.debug(`Mapping ${fields.length} fields`);
  270. const mappedFields = fields.map((field, index) => {
  271. const mappedField = {
  272. id: field.id || index,
  273. cellPosition: field.cellPosition || '',
  274. name: field.name || '',
  275. dataType: field.dataType || 'string',
  276. dataTypeFormat: field.dataTypeFormat,
  277. importTableColumnName: field.importTableColumnName || field.name || `column_${index}`,
  278. importColumnOrderNumber: field.importColumnOrderNumber || index,
  279. parsedType: this.mapFieldType(field.dataType)
  280. };
  281. logger.debug(`Field mapped`, {
  282. index,
  283. originalName: field.name,
  284. mappedName: mappedField.name,
  285. cellPosition: mappedField.cellPosition,
  286. parsedType: FieldTypeEnum[mappedField.parsedType]
  287. });
  288. return mappedField;
  289. });
  290. return mappedFields;
  291. }
  292. private mapFieldType(dataType: string): FieldTypeEnum {
  293. const type = dataType?.toLowerCase();
  294. const mappedType = (() => {
  295. switch (type) {
  296. case 'time':
  297. return FieldTypeEnum.Time;
  298. case 'decimal':
  299. case 'number':
  300. case 'float':
  301. return FieldTypeEnum.Decimal;
  302. case 'date':
  303. return FieldTypeEnum.Date;
  304. case 'int':
  305. case 'integer':
  306. case 'numeric':
  307. return FieldTypeEnum.Numeric;
  308. default:
  309. return FieldTypeEnum.String;
  310. }
  311. })();
  312. logger.debug(`Field type mapped`, {
  313. originalDataType: dataType,
  314. mappedType: FieldTypeEnum[mappedType]
  315. });
  316. return mappedType;
  317. }
  318. private convertCellValue(value: any, fieldType: FieldTypeEnum): any {
  319. if (value === null || value === undefined) {
  320. logger.debug(`Converting null/undefined value to null`, { fieldType: FieldTypeEnum[fieldType] });
  321. return null;
  322. }
  323. logger.debug(`Converting cell value`, {
  324. originalValue: value,
  325. originalType: typeof value,
  326. targetFieldType: FieldTypeEnum[fieldType]
  327. });
  328. const convertedValue = (() => {
  329. switch (fieldType) {
  330. case FieldTypeEnum.Time:
  331. if (typeof value === 'number') {
  332. // Excel time is fraction of a day
  333. const result = value * 24 * 60 * 60 * 1000; // Convert to milliseconds
  334. logger.debug(`Time conversion`, { original: value, converted: result });
  335. return result;
  336. }
  337. return value;
  338. case FieldTypeEnum.Decimal:
  339. const decimalResult = parseFloat(value.toString()) || 0;
  340. logger.debug(`Decimal conversion`, { original: value, converted: decimalResult });
  341. return decimalResult;
  342. case FieldTypeEnum.Date:
  343. if (typeof value === 'number') {
  344. // Excel date is days since 1900-01-01
  345. const excelEpoch = new Date(1900, 0, 1);
  346. const dateResult = new Date(excelEpoch.getTime() + (value - 1) * 24 * 60 * 60 * 1000);
  347. logger.debug(`Date conversion`, { original: value, converted: dateResult });
  348. return dateResult;
  349. }
  350. const dateResult = new Date(value);
  351. logger.debug(`Date conversion from string`, { original: value, converted: dateResult });
  352. return dateResult;
  353. case FieldTypeEnum.Numeric:
  354. const numericResult = parseInt(value.toString()) || 0;
  355. logger.debug(`Numeric conversion`, { original: value, converted: numericResult });
  356. return numericResult;
  357. case FieldTypeEnum.String:
  358. default:
  359. const stringResult = value.toString();
  360. logger.debug(`String conversion`, { original: value, converted: stringResult });
  361. return stringResult;
  362. }
  363. })();
  364. return convertedValue;
  365. }
  366. }