Refactor la gestion des mesures en remplaçant le gestionnaire de mesures par le gestionnaire de stockage. Ajouter des fonctions pour gérer les images et les chemins des mesures. Améliorer la gestion des erreurs et nettoyer le code.

This commit is contained in:
2025-04-03 11:03:10 +02:00
parent c3b2059428
commit 6077dfd716
6 changed files with 124 additions and 199 deletions

View File

@@ -1,6 +1,7 @@
const fs = require('fs').promises;
const path = require('path');
const PROJECTS_DIR = path.join('.');
const database_manager = require('../database/database_manager.js');
async function createFolder(name) {
const projectDir = path.join(PROJECTS_DIR, `${name}`);
@@ -89,22 +90,79 @@ async function deleteFile(name) {
}
}
async function handleFileOperation(operation, ...args) {
try {
return await operation(...args);
} catch (error) {
console.error(`[FILE OPERATION ERROR] ${error.message}`);
throw error;
}
}
const project = {
createProjectDirectory: async function (projectId) {
const projectPath = `${projectId}`;
await createFolder(projectPath);
await createFolder(`${projectPath}/images`);
await createFolder(`${projectPath}/videos`);
await handleFileOperation(createFolder, projectPath);
await handleFileOperation(createFolder, `${projectPath}/images`);
await handleFileOperation(createFolder, `${projectPath}/videos`);
console.log("[FILE] createProjectDirectory : " + projectPath);
},
deleteProjectDirectory: async function (projectId) {
const projectPath = `${projectId}`;
await deleteFolder(projectPath);
await handleFileOperation(deleteFolder, projectPath);
console.log("[FILE] deleteProjectDirectory : " + projectPath);
}
};
const measurement = {
get_measurement_image: async function (projectId, orderId) {
const projectPath = `${projectId}`;
const imagePath = `${projectPath}/images/${orderId}.jpg`;
console.log("[FILE] get_measurement_image : " + imagePath);
return await handleFileOperation(getFile, imagePath);
},
upload_measurement_image: async function (image, projectId, orderId) {
const projectPath = `${projectId}`;
const imagePath = `${projectPath}/images/${orderId}.jpg`;
console.log("[FILE] upload_measurement_image : " + imagePath);
await handleFileOperation(saveFile, imagePath, image.buffer);
return imagePath;
},
get_path_from_id: async function (projectId, orderId) {
const query = database_manager.measurement.get_measurement_by_project_and_order_id(projectId, orderId);
return query.path;
},
get_path_list: async function (IdList, projectId) {
let parsedIdList;
try {
parsedIdList = JSON.parse(IdList);
} catch (e) {
console.error("Error parsing IdList:", e);
return [];
}
const pathList = [];
for (const orderId of parsedIdList) {
const path = await this.get_path_from_id(projectId, orderId);
pathList.push(path);
}
return pathList;
},
}
const video = {
get_video: async function (projectId, orderId) {
const projectPath = `${projectId}`;
const videoPath = `${projectPath}/videos/${orderId}.mp4`;
console.log("[FILE] get_video : " + videoPath);
return await handleFileOperation(getFile, videoPath);
}
}
module.exports = {
createFolder,
deleteFolder,
@@ -112,5 +170,7 @@ module.exports = {
saveFile,
getFile,
deleteFile,
project
project,
measurement,
video
};

View File

@@ -190,9 +190,23 @@ const measurement = {
const query = `UPDATE measurements SET ${fields} WHERE id = $1 RETURNING *;`;
return (await db.query(query, values)).rows[0];
}),
edit_measurement_by_project_and_order_id: handleDatabaseOperation(async (project_id, order_id, updates) => {
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 3}`).join(', ');
const values = [project_id, order_id, ...Object.values(updates)];
const query = `UPDATE measurements SET ${fields} WHERE project_id = $1 AND order_id = $2 RETURNING *;`;
return (await db.query(query, values)).rows[0];
}),
delete_measurement_by_id: handleDatabaseOperation(async (id) => {
const query = `DELETE FROM measurements WHERE id = $1;`;
await db.query(query, [id]);
}),
get_next_order_id: handleDatabaseOperation(async (project_id) => {
const query = `SELECT COALESCE(MAX(order_id), 0) + 1 AS next_order_id FROM measurements WHERE project_id = $1;`;
const result = await db.query(query, [project_id]);
return result.rows[0].next_order_id;
})
};

View File

@@ -1,130 +0,0 @@
const db = require('../../db.js');
const path = require('path');
const storage_manager = require('../data/storage_manager.js');
async function uploadMeasureImage(image, projectId, orderId) {
try {
// Ensure that folder creation and file saving are awaited
const projectDir = await storage_manager.createFolder('./storage/' + projectId.toString());
const imagesDir = await storage_manager.createFolder(path.join(projectDir, 'images'));
const imagePath = path.join(imagesDir, `${orderId}.jpg`);
// Save the file and await completion
await storage_manager.saveFile(imagePath, image.buffer);
console.log("[FILE] uploadMeasureImage - Image saved to: " + imagePath);
return imagePath;
} catch (error) {
console.error('Error in uploadMeasureImage:', error);
throw error;
}
}
async function getMeasureImage(projectId, orderId) {
const projectPath = `${projectId}`;
const imagePath = `${projectPath}/${orderId}.jpg`;
console.log("[FILE] getMeasureImage - Image path: " + imagePath);
return storage_manager.getFile(imagePath);
}
async function getNextOrderId(projectId) {
const query = 'SELECT MAX(order_id) FROM public.measurements WHERE project_id = $1';
const values = [projectId];
const res = await db.query(query, values);
console.log("[DB] getNextOrderId - Max order_id: " + res.rows[0].max);
return res.rows[0].max + 1;
}
async function addMeasureToProject(projectId, orderId, timestamp, path, temperature, humidity) {
const query = 'INSERT INTO public.measurements (project_id, timestamp, path, temperature, humidity, order_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *';
const values = [projectId, orderId, timestamp, path, temperature, humidity];
const res = await db.query(query, values);
return res.rows[0];
}
async function getMeasurements(projectId) {
const query = 'SELECT * FROM public.measurements WHERE project_id = $1';
const values = [projectId];
const res = await db.query(query, values);
return res.rows;
}
async function getMeasurement(projectId, orderId) {
const query = 'SELECT * FROM public.measurements WHERE project_id = $1 AND order_id = $2';
const values = [projectId, orderId];
const res = await db.query(query, values);
return res.rows[0];
}
async function getMeasurementById(id) {
const query = 'SELECT * FROM public.measurements WHERE id = $1';
const values = [id];
const res = await db.query(query, values);
return res.rows[0];
}
async function updateMeasurement(projectId, orderId, timestamp, path, temperature, humidity) {
const query = 'UPDATE public.measurements SET timestamp = $3, path = $4, temperature = $5, humidity = $6 WHERE project_id = $1 AND order_id = $2 RETURNING *';
const values = [projectId, orderId, timestamp, path, temperature, humidity];
const res = await db.query(query, values);
return res.rows[0];
}
async function updateMeasurementById(id, timestamp, path, temperature, humidity) {
const query = 'UPDATE public.measurements SET timestamp = $2, path = $3, temperature = $4, humidity = $5 WHERE id = $1 RETURNING *';
const values = [id, timestamp, path, temperature, humidity];
const res = await db.query(query, values);
return res.rows[0];
}
async function deleteMeasurement(id) {
const query = 'DELETE FROM public.measurements WHERE id = $1';
const values = [id];
const res = await db.query(query, values);
return res.rows[0];
}
async function getPathFromIds(projectId, orderId) {
console.log("Getting path from ids:", projectId, orderId);
const query = 'SELECT path FROM public.measurements WHERE project_id = $1 AND order_id = $2';
const values = [projectId, orderId];
const res = await db.query(query, values);
return res.rows[0].path;
}
async function getPathList(IdList, projectId) {
// Convertir la chaîne de caractères en tableau
let parsedIdList;
try {
parsedIdList = JSON.parse(IdList);
} catch (e) {
console.error("Erreur lors de la conversion de la chaîne en tableau:", e);
return [];
}
console.log(parsedIdList);
const pathList = [];
for (const orderId of parsedIdList) {
console.log(orderId);
const path = await getPathFromIds(projectId, orderId);
console.log(path);
pathList.push(path);
}
return pathList;
}
module.exports = {
uploadMeasureImage,
addMeasureToProject,
getNextOrderId,
getMeasurements,
getMeasurement,
updateMeasurement,
deleteMeasurement,
getMeasureImage,
getMeasurementById,
updateMeasurementById,
getPathFromIds,
getPathList
};