Bladeren bron

refactor: add normalizeMongoIdToString method to handle MongoDB ID conversions; update findAll and findOne methods to use this normalization

Dave 1 maand geleden
bovenliggende
commit
be576b3b74
1 gewijzigde bestanden met toevoegingen van 21 en 2 verwijderingen
  1. 21 2
      apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

+ 21 - 2
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -39,6 +39,25 @@ export class VideoMediaService {
     private readonly mediaStorageStrategy: StorageStrategy,
   ) {}
 
+  private normalizeMongoIdToString(id: unknown): string {
+    if (typeof id === 'string') return id;
+
+    if (id && typeof id === 'object') {
+      const anyId = id as any;
+
+      // Extended JSON: { $oid: "..." }
+      if (typeof anyId.$oid === 'string') return anyId.$oid;
+
+      // BSON ObjectId instance (common)
+      if (typeof anyId.toHexString === 'function') return anyId.toHexString();
+
+      // Fallback: best-effort string conversion
+      if (typeof anyId.toString === 'function') return anyId.toString();
+    }
+
+    return String(id);
+  }
+
   // helper to generate next vid
   private async generateNextVid(): Promise<number> {
     const last = await this.prisma.videoMedia.findFirst({
@@ -178,7 +197,7 @@ export class VideoMediaService {
       page,
       pageSize,
       items: rows.map((row) => ({
-        id: row.id,
+        id: this.normalizeMongoIdToString(row.id),
         vid: row.vid ?? null,
         title: row.title,
         filename: row.filename,
@@ -300,7 +319,7 @@ export class VideoMediaService {
     ]);
 
     return {
-      id: video.id,
+      id: this.normalizeMongoIdToString(video.id),
       title: video.title,
       filename: video.filename,
       preFileName: video.preFileName,