database-excel-reader.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import * as XLSX from 'xlsx';
  3. import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
  4. import { prisma } from '@/lib/prisma';
  5. export class DatabaseExcelReaderService {
  6. async readExcelFromDatabase(
  7. fileId: string,
  8. layoutConfig: any,
  9. onProgress: (progress: ImportProgress) => void
  10. ): Promise<ReadSectionData[]> {
  11. try {
  12. // Fetch file from database
  13. const fileRecord = await prisma.file.findUnique({
  14. where: { id: fileId }
  15. });
  16. if (!fileRecord) {
  17. throw new Error(`File with ID ${fileId} not found in database`);
  18. }
  19. // Convert Buffer to ArrayBuffer for xlsx library
  20. const arrayBuffer = fileRecord.data.buffer.slice(
  21. fileRecord.data.byteOffset,
  22. fileRecord.data.byteOffset + fileRecord.data.byteLength
  23. );
  24. const workbook = XLSX.read(arrayBuffer, { type: 'array' });
  25. const results: ReadSectionData[] = [];
  26. const totalSections = layoutConfig.sections?.length || 0;
  27. // Initialize progress
  28. onProgress({
  29. importId: 0,
  30. status: 'processing',
  31. currentSection: '',
  32. currentRow: 0,
  33. totalRows: 0,
  34. errors: [],
  35. processedSections: 0,
  36. totalSections
  37. });
  38. for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
  39. const section = layoutConfig.sections[sectionIndex];
  40. const worksheet = workbook.Sheets[section.sheetName];
  41. if (!worksheet) {
  42. const error = `Worksheet '${section.sheetName}' not found`;
  43. onProgress({
  44. importId: 0,
  45. status: 'processing',
  46. currentSection: section.name,
  47. currentRow: 0,
  48. totalRows: 0,
  49. errors: [error],
  50. processedSections: sectionIndex + 1,
  51. totalSections
  52. });
  53. continue;
  54. }
  55. const sectionData = await this.processSectionFromWorksheet(
  56. worksheet,
  57. section,
  58. sectionIndex,
  59. totalSections,
  60. onProgress
  61. );
  62. results.push(sectionData);
  63. }
  64. return results;
  65. } catch (error) {
  66. throw error;
  67. }
  68. }
  69. async readExcelFromBuffer(
  70. buffer: Buffer,
  71. layoutConfig: any,
  72. onProgress: (progress: ImportProgress) => void
  73. ): Promise<ReadSectionData[]> {
  74. try {
  75. // Convert Buffer to ArrayBuffer for xlsx library
  76. const arrayBuffer = buffer.buffer.slice(
  77. buffer.byteOffset,
  78. buffer.byteOffset + buffer.byteLength
  79. );
  80. const workbook = XLSX.read(arrayBuffer, { type: 'array' });
  81. const results: ReadSectionData[] = [];
  82. const totalSections = layoutConfig.sections?.length || 0;
  83. // Initialize progress
  84. onProgress({
  85. importId: 0,
  86. status: 'processing',
  87. currentSection: '',
  88. currentRow: 0,
  89. totalRows: 0,
  90. errors: [],
  91. processedSections: 0,
  92. totalSections
  93. });
  94. for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
  95. const section = layoutConfig.sections[sectionIndex];
  96. const worksheet = workbook.Sheets[section.sheetName];
  97. if (!worksheet) {
  98. const error = `Worksheet '${section.sheetName}' not found`;
  99. onProgress({
  100. importId: 0,
  101. status: 'processing',
  102. currentSection: section.name,
  103. currentRow: 0,
  104. totalRows: 0,
  105. errors: [error],
  106. processedSections: sectionIndex + 1,
  107. totalSections
  108. });
  109. continue;
  110. }
  111. const sectionData = await this.processSectionFromWorksheet(
  112. worksheet,
  113. section,
  114. sectionIndex,
  115. totalSections,
  116. onProgress
  117. );
  118. results.push(sectionData);
  119. }
  120. return results;
  121. } catch (error) {
  122. throw error;
  123. }
  124. }
  125. private async processSectionFromWorksheet(
  126. worksheet: XLSX.WorkSheet,
  127. section: any,
  128. sectionIndex: number,
  129. totalSections: number,
  130. onProgress: (progress: ImportProgress) => void
  131. ): Promise<ReadSectionData> {
  132. const startingRow = section.startingRow || 2; // Default to 2 to skip header
  133. const endingRow = section.endingRow || Infinity;
  134. // Convert worksheet to JSON array
  135. const worksheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
  136. // Process data rows
  137. const data: Record<string, any>[] = [];
  138. const totalRows = Math.min(endingRow, worksheetData.length) - startingRow + 1;
  139. for (let rowNum = startingRow; rowNum <= Math.min(endingRow, worksheetData.length); rowNum++) {
  140. const row = worksheetData[rowNum - 1]; // Convert to 0-based index
  141. if (!row || row.every(cell => cell === null || cell === undefined || cell === '')) {
  142. continue;
  143. }
  144. const rowData: Record<string, any> = {};
  145. // Map cell values based on field configuration
  146. for (const field of section.fields || []) {
  147. try {
  148. const cellAddress = this.parseCellAddress(field.cellPosition, rowNum);
  149. const cellValue = row[cellAddress.col - 1]; // Convert to 0-based index
  150. if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
  151. const value = this.convertCellValue(
  152. field.dataType,
  153. field.dataTypeFormat,
  154. cellValue,
  155. 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. }
  161. } catch (error) {
  162. console.log(`Error processing field ${field.name} at row ${rowNum}`, {
  163. error: error instanceof Error ? error.message : String(error),
  164. field,
  165. rowNum
  166. });
  167. }
  168. }
  169. // Only add non-empty rows
  170. if (Object.keys(rowData).length > 0) {
  171. data.push(rowData);
  172. }
  173. // Update progress every 100 rows
  174. if (rowNum % 100 === 0 || rowNum === Math.min(endingRow, worksheetData.length)) {
  175. onProgress({
  176. importId: 0,
  177. status: 'processing',
  178. currentSection: section.name,
  179. currentRow: rowNum - startingRow + 1,
  180. totalRows,
  181. errors: [],
  182. processedSections: sectionIndex,
  183. totalSections
  184. });
  185. }
  186. }
  187. const result = {
  188. id: section.id || 0,
  189. name: section.name || '',
  190. tableName: section.tableName || '',
  191. sheet: section.sheetName || '',
  192. type: section.type || '',
  193. startingRow,
  194. endingRow,
  195. parsedType: this.mapSectionType(section.type),
  196. fields: this.mapFields(section.fields || []),
  197. data
  198. };
  199. return result;
  200. }
  201. private parseCellAddress(cellPosition: string, rowNumber: number): { row: number; col: number } {
  202. let match = cellPosition.match(/([A-Z]+)(\d+)/);
  203. if (!match) {
  204. const appendedCellPosition = `${cellPosition}${rowNumber}`;
  205. match = appendedCellPosition.match(/([A-Z]+)(\d+)/);
  206. if (!match) {
  207. return { row: 1, col: 1 };
  208. }
  209. }
  210. const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
  211. const row = parseInt(match[2]);
  212. return { row, col };
  213. }
  214. private mapSectionType(type: string): SectionTypeEnum {
  215. const mappedType = (() => {
  216. switch (type?.toLowerCase()) {
  217. case 'grid':
  218. return SectionTypeEnum.Grid;
  219. case 'properties':
  220. return SectionTypeEnum.Properties;
  221. default:
  222. return SectionTypeEnum.Unknown;
  223. }
  224. })();
  225. return mappedType;
  226. }
  227. private mapFields(fields: any[]): LayoutSectionField[] {
  228. const mappedFields = fields.map((field, index) => {
  229. const mappedField = {
  230. id: field.id || index,
  231. cellPosition: field.cellPosition || '',
  232. name: field.name || '',
  233. dataType: field.dataType || 'string',
  234. dataTypeFormat: field.dataTypeFormat,
  235. importTableColumnName: field.importTableColumnName || field.name || `column_${index}`,
  236. importColumnOrderNumber: field.importColumnOrderNumber || index,
  237. parsedType: this.mapFieldType(field.dataType)
  238. };
  239. return mappedField;
  240. });
  241. return mappedFields;
  242. }
  243. private mapFieldType(dataType: string): FieldTypeEnum {
  244. const type = dataType?.toLowerCase();
  245. const mappedType = (() => {
  246. switch (type) {
  247. case 'time':
  248. return FieldTypeEnum.Time;
  249. case 'decimal':
  250. case 'number':
  251. case 'float':
  252. return FieldTypeEnum.Decimal;
  253. case 'date':
  254. return FieldTypeEnum.Date;
  255. case 'int':
  256. case 'integer':
  257. case 'numeric':
  258. return FieldTypeEnum.Numeric;
  259. default:
  260. return FieldTypeEnum.String;
  261. }
  262. })();
  263. return mappedType;
  264. }
  265. private convertCellValue(dataType: string, dataTypeFormat: string | undefined, value: any, fieldType: FieldTypeEnum): any {
  266. if (value === null || value === undefined) {
  267. return null;
  268. }
  269. const convertedValue = (() => {
  270. if (dataType === "DATE")
  271. return XLSX.SSF.format(dataTypeFormat || 'yyyy-mm-dd', value);
  272. switch (fieldType) {
  273. case FieldTypeEnum.Time:
  274. if (typeof value === 'number') {
  275. // Excel time is fraction of a day
  276. const result = value * 24 * 60 * 60 * 1000; // Convert to milliseconds
  277. return result;
  278. }
  279. return value;
  280. case FieldTypeEnum.Decimal:
  281. const decimalResult = parseFloat(value.toString()) || 0;
  282. return decimalResult;
  283. case FieldTypeEnum.Date:
  284. if (typeof value === 'number') {
  285. // Excel date is days since 1900-01-01
  286. const excelEpoch = new Date(1900, 0, 1);
  287. const dateResult = new Date(excelEpoch.getTime() + (value - 1) * 24 * 60 * 60 * 1000);
  288. return dateResult;
  289. }
  290. const dateResult = new Date(value);
  291. return dateResult;
  292. case FieldTypeEnum.Numeric:
  293. const numericResult = parseInt(value.toString()) || 0;
  294. return numericResult;
  295. case FieldTypeEnum.String:
  296. default:
  297. const stringResult = value.toString();
  298. return stringResult;
  299. }
  300. })();
  301. return convertedValue;
  302. }
  303. }