Преглед на файлове

refactor: enhance importExcelTags method for improved error handling and support for different Excel libraries

Dave преди 3 седмици
родител
ревизия
4ffdf1e90c

+ 6 - 4
apps/box-app-api/src/feature/sys-params/sys-params.service.ts

@@ -88,7 +88,7 @@ export class SysParamsService {
   }
 
   async getSysCnf() {
-   try {
+    try {
       // Try to get from Redis cache first
       const cacheKey = `box:app:sysconf`;
       const conf = await this.redis.get(cacheKey);
@@ -110,7 +110,7 @@ export class SysParamsService {
   }
 
   async getSysParams() {
-   try {
+    try {
       // Try to get from Redis cache first
       const cacheKey = `box:app:sysparams`;
       const conf = await this.redis.get(cacheKey);
@@ -129,7 +129,9 @@ export class SysParamsService {
 
       const sysParams = params.map((p, idx) => ({
         id: p.id.toString(),
-        content: p.value ?? '', // Use 'value' field as content
+        // content: p.value ?? '', // Use 'value' field as content
+        name: p.name,
+        value: p.value ?? '',
         seq: idx,
       }));
 
@@ -142,5 +144,5 @@ export class SysParamsService {
     } catch (error) {
       return null;
     }
-  }  
+  }
 }

+ 55 - 16
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -346,25 +346,64 @@ export class VideoMediaService {
       throw new BadRequestException('Empty file');
     }
 
-    const workbook = XLSX.read(buffer, { type: 'buffer' });
-    const sheetName = workbook.SheetNames[0];
-    const sheet = sheetName ? workbook.Sheets[sheetName] : undefined;
+    const rowsAH: Array<{ idRaw: unknown; tagsRaw: unknown }> = [];
 
-    if (!sheet) {
-      throw new BadRequestException('No sheet found in Excel file');
-    }
+    try {
+      const workbook = XLSX.read(buffer, { type: 'buffer' });
+      const sheetName = workbook.SheetNames[0];
+      const sheet = sheetName ? workbook.Sheets[sheetName] : undefined;
 
-    const rangeRef = sheet['!ref'] ?? 'A1:A1';
-    const range = XLSX.utils.decode_range(rangeRef);
-    const rowsAH: Array<{ idRaw: unknown; tagsRaw: unknown }> = [];
+      if (!sheet) {
+        throw new BadRequestException('No sheet found in Excel file');
+      }
 
-    for (let r = range.s.r; r <= range.e.r; r += 1) {
-      const idCell = sheet[XLSX.utils.encode_cell({ r, c: 0 })];
-      const tagsCell = sheet[XLSX.utils.encode_cell({ r, c: 7 })];
-      rowsAH.push({
-        idRaw: idCell?.v ?? '',
-        tagsRaw: tagsCell?.v ?? '',
-      });
+      const rangeRef = sheet['!ref'] ?? 'A1:A1';
+      const range = XLSX.utils.decode_range(rangeRef);
+
+      for (let r = range.s.r; r <= range.e.r; r += 1) {
+        const idCell = sheet[XLSX.utils.encode_cell({ r, c: 0 })];
+        const tagsCell = sheet[XLSX.utils.encode_cell({ r, c: 7 })];
+        rowsAH.push({
+          idRaw: idCell?.v ?? '',
+          tagsRaw: tagsCell?.v ?? '',
+        });
+      }
+    } catch (error) {
+      const cellToString = (value: unknown): string => {
+        if (value == null) return '';
+        if (typeof value === 'string') return value;
+        if (typeof value === 'number' || typeof value === 'boolean') {
+          return String(value);
+        }
+        if (typeof value === 'object') {
+          const anyValue = value as any;
+          if (typeof anyValue.text === 'string') return anyValue.text;
+          if (Array.isArray(anyValue.richText)) {
+            return anyValue.richText.map((t: any) => t?.text ?? '').join('');
+          }
+          if (typeof anyValue.result !== 'undefined') {
+            return String(anyValue.result);
+          }
+        }
+        return String(value);
+      };
+
+      const workbook = new ExcelJS.Workbook();
+      await workbook.xlsx.load(buffer as any);
+      const sheet = workbook.worksheets[0];
+
+      if (!sheet) {
+        throw new BadRequestException('No sheet found in Excel file');
+      }
+
+      const lastRow = sheet.lastRow?.number ?? 0;
+      for (let r = 1; r <= lastRow; r += 1) {
+        const row = sheet.getRow(r);
+        rowsAH.push({
+          idRaw: cellToString(row.getCell(1).value),
+          tagsRaw: cellToString(row.getCell(8).value),
+        });
+      }
     }
 
     this.logger.log(