refactor file model to use UUID for userId and update file service methods accordingly

This commit is contained in:
StarAppeal
2025-09-26 05:04:19 +02:00
parent cc66b80589
commit b6f6b3c3d7
2 changed files with 14 additions and 13 deletions
+2 -3
View File
@@ -2,7 +2,7 @@ import mongoose from "mongoose";
export interface File { export interface File {
_id: mongoose.Types.ObjectId; _id: mongoose.Types.ObjectId;
userId: mongoose.Types.ObjectId; userId: string; // UUID des Benutzers statt MongoDB ObjectId
objectKey: string; objectKey: string;
originalName: string; originalName: string;
mimeType: string; mimeType: string;
@@ -13,8 +13,7 @@ export interface File {
const fileSchema = new mongoose.Schema<File>( const fileSchema = new mongoose.Schema<File>(
{ {
userId: { userId: {
type: mongoose.Schema.Types.ObjectId, type: String,
ref: "User",
required: true, required: true,
index: true, index: true,
}, },
+12 -10
View File
@@ -1,4 +1,3 @@
import mongoose from "mongoose";
import { FileModel, File } from "../../db/models/file"; import { FileModel, File } from "../../db/models/file";
export class FileService { export class FileService {
@@ -21,7 +20,7 @@ export class FileService {
size: number size: number
): Promise<File> { ): Promise<File> {
const fileRecord = new FileModel({ const fileRecord = new FileModel({
userId: new mongoose.Types.ObjectId(userId), userId,
objectKey, objectKey,
originalName, originalName,
mimeType, mimeType,
@@ -33,9 +32,7 @@ export class FileService {
} }
async getFilesByUserId(userId: string): Promise<File[]> { async getFilesByUserId(userId: string): Promise<File[]> {
return FileModel.find({ userId: new mongoose.Types.ObjectId(userId) }) return FileModel.find({ userId }).sort({ uploadedAt: -1 }).exec();
.sort({ uploadedAt: -1 })
.exec();
} }
async getFileByObjectKey(objectKey: string): Promise<File | null> { async getFileByObjectKey(objectKey: string): Promise<File | null> {
@@ -48,12 +45,17 @@ export class FileService {
} }
async isFileDuplicate(originalName: string, userId: string): Promise<boolean> { async isFileDuplicate(originalName: string, userId: string): Promise<boolean> {
const count = await FileModel.countDocuments({ try {
userId: new mongoose.Types.ObjectId(userId), const count = await FileModel.countDocuments({
originalName: originalName, 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<File | null> { async updateObjectKey(fileId: string, objectKey: string): Promise<File | null> {