excel-reader.ts 13 KB

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