From 04a37d19b7849ab3ff87c58f26e56eeb62506008 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 14 Oct 2024 11:53:54 +0200 Subject: [PATCH] Add file saving functionality and update group ID handling Implemented the save method to store files and associate them with machines, including handling optional group associations. Modified the database schema to allow null as a default for group_id and added logic to update group ID after file insertion. --- apps/backend/src/app/db/schema.ts | 2 +- apps/backend/src/app/files/files.service.ts | 25 ++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/backend/src/app/db/schema.ts b/apps/backend/src/app/db/schema.ts index f3b1a90..50cace2 100644 --- a/apps/backend/src/app/db/schema.ts +++ b/apps/backend/src/app/db/schema.ts @@ -73,7 +73,7 @@ export const FilesTable = pgTable("files", { }) .notNull(), - groupId: p.uuid("group_id").references(() => FilesGroupTable.uuid), + groupId: p.uuid("group_id").default(null).references(() => FilesGroupTable.uuid), fileSize: p.integer("file_size").notNull(), diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 7ec3265..bb73536 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -23,9 +23,6 @@ export class FilesService { private readonly database: DbService, ) {} - //TODO - //TODO - /** * Retrieves a file from the database and storage by its unique identifier. * @@ -95,7 +92,14 @@ export class FilesService { } } - //TODO save a file + /** + * Saves a file and associates it with machines and an optional group in the database. + * + * @param {Buffer} file - The file data to be saved. + * @param {Map} data - A map containing file and association metadata. + * @throws {NotFoundException} If a machine or group specified in the data does not exist in the database. + * @return {Promise} The inserted file record. + */ public async save(file: Buffer, data: Map) { const _machineIds = data.get("machineId").toString().split(","); @@ -126,6 +130,7 @@ export class FilesService { /*if (!_group) { throw new NotFoundException(`Group with ID "${_group}" not found`); }*/ + // verify that the group exist in the database const groupExists = await this.database .use() @@ -157,7 +162,6 @@ export class FilesService { .insert(FilesTable) .values({ fileName: data.get("fileName") as string, - groupId: groupExists[0].uuid || null, checksum: saveResult.fileChecksum, extension: saveResult.fileType.extension, fileSize: saveResult.fileSize, @@ -167,10 +171,19 @@ export class FilesService { uploadedBy: data.get("uploadedBy") as string, }) .returning(); + if (groupExists[0].uuid) { + await this.database.use() + .update(FilesTable) + // @ts-ignore TODO FIX + .set({groupId: groupExists[0].uuid}) + .where(eq(FilesTable.uuid, inserted[0].uuid)) + .prepare("addGroupToFile") + .execute() + } + console.log(inserted); for (const machineId of machinesIds) { - //TODO insert a link betwen fileId and MachineIds[] console.log( `Append file ${inserted[0].fileName} for machine : "${machineId}"`, );