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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-14 11:53:54 +02:00
parent 30c9c28e3d
commit 04a37d19b7
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 20 additions and 7 deletions

View File

@ -73,7 +73,7 @@ export const FilesTable = pgTable("files", {
}) })
.notNull(), .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(), fileSize: p.integer("file_size").notNull(),

View File

@ -23,9 +23,6 @@ export class FilesService {
private readonly database: DbService, private readonly database: DbService,
) {} ) {}
//TODO
//TODO
/** /**
* Retrieves a file from the database and storage by its unique identifier. * 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<string, unknown>} 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<Object>} The inserted file record.
*/
public async save(file: Buffer, data: Map<string, unknown>) { public async save(file: Buffer, data: Map<string, unknown>) {
const _machineIds = data.get("machineId").toString().split(","); const _machineIds = data.get("machineId").toString().split(",");
@ -126,6 +130,7 @@ export class FilesService {
/*if (!_group) { /*if (!_group) {
throw new NotFoundException(`Group with ID "${_group}" not found`); throw new NotFoundException(`Group with ID "${_group}" not found`);
}*/ }*/
// verify that the group exist in the database // verify that the group exist in the database
const groupExists = await this.database const groupExists = await this.database
.use() .use()
@ -157,7 +162,6 @@ export class FilesService {
.insert(FilesTable) .insert(FilesTable)
.values({ .values({
fileName: data.get("fileName") as string, fileName: data.get("fileName") as string,
groupId: groupExists[0].uuid || null,
checksum: saveResult.fileChecksum, checksum: saveResult.fileChecksum,
extension: saveResult.fileType.extension, extension: saveResult.fileType.extension,
fileSize: saveResult.fileSize, fileSize: saveResult.fileSize,
@ -167,10 +171,19 @@ export class FilesService {
uploadedBy: data.get("uploadedBy") as string, uploadedBy: data.get("uploadedBy") as string,
}) })
.returning(); .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); console.log(inserted);
for (const machineId of machinesIds) { for (const machineId of machinesIds) {
//TODO insert a link betwen fileId and MachineIds[]
console.log( console.log(
`Append file ${inserted[0].fileName} for machine : "${machineId}"`, `Append file ${inserted[0].fileName} for machine : "${machineId}"`,
); );