From 616e518a8b634d4d61827b5105d5924edae2e3b8 Mon Sep 17 00:00:00 2001 From: fabriceBJHost Date: Thu, 3 Jul 2025 09:02:15 +0200 Subject: [PATCH] push --- config/databases.js | 62 ++++++++++++++++++++ controllers/CompartimentController.js | 51 +++++++++++++++++ controllers/EmplacementController.js | 82 +++++++++++++++++++++++++++ controllers/FournisseurController.js | 41 ++++++++++++++ controllers/StockController.js | 58 +++++++++++++++++++ routes/protectedRoute.js | 14 +++++ 6 files changed, 308 insertions(+) create mode 100644 controllers/CompartimentController.js create mode 100644 controllers/EmplacementController.js create mode 100644 controllers/FournisseurController.js create mode 100644 controllers/StockController.js diff --git a/config/databases.js b/config/databases.js index ef3a6b5..f291fb0 100644 --- a/config/databases.js +++ b/config/databases.js @@ -28,6 +28,68 @@ async function initDB() { ); `); + // table fournisseurs + await connection.query(` + CREATE TABLE IF NOT EXISTS fournisseurs ( + id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + category VARCHAR(50) NOT NULL, + contact_person VARCHAR(255) DEFAULT NULL, + phone VARCHAR(20) DEFAULT NULL, + email VARCHAR(255) DEFAULT NULL, + status VARCHAR(20) DEFAULT 'active', + last_order_date DATE DEFAULT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + `); + + // Create emplacements table if it doesn't exist + await connection.query(` + CREATE TABLE IF NOT EXISTS emplacements ( + id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + type VARCHAR(50) NOT NULL, + temperature DECIMAL(5, 2) DEFAULT NULL, + capacity INT DEFAULT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + `); + + // table compartiments + await connection.query(` + CREATE TABLE IF NOT EXISTS compartiments ( + id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + capacity INT DEFAULT NULL, + uniter VARCHAR(50) NOT NULL DEFAULT 'kg', + id_emplacement BIGINT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (id_emplacement) REFERENCES emplacements(id) ON DELETE CASCADE + ); + `); + + // table stock + await connection.query(` + CREATE TABLE IF NOT EXISTS stocks ( + id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, + articles VARCHAR(255) NOT NULL, + quantity INT NOT NULL DEFAULT 0, + uniter VARCHAR(50) NOT NULL DEFAULT 'kg', + price_unit DECIMAL(10, 2) NOT NULL DEFAULT 0.00, + id_emplacement BIGINT NOT NULL, + id_compartiment BIGINT NOT NULL, + fournisseur_id BIGINT DEFAULT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (id_emplacement) REFERENCES emplacements(id) ON DELETE CASCADE, + FOREIGN KEY (fournisseur_id) REFERENCES fournisseurs(id) ON DELETE SET NULL, + FOREIGN KEY (id_compartiment) REFERENCES compartiments(id) ON DELETE CASCADE + ); + `); + // ajoute une autre table si necessaire // add a default admin user if none exists diff --git a/controllers/CompartimentController.js b/controllers/CompartimentController.js new file mode 100644 index 0000000..67d2613 --- /dev/null +++ b/controllers/CompartimentController.js @@ -0,0 +1,51 @@ +const { pool } = require('../config/databases'); + +exports.createCompartiment = async (req, res) => { + const {name, capacity, uniter, id_emplacement,} = req.body; + + if (!name) { + return res.status(400).json({ message: 'Name are required.' }); + } else if (!capacity) { + return res.status(400).json({ message: 'Capacity is required.' }); + } else if (!uniter) { + return res.status(400).json({ message: 'Uniter is required.' }); + } + + // verify if emplacement exists + try { + const [emplacement] = await pool.query('SELECT * FROM emplacements WHERE id = ?', [id_emplacement]); + + if (emplacement.length === 0) { + return res.status(404).json({ message: 'Emplacement not found.' }); + } + + // Insert compartiment + const [result] = await pool.query('INSERT INTO compartiments (name, capacity, uniter, id_emplacement) VALUES (?, ?, ?, ?)', [name, capacity, uniter, id_emplacement]); + + res.status(201).json({ + message: 'Compartiment created successfully', + compartiment: { + id: result.insertId, + name, + capacity, + uniter, + id_emplacement, + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + } + }); + } catch (error) { + console.error(error); + return res.status(500).json({ message: 'Server error while creating compartiment.' }); + } +} + +exports.getCompartiments = async (req, res) => { + try { + const [rows] = await pool.query('SELECT * FROM compartiments'); + res.json(rows); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching compartiments.' }); + } +} \ No newline at end of file diff --git a/controllers/EmplacementController.js b/controllers/EmplacementController.js new file mode 100644 index 0000000..9cfe9b6 --- /dev/null +++ b/controllers/EmplacementController.js @@ -0,0 +1,82 @@ +const { pool } = require('../config/databases'); + +exports.createEmplacement = async (req, res) => { + const { name, type, temperature, capacity } = req.body; + + if (!name || !type) { + return res.status(400).json({ message: 'Name and type are required.' }); + } else if (!temperature) { + return res.status(400).json({ message: 'Temperature is required.' }); + } else if (!capacity) { + return res.status(400).json({ message: 'Capacity is required.' }); + } + + try { + const [result] = await pool.query('INSERT INTO emplacements (name, type, temperature, capacity) VALUES (?, ?, ?, ?)', [name, type, temperature, capacity]); + + res.status(201).json({ + message: 'Emplacement created successfully', + emplacement: { + id: result.insertId, + name, + type, + temperature, + capacity, + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + } + }); + } catch (error) { + console.error(error); + return res.status(500).json({ message: 'Server error while creating emplacement.' }); + } +} + +exports.getEmplacements = async (req, res) => { + try { + const [rows] = await pool.query('SELECT * FROM emplacements'); + res.json(rows); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching emplacements.' }); + } +} + +exports.getEmplacementById = async (req, res) => { + const { id } = req.params; + + try { + const [rows] = await pool.query('SELECT * FROM emplacements WHERE id = ?', [id]); + + if (rows.length === 0) { + return res.status(404).json({ message: 'Emplacement not found.' }); + } + + res.json(rows[0]); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching emplacement.' }); + } +} + +exports.updateEmplacement = async (req, res) => { + const { id } = req.params; + const { name, type, temperature, capacity } = req.body; + + if (!name || !type || !temperature || !capacity) { + return res.status(400).json({ message: 'All fields are required.' }); + } + + try { + const [result] = await pool.query('UPDATE emplacements SET name = ?, type = ?, temperature = ?, capacity = ? WHERE id = ?', [name, type, temperature, capacity, id]); + + if (result.affectedRows === 0) { + return res.status(404).json({ message: 'Emplacement not found.' }); + } + + res.json({ message: 'Emplacement updated successfully' }); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while updating emplacement.' }); + } +} \ No newline at end of file diff --git a/controllers/FournisseurController.js b/controllers/FournisseurController.js new file mode 100644 index 0000000..0afcffa --- /dev/null +++ b/controllers/FournisseurController.js @@ -0,0 +1,41 @@ +const { pool } = require('../config/databases'); + +exports.createFournisseur = async (req, res) => { + const { name, category, contact_person, phone, email, status } = req.body; + + if (!name || !category || !contact_person || !phone || !email || !status) { + return res.status(400).json({ message: 'All fields are required.' }); + } + + try { + const [result] = await pool.query('INSERT INTO fournisseurs (name, category, contact_person, phone, email, status) VALUES(?, ?, ?, ?, ?, ?)', [name, category, contact_person, phone, email, status]); + + res.status(201).json({ + message: 'Fournisseur created successfully', + fournisseur: { + id: result.insertId, + name, + category, + contact_person, + phone, + email, + status, + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + } + }); + } catch (error) { + console.error(error); + return res.status(500).json({ message: 'Server error while creating ingredient.' }); + } +} + +exports.getFournisseurs = async (req, res) => { + try { + const [rows] = await pool.query('SELECT * FROM fournisseurs'); + res.json(rows); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching fournisseurs.' }); + } +} \ No newline at end of file diff --git a/controllers/StockController.js b/controllers/StockController.js new file mode 100644 index 0000000..e08ddfc --- /dev/null +++ b/controllers/StockController.js @@ -0,0 +1,58 @@ +const { pool } = require('../config/databases'); + +exports.createIngredient = async (req, res) => { + const { articles, quantity, uniter, price_unit, id_emplacement, id_compartiment } = req.body; + + if (!articles || !quantity || !uniter || !price_unit) { + return res.status(400).json({ message: 'All fields are required.' }); + } + + // verify if compartiment exists + try { + const [compartiment] = await pool.query('SELECT * FROM compartiments WHERE id = ?', [id_compartiment]); + + if (compartiment.length === 0) { + return res.status(404).json({ message: 'Compartiment not found.' }); + } + + // Insert ingredient + const [result] = await pool.query('INSERT INTO stocks (articles, quantity, uniter, price_unit, id_emplacement, id_compartiment) VALUES (?, ?, ?, ?, ?, ?)', [articles, quantity, uniter, price_unit, id_emplacement, id_compartiment]); + + res.status(201).json({ + message: 'Ingredient created successfully', + ingredient: { + id: result.insertId, + articles, + quantity, + uniter, + price_unit, + id_compartiment, + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + } + }); + } catch (error) { + console.error(error); + return res.status(500).json({ message: 'Server error while creating ingredient.' }); + } +} + +exports.getIngredientsInventaire = async (req, res) => { + try { + const [rows] = await pool.query('SELECT emplacements.name, stocks.id as stock_id, stocks.articles, stocks.uniter, stocks.quantity, stocks.id_emplacement, stocks.id_compartiment FROM emplacements JOIN stocks ON emplacements.id = stocks.id_emplacement'); + res.json(rows); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching ingredients.' }); + } +} + +exports.getIngredientsEmplacements = async (req, res) => { + try { + const [rows] = await pool.query('SELECT emplacements.name AS emplacement_name, emplacements.id AS emplacement_id, emplacements.type AS emplacement_type, emplacements.temperature, emplacements.capacity AS emplacement_capacity, compartiments.id AS compartiment_id, compartiments.name AS compartiment_name, compartiments.capacity AS compartiment_capacity, compartiments.uniter AS compartiment_uniter, stocks.id AS stock_id, stocks.articles, stocks.quantity, stocks.uniter AS stock_uniter, stocks.price_unit FROM emplacements JOIN compartiments ON emplacements.id = compartiments.id_emplacement JOIN stocks ON compartiments.id = stocks.id_compartiment'); + res.json(rows); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Server error while fetching ingredients.' }); + } +} \ No newline at end of file diff --git a/routes/protectedRoute.js b/routes/protectedRoute.js index 206462a..1bd71e2 100644 --- a/routes/protectedRoute.js +++ b/routes/protectedRoute.js @@ -1,12 +1,26 @@ const express = require('express'); const authMiddleware = require('../middleware/authMiddleware'); const userController = require('../controllers/UserController'); +const emplacementController = require('../controllers/EmplacementController'); +const compartimentController = require('../controllers/CompartimentController'); +const stockController = require('../controllers/StockController'); +const fournisseurController = require('../controllers/FournisseurController'); const router = express.Router(); router.get('/profile', authMiddleware(), userController.getProfile); router.get('/admin', authMiddleware('admin'), userController.getAdminPage); router.post('/create', authMiddleware('admin'), userController.createUser); +router.post('/create/emplacement', authMiddleware(), emplacementController.createEmplacement); +router.get('/emplacements', authMiddleware(), emplacementController.getEmplacements); +router.get('/emplacement/:id', authMiddleware(), emplacementController.getEmplacementById); +router.get('/compartiments', authMiddleware(), compartimentController.getCompartiments); +router.post('/create/compartiment', authMiddleware(), compartimentController.createCompartiment); +router.post('/create/ingredient', authMiddleware(), stockController.createIngredient); +router.get('/ingredients/inventaire', authMiddleware(), stockController.getIngredientsInventaire); +router.get('/ingredients/emplacement', authMiddleware(), stockController.getIngredientsEmplacements); +router.post('/create/fournisseur', authMiddleware(), fournisseurController.createFournisseur); +router.get('/fournisseurs', authMiddleware(), fournisseurController.getFournisseurs); module.exports = router;