From b6f6b3c3d7f8b988da3a1754f581f7890e11d3bf Mon Sep 17 00:00:00 2001 From: StarAppeal Date: Fri, 26 Sep 2025 05:04:19 +0200 Subject: [PATCH] refactor file model to use UUID for userId and update file service methods accordingly --- src/db/models/file.ts | 5 ++--- src/services/db/fileService.ts | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/db/models/file.ts b/src/db/models/file.ts index d7a186b..baf94af 100644 --- a/src/db/models/file.ts +++ b/src/db/models/file.ts @@ -2,7 +2,7 @@ import mongoose from "mongoose"; export interface File { _id: mongoose.Types.ObjectId; - userId: mongoose.Types.ObjectId; + userId: string; // UUID des Benutzers statt MongoDB ObjectId objectKey: string; originalName: string; mimeType: string; @@ -13,8 +13,7 @@ export interface File { const fileSchema = new mongoose.Schema( { userId: { - type: mongoose.Schema.Types.ObjectId, - ref: "User", + type: String, required: true, index: true, }, diff --git a/src/services/db/fileService.ts b/src/services/db/fileService.ts index bb03ab1..f59bef9 100644 --- a/src/services/db/fileService.ts +++ b/src/services/db/fileService.ts @@ -1,4 +1,3 @@ -import mongoose from "mongoose"; import { FileModel, File } from "../../db/models/file"; export class FileService { @@ -21,7 +20,7 @@ export class FileService { size: number ): Promise { const fileRecord = new FileModel({ - userId: new mongoose.Types.ObjectId(userId), + userId, objectKey, originalName, mimeType, @@ -33,9 +32,7 @@ export class FileService { } async getFilesByUserId(userId: string): Promise { - return FileModel.find({ userId: new mongoose.Types.ObjectId(userId) }) - .sort({ uploadedAt: -1 }) - .exec(); + return FileModel.find({ userId }).sort({ uploadedAt: -1 }).exec(); } async getFileByObjectKey(objectKey: string): Promise { @@ -48,12 +45,17 @@ export class FileService { } async isFileDuplicate(originalName: string, userId: string): Promise { - const count = await FileModel.countDocuments({ - userId: new mongoose.Types.ObjectId(userId), - originalName: originalName, - }); + try { + const count = await FileModel.countDocuments({ + userId, + originalName: originalName, + }); - return count > 0; + return count > 0; + } catch (error) { + console.error(`Error in isFileDuplicate: ${error}`); + return false; + } } async updateObjectKey(fileId: string, objectKey: string): Promise {