Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ea1e1335e3 | |||
| 323df01afe | |||
| 854b0a9bb0 | |||
| f8f6cfb102 | |||
| 9bc4e88e46 | |||
| f56615451e | |||
| 845b6e91d2 | |||
| 93c102012b | |||
| 6ee63ed29c | |||
| c35e633568 | |||
| 4284142b1e | |||
| 32f1718a8c | |||
| a59deb19a9 | |||
| 0bbc77ffc8 | |||
| 19d3dcef69 | |||
| 32a279e0ae | |||
| 7258509528 | |||
| 7fc28f4374 | |||
| af5ecaeee1 | |||
| eac103491f | |||
| 0551f0b9c1 | |||
| 89ab3673c4 | |||
| cf13b4a986 | |||
| 3f80d9318b | |||
| 84c882ac0b | |||
| ecf4a5cede | |||
| 6737ad80e4 | |||
| afa2c35c90 | |||
| 36b420639d | |||
| 9bd4b29967 |
@@ -1,3 +1,4 @@
|
||||
test_audio_tts.js,1772996026925,be4d2d713c256578bc16646116e3e81fc2627a1d89e45b211318b51e3612f259
|
||||
manifest.json,1766235870190,1fb17c7a1d11e0160d9ffe48e4e4f7fb5028d23477915a17ca496083050946e2
|
||||
flutter.js,1759914809272,d9a92a27a30723981b176a08293dedbe86c080fcc08e0128e5f8a01ce1d3fcb4
|
||||
favicon.png,1766235850956,3cf717d02cd8014f223307dee1bde538442eb9de23568e649fd8aae686dc9db0
|
||||
@@ -22,8 +23,8 @@ assets/packages/flutter_dropzone_web/assets/flutter_dropzone.js,1748366257688,d6
|
||||
assets/assets/Google__G__logo.svg,1741027482182,b365d560438f8f04caf08ffaa5d8995eff6c09b36b4483f44d6f5f82d8559d4f
|
||||
assets/assets/google.png,1741029771653,537ca60ffa74c28eca4e62d249237403a7d47d2bc90bb90431b8d5aa923a92ee
|
||||
assets/assets/EM2_NsurB.jpg,1741031103452,687267bbd4e1a663ffc1d2256c34969dd424cbaaf503b530db529a345570ddcd
|
||||
assets/assets/sounds/ok.mp3,1771938119844,cb452794752fa5e7622b2bd9413e9245464788be3f88cc838a7c9716f87f82a3
|
||||
assets/assets/sounds/error.mp3,1771938125144,5e1974fa40050421304357c75e834ab5f7c8ba7a61acfbb5885ed913afc0fc0b
|
||||
assets/assets/sounds/ok.mp3,1772996026461,cb452794752fa5e7622b2bd9413e9245464788be3f88cc838a7c9716f87f82a3
|
||||
assets/assets/sounds/error.mp3,1772996026458,5e1974fa40050421304357c75e834ab5f7c8ba7a61acfbb5885ed913afc0fc0b
|
||||
assets/assets/logos/SquareLogoWhite.png,1760462340000,786ce2571303bb96dfae1fba5faaab57a9142468fa29ad73ab6b3c1f75be3703
|
||||
assets/assets/logos/SquareLogoBlack.png,1760462340000,b4425fae1dbd25ce7c218c602d530f75d85e0eb444746b48b09b5028ed88bbd1
|
||||
assets/assets/logos/RectangleLogoWhite.png,1760462340000,1f6df22df6560a2dae2d42cf6e29f01e6df4002f1a9c20a8499923d74b02115c
|
||||
@@ -33,17 +34,16 @@ assets/assets/images/tshirt-incrust.webp,1737393735487,af7cb34adfca19c0b41c8eb63
|
||||
assets/assets/icons/truss.svg,1761734811263,8ddfbbb4f96de5614348eb23fa55f61b2eb1edb064719a8bbd791c35883ec4cc
|
||||
assets/assets/icons/tape.svg,1761734809221,631183f0ff972aa4dc3f9f51dc7abd41a607df749d1f9a44fa7e77202d95ccde
|
||||
assets/assets/icons/flight-case.svg,1761734822495,0cef47fdf5d7efdd110763c32f792ef9735df35c4f42ae7d02d5fbda40e6148d
|
||||
version.json,1772532792027,2b3f91e827bc27a1901342a048b1bd81d0aabc50935717f9851e1a3ad6cb7411
|
||||
test_audio_tts.js,1772532705302,d7b70556456d3b5e7832506b2dafe31480d94db8d0027b89c1633cc9b5c5bdae
|
||||
index.html,1772532797157,4e8c00552c71ef134bead8bc03706952e7a415d70fca602a3839dc02a3f7ae10
|
||||
flutter_bootstrap.js,1772532797146,ca3df8691f4db5962ed165489bd051dfd31307628ab4f1ee68842dc747d39fd9
|
||||
flutter_service_worker.js,1772532894886,9ce6b8d9f09c957b763a8d3db3baf03c96d4f84e805f6d629294749d9966cfad
|
||||
assets/FontManifest.json,1772532889954,e38b95988f5d060cf9b7ce97cb5ac9236d6f4cc04a11d69567df97b2b4cbc5e5
|
||||
assets/AssetManifest.json,1772532889954,0e35e7214421c832bf41b0af7c03037e66fee508b857d3143f40f6862e454dd6
|
||||
assets/AssetManifest.bin.json,1772532889954,3a244f5f866d93c17f420cc01b1ba318584b4da92af9512d9ba4acd099b49d53
|
||||
assets/AssetManifest.bin,1772532889954,205908d2fcf1ca9708b7d1f91ec7ea80c5f07eaf6cfc1458cb9364a4d9106907
|
||||
assets/shaders/ink_sparkle.frag,1772532890224,591c7517d5cb43eb91ea451e0d3f9f585cbf8298cf6c46a9144b77cb0775a406
|
||||
assets/packages/cupertino_icons/assets/CupertinoIcons.ttf,1772532893514,d41473de1f7708a0702d7f19327693486512db442f6ab0cf7774e6d6576f9fcb
|
||||
assets/fonts/MaterialIcons-Regular.otf,1772532893530,71c7128cf890cf3e18fffca405a98480f174bb3fa79d20c575b473d36c8c3093
|
||||
assets/NOTICES,1772532889955,8479783d331c9ff6d2b2e2e0a4b1705eda46ab0000b7753779fb98526ae54d74
|
||||
main.dart.js,1772532888607,df89975075062e0983691b8997b9e4a1ae4b4d5dfe6c06ca5b42ffa5407fdd3f
|
||||
version.json,1779800968600,00f600f01984c1e371af870e40f78fd44ba53d05596f2f92f9b4fc56a85f52b6
|
||||
index.html,1779800974065,4e8c00552c71ef134bead8bc03706952e7a415d70fca602a3839dc02a3f7ae10
|
||||
flutter_service_worker.js,1779801065196,879f42a05578f24ea45dc23326fdda6246d38dc59de0824ef8d4edfa4715e571
|
||||
flutter_bootstrap.js,1779800974054,977a20d5caac8da21af648cae8fa7dba00a5cd959fd2fafa7ef538a012fe87c3
|
||||
assets/FontManifest.json,1779801061892,e38b95988f5d060cf9b7ce97cb5ac9236d6f4cc04a11d69567df97b2b4cbc5e5
|
||||
assets/AssetManifest.json,1779801061891,0e35e7214421c832bf41b0af7c03037e66fee508b857d3143f40f6862e454dd6
|
||||
assets/AssetManifest.bin.json,1779801061892,3a244f5f866d93c17f420cc01b1ba318584b4da92af9512d9ba4acd099b49d53
|
||||
assets/AssetManifest.bin,1779801061891,205908d2fcf1ca9708b7d1f91ec7ea80c5f07eaf6cfc1458cb9364a4d9106907
|
||||
assets/packages/cupertino_icons/assets/CupertinoIcons.ttf,1779801064441,d41473de1f7708a0702d7f19327693486512db442f6ab0cf7774e6d6576f9fcb
|
||||
assets/shaders/ink_sparkle.frag,1779801062097,591c7517d5cb43eb91ea451e0d3f9f585cbf8298cf6c46a9144b77cb0775a406
|
||||
assets/fonts/MaterialIcons-Regular.otf,1779801064446,710dc8fc35289048b52970355f64206fb1b2c5e67c71ae77a46b53f0e2daecd6
|
||||
assets/NOTICES,1779801061893,5522e1307c65771d1fbf26fcd9dc0548c751413f42196c4acaba5ee674eede1e
|
||||
main.dart.js,1779801060964,e87fb4dfca93c3384b5cb63d627186e48b0c15d78ed63bc7f5e61544ef292dd9
|
||||
|
||||
@@ -45,3 +45,6 @@ app.*.map.json
|
||||
# Environment configuration with credentials
|
||||
lib/config/env.dev.dart
|
||||
functions/.env
|
||||
.env
|
||||
env.dart
|
||||
functions/.env.local
|
||||
|
||||
@@ -2,6 +2,35 @@
|
||||
|
||||
Toutes les modifications notables de ce projet seront documentées dans ce fichier.
|
||||
|
||||
## 26/05/2026
|
||||
Optimisation des perfomance de l'application, amélioration de la gestion des données et refonte visuelle de la page de gestion des équipements.
|
||||
|
||||
## 25/05/2026
|
||||
Ajout d'un assistant IA pour la gestion des équipments dans un événement. Il permet de suggérer des equipements selon les informations qui lui sont données (copier un événement similaire, lire un devis, etc.) et de faire des recommandations pour optimiser la préparation d'un événement.
|
||||
|
||||
## 04/05/2026
|
||||
Optimisation du lancement de l'application et amélioration de la gestion du cache.
|
||||
|
||||
## 22/04/2026
|
||||
Ajout de la recherche d'événements et gestion avancée de la suppression d'équipement
|
||||
|
||||
## 30/03/2026
|
||||
Patch bug envoi d'alerte equipement perdu, date dans les alertes, recherche par ID d'équipement.
|
||||
|
||||
## 24/03/2026
|
||||
Fix BUG : Problème de cache avec les équipements non affichés dans le dialog de sélection d'équipement. Amélioration de la gestion du cache pour éviter les problèmes d'affichage.
|
||||
|
||||
## 12/03/2026bis
|
||||
Fix BUG : Ajout equipement à un evenement existant, boutons de modification de statut d'un evenement ne fonctionnaient pas. Refonte legere de la page calendrier.
|
||||
|
||||
## 12/03/2026
|
||||
Ajout d'une page de statistiques détaillées pour les équipements et les événements.
|
||||
|
||||
## 10/03/2026
|
||||
Migration vers Google Cloud TTS exclusif pour une compatibilité maximale sur tous les navigateurs. Suppression du TTS local (Web Speech API) qui causait des problèmes de compatibilité sur certaines configurations (notamment Chromium/Linux).
|
||||
|
||||
Ajout d'un service de synthèse vocale hybride et intégration de Google Cloud TTS. Résolution bug d'affichage des événements pour les membres CREW
|
||||
|
||||
## 24/02/2026
|
||||
Ajout de la gestion des maintenance et synthèse vocale
|
||||
|
||||
|
||||
@@ -7,3 +7,4 @@ SMTP_PASS="aL8@Rx8xqFrNij$a"
|
||||
# URL de l'application
|
||||
APP_URL="https://app.em2events.fr"
|
||||
|
||||
GEMINI_API_KEY="AIzaSyB0hOvBjWeWjdrxVARzfErZ_uGuArlvmQc"
|
||||
|
||||
@@ -4,7 +4,7 @@ module.exports = {
|
||||
node: true,
|
||||
},
|
||||
parserOptions: {
|
||||
"ecmaVersion": 2018,
|
||||
"ecmaVersion": 2020,
|
||||
},
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
@@ -14,6 +14,14 @@ module.exports = {
|
||||
"no-restricted-globals": ["error", "name", "length"],
|
||||
"prefer-arrow-callback": "error",
|
||||
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
|
||||
"max-len": "off",
|
||||
"valid-jsdoc": "off",
|
||||
"require-jsdoc": "off",
|
||||
"guard-for-in": "off",
|
||||
"no-unused-vars": "warn",
|
||||
"brace-style": "off",
|
||||
"object-curly-spacing": "off",
|
||||
"arrow-parens": "off",
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
|
||||
@@ -2,3 +2,4 @@ node_modules/
|
||||
*.local
|
||||
.env
|
||||
.env.local
|
||||
serviceAccountKey.json
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,34 +1,34 @@
|
||||
const {onRequest} = require('firebase-functions/v2/https');
|
||||
const admin = require('firebase-admin');
|
||||
const nodemailer = require('nodemailer');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require('./utils/emailConfig');
|
||||
const {renderTemplate, getEmailSubject, getAlertTitle, prepareTemplateData, checkAlertPreference} = require('./utils/emailTemplates');
|
||||
const auth = require('./utils/auth');
|
||||
const {onRequest} = require("firebase-functions/v2/https");
|
||||
const admin = require("firebase-admin");
|
||||
const nodemailer = require("nodemailer");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require("./utils/emailConfig");
|
||||
const {renderTemplate, getEmailSubject, getAlertTitle, prepareTemplateData, checkAlertPreference} = require("./utils/emailTemplates");
|
||||
const auth = require("./utils/auth");
|
||||
|
||||
// Configuration CORS
|
||||
const setCorsHeaders = (res, req) => {
|
||||
// Utiliser l'origin de la requête pour permettre les credentials
|
||||
const origin = req.headers.origin || '*';
|
||||
const origin = req.headers.origin || "*";
|
||||
|
||||
res.set('Access-Control-Allow-Origin', origin);
|
||||
res.set("Access-Control-Allow-Origin", origin);
|
||||
|
||||
// N'autoriser les credentials que si on a un origin spécifique (pas '*')
|
||||
if (origin !== '*') {
|
||||
res.set('Access-Control-Allow-Credentials', 'true');
|
||||
if (origin !== "*") {
|
||||
res.set("Access-Control-Allow-Credentials", "true");
|
||||
}
|
||||
|
||||
res.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.set('Access-Control-Allow-Headers', 'Authorization, Content-Type, Accept, Origin, X-Requested-With');
|
||||
res.set('Access-Control-Max-Age', '3600');
|
||||
res.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||||
res.set("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept, Origin, X-Requested-With");
|
||||
res.set("Access-Control-Max-Age", "3600");
|
||||
};
|
||||
|
||||
const withCors = (handler) => {
|
||||
return async (req, res) => {
|
||||
setCorsHeaders(res, req);
|
||||
// Gérer les requêtes preflight OPTIONS immédiatement
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.status(204).send('');
|
||||
if (req.method === "OPTIONS") {
|
||||
res.status(204).send("");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -48,8 +48,8 @@ const withCors = (handler) => {
|
||||
*/
|
||||
exports.createAlert = onRequest({
|
||||
cors: false,
|
||||
invoker: 'public',
|
||||
region: 'europe-west9'
|
||||
invoker: "public",
|
||||
region: "europe-west9",
|
||||
}, withCors(async (req, res) => {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
@@ -70,7 +70,7 @@ exports.createAlert = onRequest({
|
||||
|
||||
// Validation des données
|
||||
if (!type || !severity || !message) {
|
||||
res.status(400).json({error: 'type, severity et message sont requis'});
|
||||
res.status(400).json({error: "type, severity et message sont requis"});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -78,12 +78,12 @@ exports.createAlert = onRequest({
|
||||
const userIds = await determineTargetUsers(type, severity, eventId);
|
||||
|
||||
if (userIds.length === 0) {
|
||||
res.status(400).json({error: 'Aucun utilisateur à notifier'});
|
||||
res.status(400).json({error: "Aucun utilisateur à notifier"});
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Créer l'alerte dans Firestore
|
||||
const alertRef = admin.firestore().collection('alerts').doc();
|
||||
const alertRef = admin.firestore().collection("alerts").doc();
|
||||
const alertData = {
|
||||
id: alertRef.id,
|
||||
type,
|
||||
@@ -99,14 +99,14 @@ exports.createAlert = onRequest({
|
||||
createdBy: decodedToken.uid,
|
||||
isRead: false,
|
||||
emailSent: false,
|
||||
status: 'ACTIVE',
|
||||
status: "ACTIVE",
|
||||
};
|
||||
|
||||
await alertRef.set(alertData);
|
||||
|
||||
// 3. Envoyer les emails si alerte critique
|
||||
let emailResults = {};
|
||||
if (severity === 'CRITICAL') {
|
||||
if (severity === "CRITICAL") {
|
||||
emailResults = await sendAlertEmails(alertRef.id, alertData, userIds);
|
||||
|
||||
// Mettre à jour le statut d'envoi
|
||||
@@ -124,7 +124,7 @@ exports.createAlert = onRequest({
|
||||
emailsSent: Object.values(emailResults).filter((v) => v).length,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('[createAlert] Erreur:', error);
|
||||
logger.error("[createAlert] Erreur:", error);
|
||||
res.status(500).json({error: `Erreur lors de la création de l'alerte: ${error.message}`});
|
||||
}
|
||||
}));
|
||||
@@ -137,23 +137,23 @@ async function determineTargetUsers(alertType, severity, eventId) {
|
||||
const targetUserIds = new Set();
|
||||
|
||||
// 1. Récupérer TOUS les utilisateurs pour déterminer lesquels sont admins
|
||||
const allUsersSnapshot = await db.collection('users').get();
|
||||
const allUsersSnapshot = await db.collection("users").get();
|
||||
|
||||
allUsersSnapshot.forEach((doc) => {
|
||||
const user = doc.data();
|
||||
if (user.role) {
|
||||
// Le rôle peut être une référence Firestore ou une string
|
||||
let rolePath = '';
|
||||
if (typeof user.role === 'string') {
|
||||
let rolePath = "";
|
||||
if (typeof user.role === "string") {
|
||||
rolePath = user.role;
|
||||
} else if (user.role.path) {
|
||||
rolePath = user.role.path;
|
||||
} else if (user.role._path && user.role._path.segments) {
|
||||
rolePath = user.role._path.segments.join('/');
|
||||
rolePath = user.role._path.segments.join("/");
|
||||
}
|
||||
|
||||
// Vérifier si c'est un admin (path = "roles/ADMIN")
|
||||
if (rolePath === 'roles/ADMIN' || rolePath === 'ADMIN') {
|
||||
if (rolePath === "roles/ADMIN" || rolePath === "ADMIN") {
|
||||
targetUserIds.add(doc.id);
|
||||
}
|
||||
}
|
||||
@@ -162,7 +162,7 @@ async function determineTargetUsers(alertType, severity, eventId) {
|
||||
// 2. Si un événement est lié, ajouter tous les membres de la workforce
|
||||
if (eventId) {
|
||||
try {
|
||||
const eventDoc = await db.collection('events').doc(eventId).get();
|
||||
const eventDoc = await db.collection("events").doc(eventId).get();
|
||||
|
||||
if (eventDoc.exists) {
|
||||
const event = eventDoc.data();
|
||||
@@ -177,7 +177,7 @@ async function determineTargetUsers(alertType, severity, eventId) {
|
||||
logger.warn(`[determineTargetUsers] Événement ${eventId} introuvable`);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('[determineTargetUsers] Erreur récupération événement:', error);
|
||||
logger.error("[determineTargetUsers] Erreur récupération événement:", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ async function sendSingleEmail(transporter, alertId, alertData, userId) {
|
||||
const db = admin.firestore();
|
||||
|
||||
// Récupérer l'utilisateur
|
||||
const userDoc = await db.collection('users').doc(userId).get();
|
||||
const userDoc = await db.collection("users").doc(userId).get();
|
||||
|
||||
if (!userDoc.exists) {
|
||||
return false;
|
||||
@@ -250,7 +250,7 @@ async function sendSingleEmail(transporter, alertId, alertData, userId) {
|
||||
const templateData = await prepareTemplateData(alertData, user);
|
||||
|
||||
// Rendre le template
|
||||
const html = await renderTemplate('alert-individual', templateData);
|
||||
const html = await renderTemplate("alert-individual", templateData);
|
||||
|
||||
// Envoyer l'email
|
||||
await transporter.sendMail({
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Cloud Function: generateTTS
|
||||
* Génère de l'audio TTS avec Google Cloud Text-to-Speech
|
||||
* Avec système de cache dans Firebase Storage
|
||||
*/
|
||||
|
||||
const textToSpeech = require("@google-cloud/text-to-speech");
|
||||
const crypto = require("crypto");
|
||||
const logger = require("firebase-functions/logger");
|
||||
|
||||
/**
|
||||
* Génère un hash MD5 pour le texte (utilisé comme clé de cache)
|
||||
* @param {string} text - Texte à hasher
|
||||
* @return {string} Hash MD5
|
||||
*/
|
||||
function generateCacheKey(text, voiceConfig = {}) {
|
||||
const cacheString = JSON.stringify({
|
||||
text,
|
||||
lang: voiceConfig.languageCode || "fr-FR",
|
||||
voice: voiceConfig.name || "fr-FR-Standard-B",
|
||||
});
|
||||
return crypto.createHash("md5").update(cacheString).digest("hex");
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère l'audio TTS et le stocke dans Firebase Storage
|
||||
* @param {string} text - Texte à synthétiser
|
||||
* @param {object} storage - Instance Firebase Storage
|
||||
* @param {object} bucket - Bucket Firebase Storage
|
||||
* @param {object} voiceConfig - Configuration de la voix (optionnel)
|
||||
* @return {Promise<{audioUrl: string, cached: boolean}>}
|
||||
*/
|
||||
async function generateTTS(text, storage, bucket, voiceConfig = {}) {
|
||||
try {
|
||||
// Validation du texte
|
||||
if (!text || text.trim().length === 0) {
|
||||
throw new Error("Text cannot be empty");
|
||||
}
|
||||
|
||||
if (text.length > 5000) {
|
||||
throw new Error("Text too long (max 5000 characters)");
|
||||
}
|
||||
|
||||
// Configuration par défaut de la voix
|
||||
const defaultVoiceConfig = {
|
||||
languageCode: "fr-FR",
|
||||
name: "fr-FR-Standard-B", // Voix masculine française (Standard = gratuit)
|
||||
ssmlGender: "MALE",
|
||||
};
|
||||
|
||||
const finalVoiceConfig = {...defaultVoiceConfig, ...voiceConfig};
|
||||
|
||||
// Générer la clé de cache
|
||||
const cacheKey = generateCacheKey(text, finalVoiceConfig);
|
||||
const fileName = `tts-cache/${cacheKey}.mp3`;
|
||||
const file = bucket.file(fileName);
|
||||
|
||||
// Vérifier si le fichier existe déjà dans le cache
|
||||
const [exists] = await file.exists();
|
||||
|
||||
if (exists) {
|
||||
logger.info("[generateTTS] ✓ Cache HIT", {cacheKey, text: text.substring(0, 50)});
|
||||
|
||||
// Générer une URL signée valide 7 jours
|
||||
const [url] = await file.getSignedUrl({
|
||||
action: "read",
|
||||
expires: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 jours
|
||||
});
|
||||
|
||||
return {
|
||||
audioUrl: url,
|
||||
cached: true,
|
||||
cacheKey,
|
||||
};
|
||||
}
|
||||
|
||||
logger.info("[generateTTS] ○ Cache MISS - Generating audio", {
|
||||
cacheKey,
|
||||
text: text.substring(0, 50),
|
||||
voice: finalVoiceConfig.name,
|
||||
});
|
||||
|
||||
// Créer le client Text-to-Speech
|
||||
const client = new textToSpeech.TextToSpeechClient();
|
||||
|
||||
// Configuration de la requête
|
||||
const request = {
|
||||
input: {text: text},
|
||||
voice: finalVoiceConfig,
|
||||
audioConfig: {
|
||||
audioEncoding: "MP3",
|
||||
speakingRate: 0.9, // Légèrement plus lent pour meilleure compréhension
|
||||
pitch: -2.0, // Voix un peu plus grave
|
||||
volumeGainDb: 0.0,
|
||||
},
|
||||
};
|
||||
|
||||
// Appeler l'API Google Cloud TTS
|
||||
const [response] = await client.synthesizeSpeech(request);
|
||||
|
||||
if (!response.audioContent) {
|
||||
throw new Error("No audio content returned from TTS API");
|
||||
}
|
||||
|
||||
logger.info("[generateTTS] ✓ Audio generated", {
|
||||
size: response.audioContent.length,
|
||||
});
|
||||
|
||||
// Sauvegarder dans Firebase Storage
|
||||
await file.save(response.audioContent, {
|
||||
metadata: {
|
||||
contentType: "audio/mpeg",
|
||||
metadata: {
|
||||
text: text.substring(0, 100), // Premier 100 caractères pour debug
|
||||
voice: finalVoiceConfig.name,
|
||||
generatedAt: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.info("[generateTTS] ✓ Audio cached", {fileName});
|
||||
|
||||
// Générer une URL signée
|
||||
const [url] = await file.getSignedUrl({
|
||||
action: "read",
|
||||
expires: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 jours
|
||||
});
|
||||
|
||||
return {
|
||||
audioUrl: url,
|
||||
cached: false,
|
||||
cacheKey,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error("[generateTTS] ✗ Error", {
|
||||
error: error.message,
|
||||
code: error.code,
|
||||
text: text?.substring(0, 50),
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {generateTTS, generateCacheKey};
|
||||
|
||||
+282
-3960
File diff suppressed because it is too large
Load Diff
@@ -2,17 +2,17 @@
|
||||
* Script de migration : Active les emails pour tous les utilisateurs existants
|
||||
* À exécuter une seule fois après le déploiement
|
||||
*/
|
||||
const admin = require('firebase-admin');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
|
||||
// AJOUTER CECI : Charger le fichier de clé
|
||||
const serviceAccount = require('./serviceAccountKey.json');
|
||||
const serviceAccount = require("./serviceAccountKey.json");
|
||||
|
||||
// Initialiser Firebase Admin avec les credentials explicites
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount), // <-- Utiliser la clé ici
|
||||
projectId: 'em2rp-951dc',
|
||||
projectId: "em2rp-951dc",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,11 +22,11 @@ const db = admin.firestore();
|
||||
* Active les notifications par email pour tous les utilisateurs existants
|
||||
*/
|
||||
async function migrateEmailPreferences() {
|
||||
console.log('=== DÉBUT MIGRATION EMAIL PREFERENCES ===\n');
|
||||
console.log("=== DÉBUT MIGRATION EMAIL PREFERENCES ===\n");
|
||||
|
||||
try {
|
||||
// 1. Récupérer tous les utilisateurs
|
||||
const usersSnapshot = await db.collection('users').get();
|
||||
const usersSnapshot = await db.collection("users").get();
|
||||
console.log(`✓ ${usersSnapshot.size} utilisateurs trouvés\n`);
|
||||
|
||||
// 2. Préparer les updates
|
||||
@@ -49,7 +49,7 @@ async function migrateEmailPreferences() {
|
||||
updates.push({
|
||||
ref: doc.ref,
|
||||
data: {
|
||||
'notificationPreferences.emailEnabled': true,
|
||||
"notificationPreferences.emailEnabled": true,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -83,7 +83,7 @@ async function migrateEmailPreferences() {
|
||||
console.log(`\n✓ Aucune mise à jour nécessaire\n`);
|
||||
}
|
||||
|
||||
console.log('=== FIN MIGRATION ===');
|
||||
console.log("=== FIN MIGRATION ===");
|
||||
return {
|
||||
success: true,
|
||||
total: usersSnapshot.size,
|
||||
@@ -91,7 +91,7 @@ async function migrateEmailPreferences() {
|
||||
updated: toUpdate,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('❌ ERREUR MIGRATION:', error);
|
||||
console.error("❌ ERREUR MIGRATION:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -99,15 +99,15 @@ async function migrateEmailPreferences() {
|
||||
// Exécuter la migration si appelé directement
|
||||
if (require.main === module) {
|
||||
migrateEmailPreferences()
|
||||
.then((result) => {
|
||||
console.log('\n✓ Migration réussie:', result);
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('\n❌ Migration échouée:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
.then((result) => {
|
||||
console.log("\n✓ Migration réussie:", result);
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("\n❌ Migration échouée:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { migrateEmailPreferences };
|
||||
module.exports = {migrateEmailPreferences};
|
||||
|
||||
|
||||
@@ -5,28 +5,28 @@
|
||||
* le champ 'id' avec la valeur du document ID si ce champ est manquant.
|
||||
*/
|
||||
|
||||
const admin = require('firebase-admin');
|
||||
const serviceAccount = require('./serviceAccountKey.json');
|
||||
const admin = require("firebase-admin");
|
||||
const serviceAccount = require("./serviceAccountKey.json");
|
||||
|
||||
// Initialiser Firebase Admin
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount)
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
});
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
async function migrateEquipmentIds() {
|
||||
console.log('🔧 Migration: Ajout du champ id aux équipements');
|
||||
console.log('================================================\n');
|
||||
console.log("🔧 Migration: Ajout du champ id aux équipements");
|
||||
console.log("================================================\n");
|
||||
|
||||
try {
|
||||
// Récupérer tous les équipements
|
||||
const equipmentsSnapshot = await db.collection('equipments').get();
|
||||
const equipmentsSnapshot = await db.collection("equipments").get();
|
||||
console.log(`📦 Total d'équipements: ${equipmentsSnapshot.size}`);
|
||||
|
||||
let missingIdCount = 0;
|
||||
let updatedCount = 0;
|
||||
let errorCount = 0;
|
||||
const errorCount = 0;
|
||||
const batch = db.batch();
|
||||
let batchCount = 0;
|
||||
|
||||
@@ -34,12 +34,12 @@ async function migrateEquipmentIds() {
|
||||
const data = doc.data();
|
||||
|
||||
// Vérifier si le champ 'id' est manquant ou vide
|
||||
if (!data.id || data.id === '') {
|
||||
if (!data.id || data.id === "") {
|
||||
missingIdCount++;
|
||||
console.log(`❌ Équipement ${doc.id} (${data.name || 'Sans nom'}) : champ 'id' manquant`);
|
||||
console.log(`❌ Équipement ${doc.id} (${data.name || "Sans nom"}) : champ 'id' manquant`);
|
||||
|
||||
// Ajouter au batch
|
||||
batch.update(doc.ref, { id: doc.id });
|
||||
batch.update(doc.ref, {id: doc.id});
|
||||
batchCount++;
|
||||
updatedCount++;
|
||||
|
||||
@@ -58,36 +58,35 @@ async function migrateEquipmentIds() {
|
||||
console.log(`✅ Batch final de ${batchCount} documents mis à jour`);
|
||||
}
|
||||
|
||||
console.log('\n================================================');
|
||||
console.log('📊 RÉSUMÉ DE LA MIGRATION');
|
||||
console.log('================================================');
|
||||
console.log("\n================================================");
|
||||
console.log("📊 RÉSUMÉ DE LA MIGRATION");
|
||||
console.log("================================================");
|
||||
console.log(`Total d'équipements: ${equipmentsSnapshot.size}`);
|
||||
console.log(`Équipements avec 'id' manquant: ${missingIdCount}`);
|
||||
console.log(`Équipements mis à jour: ${updatedCount}`);
|
||||
console.log(`Erreurs: ${errorCount}`);
|
||||
console.log('================================================\n');
|
||||
console.log("================================================\n");
|
||||
|
||||
if (missingIdCount === 0) {
|
||||
console.log('✅ Tous les équipements ont déjà un champ id !');
|
||||
console.log("✅ Tous les équipements ont déjà un champ id !");
|
||||
} else if (updatedCount === missingIdCount) {
|
||||
console.log('✅ Migration terminée avec succès !');
|
||||
console.log("✅ Migration terminée avec succès !");
|
||||
} else {
|
||||
console.log('⚠️ Migration terminée avec des erreurs');
|
||||
console.log("⚠️ Migration terminée avec des erreurs");
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Erreur lors de la migration:', error);
|
||||
console.error("❌ Erreur lors de la migration:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Exécuter la migration
|
||||
migrateEquipmentIds()
|
||||
.then(() => {
|
||||
console.log('\n✅ Script terminé');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('\n❌ Script échoué:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
.then(() => {
|
||||
console.log("\n✅ Script terminé");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("\n❌ Script échoué:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Generated
+34
-32
@@ -7,11 +7,13 @@
|
||||
"name": "functions",
|
||||
"dependencies": {
|
||||
"@google-cloud/storage": "^7.18.0",
|
||||
"@google-cloud/text-to-speech": "^5.4.0",
|
||||
"@google/generative-ai": "^0.21.0",
|
||||
"axios": "^1.13.2",
|
||||
"dotenv": "^17.2.3",
|
||||
"envdot": "^0.0.3",
|
||||
"firebase-admin": "^12.6.0",
|
||||
"firebase-functions": "^7.0.3",
|
||||
"firebase-functions": "^7.2.5",
|
||||
"handlebars": "^4.7.8",
|
||||
"nodemailer": "^6.10.1"
|
||||
},
|
||||
@@ -772,12 +774,32 @@
|
||||
"uuid": "dist/bin/uuid"
|
||||
}
|
||||
},
|
||||
"node_modules/@google-cloud/text-to-speech": {
|
||||
"version": "5.8.1",
|
||||
"resolved": "https://registry.npmjs.org/@google-cloud/text-to-speech/-/text-to-speech-5.8.1.tgz",
|
||||
"integrity": "sha512-HXyZBtfQq+ETSLwWV/k3zFRWSzt+KEfiC5/OqXNNUed+lU/LEyN0CsqqEmkFfkL8BPsVIMAK2xiYCaDsKENukg==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"google-gax": "^4.0.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@google/generative-ai": {
|
||||
"version": "0.21.0",
|
||||
"resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.21.0.tgz",
|
||||
"integrity": "sha512-7XhUbtnlkSEZK15kN3t+tzIMxsbKm/dSkKBFalj+20NvPKe1kBY7mR2P7vuijEn+f06z5+A8bVGKO0v39cr6Wg==",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@grpc/grpc-js": {
|
||||
"version": "1.13.4",
|
||||
"resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.13.4.tgz",
|
||||
"integrity": "sha512-GsFaMXCkMqkKIvwCQjCrwH+GHbPKBjhwo/8ZuUkWHqbI73Kky9I+pQltrlT0+MWpedCoosda53lgjYfyEPgxBg==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"@grpc/proto-loader": "^0.7.13",
|
||||
"@js-sdsl/ordered-map": "^4.4.2"
|
||||
@@ -791,7 +813,6 @@
|
||||
"resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.7.15.tgz",
|
||||
"integrity": "sha512-tMXdRCfYVixjuFK+Hk0Q1s38gV9zDiDJfWL3h1rv4Qc39oILCu1TRTDt7+fGUI8K4G1Fj125Hx/ru3azECWTyQ==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"lodash.camelcase": "^4.3.0",
|
||||
"long": "^5.0.0",
|
||||
@@ -1310,7 +1331,6 @@
|
||||
"resolved": "https://registry.npmjs.org/@js-sdsl/ordered-map/-/ordered-map-4.4.2.tgz",
|
||||
"integrity": "sha512-iUKgm52T8HOE/makSxjqoWhe95ZJA1/G1sYsGev2JDKUSS14KAgg1LHb+Ba+IPow0xflbnSkOsZcO08C7w1gYw==",
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/js-sdsl"
|
||||
@@ -1631,8 +1651,7 @@
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz",
|
||||
"integrity": "sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==",
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/mime": {
|
||||
"version": "1.3.5",
|
||||
@@ -1845,7 +1864,6 @@
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
|
||||
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
@@ -1855,7 +1873,6 @@
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
|
||||
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"color-convert": "^2.0.1"
|
||||
@@ -2379,7 +2396,6 @@
|
||||
"version": "8.0.1",
|
||||
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",
|
||||
"integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==",
|
||||
"devOptional": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"string-width": "^4.2.0",
|
||||
@@ -2412,7 +2428,6 @@
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
||||
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"color-name": "~1.1.4"
|
||||
@@ -2425,7 +2440,6 @@
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
|
||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/combined-stream": {
|
||||
@@ -2727,7 +2741,6 @@
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
|
||||
"integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/encodeurl": {
|
||||
@@ -2831,7 +2844,6 @@
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz",
|
||||
"integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
@@ -3352,9 +3364,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/firebase-functions": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/firebase-functions/-/firebase-functions-7.0.3.tgz",
|
||||
"integrity": "sha512-DiIjIUv0OS4KEAA3jqyIc7ymZKdcmMcaXy7FCCkrDQo/1CVMbDDWMdZIslmsgSjldA2nhau1dTE/6JQI8Urjjw==",
|
||||
"version": "7.2.5",
|
||||
"resolved": "https://registry.npmjs.org/firebase-functions/-/firebase-functions-7.2.5.tgz",
|
||||
"integrity": "sha512-K+pP0AknluAguLRbD96hibyXbnOgwnvd4hkExWdGrxnNCLoj8LBFj08uvJYxyvhsCgYzQumrUaHBW4lsXKSiRg==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
@@ -3373,7 +3385,8 @@
|
||||
"peerDependencies": {
|
||||
"@apollo/server": "^5.2.0",
|
||||
"@as-integrations/express4": "^1.1.2",
|
||||
"firebase-admin": "^11.10.0 || ^12.0.0 || ^13.0.0"
|
||||
"firebase-admin": "^11.10.0 || ^12.0.0 || ^13.0.0",
|
||||
"graphql": "^16.12.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@apollo/server": {
|
||||
@@ -3381,6 +3394,9 @@
|
||||
},
|
||||
"@as-integrations/express4": {
|
||||
"optional": true
|
||||
},
|
||||
"graphql": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -3576,7 +3592,6 @@
|
||||
"version": "2.0.5",
|
||||
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
|
||||
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
|
||||
"devOptional": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": "6.* || 8.* || >= 10.*"
|
||||
@@ -3715,7 +3730,6 @@
|
||||
"resolved": "https://registry.npmjs.org/google-gax/-/google-gax-4.6.1.tgz",
|
||||
"integrity": "sha512-V6eky/xz2mcKfAd1Ioxyd6nmA61gao3n01C+YeuIwu3vzM9EDR6wcVzMSIbLMDXWeoi9SHYctXuKYC5uJUT3eQ==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"@grpc/grpc-js": "^1.10.9",
|
||||
"@grpc/proto-loader": "^0.7.13",
|
||||
@@ -3743,7 +3757,6 @@
|
||||
"https://github.com/sponsors/ctavan"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"bin": {
|
||||
"uuid": "dist/bin/uuid"
|
||||
}
|
||||
@@ -4093,7 +4106,6 @@
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
|
||||
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
@@ -5110,8 +5122,7 @@
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
||||
"integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==",
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lodash.clonedeep": {
|
||||
"version": "4.5.0",
|
||||
@@ -5490,7 +5501,6 @@
|
||||
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz",
|
||||
"integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==",
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
@@ -5843,7 +5853,6 @@
|
||||
"resolved": "https://registry.npmjs.org/proto3-json-serializer/-/proto3-json-serializer-2.0.2.tgz",
|
||||
"integrity": "sha512-SAzp/O4Yh02jGdRc+uIrGoe87dkN/XtwxfZ4ZyafJHymd79ozp5VG5nyZ7ygqPM5+cpLDjjGnYFUkngonyDPOQ==",
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"protobufjs": "^7.2.5"
|
||||
},
|
||||
@@ -6035,7 +6044,6 @@
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||
"integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
@@ -6517,7 +6525,6 @@
|
||||
"version": "4.2.3",
|
||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
|
||||
"integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"emoji-regex": "^8.0.0",
|
||||
@@ -6532,7 +6539,6 @@
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
|
||||
"integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ansi-regex": "^5.0.1"
|
||||
@@ -6997,7 +7003,6 @@
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
|
||||
"integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ansi-styles": "^4.0.0",
|
||||
@@ -7035,7 +7040,6 @@
|
||||
"version": "5.0.8",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
|
||||
"integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==",
|
||||
"devOptional": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
@@ -7052,7 +7056,6 @@
|
||||
"version": "17.7.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
|
||||
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cliui": "^8.0.1",
|
||||
@@ -7071,7 +7074,6 @@
|
||||
"version": "21.1.1",
|
||||
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz",
|
||||
"integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==",
|
||||
"devOptional": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
|
||||
@@ -15,11 +15,13 @@
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@google-cloud/storage": "^7.18.0",
|
||||
"@google-cloud/text-to-speech": "^5.4.0",
|
||||
"@google/generative-ai": "^0.21.0",
|
||||
"axios": "^1.13.2",
|
||||
"dotenv": "^17.2.3",
|
||||
"envdot": "^0.0.3",
|
||||
"firebase-admin": "^12.6.0",
|
||||
"firebase-functions": "^7.0.3",
|
||||
"firebase-functions": "^7.2.5",
|
||||
"handlebars": "^4.7.8",
|
||||
"nodemailer": "^6.10.1"
|
||||
},
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const {onCall} = require('firebase-functions/v2/https');
|
||||
const admin = require('firebase-admin');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const nodemailer = require('nodemailer');
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require('./utils/emailConfig');
|
||||
const {onCall} = require("firebase-functions/v2/https");
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const nodemailer = require("nodemailer");
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require("./utils/emailConfig");
|
||||
/**
|
||||
* Traite la validation du matériel d'un événement
|
||||
* Appelée par le client lors du chargement/déchargement
|
||||
@@ -10,14 +10,14 @@ const {getSmtpConfig, EMAIL_CONFIG} = require('./utils/emailConfig');
|
||||
*/
|
||||
exports.processEquipmentValidation = onCall({
|
||||
cors: true,
|
||||
region: 'europe-west9'
|
||||
region: "europe-west9",
|
||||
}, async (request) => {
|
||||
try {
|
||||
// L'authentification est automatique avec onCall
|
||||
const {auth, data} = request;
|
||||
|
||||
if (!auth) {
|
||||
throw new Error('L\'utilisateur doit être authentifié');
|
||||
throw new Error("L'utilisateur doit être authentifié");
|
||||
}
|
||||
|
||||
const {
|
||||
@@ -28,34 +28,39 @@ exports.processEquipmentValidation = onCall({
|
||||
|
||||
// Validation
|
||||
if (!eventId || !equipmentList || !validationType) {
|
||||
throw new Error('eventId, equipmentList et validationType sont requis');
|
||||
throw new Error("eventId, equipmentList et validationType sont requis");
|
||||
}
|
||||
|
||||
const db = admin.firestore();
|
||||
const alerts = [];
|
||||
|
||||
// 1. Récupérer les détails de l'événement
|
||||
const eventRef = db.collection('events').doc(eventId);
|
||||
const eventRef = db.collection("events").doc(eventId);
|
||||
const eventDoc = await eventRef.get();
|
||||
|
||||
if (!eventDoc.exists) {
|
||||
throw new Error('Événement introuvable');
|
||||
throw new Error("Événement introuvable");
|
||||
}
|
||||
|
||||
const event = eventDoc.data();
|
||||
const eventName = event.Name || event.name || 'Événement inconnu';
|
||||
const eventName = event.Name || event.name || "Événement inconnu";
|
||||
const eventDate = formatEventDate(event);
|
||||
|
||||
// 2. Analyser les équipements et détecter les problèmes
|
||||
for (const equipment of equipmentList) {
|
||||
const {equipmentId, status, quantity, expectedQuantity} = equipment;
|
||||
|
||||
// Équipement non emporté: pas d'alerte de perte/manquant au retour.
|
||||
if (status === "NOT_TAKEN") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Cas 1: Équipement PERDU
|
||||
if (status === 'LOST') {
|
||||
if (status === "LOST") {
|
||||
const alertData = await createAlertInFirestore({
|
||||
type: 'LOST',
|
||||
severity: 'CRITICAL',
|
||||
title: 'Équipement perdu',
|
||||
type: "LOST",
|
||||
severity: "CRITICAL",
|
||||
title: "Équipement perdu",
|
||||
message: `Équipement "${equipment.name || equipmentId}" perdu lors de l'événement "${eventName}" (${eventDate})`,
|
||||
equipmentId,
|
||||
eventId,
|
||||
@@ -71,11 +76,11 @@ exports.processEquipmentValidation = onCall({
|
||||
}
|
||||
|
||||
// Cas 2: Équipement MANQUANT
|
||||
if (status === 'MISSING') {
|
||||
if (status === "MISSING") {
|
||||
const alertData = await createAlertInFirestore({
|
||||
type: 'EQUIPMENT_MISSING',
|
||||
severity: 'WARNING',
|
||||
title: 'Équipement manquant',
|
||||
type: "EQUIPMENT_MISSING",
|
||||
severity: "WARNING",
|
||||
title: "Équipement manquant",
|
||||
message: `Équipement "${equipment.name || equipmentId}" manquant pour l'événement "${eventName}" (${eventDate})`,
|
||||
equipmentId,
|
||||
eventId,
|
||||
@@ -91,11 +96,13 @@ exports.processEquipmentValidation = onCall({
|
||||
}
|
||||
|
||||
// Cas 3: Quantité incorrecte
|
||||
if (expectedQuantity && quantity !== expectedQuantity) {
|
||||
const hasExpectedQuantity = typeof expectedQuantity === "number";
|
||||
const hasActualQuantity = typeof quantity === "number";
|
||||
if (hasExpectedQuantity && hasActualQuantity && quantity !== expectedQuantity) {
|
||||
const alertData = await createAlertInFirestore({
|
||||
type: 'QUANTITY_MISMATCH',
|
||||
severity: 'INFO',
|
||||
title: 'Quantité incorrecte',
|
||||
type: "QUANTITY_MISMATCH",
|
||||
severity: "INFO",
|
||||
title: "Quantité incorrecte",
|
||||
message: `Quantité incorrecte pour "${equipment.name || equipmentId}": ${quantity} au lieu de ${expectedQuantity} attendus`,
|
||||
equipmentId,
|
||||
eventId,
|
||||
@@ -113,11 +120,11 @@ exports.processEquipmentValidation = onCall({
|
||||
}
|
||||
|
||||
// Cas 4: Équipement endommagé
|
||||
if (status === 'DAMAGED') {
|
||||
if (status === "DAMAGED") {
|
||||
const alertData = await createAlertInFirestore({
|
||||
type: 'DAMAGED',
|
||||
severity: 'WARNING',
|
||||
title: 'Équipement endommagé',
|
||||
type: "DAMAGED",
|
||||
severity: "WARNING",
|
||||
title: "Équipement endommagé",
|
||||
message: `Équipement "${equipment.name || equipmentId}" endommagé durant l'événement "${eventName}" (${eventDate})`,
|
||||
equipmentId,
|
||||
eventId,
|
||||
@@ -144,7 +151,7 @@ exports.processEquipmentValidation = onCall({
|
||||
});
|
||||
|
||||
// 4. Envoyer les notifications pour les alertes critiques
|
||||
const criticalAlerts = alerts.filter((a) => a.severity === 'CRITICAL');
|
||||
const criticalAlerts = alerts.filter((a) => a.severity === "CRITICAL");
|
||||
if (criticalAlerts.length > 0) {
|
||||
for (const alert of criticalAlerts) {
|
||||
try {
|
||||
@@ -162,7 +169,7 @@ exports.processEquipmentValidation = onCall({
|
||||
alertIds: alerts.map((a) => a.id),
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('[processEquipmentValidation] Erreur:', error);
|
||||
logger.error("[processEquipmentValidation] Erreur:", error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
@@ -172,14 +179,14 @@ exports.processEquipmentValidation = onCall({
|
||||
*/
|
||||
async function createAlertInFirestore(alertData) {
|
||||
const db = admin.firestore();
|
||||
const alertRef = db.collection('alerts').doc();
|
||||
const alertRef = db.collection("alerts").doc();
|
||||
|
||||
const fullAlertData = {
|
||||
id: alertRef.id,
|
||||
...alertData,
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
isRead: false,
|
||||
status: 'ACTIVE',
|
||||
status: "ACTIVE",
|
||||
emailSent: false,
|
||||
assignedTo: [],
|
||||
};
|
||||
@@ -199,7 +206,7 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
|
||||
try {
|
||||
// 1. Récupérer TOUS les utilisateurs et leurs permissions
|
||||
const allUsersSnapshot = await db.collection('users').get();
|
||||
const allUsersSnapshot = await db.collection("users").get();
|
||||
|
||||
// Créer un map pour stocker les références de rôles à récupérer
|
||||
const roleRefs = new Map();
|
||||
@@ -212,17 +219,17 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
}
|
||||
|
||||
// Extraire le chemin du rôle
|
||||
let rolePath = '';
|
||||
let roleId = '';
|
||||
let rolePath = "";
|
||||
let roleId = "";
|
||||
|
||||
if (typeof user.role === 'string') {
|
||||
if (typeof user.role === "string") {
|
||||
rolePath = user.role;
|
||||
roleId = user.role.split('/').pop();
|
||||
roleId = user.role.split("/").pop();
|
||||
} else if (user.role.path) {
|
||||
rolePath = user.role.path;
|
||||
roleId = user.role.path.split('/').pop();
|
||||
roleId = user.role.path.split("/").pop();
|
||||
} else if (user.role._path && user.role._path.segments) {
|
||||
rolePath = user.role._path.segments.join('/');
|
||||
rolePath = user.role._path.segments.join("/");
|
||||
roleId = user.role._path.segments[user.role._path.segments.length - 1];
|
||||
}
|
||||
|
||||
@@ -238,14 +245,14 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
// 2. Récupérer les permissions de chaque rôle unique
|
||||
for (const [roleId, {users, rolePath}] of roleRefs.entries()) {
|
||||
try {
|
||||
const roleDoc = await db.collection('roles').doc(roleId).get();
|
||||
const roleDoc = await db.collection("roles").doc(roleId).get();
|
||||
|
||||
if (roleDoc.exists) {
|
||||
const roleData = roleDoc.data();
|
||||
const permissions = roleData.permissions || [];
|
||||
|
||||
// Vérifier si le rôle a la permission view_all_events
|
||||
if (permissions.includes('view_all_events')) {
|
||||
if (permissions.includes("view_all_events")) {
|
||||
users.forEach((userId) => {
|
||||
usersWithPermission.add(userId);
|
||||
targetUserIds.add(userId);
|
||||
@@ -259,7 +266,7 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
|
||||
// 3. Ajouter la workforce de l'événement
|
||||
if (eventId) {
|
||||
const eventDoc = await db.collection('events').doc(eventId).get();
|
||||
const eventDoc = await db.collection("events").doc(eventId).get();
|
||||
|
||||
if (eventDoc.exists) {
|
||||
const event = eventDoc.data();
|
||||
@@ -269,14 +276,14 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
// Extraire l'userId selon différentes structures possibles
|
||||
let userId = null;
|
||||
|
||||
if (typeof member === 'string') {
|
||||
if (typeof member === "string") {
|
||||
userId = member;
|
||||
} else if (member.userId) {
|
||||
userId = member.userId;
|
||||
} else if (member.id) {
|
||||
userId = member.id;
|
||||
} else if (member.user) {
|
||||
if (typeof member.user === 'string') {
|
||||
if (typeof member.user === "string") {
|
||||
userId = member.user;
|
||||
} else if (member.user.id) {
|
||||
userId = member.user.id;
|
||||
@@ -293,18 +300,18 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
const userIds = Array.from(targetUserIds);
|
||||
|
||||
// 4. Mettre à jour l'alerte avec la liste des utilisateurs
|
||||
await db.collection('alerts').doc(alert.id).update({
|
||||
await db.collection("alerts").doc(alert.id).update({
|
||||
assignedTo: userIds,
|
||||
});
|
||||
|
||||
// 5. Envoyer les emails si alerte critique
|
||||
if (alert.severity === 'CRITICAL') {
|
||||
if (alert.severity === "CRITICAL") {
|
||||
await sendAlertEmails(alert, userIds);
|
||||
}
|
||||
|
||||
return userIds;
|
||||
} catch (error) {
|
||||
logger.error('[sendAlertNotifications] Erreur:', error);
|
||||
logger.error("[sendAlertNotifications] Erreur:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -314,12 +321,12 @@ async function sendAlertNotifications(alert, eventId) {
|
||||
*/
|
||||
async function sendAlertEmails(alert, userIds) {
|
||||
try {
|
||||
const {renderTemplate, getEmailSubject, prepareTemplateData} = require('./utils/emailTemplates');
|
||||
const {renderTemplate, getEmailSubject, prepareTemplateData} = require("./utils/emailTemplates");
|
||||
const db = admin.firestore();
|
||||
|
||||
// Vérifier que EMAIL_CONFIG est disponible
|
||||
if (!EMAIL_CONFIG || !EMAIL_CONFIG.from) {
|
||||
logger.error('[sendAlertEmails] EMAIL_CONFIG non configuré');
|
||||
logger.error("[sendAlertEmails] EMAIL_CONFIG non configuré");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -336,7 +343,7 @@ async function sendAlertEmails(alert, userIds) {
|
||||
const promises = batch.map(async (userId) => {
|
||||
try {
|
||||
// Récupérer l'utilisateur
|
||||
const userDoc = await db.collection('users').doc(userId).get();
|
||||
const userDoc = await db.collection("users").doc(userId).get();
|
||||
|
||||
if (!userDoc.exists) {
|
||||
return false;
|
||||
@@ -344,55 +351,55 @@ async function sendAlertEmails(alert, userIds) {
|
||||
|
||||
const user = userDoc.data();
|
||||
|
||||
// Vérifier les préférences email
|
||||
const prefs = user.notificationPreferences || {};
|
||||
if (!prefs.emailEnabled) {
|
||||
return false;
|
||||
}
|
||||
// Vérifier les préférences email
|
||||
const prefs = user.notificationPreferences || {};
|
||||
if (!prefs.emailEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!user.email) {
|
||||
return false;
|
||||
}
|
||||
if (!user.email) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Préparer et envoyer l'email
|
||||
let html;
|
||||
try {
|
||||
const templateData = await prepareTemplateData(alert, user);
|
||||
html = await renderTemplate('alert-individual', templateData);
|
||||
} catch (templateError) {
|
||||
logger.error(`[sendAlertEmails] Erreur template pour ${userId}:`, templateError);
|
||||
html = `
|
||||
// Préparer et envoyer l'email
|
||||
let html;
|
||||
try {
|
||||
const templateData = await prepareTemplateData(alert, user);
|
||||
html = await renderTemplate("alert-individual", templateData);
|
||||
} catch (templateError) {
|
||||
logger.error(`[sendAlertEmails] Erreur template pour ${userId}:`, templateError);
|
||||
html = `
|
||||
<html>
|
||||
<body>
|
||||
<h2>${alert.title || 'Nouvelle alerte'}</h2>
|
||||
<h2>${alert.title || "Nouvelle alerte"}</h2>
|
||||
<p>${alert.message}</p>
|
||||
<a href="${EMAIL_CONFIG.appUrl}/alerts">Voir l'alerte</a>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
|
||||
await transporter.sendMail({
|
||||
from: `"${EMAIL_CONFIG.from.name}" <${EMAIL_CONFIG.from.address}>`,
|
||||
to: user.email,
|
||||
replyTo: EMAIL_CONFIG.replyTo,
|
||||
subject: getEmailSubject(alert),
|
||||
html: html,
|
||||
text: alert.message,
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error(`[sendAlertEmails] Erreur email ${userId}:`, error);
|
||||
return false;
|
||||
}
|
||||
|
||||
await transporter.sendMail({
|
||||
from: `"${EMAIL_CONFIG.from.name}" <${EMAIL_CONFIG.from.address}>`,
|
||||
to: user.email,
|
||||
replyTo: EMAIL_CONFIG.replyTo,
|
||||
subject: getEmailSubject(alert),
|
||||
html: html,
|
||||
text: alert.message,
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error(`[sendAlertEmails] Erreur email ${userId}:`, error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
const results = await Promise.all(promises);
|
||||
successCount += results.filter((r) => r).length;
|
||||
}
|
||||
|
||||
// Mettre à jour l'alerte
|
||||
await db.collection('alerts').doc(alert.id).update({
|
||||
await db.collection("alerts").doc(alert.id).update({
|
||||
emailSent: true,
|
||||
emailSentAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
emailsSentCount: successCount,
|
||||
@@ -400,7 +407,7 @@ async function sendAlertEmails(alert, userIds) {
|
||||
|
||||
return successCount;
|
||||
} catch (error) {
|
||||
logger.error('[sendAlertEmails] Erreur globale:', error);
|
||||
logger.error("[sendAlertEmails] Erreur globale:", error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -409,10 +416,48 @@ async function sendAlertEmails(alert, userIds) {
|
||||
* Formate la date d'un événement
|
||||
*/
|
||||
function formatEventDate(event) {
|
||||
if (event.startDate) {
|
||||
const date = event.startDate.toDate ? event.startDate.toDate() : new Date(event.startDate);
|
||||
return date.toLocaleDateString('fr-FR', {day: 'numeric', month: 'numeric', year: 'numeric'});
|
||||
}
|
||||
return 'Date inconnue';
|
||||
const rawDate =
|
||||
event?.StartDateTime ||
|
||||
event?.startDateTime ||
|
||||
event?.startDate ||
|
||||
event?.eventDate;
|
||||
|
||||
const parsedDate = parseFirestoreDate(rawDate);
|
||||
const safeDate = parsedDate || new Date();
|
||||
|
||||
return safeDate.toLocaleDateString("fr-FR", {
|
||||
day: "numeric",
|
||||
month: "numeric",
|
||||
year: "numeric",
|
||||
});
|
||||
}
|
||||
|
||||
function parseFirestoreDate(value) {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof value.toDate === "function") {
|
||||
return value.toDate();
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === "string" || typeof value === "number") {
|
||||
const date = new Date(value);
|
||||
return Number.isNaN(date.getTime()) ? null : date;
|
||||
}
|
||||
|
||||
if (typeof value === "object" && typeof value.seconds === "number") {
|
||||
return new Date(value.seconds * 1000);
|
||||
}
|
||||
|
||||
if (typeof value === "object" && typeof value._seconds === "number") {
|
||||
return new Date(value._seconds * 1000);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
const {onCall} = require('firebase-functions/v2/https');
|
||||
const admin = require('firebase-admin');
|
||||
const nodemailer = require('nodemailer');
|
||||
const handlebars = require('handlebars');
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require('./utils/emailConfig');
|
||||
const {onCall} = require("firebase-functions/v2/https");
|
||||
const admin = require("firebase-admin");
|
||||
const nodemailer = require("nodemailer");
|
||||
const handlebars = require("handlebars");
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const {getSmtpConfig, EMAIL_CONFIG} = require("./utils/emailConfig");
|
||||
|
||||
/**
|
||||
* Envoie un email d'alerte à un utilisateur
|
||||
* Appelé par le client Dart via callable function
|
||||
*/
|
||||
exports.sendAlertEmail = onCall({
|
||||
region: 'europe-west9',
|
||||
cors: true
|
||||
region: "europe-west9",
|
||||
cors: true,
|
||||
}, async (request) => {
|
||||
// Vérifier l'authentification
|
||||
if (!request.auth) {
|
||||
throw new Error('L\'utilisateur doit être authentifié');
|
||||
throw new Error("L'utilisateur doit être authentifié");
|
||||
}
|
||||
|
||||
const {alertId, userId, templateType} = request.data;
|
||||
|
||||
if (!alertId || !userId) {
|
||||
throw new Error('alertId et userId sont requis');
|
||||
throw new Error("alertId et userId sont requis");
|
||||
}
|
||||
|
||||
try {
|
||||
// Récupérer l'alerte depuis Firestore
|
||||
const alertDoc = await admin.firestore()
|
||||
.collection('alerts')
|
||||
.collection("alerts")
|
||||
.doc(alertId)
|
||||
.get();
|
||||
|
||||
if (!alertDoc.exists) {
|
||||
throw new Error('Alerte introuvable');
|
||||
throw new Error("Alerte introuvable");
|
||||
}
|
||||
|
||||
const alert = alertDoc.data();
|
||||
|
||||
// Récupérer l'utilisateur
|
||||
const userDoc = await admin.firestore()
|
||||
.collection('users')
|
||||
.collection("users")
|
||||
.doc(userId)
|
||||
.get();
|
||||
|
||||
if (!userDoc.exists) {
|
||||
throw new Error('Utilisateur introuvable');
|
||||
throw new Error("Utilisateur introuvable");
|
||||
}
|
||||
|
||||
const user = userDoc.data();
|
||||
@@ -54,7 +54,7 @@ exports.sendAlertEmail = onCall({
|
||||
const prefs = user.notificationPreferences || {};
|
||||
if (!prefs.emailEnabled) {
|
||||
console.log(`Email désactivé pour l'utilisateur ${userId}`);
|
||||
return {success: true, skipped: true, reason: 'email_disabled'};
|
||||
return {success: true, skipped: true, reason: "email_disabled"};
|
||||
}
|
||||
|
||||
// Vérifier la préférence pour ce type d'alerte
|
||||
@@ -62,7 +62,7 @@ exports.sendAlertEmail = onCall({
|
||||
const shouldSend = checkAlertPreference(alertType, prefs);
|
||||
if (!shouldSend) {
|
||||
console.log(`Type d'alerte ${alertType} désactivé pour ${userId}`);
|
||||
return {success: true, skipped: true, reason: 'alert_type_disabled'};
|
||||
return {success: true, skipped: true, reason: "alert_type_disabled"};
|
||||
}
|
||||
|
||||
// Préparer les données pour le template
|
||||
@@ -70,7 +70,7 @@ exports.sendAlertEmail = onCall({
|
||||
|
||||
// Rendre le template HTML
|
||||
const html = await renderTemplate(
|
||||
templateType || 'alert-individual',
|
||||
templateType || "alert-individual",
|
||||
templateData,
|
||||
);
|
||||
|
||||
@@ -88,7 +88,7 @@ exports.sendAlertEmail = onCall({
|
||||
text: alert.message,
|
||||
});
|
||||
|
||||
console.log('Email envoyé:', info.messageId);
|
||||
console.log("Email envoyé:", info.messageId);
|
||||
|
||||
// Marquer l'email comme envoyé dans l'alerte
|
||||
await alertDoc.ref.update({
|
||||
@@ -102,7 +102,7 @@ exports.sendAlertEmail = onCall({
|
||||
skipped: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Erreur envoi email:', error);
|
||||
console.error("Erreur envoi email:", error);
|
||||
throw new Error(`Erreur lors de l'envoi de l'email: ${error.message}`);
|
||||
}
|
||||
});
|
||||
@@ -112,13 +112,13 @@ exports.sendAlertEmail = onCall({
|
||||
*/
|
||||
function checkAlertPreference(alertType, preferences) {
|
||||
const typeMapping = {
|
||||
'EVENT_CREATED': 'eventsNotifications',
|
||||
'EVENT_MODIFIED': 'eventsNotifications',
|
||||
'EVENT_CANCELLED': 'eventsNotifications',
|
||||
'LOST': 'equipmentNotifications',
|
||||
'EQUIPMENT_MISSING': 'equipmentNotifications',
|
||||
'MAINTENANCE_REMINDER': 'maintenanceNotifications',
|
||||
'STOCK_LOW': 'stockNotifications',
|
||||
"EVENT_CREATED": "eventsNotifications",
|
||||
"EVENT_MODIFIED": "eventsNotifications",
|
||||
"EVENT_CANCELLED": "eventsNotifications",
|
||||
"LOST": "equipmentNotifications",
|
||||
"EQUIPMENT_MISSING": "equipmentNotifications",
|
||||
"MAINTENANCE_REMINDER": "maintenanceNotifications",
|
||||
"STOCK_LOW": "stockNotifications",
|
||||
};
|
||||
|
||||
const prefKey = typeMapping[alertType];
|
||||
@@ -130,12 +130,12 @@ function checkAlertPreference(alertType, preferences) {
|
||||
*/
|
||||
async function prepareTemplateData(alert, user) {
|
||||
const data = {
|
||||
userName: `${user.firstName || ''} ${user.lastName || ''}`.trim() ||
|
||||
'Utilisateur',
|
||||
userName: `${user.firstName || ""} ${user.lastName || ""}`.trim() ||
|
||||
"Utilisateur",
|
||||
alertTitle: getAlertTitle(alert.type),
|
||||
alertMessage: alert.message,
|
||||
isCritical: alert.severity === 'CRITICAL',
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || '/alerts'}`,
|
||||
isCritical: alert.severity === "CRITICAL",
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || "/alerts"}`,
|
||||
appUrl: EMAIL_CONFIG.appUrl,
|
||||
unsubscribeUrl: `${EMAIL_CONFIG.appUrl}/my_account?tab=notifications`,
|
||||
year: new Date().getFullYear(),
|
||||
@@ -146,7 +146,7 @@ async function prepareTemplateData(alert, user) {
|
||||
if (alert.eventId) {
|
||||
try {
|
||||
const eventDoc = await admin.firestore()
|
||||
.collection('events')
|
||||
.collection("events")
|
||||
.doc(alert.eventId)
|
||||
.get();
|
||||
|
||||
@@ -155,22 +155,22 @@ async function prepareTemplateData(alert, user) {
|
||||
data.eventName = event.Name;
|
||||
if (event.StartDateTime) {
|
||||
const date = event.StartDateTime.toDate();
|
||||
data.eventDate = date.toLocaleDateString('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
data.eventDate = date.toLocaleDateString("fr-FR", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric",
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur récupération événement:', error);
|
||||
console.error("Erreur récupération événement:", error);
|
||||
}
|
||||
}
|
||||
|
||||
if (alert.equipmentId) {
|
||||
try {
|
||||
const eqDoc = await admin.firestore()
|
||||
.collection('equipments')
|
||||
.collection("equipments")
|
||||
.doc(alert.equipmentId)
|
||||
.get();
|
||||
|
||||
@@ -178,7 +178,7 @@ async function prepareTemplateData(alert, user) {
|
||||
data.equipmentName = eqDoc.data().name;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur récupération équipement:', error);
|
||||
console.error("Erreur récupération équipement:", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,16 +190,16 @@ async function prepareTemplateData(alert, user) {
|
||||
*/
|
||||
function getEmailSubject(alert) {
|
||||
const subjects = {
|
||||
'EVENT_CREATED': '📅 Nouvel événement créé',
|
||||
'EVENT_MODIFIED': '📝 Événement modifié',
|
||||
'EVENT_CANCELLED': '❌ Événement annulé',
|
||||
'LOST': '🔴 Alerte critique : Équipement perdu',
|
||||
'EQUIPMENT_MISSING': '⚠️ Équipement manquant',
|
||||
'MAINTENANCE_REMINDER': '🔧 Rappel de maintenance',
|
||||
'STOCK_LOW': '📦 Stock faible',
|
||||
"EVENT_CREATED": "📅 Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "📝 Événement modifié",
|
||||
"EVENT_CANCELLED": "❌ Événement annulé",
|
||||
"LOST": "🔴 Alerte critique : Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "⚠️ Équipement manquant",
|
||||
"MAINTENANCE_REMINDER": "🔧 Rappel de maintenance",
|
||||
"STOCK_LOW": "📦 Stock faible",
|
||||
};
|
||||
|
||||
return subjects[alert.type] || '🔔 Nouvelle alerte - EM2 Events';
|
||||
return subjects[alert.type] || "🔔 Nouvelle alerte - EM2 Events";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,16 +207,16 @@ function getEmailSubject(alert) {
|
||||
*/
|
||||
function getAlertTitle(type) {
|
||||
const titles = {
|
||||
'EVENT_CREATED': 'Nouvel événement créé',
|
||||
'EVENT_MODIFIED': 'Événement modifié',
|
||||
'EVENT_CANCELLED': 'Événement annulé',
|
||||
'LOST': 'Équipement perdu',
|
||||
'EQUIPMENT_MISSING': 'Équipement manquant',
|
||||
'MAINTENANCE_REMINDER': 'Maintenance requise',
|
||||
'STOCK_LOW': 'Stock faible',
|
||||
"EVENT_CREATED": "Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "Événement modifié",
|
||||
"EVENT_CANCELLED": "Événement annulé",
|
||||
"LOST": "Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "Équipement manquant",
|
||||
"MAINTENANCE_REMINDER": "Maintenance requise",
|
||||
"STOCK_LOW": "Stock faible",
|
||||
};
|
||||
|
||||
return titles[type] || 'Nouvelle alerte';
|
||||
return titles[type] || "Nouvelle alerte";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,16 +225,16 @@ function getAlertTitle(type) {
|
||||
async function renderTemplate(templateName, data) {
|
||||
try {
|
||||
// Lire le template de base
|
||||
const basePath = path.join(__dirname, 'templates', 'base-template.html');
|
||||
const baseTemplate = await fs.readFile(basePath, 'utf8');
|
||||
const basePath = path.join(__dirname, "templates", "base-template.html");
|
||||
const baseTemplate = await fs.readFile(basePath, "utf8");
|
||||
|
||||
// Lire le template de contenu
|
||||
const contentPath = path.join(
|
||||
__dirname,
|
||||
'templates',
|
||||
"templates",
|
||||
`${templateName}.html`,
|
||||
);
|
||||
const contentTemplate = await fs.readFile(contentPath, 'utf8');
|
||||
const contentTemplate = await fs.readFile(contentPath, "utf8");
|
||||
|
||||
// Compiler les templates
|
||||
const compileContent = handlebars.compile(contentTemplate);
|
||||
@@ -249,7 +249,7 @@ async function renderTemplate(templateName, data) {
|
||||
content: renderedContent,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Erreur rendu template:', error);
|
||||
console.error("Erreur rendu template:", error);
|
||||
// Fallback vers un template simple
|
||||
return `
|
||||
<html>
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
* S'exécute tous les jours à 8h00 (Europe/Paris)
|
||||
*/
|
||||
|
||||
const admin = require('firebase-admin');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const nodemailer = require('nodemailer');
|
||||
const { getSmtpConfig } = require('./utils/emailConfig');
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const nodemailer = require("nodemailer");
|
||||
const {getSmtpConfig} = require("./utils/emailConfig");
|
||||
|
||||
/**
|
||||
* Fonction principale : envoie le digest quotidien
|
||||
@@ -14,11 +14,11 @@ const { getSmtpConfig } = require('./utils/emailConfig');
|
||||
async function sendDailyDigest() {
|
||||
const db = admin.firestore();
|
||||
|
||||
logger.info('[sendDailyDigest] ===== DÉBUT ENVOI DIGEST QUOTIDIEN =====');
|
||||
logger.info("[sendDailyDigest] ===== DÉBUT ENVOI DIGEST QUOTIDIEN =====");
|
||||
|
||||
try {
|
||||
// 1. Récupérer tous les utilisateurs avec email activé
|
||||
const usersSnapshot = await db.collection('users').get();
|
||||
const usersSnapshot = await db.collection("users").get();
|
||||
const eligibleUsers = [];
|
||||
|
||||
usersSnapshot.forEach((doc) => {
|
||||
@@ -30,8 +30,8 @@ async function sendDailyDigest() {
|
||||
eligibleUsers.push({
|
||||
uid: doc.id,
|
||||
email: user.email,
|
||||
firstName: user.firstName || 'Utilisateur',
|
||||
lastName: user.lastName || '',
|
||||
firstName: user.firstName || "Utilisateur",
|
||||
lastName: user.lastName || "",
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -48,12 +48,12 @@ async function sendDailyDigest() {
|
||||
for (const user of eligibleUsers) {
|
||||
try {
|
||||
// Récupérer les alertes non lues de l'utilisateur créées dans les dernières 24h
|
||||
const alertsSnapshot = await db.collection('alerts')
|
||||
.where('assignedTo', 'array-contains', user.uid)
|
||||
.where('isRead', '==', false)
|
||||
.where('createdAt', '>=', yesterday)
|
||||
.orderBy('createdAt', 'desc')
|
||||
.get();
|
||||
const alertsSnapshot = await db.collection("alerts")
|
||||
.where("assignedTo", "array-contains", user.uid)
|
||||
.where("isRead", "==", false)
|
||||
.where("createdAt", ">=", yesterday)
|
||||
.orderBy("createdAt", "desc")
|
||||
.get();
|
||||
|
||||
if (alertsSnapshot.empty) {
|
||||
continue; // Pas d'alertes non lues pour cet utilisateur
|
||||
@@ -61,7 +61,7 @@ async function sendDailyDigest() {
|
||||
|
||||
const alerts = [];
|
||||
alertsSnapshot.forEach((doc) => {
|
||||
alerts.push({ id: doc.id, ...doc.data() });
|
||||
alerts.push({id: doc.id, ...doc.data()});
|
||||
});
|
||||
|
||||
logger.info(`[sendDailyDigest] ${user.email}: ${alerts.length} alertes non lues`);
|
||||
@@ -77,11 +77,11 @@ async function sendDailyDigest() {
|
||||
}
|
||||
|
||||
logger.info(`[sendDailyDigest] ✓ ${emailsSent}/${eligibleUsers.length} emails envoyés`);
|
||||
logger.info('[sendDailyDigest] ===== FIN DIGEST QUOTIDIEN =====');
|
||||
logger.info("[sendDailyDigest] ===== FIN DIGEST QUOTIDIEN =====");
|
||||
|
||||
return { success: true, emailsSent };
|
||||
return {success: true, emailsSent};
|
||||
} catch (error) {
|
||||
logger.error('[sendDailyDigest] Erreur globale:', error);
|
||||
logger.error("[sendDailyDigest] Erreur globale:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -92,9 +92,9 @@ async function sendDailyDigest() {
|
||||
async function sendDigestEmail(transporter, user, alerts) {
|
||||
try {
|
||||
// Grouper les alertes par sévérité
|
||||
const criticalAlerts = alerts.filter(a => a.severity === 'CRITICAL');
|
||||
const warningAlerts = alerts.filter(a => a.severity === 'WARNING');
|
||||
const infoAlerts = alerts.filter(a => a.severity === 'INFO');
|
||||
const criticalAlerts = alerts.filter((a) => a.severity === "CRITICAL");
|
||||
const warningAlerts = alerts.filter((a) => a.severity === "WARNING");
|
||||
const infoAlerts = alerts.filter((a) => a.severity === "INFO");
|
||||
|
||||
// Construire le HTML
|
||||
const html = buildDigestHtml(user, {
|
||||
@@ -125,7 +125,7 @@ async function sendDigestEmail(transporter, user, alerts) {
|
||||
function buildDigestHtml(user, alertsByType) {
|
||||
const totalAlerts = alertsByType.critical.length + alertsByType.warning.length + alertsByType.info.length;
|
||||
|
||||
let alertsHtml = '';
|
||||
let alertsHtml = "";
|
||||
|
||||
// Alertes critiques
|
||||
if (alertsByType.critical.length > 0) {
|
||||
@@ -134,7 +134,7 @@ function buildDigestHtml(user, alertsByType) {
|
||||
<h3 style="color: #dc2626; margin: 0 0 12px 0;">
|
||||
🔴 Alertes critiques (${alertsByType.critical.length})
|
||||
</h3>
|
||||
${alertsByType.critical.map(alert => formatAlertItem(alert)).join('')}
|
||||
${alertsByType.critical.map((alert) => formatAlertItem(alert)).join("")}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
@@ -146,7 +146,7 @@ function buildDigestHtml(user, alertsByType) {
|
||||
<h3 style="color: #f59e0b; margin: 0 0 12px 0;">
|
||||
⚠️ Avertissements (${alertsByType.warning.length})
|
||||
</h3>
|
||||
${alertsByType.warning.map(alert => formatAlertItem(alert)).join('')}
|
||||
${alertsByType.warning.map((alert) => formatAlertItem(alert)).join("")}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
@@ -158,7 +158,7 @@ function buildDigestHtml(user, alertsByType) {
|
||||
<h3 style="color: #3b82f6; margin: 0 0 12px 0;">
|
||||
ℹ️ Informations (${alertsByType.info.length})
|
||||
</h3>
|
||||
${alertsByType.info.map(alert => formatAlertItem(alert)).join('')}
|
||||
${alertsByType.info.map((alert) => formatAlertItem(alert)).join("")}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
@@ -216,24 +216,24 @@ function buildDigestHtml(user, alertsByType) {
|
||||
*/
|
||||
function formatAlertItem(alert) {
|
||||
const date = alert.createdAt?.toDate ?
|
||||
new Date(alert.createdAt.toDate()).toLocaleString('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
new Date(alert.createdAt.toDate()).toLocaleString("fr-FR", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
}) :
|
||||
'Date inconnue';
|
||||
"Date inconnue";
|
||||
|
||||
// Type d'alerte en français
|
||||
const typeLabels = {
|
||||
'EQUIPMENT_MISSING': 'Équipement manquant',
|
||||
'LOST': 'Équipement perdu',
|
||||
'DAMAGED': 'Équipement endommagé',
|
||||
'QUANTITY_MISMATCH': 'Écart de quantité',
|
||||
'EVENT_CREATED': 'Événement créé',
|
||||
'EVENT_MODIFIED': 'Événement modifié',
|
||||
'WORKFORCE_ADDED': 'Ajout à la workforce',
|
||||
"EQUIPMENT_MISSING": "Équipement manquant",
|
||||
"LOST": "Équipement perdu",
|
||||
"DAMAGED": "Équipement endommagé",
|
||||
"QUANTITY_MISMATCH": "Écart de quantité",
|
||||
"EVENT_CREATED": "Événement créé",
|
||||
"EVENT_MODIFIED": "Événement modifié",
|
||||
"WORKFORCE_ADDED": "Ajout à la workforce",
|
||||
};
|
||||
|
||||
const typeLabel = typeLabels[alert.type] || alert.type;
|
||||
@@ -245,7 +245,7 @@ function formatAlertItem(alert) {
|
||||
<span style="color: #6b7280; font-size: 13px;">${date}</span>
|
||||
</div>
|
||||
<p style="color: #4b5563; margin: 0; font-size: 14px; line-height: 1.5;">
|
||||
${alert.message || 'Aucun message'}
|
||||
${alert.message || "Aucun message"}
|
||||
</p>
|
||||
</div>
|
||||
`;
|
||||
@@ -256,12 +256,12 @@ function formatAlertItem(alert) {
|
||||
*/
|
||||
function getSeverityColor(severity) {
|
||||
switch (severity) {
|
||||
case 'CRITICAL': return '#dc2626';
|
||||
case 'WARNING': return '#f59e0b';
|
||||
case 'INFO': return '#3b82f6';
|
||||
default: return '#6b7280';
|
||||
case "CRITICAL": return "#dc2626";
|
||||
case "WARNING": return "#f59e0b";
|
||||
case "INFO": return "#3b82f6";
|
||||
default: return "#6b7280";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { sendDailyDigest };
|
||||
module.exports = {sendDailyDigest};
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Récupère toutes les alertes (filtrées et limitées)
|
||||
exports.getAlerts = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const snapshot = await db.collection("alerts")
|
||||
.orderBy("createdAt", "desc")
|
||||
.limit(100)
|
||||
.get();
|
||||
|
||||
const alerts = snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
return {
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(data, ["createdAt"]),
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json({alerts});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching alerts:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Marquer une alerte comme lue
|
||||
exports.markAlertAsRead = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const alertId = req.body.data?.alertId;
|
||||
if (!alertId) {
|
||||
res.status(400).json({error: "alertId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("alerts").doc(alertId).update({
|
||||
isRead: true,
|
||||
});
|
||||
|
||||
res.status(200).json({success: true});
|
||||
} catch (error) {
|
||||
logger.error("Error marking alert as read:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer une alerte
|
||||
exports.deleteAlert = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const alertId = req.body.data?.alertId;
|
||||
if (!alertId) {
|
||||
res.status(400).json({error: "alertId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("alerts").doc(alertId).delete();
|
||||
|
||||
res.status(200).json({success: true});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting alert:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,628 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Vérifie si un équipement est disponible pour une plage de dates
|
||||
exports.checkEquipmentAvailability = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentId, startDate, endDate, excludeEventId} = req.body.data;
|
||||
|
||||
if (!equipmentId || !startDate || !endDate) {
|
||||
res.status(400).json({error: "equipmentId, startDate, and endDate are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`Checking availability for equipment ${equipmentId} from ${startDate} to ${endDate}, excluding event: ${excludeEventId}`);
|
||||
|
||||
const startTimestamp = admin.firestore.Timestamp.fromDate(new Date(startDate));
|
||||
const endTimestamp = admin.firestore.Timestamp.fromDate(new Date(endDate));
|
||||
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
logger.info(`Found ${eventsSnapshot.docs.length} events to check`);
|
||||
|
||||
const conflicts = [];
|
||||
|
||||
for (const eventDoc of eventsSnapshot.docs) {
|
||||
const event = eventDoc.data();
|
||||
|
||||
if (excludeEventId && eventDoc.id === excludeEventId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let eventStart; let eventEnd;
|
||||
if (event.StartDateTime) {
|
||||
eventStart = event.StartDateTime.toDate ? event.StartDateTime.toDate() : new Date(event.StartDateTime);
|
||||
}
|
||||
if (event.EndDateTime) {
|
||||
eventEnd = event.EndDateTime.toDate ? event.EndDateTime.toDate() : new Date(event.EndDateTime);
|
||||
}
|
||||
|
||||
if (!eventStart || !eventEnd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const assignedEquipment = event.assignedEquipment || [];
|
||||
const assignedContainers = event.assignedContainers || [];
|
||||
|
||||
const isEquipmentDirectlyAssigned = assignedEquipment.some((eq) => eq.equipmentId === equipmentId);
|
||||
|
||||
let isEquipmentInAssignedContainer = false;
|
||||
if (assignedContainers.length > 0) {
|
||||
logger.info(`Event ${eventDoc.id} has ${assignedContainers.length} assigned containers`);
|
||||
for (const containerId of assignedContainers) {
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (containerDoc.exists) {
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
logger.info(`Container ${containerId} contains equipment IDs: ${equipmentIds.join(", ")}`);
|
||||
if (equipmentIds.includes(equipmentId)) {
|
||||
isEquipmentInAssignedContainer = true;
|
||||
logger.info(`Equipment ${equipmentId} found in container ${containerId} for event ${eventDoc.id}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isEquipmentDirectlyAssigned) {
|
||||
logger.info(`Equipment ${equipmentId} is directly assigned to event ${eventDoc.id}`);
|
||||
}
|
||||
|
||||
if (!isEquipmentDirectlyAssigned && !isEquipmentInAssignedContainer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const requestStart = startTimestamp.toDate();
|
||||
const requestEnd = endTimestamp.toDate();
|
||||
|
||||
const installationTime = event.InstallationTime || 0;
|
||||
const disassemblyTime = event.DisassemblyTime || 0;
|
||||
|
||||
const eventStartWithSetup = new Date(eventStart);
|
||||
eventStartWithSetup.setHours(eventStartWithSetup.getHours() - installationTime);
|
||||
|
||||
const eventEndWithTeardown = new Date(eventEnd);
|
||||
eventEndWithTeardown.setHours(eventEndWithTeardown.getHours() + disassemblyTime);
|
||||
|
||||
const hasOverlap = requestStart < eventEndWithTeardown && requestEnd > eventStartWithSetup;
|
||||
|
||||
if (hasOverlap) {
|
||||
const overlapStart = new Date(Math.max(requestStart, eventStartWithSetup));
|
||||
const overlapEnd = new Date(Math.min(requestEnd, eventEndWithTeardown));
|
||||
const overlapDays = Math.ceil((overlapEnd - overlapStart) / (1000 * 60 * 60 * 24));
|
||||
|
||||
logger.info(`Conflict detected: Equipment ${equipmentId} conflicts with event ${eventDoc.id} (${event.Name})`);
|
||||
|
||||
const eventData = helpers.serializeTimestamps(event);
|
||||
conflicts.push({
|
||||
eventId: eventDoc.id,
|
||||
eventName: event.Name,
|
||||
eventData: eventData,
|
||||
startDate: eventStart.toISOString(),
|
||||
endDate: eventEnd.toISOString(),
|
||||
overlapDays: overlapDays,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`Total conflicts found: ${conflicts.length}`);
|
||||
|
||||
res.status(200).json({conflicts, available: conflicts.length === 0});
|
||||
} catch (error) {
|
||||
logger.error("Error checking equipment availability:", error);
|
||||
res.status(500).json({error: error.message || "Failed to check equipment availability"});
|
||||
}
|
||||
};
|
||||
|
||||
// Vérifie la disponibilité d'un container et de son contenu
|
||||
exports.checkContainerAvailability = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerId, startDate, endDate, excludeEventId} = req.body.data;
|
||||
|
||||
if (!containerId || !startDate || !endDate) {
|
||||
res.status(400).json({error: "containerId, startDate, and endDate are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (!containerDoc.exists) {
|
||||
throw new Error("Container not found");
|
||||
}
|
||||
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
|
||||
const startTimestamp = admin.firestore.Timestamp.fromDate(new Date(startDate));
|
||||
const endTimestamp = admin.firestore.Timestamp.fromDate(new Date(endDate));
|
||||
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
const containerConflicts = [];
|
||||
const equipmentConflicts = {};
|
||||
|
||||
for (const eventDoc of eventsSnapshot.docs) {
|
||||
const event = eventDoc.data();
|
||||
|
||||
if (excludeEventId && eventDoc.id === excludeEventId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let eventStart; let eventEnd;
|
||||
if (event.StartDateTime) {
|
||||
eventStart = event.StartDateTime.toDate ? event.StartDateTime.toDate() : new Date(event.StartDateTime);
|
||||
}
|
||||
if (event.EndDateTime) {
|
||||
eventEnd = event.EndDateTime.toDate ? event.EndDateTime.toDate() : new Date(event.EndDateTime);
|
||||
}
|
||||
|
||||
if (!eventStart || !eventEnd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const assignedContainers = event.assignedContainers || [];
|
||||
const isContainerAssigned = assignedContainers.includes(containerId);
|
||||
|
||||
const assignedEquipment = event.assignedEquipment || [];
|
||||
const conflictingEquipmentIds = equipmentIds.filter((eqId) =>
|
||||
assignedEquipment.some((eq) => eq.equipmentId === eqId),
|
||||
);
|
||||
|
||||
if (!isContainerAssigned && conflictingEquipmentIds.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const requestStart = startTimestamp.toDate();
|
||||
const requestEnd = endTimestamp.toDate();
|
||||
|
||||
const installationTime = event.InstallationTime || 0;
|
||||
const disassemblyTime = event.DisassemblyTime || 0;
|
||||
|
||||
const eventStartWithSetup = new Date(eventStart);
|
||||
eventStartWithSetup.setHours(eventStartWithSetup.getHours() - installationTime);
|
||||
|
||||
const eventEndWithTeardown = new Date(eventEnd);
|
||||
eventEndWithTeardown.setHours(eventEndWithTeardown.getHours() + disassemblyTime);
|
||||
|
||||
const hasOverlap = requestStart < eventEndWithTeardown && requestEnd > eventStartWithSetup;
|
||||
|
||||
if (hasOverlap) {
|
||||
const overlapStart = new Date(Math.max(requestStart, eventStartWithSetup));
|
||||
const overlapEnd = new Date(Math.min(requestEnd, eventEndWithTeardown));
|
||||
const overlapDays = Math.ceil((overlapEnd - overlapStart) / (1000 * 60 * 60 * 24));
|
||||
|
||||
const conflictInfo = {
|
||||
eventId: eventDoc.id,
|
||||
eventName: event.Name,
|
||||
startDate: eventStart.toISOString(),
|
||||
endDate: eventEnd.toISOString(),
|
||||
overlapDays: overlapDays,
|
||||
};
|
||||
|
||||
if (isContainerAssigned) {
|
||||
containerConflicts.push(conflictInfo);
|
||||
}
|
||||
|
||||
conflictingEquipmentIds.forEach((eqId) => {
|
||||
if (!equipmentConflicts[eqId]) {
|
||||
equipmentConflicts[eqId] = [];
|
||||
}
|
||||
equipmentConflicts[eqId].push(conflictInfo);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const hasContainerConflict = containerConflicts.length > 0;
|
||||
const hasPartialConflict = Object.keys(equipmentConflicts).length > 0 && !hasContainerConflict;
|
||||
const conflictType = hasContainerConflict ? "complete" : (hasPartialConflict ? "partial" : "none");
|
||||
|
||||
res.status(200).json({
|
||||
conflictType,
|
||||
containerConflicts,
|
||||
equipmentConflicts,
|
||||
isAvailable: conflictType === "none",
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error checking container availability:", error);
|
||||
res.status(500).json({error: error.message || "Failed to check container availability"});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupère tous les équipements et conteneurs en conflit pour une période donnée
|
||||
exports.getConflictingEquipmentIds = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {startDate, endDate, excludeEventId, installationTime = 0, disassemblyTime = 0} = req.body.data;
|
||||
|
||||
if (!startDate || !endDate) {
|
||||
res.status(400).json({error: "startDate and endDate are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`Getting conflicting equipment IDs for period ${startDate} to ${endDate}`);
|
||||
|
||||
const requestStartDate = new Date(startDate);
|
||||
requestStartDate.setHours(requestStartDate.getHours() - installationTime);
|
||||
|
||||
const requestEndDate = new Date(endDate);
|
||||
requestEndDate.setHours(requestEndDate.getHours() + disassemblyTime);
|
||||
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
logger.info(`Found ${eventsSnapshot.docs.length} events to check`);
|
||||
|
||||
const equipmentsSnapshot = await db.collection("equipments").get();
|
||||
const equipmentsInfo = {};
|
||||
equipmentsSnapshot.docs.forEach((doc) => {
|
||||
const data = doc.data();
|
||||
equipmentsInfo[doc.id] = {
|
||||
category: data.category,
|
||||
totalQuantity: data.totalQuantity || 0,
|
||||
hasQuantity: data.category === "CABLE" || data.category === "CONSUMABLE",
|
||||
};
|
||||
});
|
||||
|
||||
const conflictingEquipmentIds = new Set();
|
||||
const conflictingContainerIds = new Set();
|
||||
const conflictDetails = {};
|
||||
const equipmentQuantities = {};
|
||||
|
||||
for (const eventDoc of eventsSnapshot.docs) {
|
||||
if (excludeEventId && eventDoc.id === excludeEventId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const event = eventDoc.data();
|
||||
|
||||
let eventStart; let eventEnd;
|
||||
if (event.StartDateTime) {
|
||||
eventStart = event.StartDateTime.toDate ? event.StartDateTime.toDate() : new Date(event.StartDateTime);
|
||||
}
|
||||
if (event.EndDateTime) {
|
||||
eventEnd = event.EndDateTime.toDate ? event.EndDateTime.toDate() : new Date(event.EndDateTime);
|
||||
}
|
||||
|
||||
if (!eventStart || !eventEnd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const eventInstallTime = event.InstallationTime || 0;
|
||||
const eventDisassemblyTime = event.DisassemblyTime || 0;
|
||||
|
||||
const eventStartWithSetup = new Date(eventStart);
|
||||
eventStartWithSetup.setHours(eventStartWithSetup.getHours() - eventInstallTime);
|
||||
|
||||
const eventEndWithTeardown = new Date(eventEnd);
|
||||
eventEndWithTeardown.setHours(eventEndWithTeardown.getHours() + eventDisassemblyTime);
|
||||
|
||||
const hasOverlap = requestStartDate < eventEndWithTeardown && requestEndDate > eventStartWithSetup;
|
||||
|
||||
if (!hasOverlap) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const assignedEquipment = event.assignedEquipment || [];
|
||||
const assignedContainers = event.assignedContainers || [];
|
||||
|
||||
const conflictInfo = {
|
||||
eventId: eventDoc.id,
|
||||
eventName: event.Name,
|
||||
startDate: eventStart.toISOString(),
|
||||
endDate: eventEnd.toISOString(),
|
||||
};
|
||||
|
||||
for (const eq of assignedEquipment) {
|
||||
const equipmentId = eq.equipmentId;
|
||||
const quantity = eq.quantity || 1;
|
||||
const equipInfo = equipmentsInfo[equipmentId];
|
||||
|
||||
if (equipInfo && equipInfo.hasQuantity) {
|
||||
if (!equipmentQuantities[equipmentId]) {
|
||||
equipmentQuantities[equipmentId] = {
|
||||
totalQuantity: equipInfo.totalQuantity,
|
||||
reservedQuantity: 0,
|
||||
availableQuantity: equipInfo.totalQuantity,
|
||||
reservations: [],
|
||||
};
|
||||
}
|
||||
|
||||
equipmentQuantities[equipmentId].reservedQuantity += quantity;
|
||||
equipmentQuantities[equipmentId].availableQuantity = equipInfo.totalQuantity - equipmentQuantities[equipmentId].reservedQuantity;
|
||||
equipmentQuantities[equipmentId].reservations.push({
|
||||
...conflictInfo,
|
||||
quantity: quantity,
|
||||
});
|
||||
|
||||
if (equipmentQuantities[equipmentId].availableQuantity <= 0) {
|
||||
conflictingEquipmentIds.add(equipmentId);
|
||||
}
|
||||
} else {
|
||||
conflictingEquipmentIds.add(equipmentId);
|
||||
}
|
||||
|
||||
if (!conflictDetails[equipmentId]) {
|
||||
conflictDetails[equipmentId] = [];
|
||||
}
|
||||
conflictDetails[equipmentId].push({
|
||||
...conflictInfo,
|
||||
quantity: quantity,
|
||||
});
|
||||
}
|
||||
|
||||
for (const containerId of assignedContainers) {
|
||||
conflictingContainerIds.add(containerId);
|
||||
|
||||
if (!conflictDetails[containerId]) {
|
||||
conflictDetails[containerId] = [];
|
||||
}
|
||||
conflictDetails[containerId].push(conflictInfo);
|
||||
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (containerDoc.exists) {
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
|
||||
for (const equipmentId of equipmentIds) {
|
||||
conflictingEquipmentIds.add(equipmentId);
|
||||
|
||||
if (!conflictDetails[equipmentId]) {
|
||||
conflictDetails[equipmentId] = [];
|
||||
}
|
||||
conflictDetails[equipmentId].push({
|
||||
...conflictInfo,
|
||||
viaContainer: containerId,
|
||||
viaContainerName: containerData.name || "Conteneur inconnu",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`Found ${conflictingEquipmentIds.size} conflicting equipment(s) and ${conflictingContainerIds.size} conflicting container(s)`);
|
||||
|
||||
res.status(200).json({
|
||||
conflictingEquipmentIds: Array.from(conflictingEquipmentIds),
|
||||
conflictingContainerIds: Array.from(conflictingContainerIds),
|
||||
conflictDetails: conflictDetails,
|
||||
equipmentQuantities: equipmentQuantities,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error getting conflicting equipment IDs:", error);
|
||||
res.status(500).json({error: error.message || "Failed to get conflicting equipment IDs"});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Trouver des alternatives (même modèle) disponibles pour une période donnée
|
||||
*/
|
||||
exports.findAlternativeEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {model, startDate, endDate} = req.body.data;
|
||||
|
||||
if (!model || !startDate || !endDate) {
|
||||
res.status(400).json({error: "model, startDate and endDate are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const start = admin.firestore.Timestamp.fromDate(new Date(startDate));
|
||||
const end = admin.firestore.Timestamp.fromDate(new Date(endDate));
|
||||
|
||||
// Récupérer tous les équipements du même modèle
|
||||
const equipmentsSnapshot = await db.collection("equipments")
|
||||
.where("model", "==", model)
|
||||
.get();
|
||||
|
||||
// Récupérer tous les événements qui chevauchent la période
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("StartDateTime", "<=", end)
|
||||
.where("EndDateTime", ">=", start)
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
// Créer un set des équipements en conflit
|
||||
const conflictingEquipmentIds = new Set();
|
||||
eventsSnapshot.docs.forEach((doc) => {
|
||||
const eventData = doc.data();
|
||||
const assignedEquipment = eventData.assignedEquipment || [];
|
||||
assignedEquipment.forEach((eq) => conflictingEquipmentIds.add(eq.equipmentId));
|
||||
});
|
||||
|
||||
// Filtrer les équipements disponibles
|
||||
const alternatives = [];
|
||||
equipmentsSnapshot.docs.forEach((doc) => {
|
||||
const data = doc.data();
|
||||
if (!conflictingEquipmentIds.has(doc.id) && data.status === "available") {
|
||||
alternatives.push({
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(data, ["purchaseDate", "nextMaintenanceDate", "lastMaintenanceDate", "createdAt", "updatedAt"]),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).json({alternatives});
|
||||
} catch (error) {
|
||||
logger.error("Error finding alternative equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculer le statut réel d'un ou plusieurs équipements basé sur les événements en cours
|
||||
*/
|
||||
exports.calculateEquipmentStatuses = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentIds} = req.body.data;
|
||||
|
||||
if (!equipmentIds || !Array.isArray(equipmentIds)) {
|
||||
res.status(400).json({error: "equipmentIds array is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer tous les événements en cours (préparation complétée mais pas encore retournés)
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
const equipmentIdsInUse = new Set();
|
||||
const containerIdsInUse = new Set();
|
||||
|
||||
eventsSnapshot.docs.forEach((doc) => {
|
||||
const event = doc.data();
|
||||
|
||||
const isPrepared = event.preparationStatus === "completed" ||
|
||||
event.preparationStatus === "completedWithMissing";
|
||||
const isReturned = event.returnStatus === "completed" ||
|
||||
event.returnStatus === "completedWithMissing";
|
||||
|
||||
if (isPrepared && !isReturned) {
|
||||
// Ajouter les équipements directs
|
||||
const assignedEquipment = event.assignedEquipment || [];
|
||||
assignedEquipment.forEach((eq) => equipmentIdsInUse.add(eq.equipmentId));
|
||||
|
||||
// Ajouter les conteneurs
|
||||
const assignedContainers = event.assignedContainers || [];
|
||||
assignedContainers.forEach((containerId) => containerIdsInUse.add(containerId));
|
||||
}
|
||||
});
|
||||
|
||||
// Récupérer les équipements dans les conteneurs en cours d'utilisation
|
||||
if (containerIdsInUse.size > 0) {
|
||||
const containersSnapshot = await db.collection("containers")
|
||||
.where(admin.firestore.FieldPath.documentId(), "in", Array.from(containerIdsInUse))
|
||||
.get();
|
||||
|
||||
containersSnapshot.docs.forEach((doc) => {
|
||||
const containerData = doc.data();
|
||||
const equipmentList = containerData.equipment || [];
|
||||
equipmentList.forEach((eq) => equipmentIdsInUse.add(eq.equipmentId));
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les données des équipements demandés
|
||||
const statuses = {};
|
||||
|
||||
for (const equipmentId of equipmentIds) {
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
|
||||
if (!equipmentDoc.exists) {
|
||||
statuses[equipmentId] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
const equipmentData = equipmentDoc.data();
|
||||
let calculatedStatus = equipmentData.status;
|
||||
|
||||
// Si l'équipement est perdu ou HS, garder ce statut
|
||||
if (equipmentData.status === "lost" || equipmentData.status === "outOfService") {
|
||||
calculatedStatus = equipmentData.status;
|
||||
} else if (equipmentIdsInUse.has(equipmentId)) {
|
||||
calculatedStatus = "inUse";
|
||||
} else if (equipmentData.status === "maintenance" ||
|
||||
equipmentData.status === "rented") {
|
||||
calculatedStatus = equipmentData.status;
|
||||
} else {
|
||||
calculatedStatus = "available";
|
||||
}
|
||||
|
||||
statuses[equipmentId] = calculatedStatus;
|
||||
}
|
||||
|
||||
res.status(200).json({statuses});
|
||||
} catch (error) {
|
||||
logger.error("Error calculating equipment statuses:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Récupérer tous les événements en cours (pour le calcul de statuts)
|
||||
*/
|
||||
exports.getActiveEvents = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "view_events");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_events permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer les événements en cours (préparation complétée mais pas encore retournés)
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
const activeEvents = [];
|
||||
|
||||
eventsSnapshot.docs.forEach((doc) => {
|
||||
const event = doc.data();
|
||||
|
||||
const isPrepared = event.preparationStatus === "completed" ||
|
||||
event.preparationStatus === "completedWithMissing";
|
||||
const isReturned = event.returnStatus === "completed" ||
|
||||
event.returnStatus === "completedWithMissing";
|
||||
|
||||
if (isPrepared && !isReturned) {
|
||||
activeEvents.push({
|
||||
id: doc.id,
|
||||
assignedEquipment: event.assignedEquipment || [],
|
||||
assignedContainers: event.assignedContainers || [],
|
||||
preparationStatus: event.preparationStatus,
|
||||
returnStatus: event.returnStatus,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).json({events: activeEvents});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching active events:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,504 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Créer un container
|
||||
exports.createContainer = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerData = req.body.data;
|
||||
const containerId = containerData.id;
|
||||
|
||||
if (!containerId) {
|
||||
res.status(400).json({error: "Container ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const existingDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (existingDoc.exists) {
|
||||
res.status(409).json({error: "Container ID already exists"});
|
||||
return;
|
||||
}
|
||||
|
||||
const dataToSave = helpers.deserializeTimestamps(containerData, ["createdAt", "updatedAt"]);
|
||||
|
||||
await db.collection("containers").doc(containerId).set(dataToSave);
|
||||
|
||||
res.status(201).json({id: containerId, message: "Container created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating container:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour un container
|
||||
exports.updateContainer = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerId, data} = req.body.data;
|
||||
|
||||
if (!containerId) {
|
||||
res.status(400).json({error: "Container ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
delete data.id;
|
||||
data.updatedAt = admin.firestore.Timestamp.now();
|
||||
|
||||
await db.collection("containers").doc(containerId).update(data);
|
||||
|
||||
res.status(200).json({message: "Container updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating container:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer un container
|
||||
exports.deleteContainer = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerId} = req.body.data;
|
||||
|
||||
if (!containerId) {
|
||||
res.status(400).json({error: "Container ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer le container pour obtenir les équipements
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (containerDoc.exists) {
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
|
||||
// Retirer le container des parentBoxIds de chaque équipement
|
||||
for (const equipmentId of equipmentIds) {
|
||||
try {
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (equipmentDoc.exists) {
|
||||
const equipmentData = equipmentDoc.data();
|
||||
const parentBoxIds = (equipmentData.parentBoxIds || []).filter((boxId) => boxId !== containerId);
|
||||
await db.collection("equipments").doc(equipmentId).update({
|
||||
parentBoxIds: parentBoxIds,
|
||||
updatedAt: admin.firestore.Timestamp.now(),
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(`Error updating equipment ${equipmentId} when deleting container:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await db.collection("containers").doc(containerId).delete();
|
||||
|
||||
res.status(200).json({message: "Container deleted successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting container:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer les containers contenant un équipement
|
||||
exports.getContainersByEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasViewAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
const hasManageAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasViewAccess && !hasManageAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentId} = req.body.data || {};
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "equipmentId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = await db.collection("containers")
|
||||
.where("equipmentIds", "array-contains", equipmentId)
|
||||
.get();
|
||||
|
||||
const containers = [];
|
||||
snapshot.forEach((doc) => {
|
||||
let data = {id: doc.id, ...doc.data()};
|
||||
data = helpers.serializeTimestamps(data);
|
||||
data = helpers.serializeReferences(data);
|
||||
containers.push(data);
|
||||
});
|
||||
|
||||
res.status(200).json({containers});
|
||||
} catch (error) {
|
||||
logger.error("Error getting containers by equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer plusieurs containers par leurs IDs
|
||||
exports.getContainersByIds = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasViewAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
const hasManageAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasViewAccess && !hasManageAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerIds} = req.body.data || {};
|
||||
|
||||
if (!containerIds || !Array.isArray(containerIds) || containerIds.length === 0) {
|
||||
res.status(400).json({error: "containerIds array is required and must not be empty"});
|
||||
return;
|
||||
}
|
||||
|
||||
if (containerIds.length > 100) {
|
||||
res.status(400).json({error: "Maximum 100 container IDs per request"});
|
||||
return;
|
||||
}
|
||||
|
||||
const promises = containerIds.map((id) => db.collection("containers").doc(id).get());
|
||||
const docs = await Promise.all(promises);
|
||||
|
||||
const containers = [];
|
||||
for (const doc of docs) {
|
||||
if (doc.exists) {
|
||||
let data = {id: doc.id, ...doc.data()};
|
||||
data = helpers.serializeTimestamps(data);
|
||||
data = helpers.serializeReferences(data);
|
||||
containers.push(data);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({containers});
|
||||
} catch (error) {
|
||||
logger.error("Error getting containers by IDs:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Ajouter un équipement à un container
|
||||
exports.addEquipmentToContainer = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerId, equipmentId, userId} = req.body.data;
|
||||
|
||||
if (!containerId || !equipmentId) {
|
||||
res.status(400).json({error: "containerId and equipmentId are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (!containerDoc.exists) {
|
||||
res.status(404).json({success: false, message: "Container non trouvé"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
|
||||
if (equipmentIds.includes(equipmentId)) {
|
||||
res.status(400).json({success: false, message: "Cet équipement est déjà dans ce container"});
|
||||
return;
|
||||
}
|
||||
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (!equipmentDoc.exists) {
|
||||
res.status(404).json({success: false, message: "Équipement non trouvé"});
|
||||
return;
|
||||
}
|
||||
|
||||
const equipmentData = equipmentDoc.data();
|
||||
const parentBoxIds = equipmentData.parentBoxIds || [];
|
||||
|
||||
const warnings = [];
|
||||
if (parentBoxIds.length > 0) {
|
||||
const otherContainersPromises = parentBoxIds.map((boxId) =>
|
||||
db.collection("containers").doc(boxId).get(),
|
||||
);
|
||||
const otherContainersDocs = await Promise.all(otherContainersPromises);
|
||||
const otherNames = otherContainersDocs
|
||||
.filter((doc) => doc.exists)
|
||||
.map((doc) => doc.data().name);
|
||||
|
||||
if (otherNames.length > 0) {
|
||||
warnings.push(`Attention : cet équipement est également dans les boites suivants : ${otherNames.join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
await db.collection("containers").doc(containerId).update({
|
||||
equipmentIds: [...equipmentIds, equipmentId],
|
||||
updatedAt: admin.firestore.Timestamp.now(),
|
||||
});
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).update({
|
||||
parentBoxIds: [...parentBoxIds, containerId],
|
||||
updatedAt: admin.firestore.Timestamp.now(),
|
||||
});
|
||||
|
||||
const history = containerData.history || [];
|
||||
const historyEntry = {
|
||||
timestamp: admin.firestore.Timestamp.now(),
|
||||
action: "equipment_added",
|
||||
equipmentId: equipmentId,
|
||||
newValue: equipmentId,
|
||||
userId: userId || decodedToken.uid,
|
||||
};
|
||||
|
||||
const updatedHistory = [...history, historyEntry].slice(-100);
|
||||
|
||||
await db.collection("containers").doc(containerId).update({
|
||||
history: updatedHistory,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: "Équipement ajouté avec succès",
|
||||
warnings: warnings.length > 0 ? warnings[0] : null,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error adding equipment to container:", error);
|
||||
res.status(500).json({success: false, message: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Retirer un équipement d'un container
|
||||
exports.removeEquipmentFromContainer = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {containerId, equipmentId, userId} = req.body.data;
|
||||
|
||||
if (!containerId || !equipmentId) {
|
||||
res.status(400).json({error: "containerId and equipmentId are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerDoc = await db.collection("containers").doc(containerId).get();
|
||||
if (!containerDoc.exists) {
|
||||
res.status(404).json({error: "Container non trouvé"});
|
||||
return;
|
||||
}
|
||||
|
||||
const containerData = containerDoc.data();
|
||||
const equipmentIds = containerData.equipmentIds || [];
|
||||
|
||||
const updatedEquipmentIds = equipmentIds.filter((id) => id !== equipmentId);
|
||||
|
||||
await db.collection("containers").doc(containerId).update({
|
||||
equipmentIds: updatedEquipmentIds,
|
||||
updatedAt: admin.firestore.Timestamp.now(),
|
||||
});
|
||||
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (equipmentDoc.exists) {
|
||||
const equipmentData = equipmentDoc.data();
|
||||
const parentBoxIds = equipmentData.parentBoxIds || [];
|
||||
const updatedParentBoxIds = parentBoxIds.filter((id) => id !== containerId);
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).update({
|
||||
parentBoxIds: updatedParentBoxIds,
|
||||
updatedAt: admin.firestore.Timestamp.now(),
|
||||
});
|
||||
}
|
||||
|
||||
const history = containerData.history || [];
|
||||
const historyEntry = {
|
||||
timestamp: admin.firestore.Timestamp.now(),
|
||||
action: "equipment_removed",
|
||||
equipmentId: equipmentId,
|
||||
previousValue: equipmentId,
|
||||
userId: userId || decodedToken.uid,
|
||||
};
|
||||
|
||||
const updatedHistory = [...history, historyEntry].slice(-100);
|
||||
|
||||
await db.collection("containers").doc(containerId).update({
|
||||
history: updatedHistory,
|
||||
});
|
||||
|
||||
res.status(200).json({success: true});
|
||||
} catch (error) {
|
||||
logger.error("Error removing equipment from container:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer les containers avec pagination et filtrage côté serveur
|
||||
exports.getContainersPaginated = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
// Vérifier les permissions
|
||||
const canView = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!canView) {
|
||||
res.status(403).json({error: "Forbidden: Requires equipment permissions"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer les paramètres de la requête
|
||||
const params = req.method === "GET" ? req.query : (req.body?.data || {});
|
||||
const limit = Math.min(parseInt(params.limit) || 20, 100);
|
||||
const startAfterId = params.startAfter || null;
|
||||
const type = params.type ? params.type.toUpperCase() : null;
|
||||
const status = params.status ? params.status.toUpperCase() : null;
|
||||
const searchQuery = params.searchQuery?.toLowerCase() || null;
|
||||
const category = params.category ? params.category.toUpperCase() : null;
|
||||
const sortBy = params.sortBy || "id";
|
||||
const sortOrder = params.sortOrder === "desc" ? "desc" : "asc";
|
||||
|
||||
logger.info(`[getContainersPaginated] Params: limit=${limit}, startAfter=${startAfterId}, type=${type}, status=${status}, category=${category}, search=${searchQuery}`);
|
||||
|
||||
let query = db.collection("containers");
|
||||
|
||||
const queryLimit = (searchQuery || category) ? Math.min(limit * 10, 200) : limit;
|
||||
|
||||
if (type) {
|
||||
query = query.where("type", "==", type);
|
||||
}
|
||||
if (status) {
|
||||
query = query.where("status", "==", status);
|
||||
}
|
||||
|
||||
if (sortBy === "id") {
|
||||
query = query.orderBy(admin.firestore.FieldPath.documentId(), sortOrder);
|
||||
} else {
|
||||
query = query.orderBy(sortBy, sortOrder);
|
||||
}
|
||||
|
||||
if (startAfterId) {
|
||||
const startAfterDoc = await db.collection("containers").doc(startAfterId).get();
|
||||
if (startAfterDoc.exists) {
|
||||
query = query.startAfter(startAfterDoc);
|
||||
}
|
||||
}
|
||||
|
||||
query = query.limit(queryLimit + 1);
|
||||
|
||||
const snapshot = await query.get();
|
||||
|
||||
const rawDocCount = snapshot.docs.length;
|
||||
const hasMoreDocs = rawDocCount > queryLimit;
|
||||
const docsToProcess = hasMoreDocs ? snapshot.docs.slice(0, queryLimit) : snapshot.docs;
|
||||
|
||||
let containers = docsToProcess.map((doc) => {
|
||||
const data = doc.data();
|
||||
return {
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(data, ["createdAt", "updatedAt"]),
|
||||
};
|
||||
});
|
||||
|
||||
const allEquipmentIds = new Set();
|
||||
containers.forEach((c) => {
|
||||
if (c.equipmentIds && Array.isArray(c.equipmentIds)) {
|
||||
c.equipmentIds.forEach((id) => allEquipmentIds.add(id));
|
||||
}
|
||||
});
|
||||
|
||||
const equipmentMap = new Map();
|
||||
if (allEquipmentIds.size > 0) {
|
||||
const equipmentIdArray = Array.from(allEquipmentIds);
|
||||
const batchSize = 30;
|
||||
|
||||
for (let i = 0; i < equipmentIdArray.length; i += batchSize) {
|
||||
const batch = equipmentIdArray.slice(i, i + batchSize);
|
||||
const equipmentSnapshot = await db.collection("equipments")
|
||||
.where(admin.firestore.FieldPath.documentId(), "in", batch)
|
||||
.get();
|
||||
|
||||
equipmentSnapshot.docs.forEach((doc) => {
|
||||
const equipmentData = doc.data();
|
||||
equipmentMap.set(doc.id, {
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(equipmentData),
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
containers = containers.map((container) => ({
|
||||
...container,
|
||||
equipment: (container.equipmentIds || [])
|
||||
.map((eqId) => equipmentMap.get(eqId))
|
||||
.filter((eq) => eq !== undefined),
|
||||
}));
|
||||
|
||||
if (category) {
|
||||
containers = containers.filter((c) => {
|
||||
return c.equipment.some((eq) => eq.category === category);
|
||||
});
|
||||
}
|
||||
|
||||
if (searchQuery) {
|
||||
containers = containers.filter((c) => {
|
||||
const searchableText = [
|
||||
c.name || "",
|
||||
c.id || "",
|
||||
...(c.equipment || []).map((eq) => eq.name || ""),
|
||||
].join(" ").toLowerCase();
|
||||
return searchableText.includes(searchQuery);
|
||||
});
|
||||
}
|
||||
|
||||
const limitedContainers = containers.slice(0, limit);
|
||||
const lastVisible = limitedContainers.length > 0 ? limitedContainers[limitedContainers.length - 1].id : null;
|
||||
|
||||
const totalEquipmentCount = limitedContainers.reduce((sum, c) => sum + (c.equipment?.length || 0), 0);
|
||||
logger.info(`[getContainersPaginated] Returning ${limitedContainers.length} containers with ${totalEquipmentCount} total equipment(s)`);
|
||||
|
||||
limitedContainers.forEach((c) => {
|
||||
logger.info(` - Container ${c.id}: ${c.equipment?.length || 0} equipment(s)`);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
containers: limitedContainers,
|
||||
hasMore: containers.length > limit || hasMoreDocs,
|
||||
lastVisible,
|
||||
total: limitedContainers.length,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching paginated containers:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,668 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Créer un équipement (admin ou manage_equipment)
|
||||
exports.createEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const equipmentData = req.body.data;
|
||||
const equipmentId = equipmentData.id;
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "Equipment ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifier unicité de l'ID
|
||||
const existingDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (existingDoc.exists) {
|
||||
res.status(409).json({error: "Equipment ID already exists"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Convertir les timestamps
|
||||
const dataToSave = helpers.deserializeTimestamps(equipmentData, [
|
||||
"createdAt", "updatedAt", "purchaseDate", "lastMaintenanceDate", "nextMaintenanceDate",
|
||||
]);
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).set(dataToSave);
|
||||
|
||||
res.status(201).json({id: equipmentId, message: "Equipment created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour un équipement
|
||||
exports.updateEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentId, data} = req.body.data;
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "Equipment ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data || typeof data !== "object" || Object.keys(data).length === 0) {
|
||||
res.status(400).json({error: "Update data is required and must be a non-empty object"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Empêcher la modification de l'ID
|
||||
delete data.id;
|
||||
|
||||
// Ajouter updatedAt
|
||||
data.updatedAt = admin.firestore.Timestamp.now();
|
||||
|
||||
const dataToSave = helpers.deserializeTimestamps(data, [
|
||||
"purchaseDate", "lastMaintenanceDate", "nextMaintenanceDate",
|
||||
]);
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).update(dataToSave);
|
||||
|
||||
res.status(200).json({message: "Equipment updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer un équipement
|
||||
exports.deleteEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentId, forceDelete = false} = req.body.data;
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "Equipment ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifier si l'équipement est utilisé dans des événements à venir
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("status", "!=", "CANCELLED")
|
||||
.get();
|
||||
|
||||
const now = new Date();
|
||||
const upcomingEvents = [];
|
||||
|
||||
for (const eventDoc of eventsSnapshot.docs) {
|
||||
const eventData = eventDoc.data();
|
||||
const assignedEquipment = eventData.assignedEquipment || [];
|
||||
|
||||
if (!assignedEquipment.some((eq) => eq.equipmentId === equipmentId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let eventStart = null;
|
||||
if (eventData.StartDateTime) {
|
||||
eventStart = eventData.StartDateTime.toDate ?
|
||||
eventData.StartDateTime.toDate() :
|
||||
new Date(eventData.StartDateTime);
|
||||
}
|
||||
|
||||
if (eventStart && eventStart > now) {
|
||||
upcomingEvents.push({
|
||||
eventId: eventDoc.id,
|
||||
eventName: eventData.Name || "",
|
||||
startDate: eventStart.toISOString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (upcomingEvents.length > 0 && !forceDelete) {
|
||||
res.status(409).json({
|
||||
error: "FUTURE_EVENT_ASSIGNMENT: Cannot delete equipment because it is assigned to upcoming events",
|
||||
upcomingEvents,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).delete();
|
||||
|
||||
res.status(200).json({message: "Equipment deleted successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer un équipement par ID
|
||||
exports.getEquipment = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasViewAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
const hasManageAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasViewAccess && !hasManageAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentId} = req.body.data || req.query;
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "Equipment ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const doc = await db.collection("equipments").doc(equipmentId).get();
|
||||
|
||||
if (!doc.exists) {
|
||||
res.status(404).json({error: "Equipment not found"});
|
||||
return;
|
||||
}
|
||||
|
||||
let data = {id: doc.id, ...doc.data()};
|
||||
data = helpers.serializeTimestamps(data);
|
||||
data = helpers.serializeReferences(data);
|
||||
|
||||
// Masquer les prix si pas de permission manage_equipment
|
||||
data = helpers.maskSensitiveFields(data, hasManageAccess);
|
||||
|
||||
res.status(200).json({equipment: data});
|
||||
} catch (error) {
|
||||
logger.error("Error getting equipment:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer plusieurs équipements par leurs IDs
|
||||
exports.getEquipmentsByIds = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasViewAccess = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
const hasManageAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasViewAccess && !hasManageAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {equipmentIds} = req.body.data || {};
|
||||
|
||||
if (!equipmentIds || !Array.isArray(equipmentIds) || equipmentIds.length === 0) {
|
||||
res.status(400).json({error: "equipmentIds array is required and must not be empty"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Limiter à 100 équipements max par requête
|
||||
if (equipmentIds.length > 100) {
|
||||
res.status(400).json({error: "Maximum 100 equipment IDs per request"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer tous les documents en parallèle
|
||||
const promises = equipmentIds.map((id) => db.collection("equipments").doc(id).get());
|
||||
const docs = await Promise.all(promises);
|
||||
|
||||
const equipments = [];
|
||||
for (const doc of docs) {
|
||||
if (doc.exists) {
|
||||
let data = {id: doc.id, ...doc.data()};
|
||||
data = helpers.serializeTimestamps(data);
|
||||
data = helpers.serializeReferences(data);
|
||||
// Masquer les prix si pas de permission manage_equipment
|
||||
data = helpers.maskSensitiveFields(data, hasManageAccess);
|
||||
equipments.push(data);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({equipments});
|
||||
} catch (error) {
|
||||
logger.error("Error getting equipments by IDs:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour uniquement le statut d'un équipement
|
||||
exports.updateEquipmentStatusOnly = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const {equipmentId, status, availableQuantity} = req.body.data;
|
||||
|
||||
if (!equipmentId) {
|
||||
res.status(400).json({error: "Equipment ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifier les permissions
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const updateData = {updatedAt: admin.firestore.Timestamp.now()};
|
||||
if (status) updateData.status = status;
|
||||
if (availableQuantity !== undefined) updateData.availableQuantity = availableQuantity;
|
||||
|
||||
await db.collection("equipments").doc(equipmentId).update(updateData);
|
||||
|
||||
res.status(200).json({message: "Equipment status updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating equipment status:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour le statut de plusieurs équipements (pour préparation/retour)
|
||||
exports.updateEquipmentStatus = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const {eventId, updates} = req.body.data;
|
||||
|
||||
if (!eventId || !updates || !Array.isArray(updates)) {
|
||||
res.status(400).json({error: "Event ID and updates array are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est assigné à l'événement ou est admin
|
||||
const isAssigned = await auth.isAssignedToEvent(decodedToken.uid, eventId);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAssigned && !isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Not assigned to this event"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Batch update
|
||||
const batch = db.batch();
|
||||
|
||||
for (const update of updates) {
|
||||
const {equipmentId, status} = update;
|
||||
if (equipmentId && status) {
|
||||
const equipmentRef = db.collection("equipments").doc(equipmentId);
|
||||
batch.update(equipmentRef, {status});
|
||||
}
|
||||
}
|
||||
|
||||
await batch.commit();
|
||||
|
||||
res.status(200).json({message: "Equipment statuses updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating equipment statuses:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupère les équipements avec pagination et filtrage côté serveur
|
||||
exports.getEquipmentsPaginated = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
// Vérifier les permissions
|
||||
const canManage = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
const canView = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!canManage && !canView) {
|
||||
res.status(403).json({error: "Forbidden: Requires equipment permissions"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupérer les paramètres de la requête
|
||||
const params = req.method === "GET" ? req.query : (req.body?.data || {});
|
||||
const limit = Math.min(parseInt(params.limit) || 20, 100);
|
||||
const startAfterId = params.startAfter || null;
|
||||
const category = params.category ? params.category.toUpperCase() : null;
|
||||
const status = params.status ? params.status.toUpperCase() : null;
|
||||
const rawSearchQuery = typeof params.searchQuery === "string" ? params.searchQuery.trim() : "";
|
||||
const searchQuery = rawSearchQuery ? rawSearchQuery.toLowerCase() : null;
|
||||
const compactSearchQuery = searchQuery ? searchQuery.replace(/[\s_-]+/g, "") : null;
|
||||
const sortBy = params.sortBy || "id";
|
||||
const sortOrder = params.sortOrder === "desc" ? "desc" : "asc";
|
||||
|
||||
logger.info(`[getEquipmentsPaginated] Params: limit=${limit}, startAfter=${startAfterId}, category=${category}, status=${status}, search=${searchQuery}`);
|
||||
|
||||
// Fast-path pour une recherche d'ID exact
|
||||
if (searchQuery && !startAfterId) {
|
||||
const exactIdCandidates = Array.from(new Set([
|
||||
rawSearchQuery,
|
||||
rawSearchQuery.toUpperCase(),
|
||||
rawSearchQuery.toLowerCase(),
|
||||
].filter(Boolean)));
|
||||
|
||||
for (const candidateId of exactIdCandidates) {
|
||||
const exactDoc = await db.collection("equipments").doc(candidateId).get();
|
||||
if (!exactDoc.exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const exactData = exactDoc.data() || {};
|
||||
const matchesCategory = !category || exactData.category === category;
|
||||
const matchesStatus = !status || exactData.status === status;
|
||||
if (!matchesCategory || !matchesStatus) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!canManage) {
|
||||
delete exactData.purchasePrice;
|
||||
delete exactData.rentalPrice;
|
||||
}
|
||||
|
||||
const exactEquipment = {
|
||||
...helpers.serializeTimestamps(exactData, ["purchaseDate", "nextMaintenanceDate", "lastMaintenanceDate", "createdAt", "updatedAt"]),
|
||||
id: exactDoc.id,
|
||||
};
|
||||
|
||||
logger.info(`[getEquipmentsPaginated] Exact ID hit for ${exactDoc.id}`);
|
||||
res.status(200).json({
|
||||
equipments: [exactEquipment],
|
||||
hasMore: false,
|
||||
lastVisible: exactDoc.id,
|
||||
total: 1,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Compatibilité legacy
|
||||
for (const legacyId of exactIdCandidates) {
|
||||
let legacyIdQuery = db.collection("equipments").where("id", "==", legacyId);
|
||||
if (category) {
|
||||
legacyIdQuery = legacyIdQuery.where("category", "==", category);
|
||||
}
|
||||
if (status) {
|
||||
legacyIdQuery = legacyIdQuery.where("status", "==", status);
|
||||
}
|
||||
|
||||
const legacySnapshot = await legacyIdQuery.limit(1).get();
|
||||
if (legacySnapshot.empty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const exactDoc = legacySnapshot.docs[0];
|
||||
const exactData = exactDoc.data() || {};
|
||||
|
||||
if (!canManage) {
|
||||
delete exactData.purchasePrice;
|
||||
delete exactData.rentalPrice;
|
||||
}
|
||||
|
||||
const exactEquipment = {
|
||||
...helpers.serializeTimestamps(exactData, ["purchaseDate", "nextMaintenanceDate", "lastMaintenanceDate", "createdAt", "updatedAt"]),
|
||||
id: exactDoc.id,
|
||||
};
|
||||
|
||||
logger.info(`[getEquipmentsPaginated] Exact legacy ID hit for ${legacyId} -> ${exactDoc.id}`);
|
||||
res.status(200).json({
|
||||
equipments: [exactEquipment],
|
||||
hasMore: false,
|
||||
lastVisible: exactDoc.id,
|
||||
total: 1,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Construire la requête Firestore
|
||||
let query = db.collection("equipments");
|
||||
|
||||
if (category) {
|
||||
query = query.where("category", "==", category);
|
||||
}
|
||||
if (status) {
|
||||
query = query.where("status", "==", status);
|
||||
}
|
||||
|
||||
if (sortBy === "id") {
|
||||
query = query.orderBy(admin.firestore.FieldPath.documentId(), sortOrder);
|
||||
} else {
|
||||
query = query.orderBy(sortBy, sortOrder);
|
||||
}
|
||||
|
||||
if (startAfterId) {
|
||||
const startAfterDoc = await db.collection("equipments").doc(startAfterId).get();
|
||||
if (startAfterDoc.exists) {
|
||||
query = query.startAfter(startAfterDoc);
|
||||
}
|
||||
}
|
||||
|
||||
const timestampFields = ["purchaseDate", "nextMaintenanceDate", "lastMaintenanceDate", "createdAt", "updatedAt"];
|
||||
|
||||
const mapEquipmentDoc = (doc) => {
|
||||
const data = {...(doc.data() || {})};
|
||||
|
||||
if (!canManage) {
|
||||
delete data.purchasePrice;
|
||||
delete data.rentalPrice;
|
||||
}
|
||||
|
||||
const legacyId = typeof data.id === "string" ? data.id : "";
|
||||
|
||||
return {
|
||||
...helpers.serializeTimestamps(data, timestampFields),
|
||||
id: doc.id,
|
||||
_legacyId: legacyId,
|
||||
};
|
||||
};
|
||||
|
||||
const matchesSearchQuery = (equipment) => {
|
||||
const searchableText = [
|
||||
equipment.name || "",
|
||||
equipment.id || "",
|
||||
equipment._legacyId || "",
|
||||
equipment.model || "",
|
||||
equipment.brand || "",
|
||||
equipment.subCategory || "",
|
||||
].join(" ").toLowerCase();
|
||||
|
||||
if (searchableText.includes(searchQuery)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!compactSearchQuery) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const compactSearchableText = searchableText.replace(/[\s_-]+/g, "");
|
||||
return compactSearchableText.includes(compactSearchQuery);
|
||||
};
|
||||
|
||||
if (!searchQuery) {
|
||||
const snapshot = await query.limit(limit + 1).get();
|
||||
const rawDocCount = snapshot.docs.length;
|
||||
const hasMoreDocs = rawDocCount > limit;
|
||||
const docsToProcess = hasMoreDocs ? snapshot.docs.slice(0, limit) : snapshot.docs;
|
||||
|
||||
const limitedEquipments = docsToProcess
|
||||
.map(mapEquipmentDoc)
|
||||
.map(({_legacyId, ...equipment}) => equipment);
|
||||
const lastVisible = limitedEquipments.length > 0 ? limitedEquipments[limitedEquipments.length - 1].id : null;
|
||||
|
||||
logger.info(`[getEquipmentsPaginated] Firestore returned ${rawDocCount} docs, hasMore=${hasMoreDocs}`);
|
||||
logger.info(`[getEquipmentsPaginated] Returning ${limitedEquipments.length} equipments, hasMore=${hasMoreDocs}`);
|
||||
|
||||
res.status(200).json({
|
||||
equipments: limitedEquipments,
|
||||
hasMore: hasMoreDocs,
|
||||
lastVisible,
|
||||
total: limitedEquipments.length,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const searchBatchSize = Math.min(Math.max(limit * 10, limit), 200);
|
||||
const matchedEquipments = [];
|
||||
let scannedDocuments = 0;
|
||||
let searchQueryRef = query;
|
||||
let hasMoreMatches = false;
|
||||
let hasMoreDocsToScan = true;
|
||||
|
||||
while (hasMoreDocsToScan && !hasMoreMatches) {
|
||||
const snapshot = await searchQueryRef.limit(searchBatchSize).get();
|
||||
|
||||
if (snapshot.empty) {
|
||||
hasMoreDocsToScan = false;
|
||||
break;
|
||||
}
|
||||
|
||||
scannedDocuments += snapshot.docs.length;
|
||||
|
||||
for (const doc of snapshot.docs) {
|
||||
const equipment = mapEquipmentDoc(doc);
|
||||
if (!matchesSearchQuery(equipment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
matchedEquipments.push(equipment);
|
||||
if (matchedEquipments.length > limit) {
|
||||
hasMoreMatches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasMoreMatches) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (snapshot.docs.length < searchBatchSize) {
|
||||
hasMoreDocsToScan = false;
|
||||
break;
|
||||
}
|
||||
|
||||
const lastDocInBatch = snapshot.docs[snapshot.docs.length - 1];
|
||||
searchQueryRef = query.startAfter(lastDocInBatch);
|
||||
}
|
||||
|
||||
const limitedEquipments = matchedEquipments
|
||||
.slice(0, limit)
|
||||
.map(({_legacyId, ...equipment}) => equipment);
|
||||
const lastVisible = limitedEquipments.length > 0 ? limitedEquipments[limitedEquipments.length - 1].id : null;
|
||||
|
||||
logger.info(`[getEquipmentsPaginated] Search scan read ${scannedDocuments} docs and found ${matchedEquipments.length} matches`);
|
||||
logger.info(`[getEquipmentsPaginated] Returning ${limitedEquipments.length} equipments, hasMore=${hasMoreMatches}`);
|
||||
|
||||
res.status(200).json({
|
||||
equipments: limitedEquipments,
|
||||
hasMore: hasMoreMatches,
|
||||
lastVisible,
|
||||
total: limitedEquipments.length,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching paginated equipments:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Recherche rapide d'équipements et containers pour l'autocomplétion
|
||||
exports.quickSearch = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
// Vérifier les permissions
|
||||
const canView = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!canView) {
|
||||
res.status(403).json({error: "Forbidden: Requires view_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const params = req.method === "GET" ? req.query : (req.body?.data || {});
|
||||
const searchQuery = params.query?.toLowerCase() || "";
|
||||
const limit = Math.min(parseInt(params.limit) || 10, 50);
|
||||
const includeEquipments = params.includeEquipments !== "false";
|
||||
const includeContainers = params.includeContainers !== "false";
|
||||
|
||||
if (!searchQuery || searchQuery.length < 2) {
|
||||
res.status(200).json({results: []});
|
||||
return;
|
||||
}
|
||||
|
||||
const results = [];
|
||||
|
||||
// Rechercher dans les équipements
|
||||
if (includeEquipments) {
|
||||
const equipmentSnapshot = await db.collection("equipments")
|
||||
.orderBy("id")
|
||||
.limit(limit * 2)
|
||||
.get();
|
||||
|
||||
equipmentSnapshot.docs.forEach((doc) => {
|
||||
const data = doc.data();
|
||||
const searchableText = [
|
||||
data.name || "",
|
||||
doc.id || "",
|
||||
data.model || "",
|
||||
data.brand || "",
|
||||
].join(" ").toLowerCase();
|
||||
|
||||
if (searchableText.includes(searchQuery)) {
|
||||
results.push({
|
||||
type: "equipment",
|
||||
id: doc.id,
|
||||
name: data.name,
|
||||
category: data.category,
|
||||
model: data.model,
|
||||
brand: data.brand,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Rechercher dans les containers
|
||||
if (includeContainers) {
|
||||
const containerSnapshot = await db.collection("containers")
|
||||
.orderBy("id")
|
||||
.limit(limit * 2)
|
||||
.get();
|
||||
|
||||
containerSnapshot.docs.forEach((doc) => {
|
||||
const data = doc.data();
|
||||
const searchableText = [
|
||||
data.name || "",
|
||||
doc.id || "",
|
||||
].join(" ").toLowerCase();
|
||||
|
||||
if (searchableText.includes(searchQuery)) {
|
||||
results.push({
|
||||
type: "container",
|
||||
id: doc.id,
|
||||
name: data.name,
|
||||
containerType: data.type,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const limitedResults = results
|
||||
.sort((a, b) => {
|
||||
const aStarts = a.id.toLowerCase().startsWith(searchQuery);
|
||||
const bStarts = b.id.toLowerCase().startsWith(searchQuery);
|
||||
if (aStarts && !bStarts) return -1;
|
||||
if (!aStarts && bStarts) return 1;
|
||||
return 0;
|
||||
})
|
||||
.slice(0, limit);
|
||||
|
||||
res.status(200).json({results: limitedResults});
|
||||
} catch (error) {
|
||||
logger.error("Error in quick search:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,328 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Créer une maintenance
|
||||
exports.createMaintenance = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_maintenances");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_maintenances permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const maintenanceData = req.body.data;
|
||||
|
||||
const dataToSave = helpers.deserializeTimestamps(maintenanceData, [
|
||||
"scheduledDate", "completedDate", "createdAt", "updatedAt",
|
||||
]);
|
||||
|
||||
const docRef = await db.collection("maintenances").add(dataToSave);
|
||||
const maintenanceId = docRef.id;
|
||||
|
||||
if (maintenanceData.equipmentIds && Array.isArray(maintenanceData.equipmentIds)) {
|
||||
for (const equipmentId of maintenanceData.equipmentIds) {
|
||||
try {
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (equipmentDoc.exists) {
|
||||
const equipmentData = equipmentDoc.data();
|
||||
const maintenanceIds = equipmentData.maintenanceIds || [];
|
||||
if (!maintenanceIds.includes(maintenanceId)) {
|
||||
maintenanceIds.push(maintenanceId);
|
||||
await db.collection("equipments").doc(equipmentId).update({
|
||||
maintenanceIds: maintenanceIds,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (maintenanceData.scheduledDate) {
|
||||
const scheduledDate = maintenanceData.scheduledDate.toDate ?
|
||||
maintenanceData.scheduledDate.toDate() :
|
||||
new Date(maintenanceData.scheduledDate);
|
||||
const sevenDaysFromNow = new Date();
|
||||
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
|
||||
|
||||
if (scheduledDate <= sevenDaysFromNow) {
|
||||
const existingAlerts = await db.collection("alerts")
|
||||
.where("equipmentId", "==", equipmentId)
|
||||
.where("type", "==", "maintenanceDue")
|
||||
.where("isRead", "==", false)
|
||||
.get();
|
||||
|
||||
let alertExists = false;
|
||||
for (const alertDoc of existingAlerts.docs) {
|
||||
const alertData = alertDoc.data();
|
||||
if (alertData.message && alertData.message.includes(maintenanceData.name || "")) {
|
||||
alertExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!alertExists) {
|
||||
const equipmentName = equipmentDoc.exists ?
|
||||
(equipmentDoc.data().name || equipmentId) :
|
||||
equipmentId;
|
||||
|
||||
const daysUntil = Math.ceil((scheduledDate - new Date()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
await db.collection("alerts").add({
|
||||
type: "maintenanceDue",
|
||||
message: `Maintenance "${maintenanceData.name || "Sans nom"}" prévue dans ${daysUntil} jour(s) pour ${equipmentName}`,
|
||||
equipmentId: equipmentId,
|
||||
createdAt: admin.firestore.Timestamp.now(),
|
||||
isRead: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(`Error updating equipment ${equipmentId} for maintenance:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.status(201).json({id: maintenanceId, message: "Maintenance created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating maintenance:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour une maintenance
|
||||
exports.updateMaintenance = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_maintenances");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_maintenances permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {maintenanceId, data} = req.body.data;
|
||||
|
||||
if (!maintenanceId) {
|
||||
res.status(400).json({error: "Maintenance ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
delete data.id;
|
||||
data.updatedAt = admin.firestore.Timestamp.now();
|
||||
|
||||
const dataToSave = helpers.deserializeTimestamps(data, [
|
||||
"scheduledDate", "completedDate",
|
||||
]);
|
||||
|
||||
await db.collection("maintenances").doc(maintenanceId).update(dataToSave);
|
||||
|
||||
res.status(200).json({message: "Maintenance updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating maintenance:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer toutes les maintenances
|
||||
exports.getMaintenances = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const {equipmentId} = req.body.data || {};
|
||||
|
||||
const canView = await auth.hasPermission(decodedToken.uid, "view_equipment");
|
||||
|
||||
if (!canView) {
|
||||
res.status(403).json({error: "Forbidden: Requires equipment permissions"});
|
||||
return;
|
||||
}
|
||||
|
||||
let query = db.collection("maintenances");
|
||||
|
||||
if (equipmentId) {
|
||||
query = query.where("equipmentIds", "array-contains", equipmentId);
|
||||
}
|
||||
|
||||
const snapshot = await query.get();
|
||||
const maintenances = snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
return {
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(data, ["scheduledDate", "completedDate", "createdAt", "updatedAt"]),
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json({maintenances});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching maintenances:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer une maintenance
|
||||
exports.deleteMaintenance = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
const canManage = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
if (!canManage) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const maintenanceId = req.body.data?.maintenanceId;
|
||||
if (!maintenanceId) {
|
||||
res.status(400).json({error: "maintenanceId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const maintenanceDoc = await db.collection("maintenances").doc(maintenanceId).get();
|
||||
if (maintenanceDoc.exists) {
|
||||
const maintenance = maintenanceDoc.data();
|
||||
|
||||
if (maintenance.equipmentIds) {
|
||||
for (const equipmentId of maintenance.equipmentIds) {
|
||||
const equipmentDoc = await db.collection("equipments").doc(equipmentId).get();
|
||||
if (equipmentDoc.exists) {
|
||||
const equipmentData = equipmentDoc.data();
|
||||
const maintenanceIds = (equipmentData.maintenanceIds || []).filter((id) => id !== maintenanceId);
|
||||
await db.collection("equipments").doc(equipmentId).update({maintenanceIds});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await db.collection("maintenances").doc(maintenanceId).delete();
|
||||
|
||||
res.status(200).json({success: true});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting maintenance:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Vérifier les maintenances à venir et créer des alertes
|
||||
*/
|
||||
exports.checkUpcomingMaintenances = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const sevenDaysFromNow = new Date();
|
||||
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
|
||||
|
||||
const now = admin.firestore.Timestamp.now();
|
||||
const sevenDaysTimestamp = admin.firestore.Timestamp.fromDate(sevenDaysFromNow);
|
||||
|
||||
// Récupérer les maintenances planifiées dans les 7 prochains jours
|
||||
const maintenancesSnapshot = await db.collection("maintenances")
|
||||
.where("scheduledDate", "<=", sevenDaysTimestamp)
|
||||
.where("scheduledDate", ">=", now)
|
||||
.get();
|
||||
|
||||
const alertsCreated = [];
|
||||
|
||||
for (const doc of maintenancesSnapshot.docs) {
|
||||
const maintenance = doc.data();
|
||||
|
||||
// Vérifier si une alerte existe déjà pour cette maintenance
|
||||
const existingAlertSnapshot = await db.collection("alerts")
|
||||
.where("type", "==", "MAINTENANCE_DUE")
|
||||
.where("relatedMaintenanceId", "==", doc.id)
|
||||
.get();
|
||||
|
||||
if (existingAlertSnapshot.empty) {
|
||||
// Créer une nouvelle alerte
|
||||
const alertData = {
|
||||
type: "MAINTENANCE_DUE",
|
||||
title: `Maintenance à venir`,
|
||||
message: `Une maintenance est prévue pour ${maintenance.equipmentIds?.length || 0} équipement(s)`,
|
||||
severity: "MEDIUM",
|
||||
isRead: false,
|
||||
relatedMaintenanceId: doc.id,
|
||||
createdAt: admin.firestore.Timestamp.now(),
|
||||
};
|
||||
|
||||
const alertRef = await db.collection("alerts").add(alertData);
|
||||
alertsCreated.push({id: alertRef.id, ...alertData});
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
alertsCreated: alertsCreated.length,
|
||||
alerts: alertsCreated,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error checking upcoming maintenances:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Compléter une maintenance
|
||||
*/
|
||||
exports.completeMaintenance = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const hasAccess = await auth.hasPermission(decodedToken.uid, "manage_equipment");
|
||||
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({error: "Forbidden: Requires manage_equipment permission"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {maintenanceId, performedBy, cost} = req.body.data;
|
||||
|
||||
if (!maintenanceId) {
|
||||
res.status(400).json({error: "maintenanceId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const now = admin.firestore.Timestamp.now();
|
||||
const updateData = {
|
||||
completedDate: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
if (performedBy) {
|
||||
updateData.performedBy = performedBy;
|
||||
}
|
||||
|
||||
if (cost !== undefined && cost !== null) {
|
||||
updateData.cost = cost;
|
||||
}
|
||||
|
||||
// Mettre à jour la maintenance
|
||||
await db.collection("maintenances").doc(maintenanceId).update(updateData);
|
||||
|
||||
// Récupérer la maintenance pour mettre à jour les équipements
|
||||
const maintenanceDoc = await db.collection("maintenances").doc(maintenanceId).get();
|
||||
const maintenanceData = maintenanceDoc.data();
|
||||
|
||||
// Mettre à jour la date de dernière maintenance des équipements
|
||||
if (maintenanceData && maintenanceData.equipmentIds) {
|
||||
const updatePromises = maintenanceData.equipmentIds.map((equipmentId) =>
|
||||
db.collection("equipments").doc(equipmentId).update({
|
||||
lastMaintenanceDate: now,
|
||||
updatedAt: now,
|
||||
}),
|
||||
);
|
||||
|
||||
await Promise.all(updatePromises);
|
||||
}
|
||||
|
||||
res.status(200).json({success: true});
|
||||
} catch (error) {
|
||||
logger.error("Error completing maintenance:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,263 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Récupérer toutes les options (public pour utilisateurs authentifiés)
|
||||
exports.getOptions = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const snapshot = await db.collection("options").get();
|
||||
const options = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(doc.data()),
|
||||
}));
|
||||
|
||||
res.status(200).json({options});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching options:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer tous les types d'événements (public pour utilisateurs authentifiés)
|
||||
exports.getEventTypes = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const snapshot = await db.collection("eventTypes").get();
|
||||
const eventTypes = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(doc.data()),
|
||||
}));
|
||||
|
||||
res.status(200).json({eventTypes});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching event types:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Créer une option
|
||||
exports.createOption = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const optionData = req.body.data;
|
||||
const optionId = optionData.id;
|
||||
|
||||
if (!optionId) {
|
||||
res.status(400).json({error: "Option ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("options").doc(optionId).set(optionData);
|
||||
|
||||
res.status(201).json({id: optionId, message: "Option created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating option:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour une option
|
||||
exports.updateOption = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {optionId, data} = req.body.data;
|
||||
|
||||
if (!optionId) {
|
||||
res.status(400).json({error: "Option ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
delete data.id;
|
||||
|
||||
await db.collection("options").doc(optionId).update(data);
|
||||
|
||||
res.status(200).json({message: "Option updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating option:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer une option
|
||||
exports.deleteOption = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {optionId} = req.body.data;
|
||||
|
||||
if (!optionId) {
|
||||
res.status(400).json({error: "Option ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("options").doc(optionId).delete();
|
||||
|
||||
res.status(200).json({message: "Option deleted successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting option:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Créer un type d'événement
|
||||
exports.createEventType = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdmin = await auth.hasPermission(decodedToken.uid, "edit_data");
|
||||
|
||||
if (!isAdmin) {
|
||||
res.status(403).json({error: "Forbidden: Admin permission required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {name, defaultPrice} = req.body.data;
|
||||
|
||||
if (!name || defaultPrice === undefined) {
|
||||
res.status(400).json({error: "Name and defaultPrice are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const existingSnapshot = await db.collection("eventTypes")
|
||||
.where("name", "==", name)
|
||||
.get();
|
||||
|
||||
if (!existingSnapshot.empty) {
|
||||
res.status(409).json({error: "Event type name already exists"});
|
||||
return;
|
||||
}
|
||||
|
||||
const eventTypeData = {
|
||||
name,
|
||||
defaultPrice,
|
||||
createdAt: admin.firestore.Timestamp.now(),
|
||||
};
|
||||
|
||||
const docRef = await db.collection("eventTypes").add(eventTypeData);
|
||||
|
||||
res.status(201).json({id: docRef.id, message: "Event type created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating event type:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour un type d'événement
|
||||
exports.updateEventType = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdmin = await auth.hasPermission(decodedToken.uid, "edit_data");
|
||||
|
||||
if (!isAdmin) {
|
||||
res.status(403).json({error: "Forbidden: Admin permission required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {eventTypeId, name, defaultPrice} = req.body.data;
|
||||
|
||||
if (!eventTypeId) {
|
||||
res.status(400).json({error: "Event type ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const docRef = db.collection("eventTypes").doc(eventTypeId);
|
||||
const doc = await docRef.get();
|
||||
|
||||
if (!doc.exists) {
|
||||
res.status(404).json({error: "Event type not found"});
|
||||
return;
|
||||
}
|
||||
|
||||
if (name) {
|
||||
const existingSnapshot = await db.collection("eventTypes")
|
||||
.where("name", "==", name)
|
||||
.get();
|
||||
|
||||
const hasDuplicate = existingSnapshot.docs.some((d) => d.id !== eventTypeId);
|
||||
if (hasDuplicate) {
|
||||
res.status(409).json({error: "Event type name already exists"});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const updateData = {};
|
||||
if (name) updateData.name = name;
|
||||
if (defaultPrice !== undefined) updateData.defaultPrice = defaultPrice;
|
||||
|
||||
await docRef.update(updateData);
|
||||
|
||||
res.status(200).json({message: "Event type updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating event type:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer un type d'événement
|
||||
exports.deleteEventType = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdmin = await auth.hasPermission(decodedToken.uid, "edit_data");
|
||||
|
||||
if (!isAdmin) {
|
||||
res.status(403).json({error: "Forbidden: Admin permission required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {eventTypeId} = req.body.data;
|
||||
|
||||
if (!eventTypeId) {
|
||||
res.status(400).json({error: "Event type ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const eventsSnapshot = await db.collection("events")
|
||||
.where("eventTypeId", "==", eventTypeId)
|
||||
.get();
|
||||
|
||||
const now = admin.firestore.Timestamp.now();
|
||||
const futureEvents = eventsSnapshot.docs.filter((doc) => {
|
||||
const startDate = doc.data().StartDateTime;
|
||||
return startDate && startDate > now;
|
||||
});
|
||||
|
||||
if (futureEvents.length > 0) {
|
||||
res.status(409).json({
|
||||
error: "Cannot delete event type with future events",
|
||||
futureEventsCount: futureEvents.length,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("eventTypes").doc(eventTypeId).delete();
|
||||
|
||||
res.status(200).json({message: "Event type deleted successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting event type:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
const admin = require("firebase-admin");
|
||||
const {Storage} = require("@google-cloud/storage");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
|
||||
const storage = new Storage();
|
||||
|
||||
exports.moveEventFileV2 = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const {sourcePath, destinationPath} = req.body.data || {};
|
||||
|
||||
if (!sourcePath || !destinationPath) {
|
||||
res.status(400).json({error: "Source and destination paths are required."});
|
||||
return;
|
||||
}
|
||||
|
||||
const bucketName = admin.storage().bucket().name;
|
||||
const bucket = storage.bucket(bucketName);
|
||||
|
||||
await bucket.file(sourcePath).copy(bucket.file(destinationPath));
|
||||
await bucket.file(sourcePath).delete();
|
||||
const [url] = await bucket.file(destinationPath).getSignedUrl({
|
||||
action: "read",
|
||||
expires: "03-01-2500",
|
||||
});
|
||||
|
||||
res.status(200).json({url});
|
||||
} catch (error) {
|
||||
logger.error("Error moving file:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
|
||||
exports.generateTTSV2 = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
logger.info("[generateTTSV2] Request from user:", {
|
||||
uid: decodedToken.uid,
|
||||
email: decodedToken.email,
|
||||
});
|
||||
|
||||
const {text, voiceConfig} = req.body.data || {};
|
||||
|
||||
if (!text) {
|
||||
res.status(400).json({error: "Text parameter is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
if (text.length > 5000) {
|
||||
res.status(400).json({error: "Text too long (max 5000 characters)"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {Storage} = require("@google-cloud/storage");
|
||||
const storage = new Storage();
|
||||
const bucketName = admin.storage().bucket().name;
|
||||
const bucket = storage.bucket(bucketName);
|
||||
|
||||
const {generateTTS} = require("../generateTTS");
|
||||
const result = await generateTTS(text, storage, bucket, voiceConfig);
|
||||
|
||||
logger.info("[generateTTSV2] ✓ Success", {
|
||||
cached: result.cached,
|
||||
cacheKey: result.cacheKey,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
audioUrl: result.audioUrl,
|
||||
cached: result.cached,
|
||||
cacheKey: result.cacheKey,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("[generateTTSV2] ✗ Error:", {
|
||||
error: error.message,
|
||||
code: error.code,
|
||||
});
|
||||
|
||||
if (error.code === "PERMISSION_DENIED") {
|
||||
res.status(403).json({error: "Permission denied. Check Google Cloud TTS API is enabled."});
|
||||
} else if (error.code === "QUOTA_EXCEEDED") {
|
||||
res.status(429).json({error: "TTS quota exceeded. Try again later."});
|
||||
} else {
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,336 @@
|
||||
const admin = require("firebase-admin");
|
||||
const db = admin.firestore();
|
||||
const logger = require("firebase-functions/logger");
|
||||
const auth = require("../utils/auth");
|
||||
const helpers = require("../utils/helpers");
|
||||
|
||||
// Créer un utilisateur
|
||||
exports.createUser = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const userData = req.body.data;
|
||||
const userId = userData.uid;
|
||||
|
||||
if (!userId) {
|
||||
res.status(400).json({error: "User ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("users").doc(userId).set(userData);
|
||||
|
||||
res.status(201).json({id: userId, message: "User created successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error creating user:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Créer un utilisateur avec invitation par email
|
||||
exports.createUserWithInvite = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {email, firstName, lastName, phoneNumber, roleId} = req.body.data;
|
||||
|
||||
if (!email || !firstName || !lastName || !roleId) {
|
||||
res.status(400).json({error: "email, firstName, lastName, and roleId are required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const tempPassword = Math.random().toString(36).slice(-12) + "Aa1!";
|
||||
|
||||
let userRecord;
|
||||
try {
|
||||
userRecord = await admin.auth().createUser({
|
||||
email: email,
|
||||
password: tempPassword,
|
||||
emailVerified: false,
|
||||
displayName: `${firstName} ${lastName}`,
|
||||
});
|
||||
} catch (authError) {
|
||||
logger.error("Error creating user in Auth:", authError);
|
||||
res.status(500).json({error: `Failed to create user in Auth: ${authError.message}`});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await db.collection("users").doc(userRecord.uid).set({
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
email: email,
|
||||
phoneNumber: phoneNumber || "",
|
||||
profilePhotoUrl: "",
|
||||
role: db.collection("roles").doc(roleId),
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
createdBy: decodedToken.uid,
|
||||
});
|
||||
} catch (firestoreError) {
|
||||
logger.error("Error creating user in Firestore:", firestoreError);
|
||||
try {
|
||||
await admin.auth().deleteUser(userRecord.uid);
|
||||
} catch (cleanupError) {
|
||||
logger.error("Error cleaning up Auth user:", cleanupError);
|
||||
}
|
||||
res.status(500).json({error: `Failed to create user in Firestore: ${firestoreError.message}`});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const axios = require("axios");
|
||||
const firebaseApiKey = "AIzaSyARQL4P-t5l-cNjQNP9cMokQrLJ8BorF0U";
|
||||
|
||||
await axios.post(
|
||||
`https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${firebaseApiKey}`,
|
||||
{
|
||||
requestType: "PASSWORD_RESET",
|
||||
email: email,
|
||||
},
|
||||
);
|
||||
logger.info(`Password reset email sent to ${email}`);
|
||||
} catch (emailError) {
|
||||
logger.warn(`Could not send password reset email to ${email}: ${emailError.message}`);
|
||||
}
|
||||
|
||||
logger.info(`User ${userRecord.uid} created by ${decodedToken.uid}`);
|
||||
res.status(201).json({
|
||||
id: userRecord.uid,
|
||||
message: "User created successfully. Password reset email sent.",
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error in createUserWithInvite:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Mettre à jour un utilisateur
|
||||
exports.updateUser = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const {userId, data} = req.body.data;
|
||||
|
||||
if (!userId) {
|
||||
res.status(400).json({error: "User ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Un utilisateur ne peut modifier que son propre compte, sauf s'il est admin
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
if (decodedToken.uid !== userId && !isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Cannot update other user accounts"});
|
||||
return;
|
||||
}
|
||||
|
||||
// Empêcher les non-admins de modifier le rôle
|
||||
if (!isAdminUser && data.role) {
|
||||
delete data.role;
|
||||
}
|
||||
|
||||
// Si le rôle est fourni et est un string, le convertir en DocumentReference
|
||||
if (data.role && typeof data.role === "string") {
|
||||
data.role = db.collection("roles").doc(data.role);
|
||||
}
|
||||
|
||||
await db.collection("users").doc(userId).update(data);
|
||||
|
||||
res.status(200).json({message: "User updated successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error updating user:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Supprimer un utilisateur
|
||||
exports.deleteUser = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const isAdminUser = await auth.isAdmin(decodedToken.uid);
|
||||
|
||||
if (!isAdminUser) {
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const {userId} = req.body.data;
|
||||
|
||||
if (!userId) {
|
||||
res.status(400).json({error: "User ID is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
if (decodedToken.uid === userId) {
|
||||
res.status(400).json({error: "Cannot delete your own account"});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("users").doc(userId).delete();
|
||||
|
||||
try {
|
||||
await admin.auth().deleteUser(userId);
|
||||
} catch (authError) {
|
||||
logger.warn(`Could not delete user from Auth: ${authError.message}`);
|
||||
}
|
||||
|
||||
res.status(200).json({message: "User deleted successfully"});
|
||||
} catch (error) {
|
||||
logger.error("Error deleting user:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupérer tous les utilisateurs (selon permissions)
|
||||
exports.getUsers = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
const canViewAll = await auth.hasPermission(decodedToken.uid, "view_all_users");
|
||||
|
||||
if (!canViewAll) {
|
||||
const userDoc = await db.collection("users").doc(decodedToken.uid).get();
|
||||
|
||||
if (!userDoc.exists) {
|
||||
res.status(404).json({error: "User not found"});
|
||||
return;
|
||||
}
|
||||
|
||||
let userData = userDoc.data();
|
||||
userData = helpers.serializeTimestamps(userData);
|
||||
userData = helpers.serializeReferences(userData);
|
||||
|
||||
res.status(200).json({
|
||||
users: [{
|
||||
id: userDoc.id,
|
||||
...userData,
|
||||
}],
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = await db.collection("users").get();
|
||||
const users = snapshot.docs.map((doc) => {
|
||||
let data = doc.data();
|
||||
data = helpers.serializeTimestamps(data);
|
||||
data = helpers.serializeReferences(data);
|
||||
return {
|
||||
id: doc.id,
|
||||
...data,
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json({users});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching users:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupère un utilisateur spécifique
|
||||
exports.getUser = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
|
||||
const {userId} = req.body.data || req.body || {};
|
||||
if (!userId) {
|
||||
res.status(400).json({error: "userId is required"});
|
||||
return;
|
||||
}
|
||||
|
||||
const userDoc = await db.collection("users").doc(userId).get();
|
||||
if (!userDoc.exists) {
|
||||
res.status(404).json({error: "User not found"});
|
||||
return;
|
||||
}
|
||||
|
||||
const user = userDoc.data();
|
||||
|
||||
const userData = {
|
||||
id: userDoc.id,
|
||||
uid: user.uid || userDoc.id,
|
||||
email: user.email || "",
|
||||
firstName: user.firstName || "",
|
||||
lastName: user.lastName || "",
|
||||
phoneNumber: user.phoneNumber || "",
|
||||
profilePhotoUrl: user.profilePhotoUrl || "",
|
||||
};
|
||||
|
||||
if (user.role) {
|
||||
const roleDoc = await user.role.get();
|
||||
if (roleDoc.exists) {
|
||||
userData.role = {
|
||||
id: roleDoc.id,
|
||||
...roleDoc.data(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({user: userData});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching user:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupère l'utilisateur actuellement authentifié avec son rôle
|
||||
exports.getCurrentUser = async (req, res) => {
|
||||
try {
|
||||
const decodedToken = await auth.authenticateUser(req);
|
||||
const userId = decodedToken.uid;
|
||||
|
||||
const userDoc = await db.collection("users").doc(userId).get();
|
||||
if (!userDoc.exists) {
|
||||
res.status(404).json({error: "User not found"});
|
||||
return;
|
||||
}
|
||||
|
||||
const userData = userDoc.data();
|
||||
|
||||
let roleData = null;
|
||||
if (userData.role) {
|
||||
const roleDoc = await userData.role.get();
|
||||
if (roleDoc.exists) {
|
||||
roleData = {id: roleDoc.id, ...roleDoc.data()};
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
user: {
|
||||
uid: userId,
|
||||
...helpers.serializeTimestamps(userData),
|
||||
role: roleData,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error getting current user:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
|
||||
// Récupère tous les rôles
|
||||
exports.getRoles = async (req, res) => {
|
||||
try {
|
||||
await auth.authenticateUser(req);
|
||||
|
||||
const snapshot = await db.collection("roles").get();
|
||||
const roles = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...helpers.serializeTimestamps(doc.data()),
|
||||
}));
|
||||
|
||||
res.status(200).json({roles});
|
||||
} catch (error) {
|
||||
logger.error("Error fetching roles:", error);
|
||||
res.status(500).json({error: error.message});
|
||||
}
|
||||
};
|
||||
@@ -1,24 +1,24 @@
|
||||
/**
|
||||
* Utilitaires d'authentification et d'autorisation
|
||||
*/
|
||||
const admin = require('firebase-admin');
|
||||
const logger = require('firebase-functions/logger');
|
||||
const admin = require("firebase-admin");
|
||||
const logger = require("firebase-functions/logger");
|
||||
|
||||
/**
|
||||
* Vérifie le token Firebase et retourne l'utilisateur
|
||||
*/
|
||||
async function authenticateUser(req) {
|
||||
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
|
||||
throw new Error('Unauthorized: No token provided');
|
||||
if (!req.headers.authorization || !req.headers.authorization.startsWith("Bearer ")) {
|
||||
throw new Error("Unauthorized: No token provided");
|
||||
}
|
||||
|
||||
const idToken = req.headers.authorization.split('Bearer ')[1];
|
||||
const idToken = req.headers.authorization.split("Bearer ")[1];
|
||||
try {
|
||||
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
||||
return decodedToken;
|
||||
} catch (e) {
|
||||
logger.error("Error verifying Firebase ID token:", e);
|
||||
throw new Error('Unauthorized: Invalid token');
|
||||
throw new Error("Unauthorized: Invalid token");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ async function authenticateUser(req) {
|
||||
* Récupère les données utilisateur depuis Firestore
|
||||
*/
|
||||
async function getUserData(uid) {
|
||||
const userDoc = await admin.firestore().collection('users').doc(uid).get();
|
||||
const userDoc = await admin.firestore().collection("users").doc(uid).get();
|
||||
if (!userDoc.exists) {
|
||||
return null;
|
||||
}
|
||||
return { uid, ...userDoc.data() };
|
||||
return {uid, ...userDoc.data()};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +40,7 @@ async function getRolePermissions(roleRef) {
|
||||
if (!roleRef) return [];
|
||||
|
||||
let roleId;
|
||||
if (typeof roleRef === 'string') {
|
||||
if (typeof roleRef === "string") {
|
||||
roleId = roleRef;
|
||||
} else if (roleRef.id) {
|
||||
roleId = roleRef.id;
|
||||
@@ -48,7 +48,7 @@ async function getRolePermissions(roleRef) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const roleDoc = await admin.firestore().collection('roles').doc(roleId).get();
|
||||
const roleDoc = await admin.firestore().collection("roles").doc(roleId).get();
|
||||
if (!roleDoc.exists) return [];
|
||||
|
||||
return roleDoc.data().permissions || [];
|
||||
@@ -74,7 +74,7 @@ async function isAdmin(uid) {
|
||||
|
||||
let roleId;
|
||||
const roleField = userData.role;
|
||||
if (typeof roleField === 'string') {
|
||||
if (typeof roleField === "string") {
|
||||
roleId = roleField;
|
||||
} else if (roleField && roleField.id) {
|
||||
roleId = roleField.id;
|
||||
@@ -82,22 +82,22 @@ async function isAdmin(uid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return roleId === 'ADMIN';
|
||||
return roleId === "ADMIN";
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si l'utilisateur est assigné à un événement
|
||||
*/
|
||||
async function isAssignedToEvent(uid, eventId) {
|
||||
const eventDoc = await admin.firestore().collection('events').doc(eventId).get();
|
||||
const eventDoc = await admin.firestore().collection("events").doc(eventId).get();
|
||||
if (!eventDoc.exists) return false;
|
||||
|
||||
const eventData = eventDoc.data();
|
||||
const workforce = eventData.workforce || [];
|
||||
|
||||
// workforce contient des références DocumentReference
|
||||
return workforce.some(ref => {
|
||||
if (typeof ref === 'string') return ref === uid;
|
||||
return workforce.some((ref) => {
|
||||
if (typeof ref === "string") return ref === uid;
|
||||
if (ref && ref.id) return ref.id === uid;
|
||||
return false;
|
||||
});
|
||||
@@ -113,7 +113,7 @@ async function authMiddleware(req, res, next) {
|
||||
req.uid = decodedToken.uid;
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(401).json({ error: error.message });
|
||||
res.status(401).json({error: error.message});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,12 +125,12 @@ function requirePermission(permission) {
|
||||
try {
|
||||
const hasAccess = await hasPermission(req.uid, permission);
|
||||
if (!hasAccess) {
|
||||
res.status(403).json({ error: `Forbidden: Requires permission '${permission}'` });
|
||||
res.status(403).json({error: `Forbidden: Requires permission '${permission}'`});
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
res.status(403).json({error: error.message});
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -142,12 +142,12 @@ async function requireAdmin(req, res, next) {
|
||||
try {
|
||||
const adminAccess = await isAdmin(req.uid);
|
||||
if (!adminAccess) {
|
||||
res.status(403).json({ error: 'Forbidden: Admin access required' });
|
||||
res.status(403).json({error: "Forbidden: Admin access required"});
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
res.status(403).json({error: error.message});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
// Pour configurer : Définir SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS dans .env ou Firebase
|
||||
const getSmtpConfig = () => {
|
||||
return {
|
||||
host: process.env.SMTP_HOST || 'mail.em2events.fr',
|
||||
port: parseInt(process.env.SMTP_PORT || '465'),
|
||||
host: process.env.SMTP_HOST || "mail.em2events.fr",
|
||||
port: parseInt(process.env.SMTP_PORT || "465"),
|
||||
secure: true, // true pour port 465, false pour autres ports
|
||||
auth: {
|
||||
user: process.env.SMTP_USER || 'notify@em2events.fr',
|
||||
pass: process.env.SMTP_PASS || '',
|
||||
user: process.env.SMTP_USER || "notify@em2events.fr",
|
||||
pass: process.env.SMTP_PASS || "",
|
||||
},
|
||||
tls: {
|
||||
// Ne pas échouer sur certificats invalides
|
||||
@@ -24,12 +24,12 @@ const getSmtpConfig = () => {
|
||||
// Configuration email par défaut
|
||||
const EMAIL_CONFIG = {
|
||||
from: {
|
||||
name: 'EM2 Events',
|
||||
address: 'notify@em2events.fr',
|
||||
name: "EM2 Events",
|
||||
address: "notify@em2events.fr",
|
||||
},
|
||||
replyTo: 'contact@em2events.fr',
|
||||
replyTo: "contact@em2events.fr",
|
||||
// URL de l'application pour les liens
|
||||
appUrl: process.env.APP_URL || 'https://app.em2events.fr',
|
||||
appUrl: process.env.APP_URL || "https://app.em2events.fr",
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
const admin = require('firebase-admin');
|
||||
const handlebars = require('handlebars');
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const {EMAIL_CONFIG} = require('./emailConfig');
|
||||
const admin = require("firebase-admin");
|
||||
const handlebars = require("handlebars");
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const {EMAIL_CONFIG} = require("./emailConfig");
|
||||
|
||||
/**
|
||||
* Vérifie si l'utilisateur souhaite recevoir ce type d'alerte
|
||||
*/
|
||||
function checkAlertPreference(alertType, preferences) {
|
||||
const typeMapping = {
|
||||
'EVENT_CREATED': 'eventsNotifications',
|
||||
'EVENT_MODIFIED': 'eventsNotifications',
|
||||
'EVENT_CANCELLED': 'eventsNotifications',
|
||||
'LOST': 'equipmentNotifications',
|
||||
'EQUIPMENT_MISSING': 'equipmentNotifications',
|
||||
'DAMAGED': 'equipmentNotifications',
|
||||
'QUANTITY_MISMATCH': 'equipmentNotifications',
|
||||
'MAINTENANCE_REMINDER': 'maintenanceNotifications',
|
||||
'STOCK_LOW': 'stockNotifications',
|
||||
"EVENT_CREATED": "eventsNotifications",
|
||||
"EVENT_MODIFIED": "eventsNotifications",
|
||||
"EVENT_CANCELLED": "eventsNotifications",
|
||||
"LOST": "equipmentNotifications",
|
||||
"EQUIPMENT_MISSING": "equipmentNotifications",
|
||||
"DAMAGED": "equipmentNotifications",
|
||||
"QUANTITY_MISMATCH": "equipmentNotifications",
|
||||
"MAINTENANCE_REMINDER": "maintenanceNotifications",
|
||||
"STOCK_LOW": "stockNotifications",
|
||||
};
|
||||
|
||||
const prefKey = typeMapping[alertType];
|
||||
@@ -29,12 +29,12 @@ function checkAlertPreference(alertType, preferences) {
|
||||
*/
|
||||
async function prepareTemplateData(alert, user) {
|
||||
const data = {
|
||||
userName: `${user.firstName || ''} ${user.lastName || ''}`.trim() ||
|
||||
'Utilisateur',
|
||||
userName: `${user.firstName || ""} ${user.lastName || ""}`.trim() ||
|
||||
"Utilisateur",
|
||||
alertTitle: getAlertTitle(alert.type),
|
||||
alertMessage: alert.message,
|
||||
isCritical: alert.severity === 'CRITICAL',
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || '/alerts'}`,
|
||||
isCritical: alert.severity === "CRITICAL",
|
||||
actionUrl: `${EMAIL_CONFIG.appUrl}${alert.actionUrl || "/alerts"}`,
|
||||
appUrl: EMAIL_CONFIG.appUrl,
|
||||
unsubscribeUrl: `${EMAIL_CONFIG.appUrl}/my_account?tab=notifications`,
|
||||
year: new Date().getFullYear(),
|
||||
@@ -45,20 +45,20 @@ async function prepareTemplateData(alert, user) {
|
||||
if (alert.eventId) {
|
||||
try {
|
||||
const eventDoc = await admin.firestore()
|
||||
.collection('events')
|
||||
.collection("events")
|
||||
.doc(alert.eventId)
|
||||
.get();
|
||||
|
||||
if (eventDoc.exists) {
|
||||
const event = eventDoc.data();
|
||||
data.eventName = event.Name || event.name || 'Événement';
|
||||
data.eventName = event.Name || event.name || "Événement";
|
||||
if (event.StartDateTime || event.startDate) {
|
||||
const dateField = event.StartDateTime || event.startDate;
|
||||
const date = dateField.toDate ? dateField.toDate() : new Date(dateField);
|
||||
data.eventDate = date.toLocaleDateString('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
data.eventDate = date.toLocaleDateString("fr-FR", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ async function prepareTemplateData(alert, user) {
|
||||
if (alert.equipmentId) {
|
||||
try {
|
||||
const eqDoc = await admin.firestore()
|
||||
.collection('equipments')
|
||||
.collection("equipments")
|
||||
.doc(alert.equipmentId)
|
||||
.get();
|
||||
|
||||
@@ -90,18 +90,18 @@ async function prepareTemplateData(alert, user) {
|
||||
*/
|
||||
function getEmailSubject(alert) {
|
||||
const subjects = {
|
||||
'EVENT_CREATED': '📅 Nouvel événement créé',
|
||||
'EVENT_MODIFIED': '📝 Événement modifié',
|
||||
'EVENT_CANCELLED': '❌ Événement annulé',
|
||||
'LOST': '🔴 Alerte critique : Équipement perdu',
|
||||
'EQUIPMENT_MISSING': '⚠️ Équipement manquant',
|
||||
'DAMAGED': '⚠️ Équipement endommagé',
|
||||
'QUANTITY_MISMATCH': 'ℹ️ Quantité incorrecte',
|
||||
'MAINTENANCE_REMINDER': '🔧 Rappel de maintenance',
|
||||
'STOCK_LOW': '📦 Stock faible',
|
||||
"EVENT_CREATED": "📅 Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "📝 Événement modifié",
|
||||
"EVENT_CANCELLED": "❌ Événement annulé",
|
||||
"LOST": "🔴 Alerte critique : Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "⚠️ Équipement manquant",
|
||||
"DAMAGED": "⚠️ Équipement endommagé",
|
||||
"QUANTITY_MISMATCH": "ℹ️ Quantité incorrecte",
|
||||
"MAINTENANCE_REMINDER": "🔧 Rappel de maintenance",
|
||||
"STOCK_LOW": "📦 Stock faible",
|
||||
};
|
||||
|
||||
return subjects[alert.type] || '🔔 Nouvelle alerte - EM2 Events';
|
||||
return subjects[alert.type] || "🔔 Nouvelle alerte - EM2 Events";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,18 +109,18 @@ function getEmailSubject(alert) {
|
||||
*/
|
||||
function getAlertTitle(type) {
|
||||
const titles = {
|
||||
'EVENT_CREATED': 'Nouvel événement créé',
|
||||
'EVENT_MODIFIED': 'Événement modifié',
|
||||
'EVENT_CANCELLED': 'Événement annulé',
|
||||
'LOST': 'Équipement perdu',
|
||||
'EQUIPMENT_MISSING': 'Équipement manquant',
|
||||
'DAMAGED': 'Équipement endommagé',
|
||||
'QUANTITY_MISMATCH': 'Quantité incorrecte',
|
||||
'MAINTENANCE_REMINDER': 'Maintenance requise',
|
||||
'STOCK_LOW': 'Stock faible',
|
||||
"EVENT_CREATED": "Nouvel événement créé",
|
||||
"EVENT_MODIFIED": "Événement modifié",
|
||||
"EVENT_CANCELLED": "Événement annulé",
|
||||
"LOST": "Équipement perdu",
|
||||
"EQUIPMENT_MISSING": "Équipement manquant",
|
||||
"DAMAGED": "Équipement endommagé",
|
||||
"QUANTITY_MISMATCH": "Quantité incorrecte",
|
||||
"MAINTENANCE_REMINDER": "Maintenance requise",
|
||||
"STOCK_LOW": "Stock faible",
|
||||
};
|
||||
|
||||
return titles[type] || 'Nouvelle alerte';
|
||||
return titles[type] || "Nouvelle alerte";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,17 +129,17 @@ function getAlertTitle(type) {
|
||||
async function renderTemplate(templateName, data) {
|
||||
try {
|
||||
// Lire le template de base
|
||||
const basePath = path.join(__dirname, '..', 'templates', 'base-template.html');
|
||||
const baseTemplate = await fs.readFile(basePath, 'utf8');
|
||||
const basePath = path.join(__dirname, "..", "templates", "base-template.html");
|
||||
const baseTemplate = await fs.readFile(basePath, "utf8");
|
||||
|
||||
// Lire le template de contenu
|
||||
const contentPath = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'templates',
|
||||
"..",
|
||||
"templates",
|
||||
`${templateName}.html`,
|
||||
);
|
||||
const contentTemplate = await fs.readFile(contentPath, 'utf8');
|
||||
const contentTemplate = await fs.readFile(contentPath, "utf8");
|
||||
|
||||
// Compiler les templates
|
||||
const compileContent = handlebars.compile(contentTemplate);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Helpers pour la manipulation de données Firestore
|
||||
*/
|
||||
const admin = require('firebase-admin');
|
||||
const admin = require("firebase-admin");
|
||||
|
||||
/**
|
||||
* Convertit les Timestamps Firestore en ISO strings pour JSON
|
||||
@@ -19,7 +19,7 @@ function serializeTimestamps(data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const key in result) {
|
||||
const value = result[key];
|
||||
@@ -29,31 +29,31 @@ function serializeTimestamps(data) {
|
||||
}
|
||||
|
||||
// Gérer les Timestamps Firestore
|
||||
if (value.toDate && typeof value.toDate === 'function') {
|
||||
if (value.toDate && typeof value.toDate === "function") {
|
||||
result[key] = value.toDate().toISOString();
|
||||
}
|
||||
// Gérer les DocumentReference
|
||||
else if (value.path && value.id && typeof value.path === 'string') {
|
||||
else if (value.path && value.id && typeof value.path === "string") {
|
||||
result[key] = value.path;
|
||||
}
|
||||
// Gérer les GeoPoint
|
||||
else if (value.latitude !== undefined && value.longitude !== undefined) {
|
||||
result[key] = {
|
||||
latitude: value.latitude,
|
||||
longitude: value.longitude
|
||||
longitude: value.longitude,
|
||||
};
|
||||
}
|
||||
// Gérer les tableaux
|
||||
else if (Array.isArray(value)) {
|
||||
result[key] = value.map(item => {
|
||||
if (!item || typeof item !== 'object') return item;
|
||||
result[key] = value.map((item) => {
|
||||
if (!item || typeof item !== "object") return item;
|
||||
|
||||
// DocumentReference dans un tableau
|
||||
if (item.path && item.id) {
|
||||
return item.path;
|
||||
}
|
||||
// Timestamp dans un tableau
|
||||
if (item.toDate && typeof item.toDate === 'function') {
|
||||
if (item.toDate && typeof item.toDate === "function") {
|
||||
return item.toDate().toISOString();
|
||||
}
|
||||
// Objet normal
|
||||
@@ -61,7 +61,7 @@ function serializeTimestamps(data) {
|
||||
});
|
||||
}
|
||||
// Gérer les objets imbriqués (mais pas les objets Firestore)
|
||||
else if (typeof value === 'object' && !value._firestore && !value._path) {
|
||||
else if (typeof value === "object" && !value._firestore && !value._path) {
|
||||
result[key] = serializeTimestamps(value);
|
||||
}
|
||||
}
|
||||
@@ -75,10 +75,10 @@ function serializeTimestamps(data) {
|
||||
function deserializeTimestamps(data, timestampFields = []) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const field of timestampFields) {
|
||||
if (result[field] && typeof result[field] === 'string') {
|
||||
if (result[field] && typeof result[field] === "string") {
|
||||
result[field] = admin.firestore.Timestamp.fromDate(new Date(result[field]));
|
||||
}
|
||||
}
|
||||
@@ -92,15 +92,15 @@ function deserializeTimestamps(data, timestampFields = []) {
|
||||
function serializeReferences(data) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
for (const key in result) {
|
||||
if (result[key] && result[key].path && typeof result[key].path === 'string') {
|
||||
if (result[key] && result[key].path && typeof result[key].path === "string") {
|
||||
// C'est une DocumentReference
|
||||
result[key] = result[key].id;
|
||||
} else if (Array.isArray(result[key])) {
|
||||
result[key] = result[key].map(item => {
|
||||
if (item && item.path && typeof item.path === 'string') {
|
||||
result[key] = result[key].map((item) => {
|
||||
if (item && item.path && typeof item.path === "string") {
|
||||
return item.id;
|
||||
}
|
||||
return item;
|
||||
@@ -117,7 +117,7 @@ function serializeReferences(data) {
|
||||
function maskSensitiveFields(data, canViewSensitive) {
|
||||
if (canViewSensitive) return data;
|
||||
|
||||
const masked = { ...data };
|
||||
const masked = {...data};
|
||||
|
||||
// Masquer les prix si pas de permission manage_equipment
|
||||
delete masked.purchasePrice;
|
||||
@@ -143,34 +143,34 @@ function paginate(query, limit = 50, startAfter = null) {
|
||||
* Filtre les événements annulés
|
||||
*/
|
||||
function filterCancelledEvents(events) {
|
||||
return events.filter(event => event.status !== 'CANCELLED');
|
||||
return events.filter((event) => event.status !== "CANCELLED");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit les IDs en DocumentReference pour maintenir la compatibilité avec l'ancien format
|
||||
* @param {Object} data - Données de l'événement
|
||||
* @returns {Object} - Données avec DocumentReference
|
||||
* @return {Object} - Données avec DocumentReference
|
||||
*/
|
||||
function convertIdsToReferences(data) {
|
||||
if (!data) return data;
|
||||
|
||||
const result = { ...data };
|
||||
const result = {...data};
|
||||
|
||||
// Convertir EventType (ID → DocumentReference)
|
||||
if (result.EventType && typeof result.EventType === 'string' && !result.EventType.includes('/')) {
|
||||
result.EventType = admin.firestore().collection('eventTypes').doc(result.EventType);
|
||||
if (result.EventType && typeof result.EventType === "string" && !result.EventType.includes("/")) {
|
||||
result.EventType = admin.firestore().collection("eventTypes").doc(result.EventType);
|
||||
}
|
||||
|
||||
// Convertir customer (ID → DocumentReference)
|
||||
if (result.customer && typeof result.customer === 'string' && !result.customer.includes('/')) {
|
||||
result.customer = admin.firestore().collection('customers').doc(result.customer);
|
||||
if (result.customer && typeof result.customer === "string" && !result.customer.includes("/")) {
|
||||
result.customer = admin.firestore().collection("customers").doc(result.customer);
|
||||
}
|
||||
|
||||
// Convertir workforce (IDs → DocumentReference)
|
||||
if (Array.isArray(result.workforce)) {
|
||||
result.workforce = result.workforce.map(item => {
|
||||
if (typeof item === 'string' && !item.includes('/')) {
|
||||
return admin.firestore().collection('users').doc(item);
|
||||
result.workforce = result.workforce.map((item) => {
|
||||
if (typeof item === "string" && !item.includes("/")) {
|
||||
return admin.firestore().collection("users").doc(item);
|
||||
}
|
||||
return item;
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/// Configuration de la version de l'application
|
||||
class AppVersion {
|
||||
static const String version = '1.1.14';
|
||||
static const String version = '1.2.3';
|
||||
|
||||
/// Retourne la version complète de l'application
|
||||
static String get fullVersion => 'v$version';
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
class Env {
|
||||
static const bool isDevelopment = true;
|
||||
static const bool isDevelopment = false;
|
||||
|
||||
// Configuration de l'auto-login en développement
|
||||
static const String devAdminEmail = 'paul.fournel@em2events.fr';
|
||||
static const String devAdminPassword = 'Pastis51!';
|
||||
static const String devAdminEmail = '';
|
||||
static const String devAdminPassword = '';
|
||||
|
||||
// URLs et endpoints
|
||||
static const String baseUrl = 'https://em2rp-951dc.firebaseapp.com';
|
||||
@@ -14,4 +14,3 @@ class Env {
|
||||
// Autres configurations
|
||||
static const int apiTimeout = 30000; // 30 secondes
|
||||
}
|
||||
|
||||
|
||||
+153
-254
@@ -1,3 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:em2rp/providers/users_provider.dart';
|
||||
import 'package:em2rp/providers/event_provider.dart';
|
||||
import 'package:em2rp/providers/equipment_provider.dart';
|
||||
@@ -5,7 +9,6 @@ import 'package:em2rp/providers/container_provider.dart';
|
||||
import 'package:em2rp/providers/maintenance_provider.dart';
|
||||
import 'package:em2rp/providers/alert_provider.dart';
|
||||
import 'package:em2rp/utils/auth_guard_widget.dart';
|
||||
import 'package:em2rp/utils/performance_monitor.dart';
|
||||
import 'package:em2rp/views/alerts_page.dart';
|
||||
import 'package:em2rp/views/calendar_page.dart';
|
||||
import 'package:em2rp/views/login_page.dart';
|
||||
@@ -15,13 +18,12 @@ import 'package:em2rp/views/maintenance_management_page.dart';
|
||||
import 'package:em2rp/views/container_form_page.dart';
|
||||
import 'package:em2rp/views/container_detail_page.dart';
|
||||
import 'package:em2rp/views/event_preparation_page.dart';
|
||||
import 'package:em2rp/views/event_statistics_page.dart';
|
||||
import 'package:em2rp/models/container_model.dart';
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'firebase_options.dart';
|
||||
import 'package:em2rp/services/app_initializer.dart';
|
||||
import 'utils/colors.dart';
|
||||
import 'views/my_account_page.dart';
|
||||
import 'views/user_management_page.dart';
|
||||
@@ -29,36 +31,21 @@ import 'package:provider/provider.dart';
|
||||
import 'providers/local_user_provider.dart';
|
||||
import 'views/reset_password_page.dart';
|
||||
import 'config/env.dart';
|
||||
import 'services/update_service.dart';
|
||||
import 'views/widgets/common/update_dialog.dart';
|
||||
import 'config/api_config.dart';
|
||||
import 'utils/app_start_gate.dart';
|
||||
import 'views/widgets/common/startup_splash_screen.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'views/widgets/common/update_dialog.dart';
|
||||
|
||||
void main() async {
|
||||
void main() {
|
||||
// Ne pas effectuer d'initialisations asynchrones lourdes ici.
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
|
||||
// Configuration des émulateurs en mode développement
|
||||
if (ApiConfig.isDevelopment) {
|
||||
print('🔧 Mode développement activé - Utilisation des émulateurs');
|
||||
|
||||
// Configurer l'émulateur Auth
|
||||
await FirebaseAuth.instance.useAuthEmulator('localhost', 9199);
|
||||
print('✓ Auth émulateur configuré: localhost:9199');
|
||||
|
||||
// Configurer l'émulateur Firestore
|
||||
FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8088);
|
||||
print('✓ Firestore émulateur configuré: localhost:8088');
|
||||
}
|
||||
|
||||
await FirebaseAuth.instance.setPersistence(Persistence.LOCAL);
|
||||
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
// Fournisseur d'initialisation de l'application (initialise Firebase et cache en tâche de fond)
|
||||
ChangeNotifierProvider<AppInitializer>(
|
||||
create: (_) => AppInitializer(),
|
||||
),
|
||||
// LocalUserProvider pour la gestion de l'authentification
|
||||
ChangeNotifierProvider<LocalUserProvider>(
|
||||
create: (context) => LocalUserProvider()),
|
||||
@@ -96,239 +83,151 @@ void main() async {
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
late final Future<void> _startupFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_startupFuture = _bootstrapApp();
|
||||
}
|
||||
|
||||
Future<void> _bootstrapApp() async {
|
||||
final initializer = context.read<AppInitializer>();
|
||||
final localAuthProvider = context.read<LocalUserProvider>();
|
||||
|
||||
await initializer.initialize();
|
||||
|
||||
// Lancer la connexion automatique en dev sans bloquer le démarrage initial
|
||||
if (Env.isDevelopment && FirebaseAuth.instance.currentUser == null) {
|
||||
unawaited(
|
||||
localAuthProvider.signInWithEmailAndPassword(
|
||||
Env.devAdminEmail,
|
||||
Env.devAdminPassword,
|
||||
).then((_) {
|
||||
return localAuthProvider.loadUserData();
|
||||
}).catchError((e) {
|
||||
if (kDebugMode) debugPrint('Dev auto-login failed: $e');
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'EM2 Hub',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.red,
|
||||
primaryColor: AppColors.noir,
|
||||
colorScheme:
|
||||
ColorScheme.fromSwatch().copyWith(secondary: AppColors.rouge),
|
||||
textTheme: const TextTheme(
|
||||
bodyMedium: TextStyle(color: AppColors.noir),
|
||||
),
|
||||
inputDecorationTheme: const InputDecorationTheme(
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(color: AppColors.noir),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(color: AppColors.gris),
|
||||
),
|
||||
labelStyle: TextStyle(color: AppColors.noir),
|
||||
hintStyle: TextStyle(color: AppColors.gris),
|
||||
),
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
foregroundColor: AppColors.blanc,
|
||||
backgroundColor: AppColors.noir,
|
||||
return FutureBuilder<void>(
|
||||
future: _startupFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: StartupSplashScreen(),
|
||||
);
|
||||
}
|
||||
|
||||
return MaterialApp(
|
||||
title: 'EM2 Hub',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.red,
|
||||
primaryColor: AppColors.noir,
|
||||
colorScheme:
|
||||
ColorScheme.fromSwatch().copyWith(secondary: AppColors.rouge),
|
||||
textTheme: const TextTheme(
|
||||
bodyMedium: TextStyle(color: AppColors.noir),
|
||||
),
|
||||
inputDecorationTheme: const InputDecorationTheme(
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(color: AppColors.noir),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(color: AppColors.gris),
|
||||
),
|
||||
labelStyle: TextStyle(color: AppColors.noir),
|
||||
hintStyle: TextStyle(color: AppColors.gris),
|
||||
),
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
foregroundColor: AppColors.blanc,
|
||||
backgroundColor: AppColors.noir,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
locale: const Locale('fr', 'FR'),
|
||||
supportedLocales: const [
|
||||
Locale('fr', 'FR'),
|
||||
],
|
||||
localizationsDelegates: const [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => const AutoLoginWrapper(),
|
||||
'/login': (context) => const LoginPage(),
|
||||
'/alerts': (context) => const AuthGuard(child: AlertsPage()),
|
||||
'/calendar': (context) => const AuthGuard(child: CalendarPage()),
|
||||
'/my_account': (context) => const AuthGuard(child: MyAccountPage()),
|
||||
'/user_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_all_users", child: UserManagementPage()),
|
||||
'/reset_password': (context) {
|
||||
final args = ModalRoute.of(context)!.settings.arguments
|
||||
as Map<String, dynamic>;
|
||||
return ResetPasswordPage(
|
||||
email: args['email'] as String,
|
||||
actionCode: args['actionCode'] as String,
|
||||
);
|
||||
},
|
||||
'/equipment_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: EquipmentManagementPage()),
|
||||
'/container_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: ContainerManagementPage()),
|
||||
'/maintenance_management': (context) => const AuthGuard(
|
||||
requiredPermission: "manage_maintenances",
|
||||
child: MaintenanceManagementPage()),
|
||||
'/container_form': (context) {
|
||||
final args = ModalRoute.of(context)?.settings.arguments;
|
||||
return AuthGuard(
|
||||
requiredPermission: "manage_equipment",
|
||||
child: ContainerFormPage(
|
||||
container: args as ContainerModel?,
|
||||
),
|
||||
);
|
||||
},
|
||||
'/container_detail': (context) {
|
||||
final container = ModalRoute.of(context)!.settings.arguments as ContainerModel;
|
||||
return AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: ContainerDetailPage(container: container),
|
||||
);
|
||||
},
|
||||
'/event_preparation': (context) {
|
||||
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
|
||||
final event = args['event'] as EventModel;
|
||||
return AuthGuard(
|
||||
child: EventPreparationPage(
|
||||
initialEvent: event,
|
||||
),
|
||||
);
|
||||
},
|
||||
locale: const Locale('fr', 'FR'),
|
||||
supportedLocales: const [
|
||||
Locale('fr', 'FR'),
|
||||
],
|
||||
localizationsDelegates: const [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
routes: {
|
||||
'/login': (context) => const LoginPage(),
|
||||
'/alerts': (context) => const AuthGuard(child: AlertsPage()),
|
||||
'/calendar': (context) => const AuthGuard(
|
||||
allowWhileLoading: true, child: CalendarPage()),
|
||||
'/my_account': (context) => const AuthGuard(child: MyAccountPage()),
|
||||
'/user_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_all_users",
|
||||
child: UserManagementPage()),
|
||||
'/reset_password': (context) {
|
||||
final args = ModalRoute.of(context)!.settings.arguments
|
||||
as Map<String, dynamic>;
|
||||
return ResetPasswordPage(
|
||||
email: args['email'] as String,
|
||||
actionCode: args['actionCode'] as String,
|
||||
);
|
||||
},
|
||||
'/equipment_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: EquipmentManagementPage()),
|
||||
'/container_management': (context) => const AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: ContainerManagementPage()),
|
||||
'/maintenance_management': (context) => const AuthGuard(
|
||||
requiredPermission: "manage_maintenances",
|
||||
child: MaintenanceManagementPage()),
|
||||
'/container_form': (context) {
|
||||
final args = ModalRoute.of(context)?.settings.arguments;
|
||||
return AuthGuard(
|
||||
requiredPermission: "manage_equipment",
|
||||
child: ContainerFormPage(
|
||||
container: args as ContainerModel?,
|
||||
),
|
||||
);
|
||||
},
|
||||
'/container_detail': (context) {
|
||||
final container = ModalRoute.of(context)!.settings.arguments
|
||||
as ContainerModel;
|
||||
return AuthGuard(
|
||||
requiredPermission: "view_equipment",
|
||||
child: ContainerDetailPage(container: container),
|
||||
);
|
||||
},
|
||||
'/event_preparation': (context) {
|
||||
final args = ModalRoute.of(context)!.settings.arguments
|
||||
as Map<String, dynamic>;
|
||||
final event = args['event'] as EventModel;
|
||||
return AuthGuard(
|
||||
child: EventPreparationPage(
|
||||
initialEvent: event,
|
||||
),
|
||||
);
|
||||
},
|
||||
'/event_statistics': (context) => const AuthGuard(
|
||||
requiredPermission: 'generate_reports',
|
||||
child: EventStatisticsPage()),
|
||||
},
|
||||
home: const AppStartGate(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AutoLoginWrapper extends StatefulWidget {
|
||||
const AutoLoginWrapper({super.key});
|
||||
|
||||
@override
|
||||
State<AutoLoginWrapper> createState() => _AutoLoginWrapperState();
|
||||
}
|
||||
|
||||
class _AutoLoginWrapperState extends State<AutoLoginWrapper> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Attendre la fin du premier build avant de naviguer
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_autoLogin();
|
||||
// Vérifier les mises à jour après un délai pour ne pas interférer avec l'autologin
|
||||
_checkForUpdateDelayed();
|
||||
});
|
||||
}
|
||||
|
||||
/// Vérifie les mises à jour après un délai
|
||||
Future<void> _checkForUpdateDelayed() async {
|
||||
try {
|
||||
// Attendre que l'app soit complètement chargée (navigation effectuée, etc.)
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
final updateInfo = await UpdateService.checkForUpdate();
|
||||
|
||||
if (updateInfo != null && mounted) {
|
||||
// Attendre encore un peu pour être sûr que le bon contexte est disponible
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
if (mounted) {
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: !updateInfo.forceUpdate,
|
||||
builder: (context) => UpdateDialog(updateInfo: updateInfo),
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[AutoLoginWrapper] Error checking for update: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _autoLogin() async {
|
||||
PerformanceMonitor.start('App.autoLogin');
|
||||
try {
|
||||
final localAuthProvider =
|
||||
Provider.of<LocalUserProvider>(context, listen: false);
|
||||
|
||||
// Vérifier si l'utilisateur est déjà connecté
|
||||
if (FirebaseAuth.instance.currentUser == null && Env.isDevelopment) {
|
||||
PerformanceMonitor.start('App.signIn');
|
||||
// Connexion automatique en mode développement
|
||||
await localAuthProvider.signInWithEmailAndPassword(
|
||||
Env.devAdminEmail,
|
||||
Env.devAdminPassword,
|
||||
);
|
||||
PerformanceMonitor.end('App.signIn');
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
// MODIFIÉ : Vérifier si une route spécifique est demandée dans l'URL
|
||||
// En Flutter Web, on peut vérifier window.location.hash
|
||||
final currentUri = Uri.base;
|
||||
final fragment = currentUri.fragment; // Ex: "/alerts" si URL est /#/alerts
|
||||
|
||||
print('[AutoLoginWrapper] Fragment URL: $fragment');
|
||||
|
||||
// Navigation immédiate sans attendre le chargement des données
|
||||
if (fragment.isNotEmpty && fragment != '/' && fragment != '/calendar') {
|
||||
print('[AutoLoginWrapper] Redirection vers: $fragment');
|
||||
Navigator.of(context).pushReplacementNamed(fragment);
|
||||
} else {
|
||||
// Route par défaut : calendrier
|
||||
print('[AutoLoginWrapper] Redirection vers: /calendar (défaut)');
|
||||
Navigator.of(context).pushReplacementNamed('/calendar');
|
||||
}
|
||||
|
||||
PerformanceMonitor.end('App.autoLogin');
|
||||
PerformanceMonitor.printSummary();
|
||||
|
||||
// Charger les données utilisateur en arrière-plan
|
||||
localAuthProvider.loadUserData().catchError((e) {
|
||||
print('Error loading user data: $e');
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
print('Auto login failed: $e');
|
||||
PerformanceMonitor.end('App.autoLogin');
|
||||
if (mounted) {
|
||||
Navigator.of(context).pushReplacementNamed('/login');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Logo de l'application
|
||||
Image.asset(
|
||||
'assets/logos/RectangleLogoBlack.png',
|
||||
width: 200,
|
||||
height: 200,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return const Icon(
|
||||
Icons.event_available,
|
||||
size: 80,
|
||||
color: AppColors.rouge,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
const CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppColors.rouge),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
const Text(
|
||||
'Chargement...',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey,
|
||||
fontWeight: FontWeight.w400,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ class AlertModel {
|
||||
|
||||
factory AlertModel.fromMap(Map<String, dynamic> map, String id) {
|
||||
// Fonction helper pour convertir Timestamp ou String ISO en DateTime
|
||||
DateTime _parseDate(dynamic value) {
|
||||
DateTime parseDate(dynamic value) {
|
||||
if (value == null) return DateTime.now();
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value) ?? DateTime.now();
|
||||
@@ -174,13 +174,13 @@ class AlertModel {
|
||||
eventId: map['eventId'],
|
||||
equipmentId: map['equipmentId'],
|
||||
createdByUserId: map['createdByUserId'] ?? map['createdBy'],
|
||||
createdAt: _parseDate(map['createdAt']),
|
||||
dueDate: map['dueDate'] != null ? _parseDate(map['dueDate']) : null,
|
||||
createdAt: parseDate(map['createdAt']),
|
||||
dueDate: map['dueDate'] != null ? parseDate(map['dueDate']) : null,
|
||||
actionUrl: map['actionUrl'],
|
||||
isRead: map['isRead'] ?? false,
|
||||
isResolved: map['isResolved'] ?? false,
|
||||
resolution: map['resolution'],
|
||||
resolvedAt: map['resolvedAt'] != null ? _parseDate(map['resolvedAt']) : null,
|
||||
resolvedAt: map['resolvedAt'] != null ? parseDate(map['resolvedAt']) : null,
|
||||
resolvedByUserId: map['resolvedByUserId'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ class ContainerModel {
|
||||
/// Factory depuis Firestore
|
||||
factory ContainerModel.fromMap(Map<String, dynamic> map, String id) {
|
||||
// Fonction helper pour convertir Timestamp ou String ISO en DateTime
|
||||
DateTime? _parseDate(dynamic value) {
|
||||
DateTime? parseDate(dynamic value) {
|
||||
if (value == null) return null;
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value);
|
||||
@@ -270,8 +270,8 @@ class ContainerModel {
|
||||
equipmentIds: equipmentIds,
|
||||
eventId: map['eventId'],
|
||||
notes: map['notes'],
|
||||
createdAt: _parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: _parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
createdAt: parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
history: history,
|
||||
);
|
||||
}
|
||||
@@ -351,7 +351,7 @@ class ContainerHistoryEntry {
|
||||
|
||||
factory ContainerHistoryEntry.fromMap(Map<String, dynamic> map) {
|
||||
// Helper pour parser la date
|
||||
DateTime _parseDate(dynamic value) {
|
||||
DateTime parseDate(dynamic value) {
|
||||
if (value == null) return DateTime.now();
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value) ?? DateTime.now();
|
||||
@@ -359,7 +359,7 @@ class ContainerHistoryEntry {
|
||||
}
|
||||
|
||||
return ContainerHistoryEntry(
|
||||
timestamp: _parseDate(map['timestamp']),
|
||||
timestamp: parseDate(map['timestamp']),
|
||||
action: map['action'] ?? '',
|
||||
equipmentId: map['equipmentId'],
|
||||
previousValue: map['previousValue'],
|
||||
|
||||
@@ -388,7 +388,7 @@ class EquipmentModel {
|
||||
|
||||
factory EquipmentModel.fromMap(Map<String, dynamic> map, String id) {
|
||||
// Fonction helper pour convertir Timestamp ou String ISO en DateTime
|
||||
DateTime? _parseDate(dynamic value) {
|
||||
DateTime? parseDate(dynamic value) {
|
||||
if (value == null) return null;
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value);
|
||||
@@ -416,13 +416,13 @@ class EquipmentModel {
|
||||
length: map['length']?.toDouble(),
|
||||
width: map['width']?.toDouble(),
|
||||
height: map['height']?.toDouble(),
|
||||
purchaseDate: _parseDate(map['purchaseDate']),
|
||||
nextMaintenanceDate: _parseDate(map['nextMaintenanceDate']),
|
||||
purchaseDate: parseDate(map['purchaseDate']),
|
||||
nextMaintenanceDate: parseDate(map['nextMaintenanceDate']),
|
||||
maintenanceIds: maintenanceIds,
|
||||
imageUrl: map['imageUrl'],
|
||||
notes: map['notes'],
|
||||
createdAt: _parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: _parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
createdAt: parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -174,6 +174,7 @@ ReturnStatus returnStatusFromString(String? status) {
|
||||
class EventEquipment {
|
||||
final String equipmentId; // ID de l'équipement
|
||||
final int quantity; // Quantité initiale assignée
|
||||
final String? rationale; // Explication/Justification (ex: IA alternative)
|
||||
final bool isPrepared; // Validé en préparation
|
||||
final bool isLoaded; // Validé au chargement
|
||||
final bool isUnloaded; // Validé au déchargement
|
||||
@@ -194,6 +195,7 @@ class EventEquipment {
|
||||
EventEquipment({
|
||||
required this.equipmentId,
|
||||
this.quantity = 1,
|
||||
this.rationale,
|
||||
this.isPrepared = false,
|
||||
this.isLoaded = false,
|
||||
this.isUnloaded = false,
|
||||
@@ -212,6 +214,7 @@ class EventEquipment {
|
||||
return EventEquipment(
|
||||
equipmentId: map['equipmentId'] ?? '',
|
||||
quantity: map['quantity'] ?? 1,
|
||||
rationale: map['rationale'],
|
||||
isPrepared: map['isPrepared'] ?? false,
|
||||
isLoaded: map['isLoaded'] ?? false,
|
||||
isUnloaded: map['isUnloaded'] ?? false,
|
||||
@@ -231,6 +234,7 @@ class EventEquipment {
|
||||
return {
|
||||
'equipmentId': equipmentId,
|
||||
'quantity': quantity,
|
||||
'rationale': rationale,
|
||||
'isPrepared': isPrepared,
|
||||
'isLoaded': isLoaded,
|
||||
'isUnloaded': isUnloaded,
|
||||
@@ -249,6 +253,7 @@ class EventEquipment {
|
||||
EventEquipment copyWith({
|
||||
String? equipmentId,
|
||||
int? quantity,
|
||||
String? rationale,
|
||||
bool? isPrepared,
|
||||
bool? isLoaded,
|
||||
bool? isUnloaded,
|
||||
@@ -265,6 +270,7 @@ class EventEquipment {
|
||||
return EventEquipment(
|
||||
equipmentId: equipmentId ?? this.equipmentId,
|
||||
quantity: quantity ?? this.quantity,
|
||||
rationale: rationale ?? this.rationale,
|
||||
isPrepared: isPrepared ?? this.isPrepared,
|
||||
isLoaded: isLoaded ?? this.isLoaded,
|
||||
isUnloaded: isUnloaded ?? this.isUnloaded,
|
||||
@@ -347,7 +353,7 @@ class EventModel {
|
||||
factory EventModel.fromMap(Map<String, dynamic> map, String id) {
|
||||
try {
|
||||
// Fonction helper pour convertir Timestamp ou String ISO en DateTime
|
||||
DateTime _parseDate(dynamic value, DateTime defaultValue) {
|
||||
DateTime parseDate(dynamic value, DateTime defaultValue) {
|
||||
if (value == null) return defaultValue;
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value) ?? defaultValue;
|
||||
@@ -370,8 +376,8 @@ class EventModel {
|
||||
}
|
||||
|
||||
// Gestion sécurisée des timestamps avec support ISO string
|
||||
final DateTime startDate = _parseDate(map['StartDateTime'], DateTime.now());
|
||||
final DateTime endDate = _parseDate(map['EndDateTime'], startDate.add(const Duration(hours: 1)));
|
||||
final DateTime startDate = parseDate(map['StartDateTime'], DateTime.now());
|
||||
final DateTime endDate = parseDate(map['EndDateTime'], startDate.add(const Duration(hours: 1)));
|
||||
|
||||
// Gestion sécurisée des documents
|
||||
final docsRaw = map['documents'] ?? [];
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class EventStatisticsFilter {
|
||||
final DateTimeRange period;
|
||||
final Set<String> eventTypeIds;
|
||||
final bool includeCanceled;
|
||||
final Set<EventStatus> selectedStatuses;
|
||||
|
||||
const EventStatisticsFilter({
|
||||
required this.period,
|
||||
this.eventTypeIds = const {},
|
||||
this.includeCanceled = false,
|
||||
this.selectedStatuses = const {
|
||||
EventStatus.confirmed,
|
||||
EventStatus.waitingForApproval,
|
||||
},
|
||||
});
|
||||
|
||||
EventStatisticsFilter copyWith({
|
||||
DateTimeRange? period,
|
||||
Set<String>? eventTypeIds,
|
||||
bool? includeCanceled,
|
||||
Set<EventStatus>? selectedStatuses,
|
||||
}) {
|
||||
return EventStatisticsFilter(
|
||||
period: period ?? this.period,
|
||||
eventTypeIds: eventTypeIds ?? this.eventTypeIds,
|
||||
includeCanceled: includeCanceled ?? this.includeCanceled,
|
||||
selectedStatuses: selectedStatuses ?? this.selectedStatuses,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EventTypeStatistics {
|
||||
final String eventTypeId;
|
||||
final String eventTypeName;
|
||||
final int totalEvents;
|
||||
final double totalAmount;
|
||||
final double validatedAmount;
|
||||
final double pendingAmount;
|
||||
final double canceledAmount;
|
||||
|
||||
const EventTypeStatistics({
|
||||
required this.eventTypeId,
|
||||
required this.eventTypeName,
|
||||
required this.totalEvents,
|
||||
required this.totalAmount,
|
||||
required this.validatedAmount,
|
||||
required this.pendingAmount,
|
||||
required this.canceledAmount,
|
||||
});
|
||||
}
|
||||
|
||||
class OptionStatistics {
|
||||
final String optionKey;
|
||||
final String optionLabel;
|
||||
final int usageCount;
|
||||
final int validatedUsageCount;
|
||||
final int quantity;
|
||||
final double totalAmount;
|
||||
|
||||
const OptionStatistics({
|
||||
required this.optionKey,
|
||||
required this.optionLabel,
|
||||
required this.usageCount,
|
||||
required this.validatedUsageCount,
|
||||
required this.quantity,
|
||||
required this.totalAmount,
|
||||
});
|
||||
}
|
||||
|
||||
class EventStatisticsSummary {
|
||||
final int totalEvents;
|
||||
final int validatedEvents;
|
||||
final int pendingEvents;
|
||||
final int canceledEvents;
|
||||
|
||||
final double totalAmount;
|
||||
final double validatedAmount;
|
||||
final double pendingAmount;
|
||||
final double canceledAmount;
|
||||
|
||||
final double baseAmount;
|
||||
final double optionsAmount;
|
||||
final double medianAmount;
|
||||
final List<EventTypeStatistics> byEventType;
|
||||
final List<OptionStatistics> topOptions;
|
||||
|
||||
const EventStatisticsSummary({
|
||||
required this.totalEvents,
|
||||
required this.validatedEvents,
|
||||
required this.pendingEvents,
|
||||
required this.canceledEvents,
|
||||
required this.totalAmount,
|
||||
required this.validatedAmount,
|
||||
required this.pendingAmount,
|
||||
required this.canceledAmount,
|
||||
required this.baseAmount,
|
||||
required this.optionsAmount,
|
||||
required this.medianAmount,
|
||||
required this.byEventType,
|
||||
required this.topOptions,
|
||||
});
|
||||
|
||||
static const empty = EventStatisticsSummary(
|
||||
totalEvents: 0,
|
||||
validatedEvents: 0,
|
||||
pendingEvents: 0,
|
||||
canceledEvents: 0,
|
||||
totalAmount: 0,
|
||||
validatedAmount: 0,
|
||||
pendingAmount: 0,
|
||||
canceledAmount: 0,
|
||||
baseAmount: 0,
|
||||
optionsAmount: 0,
|
||||
medianAmount: 0,
|
||||
byEventType: [],
|
||||
topOptions: [],
|
||||
);
|
||||
|
||||
double get averageAmount => totalEvents == 0 ? 0 : totalAmount / totalEvents;
|
||||
|
||||
double get validationRate =>
|
||||
totalEvents == 0 ? 0 : validatedEvents / totalEvents;
|
||||
|
||||
double get baseContributionRate =>
|
||||
totalAmount == 0 ? 0 : baseAmount / totalAmount;
|
||||
|
||||
double get optionsContributionRate =>
|
||||
totalAmount == 0 ? 0 : optionsAmount / totalAmount;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ class MaintenanceModel {
|
||||
|
||||
factory MaintenanceModel.fromMap(Map<String, dynamic> map, String id) {
|
||||
// Fonction helper pour convertir Timestamp ou String ISO en DateTime
|
||||
DateTime? _parseDate(dynamic value) {
|
||||
DateTime? parseDate(dynamic value) {
|
||||
if (value == null) return null;
|
||||
if (value is Timestamp) return value.toDate();
|
||||
if (value is String) return DateTime.tryParse(value);
|
||||
@@ -76,15 +76,15 @@ class MaintenanceModel {
|
||||
id: id,
|
||||
equipmentIds: equipmentIds,
|
||||
type: maintenanceTypeFromString(map['type']),
|
||||
scheduledDate: _parseDate(map['scheduledDate']) ?? DateTime.now(),
|
||||
completedDate: _parseDate(map['completedDate']),
|
||||
scheduledDate: parseDate(map['scheduledDate']) ?? DateTime.now(),
|
||||
completedDate: parseDate(map['completedDate']),
|
||||
name: map['name'] ?? '',
|
||||
description: map['description'] ?? '',
|
||||
performedBy: map['performedBy'],
|
||||
cost: map['cost']?.toDouble(),
|
||||
notes: map['notes'],
|
||||
createdAt: _parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: _parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
createdAt: parseDate(map['createdAt']) ?? DateTime.now(),
|
||||
updatedAt: parseDate(map['updatedAt']) ?? DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ class AlertProvider extends ChangeNotifier {
|
||||
return AlertModel.fromMap(data as Map<String, dynamic>, data['id'] as String);
|
||||
}).toList();
|
||||
} catch (e) {
|
||||
print('Error loading alerts: $e');
|
||||
if (kDebugMode) debugPrint('Error loading alerts: $e');
|
||||
_alerts = [];
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
@@ -67,7 +67,7 @@ class AlertProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error marking alert as read: $e');
|
||||
if (kDebugMode) debugPrint('Error marking alert as read: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ class AlertProvider extends ChangeNotifier {
|
||||
_alerts.removeWhere((a) => a.id == alertId);
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print('Error deleting alert: $e');
|
||||
if (kDebugMode) debugPrint('Error deleting alert: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ class AlertProvider extends ChangeNotifier {
|
||||
await markAsRead(alertId);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error marking all alerts as read: $e');
|
||||
if (kDebugMode) debugPrint('Error marking all alerts as read: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,7 @@ class AlertProvider extends ChangeNotifier {
|
||||
await deleteAlert(alertId);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error deleting read alerts: $e');
|
||||
if (kDebugMode) debugPrint('Error deleting read alerts: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:em2rp/models/alert_model.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
class AlertProvider extends ChangeNotifier {
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
|
||||
List<AlertModel> _alerts = [];
|
||||
bool _isLoading = false;
|
||||
|
||||
List<AlertModel> get alerts => _alerts;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
/// Nombre d'alertes non lues
|
||||
int get unreadCount => _alerts.where((a) => !a.isRead).length;
|
||||
|
||||
/// Charger toutes les alertes via l'API
|
||||
Future<void> loadAlerts() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final alertsData = await _dataService.getAlerts();
|
||||
|
||||
_alerts = alertsData.map((data) {
|
||||
return AlertModel.fromMap(data, data['id'] as String);
|
||||
}).toList();
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print('Error loading alerts: $e');
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Recharger les alertes
|
||||
Future<void> refresh() async {
|
||||
await loadAlerts();
|
||||
}
|
||||
|
||||
/// Obtenir les alertes non lues
|
||||
List<AlertModel> get unreadAlerts {
|
||||
return _alerts.where((a) => !a.isRead).toList();
|
||||
}
|
||||
|
||||
/// Obtenir les alertes par type
|
||||
List<AlertModel> getByType(AlertType type) {
|
||||
return _alerts.where((a) => a.type == type).toList();
|
||||
}
|
||||
|
||||
/// Obtenir les alertes critiques (stock bas, équipement perdu)
|
||||
List<AlertModel> get criticalAlerts {
|
||||
return _alerts.where((a) =>
|
||||
a.type == AlertType.lowStock || a.type == AlertType.lost
|
||||
).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@ class ContainerProvider with ChangeNotifier {
|
||||
Timer? _searchDebounceTimer;
|
||||
|
||||
// Liste paginée pour la page de gestion
|
||||
List<ContainerModel> _paginatedContainers = [];
|
||||
final List<ContainerModel> _paginatedContainers = [];
|
||||
bool _hasMore = true;
|
||||
bool _isLoadingMore = false;
|
||||
String? _lastVisible;
|
||||
|
||||
// Cache complet pour compatibilité
|
||||
List<ContainerModel> _containers = [];
|
||||
final List<ContainerModel> _containers = [];
|
||||
|
||||
// Filtres et recherche
|
||||
ContainerType? _selectedType;
|
||||
@@ -66,7 +66,7 @@ class ContainerProvider with ChangeNotifier {
|
||||
// Charger toutes les pages en boucle
|
||||
while (hasMore) {
|
||||
pageCount++;
|
||||
print('[ContainerProvider] Loading page $pageCount...');
|
||||
DebugLog.info('[ContainerProvider] Loading page $pageCount...');
|
||||
|
||||
final result = await _dataService.getContainersPaginated(
|
||||
limit: 100, // Charger 100 par page pour aller plus vite
|
||||
@@ -86,14 +86,14 @@ class ContainerProvider with ChangeNotifier {
|
||||
hasMore = result['hasMore'] as bool? ?? false;
|
||||
lastVisible = result['lastVisible'] as String?;
|
||||
|
||||
print('[ContainerProvider] Loaded ${containers.length} containers, total: ${_containers.length}, hasMore: $hasMore');
|
||||
DebugLog.info('[ContainerProvider] Loaded ${containers.length} containers, total: ${_containers.length}, hasMore: $hasMore');
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
_isInitialized = true;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print('Error loading containers: $e');
|
||||
DebugLog.error('[ContainerProvider] Error loading containers', e);
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -292,7 +292,7 @@ class ContainerProvider with ChangeNotifier {
|
||||
Future<List<ContainerModel>> getContainersByIds(List<String> containerIds) async {
|
||||
if (containerIds.isEmpty) return [];
|
||||
|
||||
print('[ContainerProvider] Loading ${containerIds.length} containers by IDs...');
|
||||
DebugLog.info('[ContainerProvider] Loading ${containerIds.length} containers by IDs...');
|
||||
|
||||
try {
|
||||
// Vérifier d'abord le cache local
|
||||
@@ -320,7 +320,7 @@ class ContainerProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
print('[ContainerProvider] Found ${cachedContainers.length} in cache, ${missingIds.length} missing');
|
||||
DebugLog.info('[ContainerProvider] Found ${cachedContainers.length} in cache, ${missingIds.length} missing');
|
||||
|
||||
// Si tous sont en cache, retourner directement
|
||||
if (missingIds.isEmpty) {
|
||||
@@ -341,12 +341,12 @@ class ContainerProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
print('[ContainerProvider] Loaded ${loadedContainers.length} containers from API');
|
||||
DebugLog.info('[ContainerProvider] Loaded ${loadedContainers.length} containers from API');
|
||||
|
||||
// Retourner tous les conteneurs (cache + chargés)
|
||||
return [...cachedContainers, ...loadedContainers];
|
||||
} catch (e) {
|
||||
print('[ContainerProvider] Error loading containers by IDs: $e');
|
||||
DebugLog.error('[ContainerProvider] Error loading containers by IDs', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ class EquipmentProvider extends ChangeNotifier {
|
||||
Timer? _searchDebounceTimer;
|
||||
|
||||
// Liste paginée pour la page de gestion
|
||||
List<EquipmentModel> _paginatedEquipment = [];
|
||||
final List<EquipmentModel> _paginatedEquipment = [];
|
||||
bool _hasMore = true;
|
||||
bool _isLoadingMore = false;
|
||||
String? _lastVisible;
|
||||
|
||||
// Cache complet pour getEquipmentsByIds et compatibilité
|
||||
List<EquipmentModel> _equipment = [];
|
||||
final List<EquipmentModel> _equipment = [];
|
||||
List<String> _models = [];
|
||||
List<String> _brands = [];
|
||||
|
||||
@@ -80,7 +80,7 @@ class EquipmentProvider extends ChangeNotifier {
|
||||
Future<void> loadEquipments() async {
|
||||
print('[EquipmentProvider] Starting to load ALL equipments...');
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
scheduleMicrotask(notifyListeners);
|
||||
|
||||
try {
|
||||
_equipment.clear();
|
||||
@@ -272,7 +272,7 @@ class EquipmentProvider extends ChangeNotifier {
|
||||
_lastVisible = null;
|
||||
_hasMore = true;
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
scheduleMicrotask(notifyListeners);
|
||||
|
||||
try {
|
||||
await loadNextPage();
|
||||
@@ -296,7 +296,7 @@ class EquipmentProvider extends ChangeNotifier {
|
||||
|
||||
_isLoadingMore = true;
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
scheduleMicrotask(notifyListeners);
|
||||
|
||||
try {
|
||||
final result = await _dataService.getEquipmentsPaginated(
|
||||
@@ -433,9 +433,9 @@ class EquipmentProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
/// Supprimer un équipement
|
||||
Future<void> deleteEquipment(String equipmentId) async {
|
||||
Future<void> deleteEquipment(String equipmentId, {bool forceDelete = false}) async {
|
||||
try {
|
||||
await _dataService.deleteEquipment(equipmentId);
|
||||
await _dataService.deleteEquipment(equipmentId, forceDelete: forceDelete);
|
||||
if (_usePagination) {
|
||||
await reload();
|
||||
} else {
|
||||
|
||||
@@ -19,7 +19,8 @@ class EventProvider with ChangeNotifier {
|
||||
bool _lastCanViewAll = false;
|
||||
|
||||
// Nouveau: Cache par mois pour le lazy loading
|
||||
Map<String, List<EventModel>> _eventsByMonth = {}; // "2026-02" => [events]
|
||||
final Map<String, List<EventModel>> _eventsByMonth =
|
||||
{}; // "2026-02" => [events]
|
||||
String? _currentMonth; // Mois actuellement affiché
|
||||
|
||||
List<EventModel> get events => _events;
|
||||
@@ -28,7 +29,8 @@ class EventProvider with ChangeNotifier {
|
||||
/// Vérifie si les données doivent être rechargées (cache de 30 secondes)
|
||||
bool _shouldReload(String userId, bool canViewAllEvents) {
|
||||
if (_lastLoadTime == null) return true;
|
||||
if (_lastUserId != userId || _lastCanViewAll != canViewAllEvents) return true;
|
||||
if (_lastUserId != userId || _lastCanViewAll != canViewAllEvents)
|
||||
return true;
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(_lastLoadTime!);
|
||||
@@ -36,12 +38,14 @@ class EventProvider with ChangeNotifier {
|
||||
}
|
||||
|
||||
/// Charger les événements d'un utilisateur via l'API
|
||||
Future<void> loadUserEvents(String userId, {bool canViewAllEvents = false, bool forceReload = false}) async {
|
||||
Future<void> loadUserEvents(String userId,
|
||||
{bool canViewAllEvents = false, bool forceReload = false}) async {
|
||||
PerformanceMonitor.start('EventProvider.loadUserEvents');
|
||||
|
||||
// Éviter les rechargements inutiles
|
||||
if (!forceReload && !_shouldReload(userId, canViewAllEvents)) {
|
||||
print('Using cached events (loaded ${DateTime.now().difference(_lastLoadTime!).inSeconds}s ago)');
|
||||
print(
|
||||
'Using cached events (loaded ${DateTime.now().difference(_lastLoadTime!).inSeconds}s ago)');
|
||||
PerformanceMonitor.end('EventProvider.loadUserEvents');
|
||||
return;
|
||||
}
|
||||
@@ -50,7 +54,8 @@ class EventProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
print('Loading events for user: $userId (canViewAllEvents: $canViewAllEvents)');
|
||||
print(
|
||||
'Loading events for user: $userId (canViewAllEvents: $canViewAllEvents)');
|
||||
|
||||
PerformanceMonitor.start('EventProvider.getEvents_API');
|
||||
// Charger via l'API - les permissions sont vérifiées côté serveur
|
||||
@@ -61,9 +66,8 @@ class EventProvider with ChangeNotifier {
|
||||
final usersData = result['users'] as Map<String, dynamic>;
|
||||
|
||||
// Stocker les utilisateurs dans le cache
|
||||
_usersCache = usersData.map((key, value) =>
|
||||
MapEntry(key, value as Map<String, dynamic>)
|
||||
);
|
||||
_usersCache = usersData
|
||||
.map((key, value) => MapEntry(key, value as Map<String, dynamic>));
|
||||
|
||||
print('Found ${eventsData.length} events from API');
|
||||
|
||||
@@ -74,7 +78,8 @@ class EventProvider with ChangeNotifier {
|
||||
// Parser chaque événement
|
||||
for (var eventData in eventsData) {
|
||||
try {
|
||||
final event = EventModel.fromMap(eventData, eventData['id'] as String);
|
||||
final event =
|
||||
EventModel.fromMap(eventData, eventData['id'] as String);
|
||||
allEvents.add(event);
|
||||
} catch (e) {
|
||||
print('Failed to parse event ${eventData['id']}: $e');
|
||||
@@ -88,7 +93,8 @@ class EventProvider with ChangeNotifier {
|
||||
_lastUserId = userId;
|
||||
_lastCanViewAll = canViewAllEvents;
|
||||
|
||||
print('Successfully loaded ${_events.length} events (${failedCount} failed)');
|
||||
print(
|
||||
'Successfully loaded ${_events.length} events ($failedCount failed)');
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
@@ -104,8 +110,9 @@ class EventProvider with ChangeNotifier {
|
||||
|
||||
/// Charger les événements d'un mois spécifique (lazy loading optimisé)
|
||||
Future<void> loadMonthEvents(String userId, int year, int month,
|
||||
{bool canViewAllEvents = false, bool forceReload = false, bool silent = false}) async {
|
||||
|
||||
{bool canViewAllEvents = false,
|
||||
bool forceReload = false,
|
||||
bool silent = false}) async {
|
||||
final monthKey = '$year-${month.toString().padLeft(2, '0')}';
|
||||
|
||||
// Vérifier le cache
|
||||
@@ -130,19 +137,15 @@ class EventProvider with ChangeNotifier {
|
||||
|
||||
PerformanceMonitor.start('EventProvider.loadMonthEvents_API');
|
||||
final result = await _dataService.getEventsByMonth(
|
||||
userId: userId,
|
||||
year: year,
|
||||
month: month
|
||||
);
|
||||
userId: userId, year: year, month: month);
|
||||
PerformanceMonitor.end('EventProvider.loadMonthEvents_API');
|
||||
|
||||
final eventsData = result['events'] as List<Map<String, dynamic>>;
|
||||
final usersData = result['users'] as Map<String, dynamic>;
|
||||
|
||||
// Mettre à jour le cache utilisateurs (addAll pour cumuler)
|
||||
_usersCache.addAll(
|
||||
usersData.map((key, value) => MapEntry(key, value as Map<String, dynamic>))
|
||||
);
|
||||
_usersCache.addAll(usersData
|
||||
.map((key, value) => MapEntry(key, value as Map<String, dynamic>)));
|
||||
|
||||
print('[EventProvider] Found ${eventsData.length} events for $monthKey');
|
||||
|
||||
@@ -153,7 +156,8 @@ class EventProvider with ChangeNotifier {
|
||||
// Parser les événements
|
||||
for (var eventData in eventsData) {
|
||||
try {
|
||||
final event = EventModel.fromMap(eventData, eventData['id'] as String);
|
||||
final event =
|
||||
EventModel.fromMap(eventData, eventData['id'] as String);
|
||||
monthEvents.add(event);
|
||||
} catch (e) {
|
||||
print('[EventProvider] Failed to parse event ${eventData['id']}: $e');
|
||||
@@ -176,7 +180,8 @@ class EventProvider with ChangeNotifier {
|
||||
_lastUserId = userId;
|
||||
_lastCanViewAll = canViewAllEvents;
|
||||
|
||||
print('[EventProvider] Successfully loaded ${monthEvents.length} events for $monthKey (${failedCount} failed)');
|
||||
print(
|
||||
'[EventProvider] Successfully loaded ${monthEvents.length} events for $monthKey ($failedCount failed)');
|
||||
|
||||
if (!silent) {
|
||||
_isLoading = false;
|
||||
@@ -195,7 +200,6 @@ class EventProvider with ChangeNotifier {
|
||||
/// Précharger les mois adjacents en arrière-plan
|
||||
void preloadAdjacentMonths(String userId, int year, int month,
|
||||
{bool canViewAllEvents = false}) {
|
||||
|
||||
// Mois précédent
|
||||
final prevMonth = month == 1 ? 12 : month - 1;
|
||||
final prevYear = month == 1 ? year - 1 : year;
|
||||
@@ -220,9 +224,20 @@ class EventProvider with ChangeNotifier {
|
||||
});
|
||||
}
|
||||
|
||||
/// Vide entièrement le cache (mois + métadonnées) pour forcer un rechargement complet
|
||||
void clearAllCache() {
|
||||
_eventsByMonth.clear();
|
||||
_lastLoadTime = null;
|
||||
_lastUserId = null;
|
||||
_currentMonth = null;
|
||||
print('[EventProvider] Cache entièrement vidé');
|
||||
}
|
||||
|
||||
/// Recharger les événements (utilise le dernier userId)
|
||||
Future<void> refreshEvents(String userId, {bool canViewAllEvents = false}) async {
|
||||
await loadUserEvents(userId, canViewAllEvents: canViewAllEvents, forceReload: true);
|
||||
Future<void> refreshEvents(String userId,
|
||||
{bool canViewAllEvents = false}) async {
|
||||
await loadUserEvents(userId,
|
||||
canViewAllEvents: canViewAllEvents, forceReload: true);
|
||||
}
|
||||
|
||||
/// Récupérer un événement spécifique par ID
|
||||
@@ -234,6 +249,41 @@ class EventProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Recherche des événements accessibles à l'utilisateur.
|
||||
Future<List<EventModel>> searchEvents({
|
||||
required String userId,
|
||||
required String query,
|
||||
int limit = 20,
|
||||
}) async {
|
||||
final trimmedQuery = query.trim();
|
||||
if (trimmedQuery.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final result = await _dataService.searchEvents(
|
||||
userId: userId,
|
||||
query: trimmedQuery,
|
||||
limit: limit,
|
||||
);
|
||||
|
||||
final events = <EventModel>[];
|
||||
for (final eventData in result) {
|
||||
try {
|
||||
final eventId = eventData['id'] as String?;
|
||||
if (eventId == null || eventId.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
events.add(EventModel.fromMap(eventData, eventId));
|
||||
} catch (e) {
|
||||
print('Failed to parse searched event ${eventData['id']}: $e');
|
||||
}
|
||||
}
|
||||
|
||||
events.sort((a, b) => a.startDateTime.compareTo(b.startDateTime));
|
||||
return events;
|
||||
}
|
||||
|
||||
/// Ajouter un nouvel événement
|
||||
Future<void> addEvent(EventModel event) async {
|
||||
try {
|
||||
@@ -241,7 +291,8 @@ class EventProvider with ChangeNotifier {
|
||||
_events.add(event);
|
||||
|
||||
// Ajouter dans le cache par mois
|
||||
final monthKey = '${event.startDateTime.year}-${event.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
final monthKey =
|
||||
'${event.startDateTime.year}-${event.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
if (_eventsByMonth.containsKey(monthKey)) {
|
||||
_eventsByMonth[monthKey]!.add(event);
|
||||
}
|
||||
@@ -263,8 +314,10 @@ class EventProvider with ChangeNotifier {
|
||||
_events[index] = event;
|
||||
|
||||
// Mettre à jour dans le cache par mois
|
||||
final oldMonthKey = '${oldEvent.startDateTime.year}-${oldEvent.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
final newMonthKey = '${event.startDateTime.year}-${event.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
final oldMonthKey =
|
||||
'${oldEvent.startDateTime.year}-${oldEvent.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
final newMonthKey =
|
||||
'${event.startDateTime.year}-${event.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
|
||||
// Si le mois a changé, supprimer de l'ancien et ajouter au nouveau
|
||||
if (oldMonthKey != newMonthKey) {
|
||||
@@ -277,7 +330,8 @@ class EventProvider with ChangeNotifier {
|
||||
} else {
|
||||
// Même mois, juste mettre à jour
|
||||
if (_eventsByMonth.containsKey(newMonthKey)) {
|
||||
final monthIndex = _eventsByMonth[newMonthKey]!.indexWhere((e) => e.id == event.id);
|
||||
final monthIndex = _eventsByMonth[newMonthKey]!
|
||||
.indexWhere((e) => e.id == event.id);
|
||||
if (monthIndex != -1) {
|
||||
_eventsByMonth[newMonthKey]![monthIndex] = event;
|
||||
}
|
||||
@@ -299,7 +353,8 @@ class EventProvider with ChangeNotifier {
|
||||
|
||||
// Trouver l'événement pour obtenir sa date avant de le supprimer
|
||||
final eventToDelete = _events.firstWhere((e) => e.id == eventId);
|
||||
final monthKey = '${eventToDelete.startDateTime.year}-${eventToDelete.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
final monthKey =
|
||||
'${eventToDelete.startDateTime.year}-${eventToDelete.startDateTime.month.toString().padLeft(2, '0')}';
|
||||
|
||||
// Supprimer de _events
|
||||
_events.removeWhere((event) => event.id == eventId);
|
||||
|
||||
@@ -12,7 +12,7 @@ import '../utils/performance_monitor.dart';
|
||||
class LocalUserProvider with ChangeNotifier {
|
||||
UserModel? _currentUser;
|
||||
RoleModel? _currentRole;
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
FirebaseAuth? _auth;
|
||||
final FirebaseStorageManager _storageManager = FirebaseStorageManager();
|
||||
final DataService _dataService = DataService(apiService);
|
||||
|
||||
@@ -43,11 +43,41 @@ class LocalUserProvider with ChangeNotifier {
|
||||
|
||||
/// Charge les données de l'utilisateur actuel via Cloud Function
|
||||
Future<void> loadUserData({bool forceReload = false}) async {
|
||||
if (_auth.currentUser == null) {
|
||||
// Si FirebaseAuth n'est pas encore disponible
|
||||
final FirebaseAuth auth;
|
||||
try {
|
||||
auth = _getAuthInstance();
|
||||
} catch (e) {
|
||||
print('Auth instance not ready in loadUserData: $e');
|
||||
return;
|
||||
}
|
||||
|
||||
if (auth.currentUser == null) {
|
||||
print('No current user in Auth');
|
||||
return;
|
||||
}
|
||||
|
||||
// Bootstrap léger : rendre l'UID disponible tout de suite pour les écrans
|
||||
// qui en ont besoin, même si le profil complet n'est pas encore chargé.
|
||||
if (_currentUser == null) {
|
||||
final firebaseUser = auth.currentUser!;
|
||||
_currentUser = UserModel(
|
||||
uid: firebaseUser.uid,
|
||||
email: firebaseUser.email ?? '',
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
role: 'USER',
|
||||
phoneNumber: '',
|
||||
profilePhotoUrl: firebaseUser.photoURL ?? '',
|
||||
);
|
||||
_currentRole = RoleModel(
|
||||
id: 'USER',
|
||||
name: '',
|
||||
permissions: const [],
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// Éviter les rechargements inutiles
|
||||
if (!forceReload && !_shouldReloadUserData()) {
|
||||
print('Using cached user data');
|
||||
@@ -62,7 +92,7 @@ class LocalUserProvider with ChangeNotifier {
|
||||
|
||||
_isLoadingUserData = true;
|
||||
PerformanceMonitor.start('LocalUserProvider.loadUserData');
|
||||
print('Loading user data for: ${_auth.currentUser!.uid}');
|
||||
print('Loading user data for: ${_auth!.currentUser!.uid}');
|
||||
try {
|
||||
// Utiliser la Cloud Function getCurrentUser
|
||||
PerformanceMonitor.start('LocalUserProvider.getCurrentUser_API');
|
||||
@@ -194,7 +224,8 @@ class LocalUserProvider with ChangeNotifier {
|
||||
Future<UserCredential> signInWithEmailAndPassword(
|
||||
String email, String password) async {
|
||||
try {
|
||||
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
|
||||
final auth = _getAuthInstance();
|
||||
UserCredential userCredential = await auth.signInWithEmailAndPassword(
|
||||
email: email, password: password);
|
||||
// Note: loadUserData() sera appelé en arrière-plan dans main.dart
|
||||
// pour ne pas bloquer la navigation
|
||||
@@ -206,10 +237,25 @@ class LocalUserProvider with ChangeNotifier {
|
||||
|
||||
/// Déconnexion
|
||||
Future<void> signOut() async {
|
||||
await _auth.signOut();
|
||||
try {
|
||||
final auth = _getAuthInstance();
|
||||
await auth.signOut();
|
||||
} catch (e) {
|
||||
debugPrint('Error during signOut: $e');
|
||||
}
|
||||
clearUser();
|
||||
}
|
||||
|
||||
FirebaseAuth _getAuthInstance() {
|
||||
try {
|
||||
_auth ??= FirebaseAuth.instance;
|
||||
return _auth!;
|
||||
} catch (e, st) {
|
||||
debugPrint('[LocalUserProvider] FirebaseAuth.instance access error: $e\n$st');
|
||||
throw Exception('FirebaseAuth not available');
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifie si l'utilisateur a une permission spécifique
|
||||
bool hasPermission(String permission) {
|
||||
return _currentRole?.permissions.contains(permission) ?? false;
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:em2rp/models/maintenance_model.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
class MaintenanceProvider extends ChangeNotifier {
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
|
||||
List<MaintenanceModel> _maintenances = [];
|
||||
bool _isLoading = false;
|
||||
|
||||
List<MaintenanceModel> get maintenances => _maintenances;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
/// Charger toutes les maintenances via l'API
|
||||
Future<void> loadMaintenances({String? equipmentId}) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final maintenancesData = await _dataService.getMaintenances(
|
||||
equipmentId: equipmentId,
|
||||
);
|
||||
|
||||
_maintenances = maintenancesData.map((data) {
|
||||
return MaintenanceModel.fromMap(data, data['id'] as String);
|
||||
}).toList();
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print('Error loading maintenances: $e');
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Recharger les maintenances
|
||||
Future<void> refresh({String? equipmentId}) async {
|
||||
await loadMaintenances(equipmentId: equipmentId);
|
||||
}
|
||||
|
||||
/// Obtenir les maintenances pour un équipement spécifique
|
||||
List<MaintenanceModel> getForEquipment(String equipmentId) {
|
||||
return _maintenances.where((m) =>
|
||||
m.equipmentIds.contains(equipmentId)
|
||||
).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les alertes.
|
||||
class AlertRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
AlertRepository(this._apiService);
|
||||
|
||||
/// Récupère toutes les alertes
|
||||
Future<List<Map<String, dynamic>>> getAlerts() async {
|
||||
try {
|
||||
final result = await _apiService.call('getAlerts', {});
|
||||
final alerts = result['alerts'] as List<dynamic>?;
|
||||
if (alerts == null) return [];
|
||||
return alerts.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des alertes: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Marque une alerte comme lue
|
||||
Future<void> markAlertAsRead(String alertId) async {
|
||||
try {
|
||||
await _apiService.call('markAlertAsRead', {'alertId': alertId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors du marquage de l\'alerte comme lue: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime une alerte
|
||||
Future<void> deleteAlert(String alertId) async {
|
||||
try {
|
||||
await _apiService.call('deleteAlert', {'alertId': alertId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'alerte: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les conteneurs.
|
||||
class ContainerRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
ContainerRepository(this._apiService);
|
||||
|
||||
/// Récupère tous les conteneurs
|
||||
Future<List<Map<String, dynamic>>> getContainers() async {
|
||||
try {
|
||||
final result = await _apiService.call('getContainers', {});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) return [];
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des conteneurs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère plusieurs containers par leurs IDs
|
||||
Future<List<Map<String, dynamic>>> getContainersByIds(
|
||||
List<String> containerIds) async {
|
||||
try {
|
||||
if (containerIds.isEmpty) return [];
|
||||
|
||||
print(
|
||||
'[ContainerRepository] Getting containers by IDs: ${containerIds.length} items');
|
||||
final result = await _apiService.call('getContainersByIds', {
|
||||
'containerIds': containerIds,
|
||||
});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) {
|
||||
print('[ContainerRepository] No containers in result');
|
||||
return [];
|
||||
}
|
||||
print('[ContainerRepository] Found ${containers.length} containers by IDs');
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[ContainerRepository] Error getting containers by IDs: $e');
|
||||
throw Exception('Erreur lors de la récupération des containers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les containers avec pagination et filtrage
|
||||
Future<Map<String, dynamic>> getContainersPaginated({
|
||||
int limit = 20,
|
||||
String? startAfter,
|
||||
String? type,
|
||||
String? status,
|
||||
String? searchQuery,
|
||||
String? category,
|
||||
String sortBy = 'id',
|
||||
String sortOrder = 'asc',
|
||||
}) async {
|
||||
try {
|
||||
final params = <String, dynamic>{
|
||||
'limit': limit,
|
||||
'sortBy': sortBy,
|
||||
'sortOrder': sortOrder,
|
||||
};
|
||||
|
||||
if (startAfter != null) params['startAfter'] = startAfter;
|
||||
if (type != null) params['type'] = type;
|
||||
if (status != null) params['status'] = status;
|
||||
if (category != null) params['category'] = category;
|
||||
if (searchQuery != null && searchQuery.isNotEmpty) {
|
||||
params['searchQuery'] = searchQuery;
|
||||
}
|
||||
|
||||
final result =
|
||||
await (_apiService as FirebaseFunctionsApiService).callPaginated(
|
||||
'getContainersPaginated',
|
||||
params,
|
||||
);
|
||||
|
||||
return {
|
||||
'containers': (result['containers'] as List<dynamic>?)
|
||||
?.map((e) => e as Map<String, dynamic>)
|
||||
.toList() ??
|
||||
[],
|
||||
'hasMore': result['hasMore'] as bool? ?? false,
|
||||
'lastVisible': result['lastVisible'] as String?,
|
||||
'total': result['total'] as int? ?? 0,
|
||||
};
|
||||
} catch (e) {
|
||||
DebugLog.error('[ContainerRepository] Error in getContainersPaginated', e);
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération paginée des containers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les containers contenant un équipement
|
||||
Future<List<Map<String, dynamic>>> getContainersByEquipment(
|
||||
String equipmentId) async {
|
||||
try {
|
||||
final result = await _apiService.call('getContainersByEquipment', {
|
||||
'equipmentId': equipmentId,
|
||||
});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) return [];
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des containers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifie la disponibilité d'un container
|
||||
Future<Map<String, dynamic>> checkContainerAvailability({
|
||||
required String containerId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('checkContainerAvailability', {
|
||||
'containerId': containerId,
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
if (excludeEventId != null) 'excludeEventId': excludeEventId,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la vérification de disponibilité du container: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les équipements.
|
||||
class EquipmentRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
EquipmentRepository(this._apiService);
|
||||
|
||||
/// Récupère tous les équipements (avec masquage des prix selon permissions)
|
||||
Future<List<Map<String, dynamic>>> getEquipments() async {
|
||||
try {
|
||||
print('[EquipmentRepository] Calling getEquipments API...');
|
||||
final result = await _apiService.call('getEquipments', {});
|
||||
print('[EquipmentRepository] API call successful, parsing result...');
|
||||
final equipments = result['equipments'] as List<dynamic>?;
|
||||
if (equipments == null) {
|
||||
print('[EquipmentRepository] No equipments in result');
|
||||
return [];
|
||||
}
|
||||
print('[EquipmentRepository] Found ${equipments.length} equipments');
|
||||
return equipments.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[EquipmentRepository] Error getting equipments: $e');
|
||||
throw Exception('Erreur lors de la récupération des équipements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère plusieurs équipements par leurs IDs
|
||||
Future<List<Map<String, dynamic>>> getEquipmentsByIds(
|
||||
List<String> equipmentIds) async {
|
||||
try {
|
||||
if (equipmentIds.isEmpty) return [];
|
||||
|
||||
print(
|
||||
'[EquipmentRepository] Getting equipments by IDs: ${equipmentIds.length} items');
|
||||
final result = await _apiService.call('getEquipmentsByIds', {
|
||||
'equipmentIds': equipmentIds,
|
||||
});
|
||||
final equipments = result['equipments'] as List<dynamic>?;
|
||||
if (equipments == null) {
|
||||
print('[EquipmentRepository] No equipments in result');
|
||||
return [];
|
||||
}
|
||||
print('[EquipmentRepository] Found ${equipments.length} equipments by IDs');
|
||||
return equipments.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[EquipmentRepository] Error getting equipments by IDs: $e');
|
||||
throw Exception('Erreur lors de la récupération des équipements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les équipements avec pagination et filtrage
|
||||
Future<Map<String, dynamic>> getEquipmentsPaginated({
|
||||
int limit = 20,
|
||||
String? startAfter,
|
||||
String? category,
|
||||
String? status,
|
||||
String? searchQuery,
|
||||
String sortBy = 'id',
|
||||
String sortOrder = 'asc',
|
||||
}) async {
|
||||
try {
|
||||
final params = <String, dynamic>{
|
||||
'limit': limit,
|
||||
'sortBy': sortBy,
|
||||
'sortOrder': sortOrder,
|
||||
};
|
||||
|
||||
if (startAfter != null) params['startAfter'] = startAfter;
|
||||
if (category != null) params['category'] = category;
|
||||
if (status != null) params['status'] = status;
|
||||
if (searchQuery != null && searchQuery.isNotEmpty) {
|
||||
params['searchQuery'] = searchQuery;
|
||||
}
|
||||
|
||||
final result =
|
||||
await (_apiService as FirebaseFunctionsApiService).callPaginated(
|
||||
'getEquipmentsPaginated',
|
||||
params,
|
||||
);
|
||||
|
||||
return {
|
||||
'equipments': (result['equipments'] as List<dynamic>?)
|
||||
?.map((e) => e as Map<String, dynamic>)
|
||||
.toList() ??
|
||||
[],
|
||||
'hasMore': result['hasMore'] as bool? ?? false,
|
||||
'lastVisible': result['lastVisible'] as String?,
|
||||
'total': result['total'] as int? ?? 0,
|
||||
};
|
||||
} catch (e) {
|
||||
DebugLog.error('[EquipmentRepository] Error in getEquipmentsPaginated', e);
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération paginée des équipements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un équipement
|
||||
Future<void> createEquipment(
|
||||
String equipmentId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final equipmentData = Map<String, dynamic>.from(data);
|
||||
equipmentData['id'] = equipmentId;
|
||||
|
||||
await _apiService.call('createEquipment', equipmentData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un équipement
|
||||
Future<void> updateEquipment(
|
||||
String equipmentId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
await _apiService.call('updateEquipment', {
|
||||
'equipmentId': equipmentId,
|
||||
'data': data,
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un équipement
|
||||
Future<void> deleteEquipment(String equipmentId,
|
||||
{bool forceDelete = false}) async {
|
||||
try {
|
||||
await _apiService.call('deleteEquipment', {
|
||||
'equipmentId': equipmentId,
|
||||
'forceDelete': forceDelete,
|
||||
});
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour uniquement le statut d'un équipement
|
||||
Future<void> updateEquipmentStatusOnly({
|
||||
required String equipmentId,
|
||||
String? status,
|
||||
int? availableQuantity,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'equipmentId': equipmentId};
|
||||
|
||||
if (status != null) data['status'] = status;
|
||||
if (availableQuantity != null) {
|
||||
data['availableQuantity'] = availableQuantity;
|
||||
}
|
||||
|
||||
await _apiService.call('updateEquipmentStatusOnly', data);
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Erreur lors de la mise à jour du statut de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Recherche rapide (autocomplétion)
|
||||
Future<List<Map<String, dynamic>>> quickSearch(
|
||||
String query, {
|
||||
int limit = 10,
|
||||
bool includeEquipments = true,
|
||||
bool includeContainers = true,
|
||||
}) async {
|
||||
try {
|
||||
return await (_apiService as FirebaseFunctionsApiService).quickSearch(
|
||||
query,
|
||||
limit: limit,
|
||||
includeEquipments: includeEquipments,
|
||||
includeContainers: includeContainers,
|
||||
);
|
||||
} catch (e) {
|
||||
DebugLog.error('[EquipmentRepository] Error in quickSearch', e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Recherche des équipements pour l'assistant IA avec fallback paginé.
|
||||
Future<List<Map<String, dynamic>>> searchEquipmentsForAssistant({
|
||||
required String query,
|
||||
int limit = 12,
|
||||
}) async {
|
||||
final normalizedQuery = query.trim();
|
||||
if (normalizedQuery.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
final quickResults = await quickSearch(
|
||||
normalizedQuery,
|
||||
limit: limit,
|
||||
includeEquipments: true,
|
||||
includeContainers: false,
|
||||
);
|
||||
|
||||
final equipmentResults = quickResults
|
||||
.where((item) =>
|
||||
(item['type']?.toString().toLowerCase() ?? '') == 'equipment')
|
||||
.map(_normalizeAssistantEquipment)
|
||||
.toList();
|
||||
|
||||
if (equipmentResults.isNotEmpty) {
|
||||
return equipmentResults;
|
||||
}
|
||||
|
||||
final paginated = await getEquipmentsPaginated(
|
||||
limit: limit,
|
||||
searchQuery: normalizedQuery,
|
||||
sortBy: 'id',
|
||||
sortOrder: 'asc',
|
||||
);
|
||||
|
||||
final equipments =
|
||||
paginated['equipments'] as List<Map<String, dynamic>>? ?? [];
|
||||
return equipments.map(_normalizeAssistantEquipment).toList();
|
||||
} catch (e) {
|
||||
DebugLog.error('[EquipmentRepository] Error in searchEquipmentsForAssistant', e);
|
||||
throw Exception('Erreur lors de la recherche de matériel: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifie la disponibilité d'un équipement dans un format normalisé pour l'IA.
|
||||
Future<Map<String, dynamic>> checkEquipmentAvailabilityForAssistant({
|
||||
required String equipmentId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await checkEquipmentAvailability(
|
||||
equipmentId: equipmentId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
final available = result['available'] as bool? ?? true;
|
||||
final conflicts = (result['conflicts'] as List<dynamic>? ?? const [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map((conflict) {
|
||||
final eventData =
|
||||
conflict['eventData'] as Map<String, dynamic>? ?? const {};
|
||||
final eventName =
|
||||
(eventData['Name'] ?? conflict['eventName'] ?? '').toString();
|
||||
return {
|
||||
'eventId': conflict['eventId']?.toString() ?? '',
|
||||
'eventName': eventName,
|
||||
'overlapDays': conflict['overlapDays'] as int? ?? 0,
|
||||
};
|
||||
}).toList();
|
||||
|
||||
return {
|
||||
'equipmentId': equipmentId,
|
||||
'available': available,
|
||||
'conflictCount': conflicts.length,
|
||||
'conflicts': conflicts,
|
||||
};
|
||||
} catch (e) {
|
||||
DebugLog.error(
|
||||
'[EquipmentRepository] Error in checkEquipmentAvailabilityForAssistant', e);
|
||||
throw Exception('Erreur lors de la vérification de disponibilité: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifie la disponibilité d'un équipement
|
||||
Future<Map<String, dynamic>> checkEquipmentAvailability({
|
||||
required String equipmentId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('checkEquipmentAvailability', {
|
||||
'equipmentId': equipmentId,
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
if (excludeEventId != null) 'excludeEventId': excludeEventId,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la vérification de disponibilité: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les IDs d'équipements et conteneurs en conflit pour une période
|
||||
/// Optimisé : une seule requête au lieu d'une par équipement
|
||||
Future<Map<String, dynamic>> getConflictingEquipmentIds({
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
int installationTime = 0,
|
||||
int disassemblyTime = 0,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('getConflictingEquipmentIds', {
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
if (excludeEventId != null) 'excludeEventId': excludeEventId,
|
||||
'installationTime': installationTime,
|
||||
'disassemblyTime': disassemblyTime,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération des équipements en conflit: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _normalizeAssistantEquipment(Map<String, dynamic> item) {
|
||||
return {
|
||||
'id': (item['id'] ?? '').toString(),
|
||||
'name': (item['name'] ?? item['id'] ?? '').toString(),
|
||||
'category': (item['category'] ?? '').toString(),
|
||||
'status': (item['status'] ?? '').toString(),
|
||||
'brand': item['brand']?.toString(),
|
||||
'model': item['model']?.toString(),
|
||||
'availableQuantity': item['availableQuantity'],
|
||||
'totalQuantity': item['totalQuantity'],
|
||||
};
|
||||
}
|
||||
|
||||
/// Récupère toutes les maintenances
|
||||
Future<List<Map<String, dynamic>>> getMaintenances(
|
||||
{String? equipmentId}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{};
|
||||
if (equipmentId != null) data['equipmentId'] = equipmentId;
|
||||
|
||||
final result = await _apiService.call('getMaintenances', data);
|
||||
final maintenances = result['maintenances'] as List<dynamic>?;
|
||||
if (maintenances == null) return [];
|
||||
return maintenances.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des maintenances: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime une maintenance
|
||||
Future<void> deleteMaintenance(String maintenanceId) async {
|
||||
try {
|
||||
await _apiService
|
||||
.call('deleteMaintenance', {'maintenanceId': maintenanceId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de la maintenance: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les événements.
|
||||
class EventRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
EventRepository(this._apiService);
|
||||
|
||||
/// Met à jour les équipements d'un événement
|
||||
Future<void> updateEventEquipment({
|
||||
required String eventId,
|
||||
List<Map<String, dynamic>>? assignedEquipment,
|
||||
String? preparationStatus,
|
||||
String? loadingStatus,
|
||||
String? unloadingStatus,
|
||||
String? returnStatus,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'eventId': eventId};
|
||||
|
||||
if (assignedEquipment != null) {
|
||||
data['assignedEquipment'] = assignedEquipment;
|
||||
}
|
||||
if (preparationStatus != null) {
|
||||
data['preparationStatus'] = preparationStatus;
|
||||
}
|
||||
if (loadingStatus != null) data['loadingStatus'] = loadingStatus;
|
||||
if (unloadingStatus != null) data['unloadingStatus'] = unloadingStatus;
|
||||
if (returnStatus != null) data['returnStatus'] = returnStatus;
|
||||
|
||||
await _apiService.call('updateEventEquipment', data);
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Erreur lors de la mise à jour des équipements de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un événement
|
||||
Future<void> updateEvent(String eventId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final requestData = {'eventId': eventId, ...data};
|
||||
await _apiService.call('updateEvent', requestData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un événement
|
||||
Future<void> deleteEvent(String eventId) async {
|
||||
try {
|
||||
await _apiService.call('deleteEvent', {'eventId': eventId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les événements utilisant un type d'événement donné
|
||||
Future<List<Map<String, dynamic>>> getEventsByEventType(
|
||||
String eventTypeId) async {
|
||||
try {
|
||||
final result = await _apiService
|
||||
.call('getEventsByEventType', {'eventTypeId': eventTypeId});
|
||||
final events = result['events'] as List<dynamic>?;
|
||||
if (events == null) return [];
|
||||
return events.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les événements (filtrés selon permissions)
|
||||
/// Retourne { events: List<Map>, users: Map<String, Map> }
|
||||
Future<Map<String, dynamic>> getEvents({String? userId}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{};
|
||||
if (userId != null) data['userId'] = userId;
|
||||
|
||||
final result = await _apiService.call('getEvents', data);
|
||||
|
||||
// Extraire events et users
|
||||
final events = result['events'] as List<dynamic>? ?? [];
|
||||
final users = result['users'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return {
|
||||
'events': events.map((e) => e as Map<String, dynamic>).toList(),
|
||||
'users': users,
|
||||
};
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les événements d'un mois spécifique (lazy loading optimisé)
|
||||
Future<Map<String, dynamic>> getEventsByMonth({
|
||||
required String userId,
|
||||
required int year,
|
||||
required int month,
|
||||
}) async {
|
||||
try {
|
||||
print('[EventRepository] Calling getEventsByMonth for $year-$month');
|
||||
final result = await _apiService.call('getEventsByMonth', {
|
||||
'userId': userId,
|
||||
'year': year,
|
||||
'month': month,
|
||||
});
|
||||
|
||||
// Extraire events et users
|
||||
final events = result['events'] as List<dynamic>? ?? [];
|
||||
final users = result['users'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
print(
|
||||
'[EventRepository] Events loaded for $year-$month: ${events.length} events');
|
||||
|
||||
return {
|
||||
'events': events.map((e) => e as Map<String, dynamic>).toList(),
|
||||
'users': users,
|
||||
};
|
||||
} catch (e) {
|
||||
print('[EventRepository] Error getting events by month: $e');
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération des événements du mois: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Recherche des événements accessibles à l'utilisateur.
|
||||
Future<List<Map<String, dynamic>>> searchEvents({
|
||||
required String userId,
|
||||
required String query,
|
||||
int limit = 20,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('searchEvents', {
|
||||
'userId': userId,
|
||||
'query': query,
|
||||
'limit': limit,
|
||||
});
|
||||
|
||||
final events = result['events'] as List<dynamic>?;
|
||||
if (events == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return events.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la recherche d\'événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère un événement avec tous les détails (équipements complets + containers avec enfants)
|
||||
Future<Map<String, dynamic>> getEventWithDetails(String eventId) async {
|
||||
try {
|
||||
print('[EventRepository] Getting event with details: $eventId');
|
||||
final result = await _apiService.call('getEventWithDetails', {
|
||||
'eventId': eventId,
|
||||
});
|
||||
|
||||
final event = result['event'] as Map<String, dynamic>?;
|
||||
final equipments = result['equipments'] as Map<String, dynamic>? ?? {};
|
||||
final containers = result['containers'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
if (event == null) {
|
||||
throw Exception('Event not found');
|
||||
}
|
||||
|
||||
print(
|
||||
'[EventRepository] Event loaded with ${equipments.length} equipments and ${containers.length} containers');
|
||||
|
||||
return {
|
||||
'event': event,
|
||||
'equipments': equipments,
|
||||
'containers': containers,
|
||||
};
|
||||
} catch (e) {
|
||||
print('[EventRepository] Error getting event with details: $e');
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération de l\'événement avec détails: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les options et types d'événements.
|
||||
class OptionRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
OptionRepository(this._apiService);
|
||||
|
||||
/// Récupère toutes les options
|
||||
Future<List<Map<String, dynamic>>> getOptions() async {
|
||||
try {
|
||||
final result = await _apiService.call('getOptions', {});
|
||||
final options = result['options'] as List<dynamic>?;
|
||||
if (options == null) return [];
|
||||
return options.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des options: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les types d'événements
|
||||
Future<List<Map<String, dynamic>>> getEventTypes() async {
|
||||
try {
|
||||
final result = await _apiService.call('getEventTypes', {});
|
||||
final eventTypes = result['eventTypes'] as List<dynamic>?;
|
||||
if (eventTypes == null) return [];
|
||||
return eventTypes.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération des types d\'événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un type d'événement
|
||||
Future<String> createEventType({
|
||||
required String name,
|
||||
required double defaultPrice,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('createEventType', {
|
||||
'name': name,
|
||||
'defaultPrice': defaultPrice,
|
||||
});
|
||||
return result['id'] as String;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un type d'événement
|
||||
Future<void> updateEventType({
|
||||
required String eventTypeId,
|
||||
String? name,
|
||||
double? defaultPrice,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'eventTypeId': eventTypeId};
|
||||
if (name != null) data['name'] = name;
|
||||
if (defaultPrice != null) data['defaultPrice'] = defaultPrice;
|
||||
|
||||
await _apiService.call('updateEventType', data);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un type d'événement
|
||||
Future<void> deleteEventType(String eventTypeId) async {
|
||||
try {
|
||||
await _apiService.call('deleteEventType', {'eventTypeId': eventTypeId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée une option
|
||||
Future<String> createOption(String code, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final requestData = {
|
||||
'id': code,
|
||||
'code': code,
|
||||
...data
|
||||
};
|
||||
final result = await _apiService.call('createOption', requestData);
|
||||
return result['id'] as String? ?? code;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'option: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour une option
|
||||
Future<void> updateOption(String optionId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final requestData = {'optionId': optionId, 'data': data};
|
||||
await _apiService.call('updateOption', requestData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'option: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime une option
|
||||
Future<void> deleteOption(String optionId) async {
|
||||
try {
|
||||
await _apiService.call('deleteOption', {'optionId': optionId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'option: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
/// Repository pour gérer toutes les opérations sur les utilisateurs et les rôles.
|
||||
class UserRepository {
|
||||
final ApiService _apiService;
|
||||
|
||||
UserRepository(this._apiService);
|
||||
|
||||
/// Récupère l'utilisateur actuellement authentifié avec son rôle
|
||||
Future<Map<String, dynamic>> getCurrentUser() async {
|
||||
try {
|
||||
print('[UserRepository] Calling getCurrentUser API...');
|
||||
final result = await _apiService.call('getCurrentUser', {});
|
||||
print('[UserRepository] Current user loaded successfully');
|
||||
return result['user'] as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
print('[UserRepository] Error getting current user: $e');
|
||||
throw Exception(
|
||||
'Erreur lors de la récupération de l\'utilisateur actuel: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les utilisateurs (selon permissions)
|
||||
Future<List<Map<String, dynamic>>> getUsers() async {
|
||||
try {
|
||||
final result = await _apiService.call('getUsers', {});
|
||||
final users = result['users'] as List<dynamic>?;
|
||||
if (users == null) return [];
|
||||
return users.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des utilisateurs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère un utilisateur spécifique
|
||||
Future<Map<String, dynamic>> getUser(String userId) async {
|
||||
try {
|
||||
final result = await _apiService.call('getUser', {'userId': userId});
|
||||
return result['user'] as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un utilisateur (Auth + Firestore)
|
||||
Future<void> deleteUser(String userId) async {
|
||||
try {
|
||||
await _apiService.call('deleteUser', {'userId': userId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un utilisateur
|
||||
Future<void> updateUser(String userId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
await _apiService.call('updateUser', {
|
||||
'userId': userId,
|
||||
'data': data,
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un utilisateur avec invitation par email
|
||||
Future<Map<String, dynamic>> createUserWithInvite({
|
||||
required String email,
|
||||
required String firstName,
|
||||
required String lastName,
|
||||
String? phoneNumber,
|
||||
required String roleId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('createUserWithInvite', {
|
||||
'email': email,
|
||||
'firstName': firstName,
|
||||
'lastName': lastName,
|
||||
'phoneNumber': phoneNumber ?? '',
|
||||
'roleId': roleId,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les rôles
|
||||
Future<List<Map<String, dynamic>>> getRoles() async {
|
||||
try {
|
||||
final result = await _apiService.call('getRoles', {});
|
||||
final roles = result['roles'] as List<dynamic>?;
|
||||
if (roles == null) return [];
|
||||
return roles.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des rôles: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Représente un tour de conversation dans le chat.
|
||||
class AiAssistantChatTurn {
|
||||
final bool isUser;
|
||||
final String text;
|
||||
|
||||
const AiAssistantChatTurn({required this.isUser, required this.text});
|
||||
}
|
||||
|
||||
/// Document à attacher pour demander à l'IA d'analyser un devis, etc.
|
||||
class AiEquipmentDocument {
|
||||
final String base64Data;
|
||||
final String mimeType;
|
||||
final String? fileName;
|
||||
|
||||
const AiEquipmentDocument({
|
||||
required this.base64Data,
|
||||
required this.mimeType,
|
||||
this.fileName,
|
||||
});
|
||||
}
|
||||
|
||||
/// Un item proposé par l'IA dans la liste de matériel.
|
||||
class AiEquipmentProposalItem {
|
||||
final String equipmentId;
|
||||
final int quantity;
|
||||
final String rationale;
|
||||
|
||||
const AiEquipmentProposalItem({
|
||||
required this.equipmentId,
|
||||
required this.quantity,
|
||||
required this.rationale,
|
||||
});
|
||||
}
|
||||
|
||||
/// Métadonnées pour un container proposé par l'IA.
|
||||
class AiEquipmentProposalContainer {
|
||||
final String containerId;
|
||||
final String rationale;
|
||||
final List<String> equipmentIds;
|
||||
final List<String> matchingEquipmentIds;
|
||||
final List<String> missingEquipmentIds;
|
||||
final bool partial;
|
||||
final bool? available;
|
||||
final dynamic availabilityDetail;
|
||||
|
||||
const AiEquipmentProposalContainer({
|
||||
required this.containerId,
|
||||
required this.rationale,
|
||||
this.equipmentIds = const [],
|
||||
this.matchingEquipmentIds = const [],
|
||||
this.missingEquipmentIds = const [],
|
||||
this.partial = false,
|
||||
this.available,
|
||||
this.availabilityDetail,
|
||||
});
|
||||
}
|
||||
|
||||
/// Proposition complète retournée par l'IA.
|
||||
class AiEquipmentProposal {
|
||||
final String summary;
|
||||
final List<AiEquipmentProposalItem> items;
|
||||
|
||||
/// Équipements individuels prêts à être injectés dans l'état local de l'événement.
|
||||
final List<EventEquipment> asEventEquipment;
|
||||
|
||||
/// Containers (métadonnées) proposés par l'IA.
|
||||
final List<AiEquipmentProposalContainer> containers;
|
||||
|
||||
List<String> get containerIds => containers.map((c) => c.containerId).toList();
|
||||
|
||||
const AiEquipmentProposal({
|
||||
required this.summary,
|
||||
required this.items,
|
||||
required this.asEventEquipment,
|
||||
required this.containers,
|
||||
});
|
||||
}
|
||||
|
||||
/// Réponse complète de l'assistant IA (message + proposition optionnelle).
|
||||
class AiEquipmentAssistantResponse {
|
||||
final String assistantMessage;
|
||||
final AiEquipmentProposal? proposal;
|
||||
final List<String> debugLogs;
|
||||
|
||||
const AiEquipmentAssistantResponse({
|
||||
required this.assistantMessage,
|
||||
this.proposal,
|
||||
this.debugLogs = const [],
|
||||
});
|
||||
}
|
||||
|
||||
/// Service assistant IA logisticien.
|
||||
/// Délègue tous les appels Gemini à la Cloud Function [aiEquipmentProposal].
|
||||
/// L'authentification Firebase (token Bearer) suffit — aucune clé API côté client.
|
||||
class AiEquipmentAssistantService {
|
||||
final ApiService _apiService;
|
||||
|
||||
AiEquipmentAssistantService({ApiService? apiService})
|
||||
: _apiService = apiService ?? FirebaseFunctionsApiService();
|
||||
|
||||
/// Envoie un message et retourne la réponse de l'assistant IA.
|
||||
Future<AiEquipmentAssistantResponse> generateProposal({
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
required List<AiAssistantChatTurn> history,
|
||||
required String userMessage,
|
||||
String? eventTypeId,
|
||||
String? excludeEventId,
|
||||
List<EventEquipment> currentAssignedEquipment = const [],
|
||||
List<EventEquipment> workingProposalEquipment = const [],
|
||||
AiEquipmentDocument? document,
|
||||
}) async {
|
||||
final payload = <String, dynamic>{
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
'userMessage': userMessage.trim(),
|
||||
'history': history
|
||||
.where((turn) => turn.text.trim().isNotEmpty)
|
||||
.map((turn) => {'isUser': turn.isUser, 'text': turn.text.trim()})
|
||||
.toList(),
|
||||
'currentEquipment': currentAssignedEquipment
|
||||
.map((eq) => {'equipmentId': eq.equipmentId, 'quantity': eq.quantity})
|
||||
.toList(),
|
||||
'workingProposal': workingProposalEquipment
|
||||
.map((eq) => {'equipmentId': eq.equipmentId, 'quantity': eq.quantity})
|
||||
.toList(),
|
||||
};
|
||||
|
||||
if (eventTypeId != null) payload['eventTypeId'] = eventTypeId;
|
||||
if (excludeEventId != null) payload['excludeEventId'] = excludeEventId;
|
||||
|
||||
if (document != null) {
|
||||
payload['document'] = {
|
||||
'mimeType': document.mimeType,
|
||||
'data': document.base64Data,
|
||||
if (document.fileName != null) 'fileName': document.fileName,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
DebugLog.info('[AiEquipmentAssistantService] Calling aiEquipmentProposal Cloud Function');
|
||||
|
||||
final result = await _apiService.call('aiEquipmentProposal', payload);
|
||||
final assistantMessage = result['assistantMessage']?.toString().trim() ?? '';
|
||||
final proposal = _parseProposal(result['proposal']);
|
||||
|
||||
final rawLogs = result['debugLogs'];
|
||||
final debugLogs = (rawLogs is List) ? rawLogs.map((e) => e.toString()).toList() : <String>[];
|
||||
|
||||
DebugLog.info(
|
||||
'[AiEquipmentAssistantService] Response received, items: ${proposal?.items.length ?? 0}',
|
||||
);
|
||||
|
||||
return AiEquipmentAssistantResponse(
|
||||
assistantMessage: assistantMessage.isNotEmpty
|
||||
? assistantMessage
|
||||
: 'Je n\'ai pas pu générer de réponse.',
|
||||
proposal: proposal,
|
||||
debugLogs: debugLogs,
|
||||
);
|
||||
} on ApiException catch (e) {
|
||||
DebugLog.error('[AiEquipmentAssistantService] API error', e);
|
||||
if (e.isUnauthorized) {
|
||||
throw Exception('Vous n\'êtes pas authentifié. Reconnectez-vous et réessayez.');
|
||||
}
|
||||
throw Exception('Erreur du service IA (${e.statusCode}): ${e.message}');
|
||||
} catch (e) {
|
||||
DebugLog.error('[AiEquipmentAssistantService] Error', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
AiEquipmentProposal? _parseProposal(dynamic rawProposal) {
|
||||
if (rawProposal == null || rawProposal is! Map<String, dynamic>) return null;
|
||||
|
||||
final proposalItems = <AiEquipmentProposalItem>[];
|
||||
final eventEquipmentList = <EventEquipment>[];
|
||||
// legacy containerIds variable removed (we now use containersMeta)
|
||||
|
||||
final rawItems = rawProposal['items'];
|
||||
if (rawItems is List) {
|
||||
for (final rawItem in rawItems) {
|
||||
if (rawItem is! Map) continue;
|
||||
final item = Map<String, dynamic>.from(rawItem);
|
||||
|
||||
final equipmentId = item['equipmentId']?.toString().trim() ?? '';
|
||||
final quantity = int.tryParse(item['quantity']?.toString() ?? '1') ?? 1;
|
||||
|
||||
if (equipmentId.isEmpty || quantity <= 0) continue;
|
||||
|
||||
final rationale = item['rationale']?.toString().trim() ?? 'Proposition IA';
|
||||
|
||||
proposalItems.add(AiEquipmentProposalItem(
|
||||
equipmentId: equipmentId,
|
||||
quantity: quantity,
|
||||
rationale: rationale,
|
||||
));
|
||||
eventEquipmentList.add(EventEquipment(
|
||||
equipmentId: equipmentId,
|
||||
quantity: quantity,
|
||||
rationale: rationale,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
final containersMeta = <AiEquipmentProposalContainer>[];
|
||||
final rawContainers = rawProposal['containers'];
|
||||
if (rawContainers is List) {
|
||||
for (final rawContainer in rawContainers) {
|
||||
if (rawContainer is String) {
|
||||
final cid = rawContainer.toString().trim();
|
||||
if (cid.isNotEmpty) {
|
||||
containersMeta.add(AiEquipmentProposalContainer(containerId: cid, rationale: 'Proposition IA'));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (rawContainer is! Map) continue;
|
||||
final container = Map<String, dynamic>.from(rawContainer);
|
||||
final containerId = container['containerId']?.toString().trim() ?? '';
|
||||
if (containerId.isEmpty) continue;
|
||||
|
||||
final rationale = container['rationale']?.toString().trim() ?? 'Proposition IA';
|
||||
final equipmentIds = <String>[];
|
||||
final matching = <String>[];
|
||||
final missing = <String>[];
|
||||
|
||||
if (container['equipmentIds'] is List) {
|
||||
for (final v in container['equipmentIds']) {
|
||||
final s = v == null ? null : v.toString().trim();
|
||||
if (s != null && s.isNotEmpty) equipmentIds.add(s);
|
||||
}
|
||||
}
|
||||
if (container['matchingEquipmentIds'] is List) {
|
||||
for (final v in container['matchingEquipmentIds']) {
|
||||
final s = v == null ? null : v.toString().trim();
|
||||
if (s != null && s.isNotEmpty) matching.add(s);
|
||||
}
|
||||
}
|
||||
if (container['missingEquipmentIds'] is List) {
|
||||
for (final v in container['missingEquipmentIds']) {
|
||||
final s = v == null ? null : v.toString().trim();
|
||||
if (s != null && s.isNotEmpty) missing.add(s);
|
||||
}
|
||||
}
|
||||
|
||||
final partial = container['partial'] is bool ? container['partial'] as bool : (missing.isNotEmpty);
|
||||
final available = container.containsKey('available') ? (container['available'] is bool ? container['available'] as bool : null) : null;
|
||||
final availabilityDetail = container.containsKey('availabilityDetail') ? container['availabilityDetail'] : null;
|
||||
|
||||
containersMeta.add(AiEquipmentProposalContainer(
|
||||
containerId: containerId,
|
||||
rationale: rationale,
|
||||
equipmentIds: equipmentIds,
|
||||
matchingEquipmentIds: matching,
|
||||
missingEquipmentIds: missing,
|
||||
partial: partial,
|
||||
available: available,
|
||||
availabilityDetail: availabilityDetail,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if (proposalItems.isEmpty && containersMeta.isEmpty) return null;
|
||||
|
||||
return AiEquipmentProposal(
|
||||
summary: rawProposal['summary']?.toString().trim().isNotEmpty == true
|
||||
? rawProposal['summary'].toString().trim()
|
||||
: 'Proposition matériel générée automatiquement.',
|
||||
items: proposalItems,
|
||||
asEventEquipment: eventEquipmentList,
|
||||
containers: containersMeta,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import 'api_service.dart' show FirebaseFunctionsApiService;
|
||||
/// Architecture simplifiée : le client appelle uniquement les Cloud Functions
|
||||
/// Toute la logique métier est gérée côté backend
|
||||
class AlertService {
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
FirebaseFirestore get _firestore => FirebaseFirestore.instance;
|
||||
FirebaseAuth get _auth => FirebaseAuth.instance;
|
||||
|
||||
/// Stream des alertes pour l'utilisateur connecté
|
||||
Stream<List<AlertModel>> getAlertsStream() {
|
||||
|
||||
@@ -173,6 +173,8 @@ class FirebaseFunctionsApiService implements ApiService {
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
DebugLog.error('[API] Error during request: $functionName', e);
|
||||
throw ApiException(
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../firebase_options.dart';
|
||||
import '../config/api_config.dart';
|
||||
import 'cache_service.dart';
|
||||
|
||||
/// Service responsable des initialisations lourdes en tâche de fond.
|
||||
///
|
||||
/// Objectif : réduire au maximum le travail synchrone dans main(),
|
||||
/// afficher immédiatement une UI minimale, puis effectuer l'init asynchrone.
|
||||
class AppInitializer with ChangeNotifier {
|
||||
bool _isInitialized = false;
|
||||
bool _isInitializing = false;
|
||||
|
||||
bool get isInitialized => _isInitialized;
|
||||
bool get isInitializing => _isInitializing;
|
||||
|
||||
final CacheService cacheService = CacheService();
|
||||
|
||||
/// Démarre l'initialisation asynchrone. Idempotent.
|
||||
Future<void> initialize() async {
|
||||
if (_isInitialized || _isInitializing) return;
|
||||
_isInitializing = true;
|
||||
scheduleMicrotask(() => notifyListeners());
|
||||
|
||||
try {
|
||||
// Initialiser Firebase
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
|
||||
// Configurer les émulateurs en dev si demandé
|
||||
if (ApiConfig.isDevelopment) {
|
||||
try {
|
||||
await FirebaseAuth.instance.useAuthEmulator('localhost', 9199);
|
||||
FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8088);
|
||||
} catch (e) {
|
||||
// Ignorer si non supporté
|
||||
if (kDebugMode) print('Emulator setup failed: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Initialiser le cache local sans bloquer l'écran de démarrage.
|
||||
unawaited(cacheService.init());
|
||||
|
||||
// Précharger des assets critiques de façon asynchrone
|
||||
unawaited(_preloadAssets());
|
||||
|
||||
// TODO: lancer ici d'autres initialisations non bloquantes
|
||||
|
||||
_isInitialized = true;
|
||||
_isInitializing = false;
|
||||
notifyListeners();
|
||||
} catch (e, st) {
|
||||
if (kDebugMode) print('AppInitializer failed: $e\n$st');
|
||||
_isInitializing = false;
|
||||
// Ne rethrow pas pour éviter de planter l'app; laisser l'UI gérer les erreurs.
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _preloadAssets() async {
|
||||
try {
|
||||
// Charger quelques assets en mémoire pour rendre l'affichage initial fluide
|
||||
await rootBundle.load('assets/logos/RectangleLogoBlack.png');
|
||||
await rootBundle.load('assets/logos/SquareLogoWhite.png');
|
||||
} catch (e) {
|
||||
if (kDebugMode) print('Preload assets failed: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Service simple de cache local basé sur SharedPreferences.
|
||||
///
|
||||
/// Fonctionne sur mobile et sur Flutter Web pour conserver des données
|
||||
/// locales légères quand cela apporte une vraie valeur.
|
||||
class CacheService {
|
||||
SharedPreferences? _prefs;
|
||||
|
||||
Future<void> init() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
bool ready() => _prefs != null;
|
||||
|
||||
Future<void> setJson(String key, Map<String, dynamic> value) async {
|
||||
if (_prefs == null) return;
|
||||
await _prefs!.setString(key, jsonEncode(value));
|
||||
}
|
||||
|
||||
Map<String, dynamic>? getJson(String key) {
|
||||
if (_prefs == null) return null;
|
||||
final s = _prefs!.getString(key);
|
||||
if (s == null) return null;
|
||||
try {
|
||||
return jsonDecode(s) as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
if (kDebugMode) print('CacheService getJson error: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setString(String key, String value) async {
|
||||
if (_prefs == null) return;
|
||||
await _prefs!.setString(key, value);
|
||||
}
|
||||
|
||||
String? getString(String key) => _prefs?.getString(key);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:em2rp/config/api_config.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
/// Service de Text-to-Speech utilisant Google Cloud TTS via Cloud Function
|
||||
/// Avec système de cache pour optimiser les performances et réduire les coûts
|
||||
class CloudTextToSpeechService {
|
||||
static final Map<String, String> _audioCache = {};
|
||||
static final Map<String, web.HTMLAudioElement> _audioPlayers = {};
|
||||
|
||||
/// Générer l'audio TTS via Cloud Function
|
||||
/// Retourne l'URL de l'audio (mise en cache automatiquement côté serveur)
|
||||
static Future<String?> generateAudio(String text) async {
|
||||
try {
|
||||
// Vérifier le cache local d'abord
|
||||
if (_audioCache.containsKey(text)) {
|
||||
DebugLog.info('[CloudTTS] ✓ Local cache HIT: "${text.substring(0, 30)}..."');
|
||||
return _audioCache[text];
|
||||
}
|
||||
|
||||
DebugLog.info('[CloudTTS] Generating audio for: "$text"');
|
||||
|
||||
// Récupérer le token d'authentification
|
||||
final user = FirebaseAuth.instance.currentUser;
|
||||
if (user == null) {
|
||||
DebugLog.error('[CloudTTS] User not authenticated');
|
||||
return null;
|
||||
}
|
||||
|
||||
final token = await user.getIdToken();
|
||||
if (token == null) {
|
||||
DebugLog.error('[CloudTTS] Failed to get auth token');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Préparer la requête
|
||||
final url = '${ApiConfig.baseUrl}/generateTTSV2';
|
||||
final headers = {
|
||||
'Authorization': 'Bearer $token',
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
final body = json.encode({
|
||||
'data': {
|
||||
'text': text,
|
||||
'voiceConfig': {
|
||||
'languageCode': 'fr-FR',
|
||||
'name': 'fr-FR-Standard-B', // Voix masculine gratuite
|
||||
'ssmlGender': 'MALE',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
DebugLog.info('[CloudTTS] Calling Cloud Function...');
|
||||
final startTime = DateTime.now();
|
||||
|
||||
// Appeler la Cloud Function
|
||||
final response = await http.post(
|
||||
Uri.parse(url),
|
||||
headers: headers,
|
||||
body: body,
|
||||
);
|
||||
|
||||
final duration = DateTime.now().difference(startTime).inMilliseconds;
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
final audioUrl = data['audioUrl'] as String?;
|
||||
final cached = data['cached'] as bool? ?? false;
|
||||
|
||||
if (audioUrl != null) {
|
||||
// Mettre en cache localement
|
||||
_audioCache[text] = audioUrl;
|
||||
|
||||
DebugLog.info('[CloudTTS] ✓ Audio generated - cached: $cached, duration: ${duration}ms');
|
||||
|
||||
return audioUrl;
|
||||
}
|
||||
}
|
||||
|
||||
DebugLog.error('[CloudTTS] Failed to generate audio', {
|
||||
'status': response.statusCode,
|
||||
'body': response.body,
|
||||
});
|
||||
|
||||
return null;
|
||||
} catch (e) {
|
||||
DebugLog.error('[CloudTTS] Exception:', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Lire un audio depuis une URL
|
||||
static void playAudio(String audioUrl) {
|
||||
try {
|
||||
DebugLog.info('[CloudTTS] Playing audio...');
|
||||
|
||||
// Créer ou réutiliser un HTMLAudioElement
|
||||
final player = _audioPlayers[audioUrl] ?? web.HTMLAudioElement();
|
||||
if (!_audioPlayers.containsKey(audioUrl)) {
|
||||
player.src = audioUrl;
|
||||
_audioPlayers[audioUrl] = player;
|
||||
}
|
||||
|
||||
// Configurer le volume
|
||||
player.volume = 1.0;
|
||||
|
||||
// Écouter les événements
|
||||
player.onEnded.listen((_) {
|
||||
DebugLog.info('[CloudTTS] ✓ Playback finished');
|
||||
});
|
||||
|
||||
player.onError.listen((event) {
|
||||
DebugLog.error('[CloudTTS] ✗ Playback error:', event);
|
||||
});
|
||||
|
||||
// Lire l'audio (pas de await avec package:web)
|
||||
player.play();
|
||||
|
||||
DebugLog.info('[CloudTTS] ✓ Playback started');
|
||||
} catch (e) {
|
||||
DebugLog.error('[CloudTTS] Error playing audio:', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Générer et lire l'audio en une seule opération
|
||||
static Future<void> speak(String text) async {
|
||||
try {
|
||||
final audioUrl = await generateAudio(text);
|
||||
|
||||
if (audioUrl != null) {
|
||||
playAudio(audioUrl);
|
||||
} else {
|
||||
DebugLog.error('[CloudTTS] Failed to generate audio for speech');
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLog.error('[CloudTTS] Error in speak:', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Arrêter tous les audios en cours
|
||||
static void stopAll() {
|
||||
for (final player in _audioPlayers.values) {
|
||||
try {
|
||||
player.pause();
|
||||
player.currentTime = 0;
|
||||
} catch (e) {
|
||||
// Ignorer les erreurs de pause
|
||||
}
|
||||
}
|
||||
DebugLog.info('[CloudTTS] All players stopped');
|
||||
}
|
||||
|
||||
/// Nettoyer le cache
|
||||
static void clearCache() {
|
||||
_audioCache.clear();
|
||||
_audioPlayers.clear();
|
||||
DebugLog.info('[CloudTTS] Cache cleared');
|
||||
}
|
||||
|
||||
/// Pré-charger des audios fréquemment utilisés
|
||||
static Future<void> preloadCommonPhrases() async {
|
||||
final phrases = [
|
||||
'Équipement scanné',
|
||||
'Flight case',
|
||||
'Conteneur',
|
||||
'Validé',
|
||||
'Erreur',
|
||||
];
|
||||
|
||||
DebugLog.info('[CloudTTS] Preloading ${phrases.length} common phrases...');
|
||||
|
||||
for (final phrase in phrases) {
|
||||
try {
|
||||
await generateAudio(phrase);
|
||||
} catch (e) {
|
||||
DebugLog.warning('[CloudTTS] Failed to preload: $phrase - $e');
|
||||
}
|
||||
}
|
||||
|
||||
DebugLog.info('[CloudTTS] ✓ Preload complete');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +141,6 @@ class ContainerService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifier la disponibilité d'un container et de son contenu pour un événement
|
||||
Future<Map<String, dynamic>> checkContainerAvailability({
|
||||
required String containerId,
|
||||
required DateTime startDate,
|
||||
@@ -149,43 +148,21 @@ class ContainerService {
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
try {
|
||||
final container = await getContainerById(containerId);
|
||||
if (container == null) {
|
||||
return {'available': false, 'message': 'Container non trouvé'};
|
||||
}
|
||||
|
||||
// Vérifier le statut du container
|
||||
if (container.status != EquipmentStatus.available) {
|
||||
return {
|
||||
'available': false,
|
||||
'message': 'Container ${container.name} n\'est pas disponible (statut: ${container.status})',
|
||||
};
|
||||
}
|
||||
|
||||
// Vérifier la disponibilité de chaque équipement dans le container
|
||||
List<String> unavailableEquipment = [];
|
||||
|
||||
if (container.equipmentIds.isNotEmpty) {
|
||||
final equipmentsData = await _dataService.getEquipmentsByIds(container.equipmentIds);
|
||||
|
||||
for (var data in equipmentsData) {
|
||||
final id = data['id'] as String;
|
||||
final equipment = EquipmentModel.fromMap(data, id);
|
||||
if (equipment.status != EquipmentStatus.available) {
|
||||
unavailableEquipment.add('${equipment.name} (${equipment.status})');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unavailableEquipment.isNotEmpty) {
|
||||
return {
|
||||
'available': false,
|
||||
'message': 'Certains équipements ne sont pas disponibles',
|
||||
'unavailableItems': unavailableEquipment,
|
||||
};
|
||||
}
|
||||
|
||||
return {'available': true, 'message': 'Container et tout son contenu disponibles'};
|
||||
final result = await _dataService.checkContainerAvailability(
|
||||
containerId: containerId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
return {
|
||||
'available': result['isAvailable'] ?? false,
|
||||
'message': result['isAvailable'] == true
|
||||
? 'Container et tout son contenu disponibles'
|
||||
: 'Container non disponible ou en conflit',
|
||||
'conflictType': result['conflictType'],
|
||||
'containerConflicts': result['containerConflicts'],
|
||||
'equipmentConflicts': result['equipmentConflicts'],
|
||||
};
|
||||
} catch (e) {
|
||||
print('Error checking container availability: $e');
|
||||
return {'available': false, 'message': 'Erreur: $e'};
|
||||
|
||||
@@ -1,49 +1,55 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
import 'package:em2rp/repositories/event_repository.dart';
|
||||
import 'package:em2rp/repositories/equipment_repository.dart';
|
||||
import 'package:em2rp/repositories/container_repository.dart';
|
||||
import 'package:em2rp/repositories/alert_repository.dart';
|
||||
import 'package:em2rp/repositories/user_repository.dart';
|
||||
import 'package:em2rp/repositories/option_repository.dart';
|
||||
|
||||
/// Service générique pour les opérations de lecture de données via Cloud Functions
|
||||
/// Service façade pour rétrocompatibilité.
|
||||
/// Délègue les opérations aux Repositories de domaine respectifs.
|
||||
class DataService {
|
||||
final ApiService _apiService;
|
||||
final EventRepository eventRepository;
|
||||
final EquipmentRepository equipmentRepository;
|
||||
final ContainerRepository containerRepository;
|
||||
final AlertRepository alertRepository;
|
||||
final UserRepository userRepository;
|
||||
final OptionRepository optionRepository;
|
||||
|
||||
DataService(this._apiService);
|
||||
DataService(ApiService apiService)
|
||||
: eventRepository = EventRepository(apiService),
|
||||
equipmentRepository = EquipmentRepository(apiService),
|
||||
containerRepository = ContainerRepository(apiService),
|
||||
alertRepository = AlertRepository(apiService),
|
||||
userRepository = UserRepository(apiService),
|
||||
optionRepository = OptionRepository(apiService);
|
||||
|
||||
/// Récupère toutes les options
|
||||
Future<List<Map<String, dynamic>>> getOptions() async {
|
||||
try {
|
||||
final result = await _apiService.call('getOptions', {});
|
||||
final options = result['options'] as List<dynamic>?;
|
||||
if (options == null) return [];
|
||||
return options.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des options: $e');
|
||||
}
|
||||
}
|
||||
// ============================================================================
|
||||
// OPTIONS & METADATA (delegated to OptionRepository)
|
||||
// ============================================================================
|
||||
Future<List<Map<String, dynamic>>> getOptions() => optionRepository.getOptions();
|
||||
|
||||
/// Récupère tous les types d'événements
|
||||
Future<List<Map<String, dynamic>>> getEventTypes() async {
|
||||
try {
|
||||
final result = await _apiService.call('getEventTypes', {});
|
||||
final eventTypes = result['eventTypes'] as List<dynamic>?;
|
||||
if (eventTypes == null) return [];
|
||||
return eventTypes.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des types d\'événements: $e');
|
||||
}
|
||||
}
|
||||
Future<List<Map<String, dynamic>>> getEventTypes() => optionRepository.getEventTypes();
|
||||
|
||||
/// Récupère tous les rôles
|
||||
Future<List<Map<String, dynamic>>> getRoles() async {
|
||||
try {
|
||||
final result = await _apiService.call('getRoles', {});
|
||||
final roles = result['roles'] as List<dynamic>?;
|
||||
if (roles == null) return [];
|
||||
return roles.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des rôles: $e');
|
||||
}
|
||||
}
|
||||
Future<String> createEventType({required String name, required double defaultPrice}) =>
|
||||
optionRepository.createEventType(name: name, defaultPrice: defaultPrice);
|
||||
|
||||
/// Met à jour les équipements d'un événement
|
||||
Future<void> updateEventType({required String eventTypeId, String? name, double? defaultPrice}) =>
|
||||
optionRepository.updateEventType(eventTypeId: eventTypeId, name: name, defaultPrice: defaultPrice);
|
||||
|
||||
Future<void> deleteEventType(String eventTypeId) => optionRepository.deleteEventType(eventTypeId);
|
||||
|
||||
Future<String> createOption(String code, Map<String, dynamic> data) =>
|
||||
optionRepository.createOption(code, data);
|
||||
|
||||
Future<void> updateOption(String optionId, Map<String, dynamic> data) =>
|
||||
optionRepository.updateOption(optionId, data);
|
||||
|
||||
Future<void> deleteOption(String optionId) => optionRepository.deleteOption(optionId);
|
||||
|
||||
// ============================================================================
|
||||
// EVENTS (delegated to EventRepository)
|
||||
// ============================================================================
|
||||
Future<void> updateEventEquipment({
|
||||
required String eventId,
|
||||
List<Map<String, dynamic>>? assignedEquipment,
|
||||
@@ -51,347 +57,45 @@ class DataService {
|
||||
String? loadingStatus,
|
||||
String? unloadingStatus,
|
||||
String? returnStatus,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'eventId': eventId};
|
||||
}) =>
|
||||
eventRepository.updateEventEquipment(
|
||||
eventId: eventId,
|
||||
assignedEquipment: assignedEquipment,
|
||||
preparationStatus: preparationStatus,
|
||||
loadingStatus: loadingStatus,
|
||||
unloadingStatus: unloadingStatus,
|
||||
returnStatus: returnStatus,
|
||||
);
|
||||
|
||||
if (assignedEquipment != null) data['assignedEquipment'] = assignedEquipment;
|
||||
if (preparationStatus != null) data['preparationStatus'] = preparationStatus;
|
||||
if (loadingStatus != null) data['loadingStatus'] = loadingStatus;
|
||||
if (unloadingStatus != null) data['unloadingStatus'] = unloadingStatus;
|
||||
if (returnStatus != null) data['returnStatus'] = returnStatus;
|
||||
Future<void> updateEvent(String eventId, Map<String, dynamic> data) =>
|
||||
eventRepository.updateEvent(eventId, data);
|
||||
|
||||
await _apiService.call('updateEventEquipment', data);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour des équipements de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
Future<void> deleteEvent(String eventId) => eventRepository.deleteEvent(eventId);
|
||||
|
||||
/// Met à jour uniquement le statut d'un équipement
|
||||
Future<void> updateEquipmentStatusOnly({
|
||||
required String equipmentId,
|
||||
String? status,
|
||||
int? availableQuantity,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'equipmentId': equipmentId};
|
||||
Future<List<Map<String, dynamic>>> getEventsByEventType(String eventTypeId) =>
|
||||
eventRepository.getEventsByEventType(eventTypeId);
|
||||
|
||||
if (status != null) data['status'] = status;
|
||||
if (availableQuantity != null) data['availableQuantity'] = availableQuantity;
|
||||
Future<Map<String, dynamic>> getEvents({String? userId}) =>
|
||||
eventRepository.getEvents(userId: userId);
|
||||
|
||||
await _apiService.call('updateEquipmentStatusOnly', data);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour du statut de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
Future<Map<String, dynamic>> getEventsByMonth({required String userId, required int year, required int month}) =>
|
||||
eventRepository.getEventsByMonth(userId: userId, year: year, month: month);
|
||||
|
||||
/// Met à jour un événement
|
||||
Future<void> updateEvent(String eventId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
// Correction : fusionner eventId et les champs de data à la racine
|
||||
final requestData = {'eventId': eventId, ...data};
|
||||
await _apiService.call('updateEvent', requestData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
Future<List<Map<String, dynamic>>> searchEvents({required String userId, required String query, int limit = 20}) =>
|
||||
eventRepository.searchEvents(userId: userId, query: query, limit: limit);
|
||||
|
||||
/// Supprime un événement
|
||||
Future<void> deleteEvent(String eventId) async {
|
||||
try {
|
||||
await _apiService.call('deleteEvent', {'eventId': eventId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un équipement
|
||||
Future<void> createEquipment(String equipmentId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
// S'assurer que l'ID est dans les données
|
||||
final equipmentData = Map<String, dynamic>.from(data);
|
||||
equipmentData['id'] = equipmentId;
|
||||
|
||||
await _apiService.call('createEquipment', equipmentData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un équipement
|
||||
Future<void> updateEquipment(String equipmentId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
await _apiService.call('updateEquipment', {
|
||||
'equipmentId': equipmentId,
|
||||
'data': data,
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un équipement
|
||||
Future<void> deleteEquipment(String equipmentId) async {
|
||||
try {
|
||||
await _apiService.call('deleteEquipment', {'equipmentId': equipmentId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'équipement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les événements utilisant un type d'événement donné
|
||||
Future<List<Map<String, dynamic>>> getEventsByEventType(String eventTypeId) async {
|
||||
try {
|
||||
final result = await _apiService.call('getEventsByEventType', {'eventTypeId': eventTypeId});
|
||||
final events = result['events'] as List<dynamic>?;
|
||||
if (events == null) return [];
|
||||
return events.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un type d'événement
|
||||
Future<String> createEventType({
|
||||
required String name,
|
||||
required double defaultPrice,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('createEventType', {
|
||||
'name': name,
|
||||
'defaultPrice': defaultPrice,
|
||||
});
|
||||
return result['id'] as String;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un type d'événement
|
||||
Future<void> updateEventType({
|
||||
required String eventTypeId,
|
||||
String? name,
|
||||
double? defaultPrice,
|
||||
}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{'eventTypeId': eventTypeId};
|
||||
if (name != null) data['name'] = name;
|
||||
if (defaultPrice != null) data['defaultPrice'] = defaultPrice;
|
||||
|
||||
await _apiService.call('updateEventType', data);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un type d'événement
|
||||
Future<void> deleteEventType(String eventTypeId) async {
|
||||
try {
|
||||
await _apiService.call('deleteEventType', {'eventTypeId': eventTypeId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression du type d\'événement: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée une option
|
||||
Future<String> createOption(String code, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final requestData = {
|
||||
'id': code, // Ajouter l'ID en utilisant le code comme identifiant
|
||||
'code': code,
|
||||
...data
|
||||
};
|
||||
final result = await _apiService.call('createOption', requestData);
|
||||
return result['id'] as String? ?? code;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'option: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour une option
|
||||
Future<void> updateOption(String optionId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
final requestData = {'optionId': optionId, ...data};
|
||||
await _apiService.call('updateOption', requestData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'option: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime une option
|
||||
Future<void> deleteOption(String optionId) async {
|
||||
try {
|
||||
await _apiService.call('deleteOption', {'optionId': optionId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'option: $e');
|
||||
}
|
||||
}
|
||||
Future<Map<String, dynamic>> getEventWithDetails(String eventId) =>
|
||||
eventRepository.getEventWithDetails(eventId);
|
||||
|
||||
// ============================================================================
|
||||
// LECTURE DES DONNÉES (avec permissions côté serveur)
|
||||
// EQUIPMENTS & AVAILABILITY (delegated to EquipmentRepository)
|
||||
// ============================================================================
|
||||
Future<List<Map<String, dynamic>>> getEquipments() =>
|
||||
equipmentRepository.getEquipments();
|
||||
|
||||
/// Récupère tous les événements (filtrés selon permissions)
|
||||
/// Retourne { events: List<Map>, users: Map<String, Map> }
|
||||
Future<Map<String, dynamic>> getEvents({String? userId}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{};
|
||||
if (userId != null) data['userId'] = userId;
|
||||
Future<List<Map<String, dynamic>>> getEquipmentsByIds(List<String> equipmentIds) =>
|
||||
equipmentRepository.getEquipmentsByIds(equipmentIds);
|
||||
|
||||
final result = await _apiService.call('getEvents', data);
|
||||
|
||||
// Extraire events et users
|
||||
final events = result['events'] as List<dynamic>? ?? [];
|
||||
final users = result['users'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return {
|
||||
'events': events.map((e) => e as Map<String, dynamic>).toList(),
|
||||
'users': users,
|
||||
};
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des événements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les événements d'un mois spécifique (lazy loading optimisé)
|
||||
Future<Map<String, dynamic>> getEventsByMonth({
|
||||
required String userId,
|
||||
required int year,
|
||||
required int month,
|
||||
}) async {
|
||||
try {
|
||||
print('[DataService] Calling getEventsByMonth for $year-$month');
|
||||
final result = await _apiService.call('getEventsByMonth', {
|
||||
'userId': userId,
|
||||
'year': year,
|
||||
'month': month,
|
||||
});
|
||||
|
||||
// Extraire events et users
|
||||
final events = result['events'] as List<dynamic>? ?? [];
|
||||
final users = result['users'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
print('[DataService] Events loaded for $year-$month: ${events.length} events');
|
||||
|
||||
return {
|
||||
'events': events.map((e) => e as Map<String, dynamic>).toList(),
|
||||
'users': users,
|
||||
};
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting events by month: $e');
|
||||
throw Exception('Erreur lors de la récupération des événements du mois: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère un événement avec tous les détails (équipements complets + containers avec enfants)
|
||||
Future<Map<String, dynamic>> getEventWithDetails(String eventId) async {
|
||||
try {
|
||||
print('[DataService] Getting event with details: $eventId');
|
||||
final result = await _apiService.call('getEventWithDetails', {
|
||||
'eventId': eventId,
|
||||
});
|
||||
|
||||
final event = result['event'] as Map<String, dynamic>?;
|
||||
final equipments = result['equipments'] as Map<String, dynamic>? ?? {};
|
||||
final containers = result['containers'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
if (event == null) {
|
||||
throw Exception('Event not found');
|
||||
}
|
||||
|
||||
print('[DataService] Event loaded with ${equipments.length} equipments and ${containers.length} containers');
|
||||
|
||||
return {
|
||||
'event': event,
|
||||
'equipments': equipments,
|
||||
'containers': containers,
|
||||
};
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting event with details: $e');
|
||||
throw Exception('Erreur lors de la récupération de l\'événement avec détails: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les équipements (avec masquage des prix selon permissions)
|
||||
Future<List<Map<String, dynamic>>> getEquipments() async {
|
||||
try {
|
||||
print('[DataService] Calling getEquipments API...');
|
||||
final result = await _apiService.call('getEquipments', {});
|
||||
print('[DataService] API call successful, parsing result...');
|
||||
final equipments = result['equipments'] as List<dynamic>?;
|
||||
if (equipments == null) {
|
||||
print('[DataService] No equipments in result');
|
||||
return [];
|
||||
}
|
||||
print('[DataService] Found ${equipments.length} equipments');
|
||||
return equipments.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting equipments: $e');
|
||||
throw Exception('Erreur lors de la récupération des équipements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère plusieurs équipements par leurs IDs
|
||||
Future<List<Map<String, dynamic>>> getEquipmentsByIds(List<String> equipmentIds) async {
|
||||
try {
|
||||
if (equipmentIds.isEmpty) return [];
|
||||
|
||||
print('[DataService] Getting equipments by IDs: ${equipmentIds.length} items');
|
||||
final result = await _apiService.call('getEquipmentsByIds', {
|
||||
'equipmentIds': equipmentIds,
|
||||
});
|
||||
final equipments = result['equipments'] as List<dynamic>?;
|
||||
if (equipments == null) {
|
||||
print('[DataService] No equipments in result');
|
||||
return [];
|
||||
}
|
||||
print('[DataService] Found ${equipments.length} equipments by IDs');
|
||||
return equipments.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting equipments by IDs: $e');
|
||||
throw Exception('Erreur lors de la récupération des équipements: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les conteneurs
|
||||
Future<List<Map<String, dynamic>>> getContainers() async {
|
||||
try {
|
||||
final result = await _apiService.call('getContainers', {});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) return [];
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des conteneurs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère plusieurs containers par leurs IDs
|
||||
Future<List<Map<String, dynamic>>> getContainersByIds(List<String> containerIds) async {
|
||||
try {
|
||||
if (containerIds.isEmpty) return [];
|
||||
|
||||
print('[DataService] Getting containers by IDs: ${containerIds.length} items');
|
||||
final result = await _apiService.call('getContainersByIds', {
|
||||
'containerIds': containerIds,
|
||||
});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) {
|
||||
print('[DataService] No containers in result');
|
||||
return [];
|
||||
}
|
||||
print('[DataService] Found ${containers.length} containers by IDs');
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting containers by IDs: $e');
|
||||
throw Exception('Erreur lors de la récupération des containers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EQUIPMENTS & CONTAINERS - Pagination
|
||||
// ============================================================================
|
||||
|
||||
/// Récupère les équipements avec pagination et filtrage
|
||||
Future<Map<String, dynamic>> getEquipmentsPaginated({
|
||||
int limit = 20,
|
||||
String? startAfter,
|
||||
@@ -400,41 +104,92 @@ class DataService {
|
||||
String? searchQuery,
|
||||
String sortBy = 'id',
|
||||
String sortOrder = 'asc',
|
||||
}) async {
|
||||
try {
|
||||
final params = <String, dynamic>{
|
||||
'limit': limit,
|
||||
'sortBy': sortBy,
|
||||
'sortOrder': sortOrder,
|
||||
};
|
||||
|
||||
if (startAfter != null) params['startAfter'] = startAfter;
|
||||
if (category != null) params['category'] = category;
|
||||
if (status != null) params['status'] = status;
|
||||
if (searchQuery != null && searchQuery.isNotEmpty) {
|
||||
params['searchQuery'] = searchQuery;
|
||||
}
|
||||
|
||||
final result = await (_apiService as FirebaseFunctionsApiService).callPaginated(
|
||||
'getEquipmentsPaginated',
|
||||
params,
|
||||
}) =>
|
||||
equipmentRepository.getEquipmentsPaginated(
|
||||
limit: limit,
|
||||
startAfter: startAfter,
|
||||
category: category,
|
||||
status: status,
|
||||
searchQuery: searchQuery,
|
||||
sortBy: sortBy,
|
||||
sortOrder: sortOrder,
|
||||
);
|
||||
|
||||
return {
|
||||
'equipments': (result['equipments'] as List<dynamic>?)
|
||||
?.map((e) => e as Map<String, dynamic>)
|
||||
.toList() ?? [],
|
||||
'hasMore': result['hasMore'] as bool? ?? false,
|
||||
'lastVisible': result['lastVisible'] as String?,
|
||||
'total': result['total'] as int? ?? 0,
|
||||
};
|
||||
} catch (e) {
|
||||
DebugLog.error('[DataService] Error in getEquipmentsPaginated', e);
|
||||
throw Exception('Erreur lors de la récupération paginée des équipements: $e');
|
||||
}
|
||||
}
|
||||
Future<void> createEquipment(String equipmentId, Map<String, dynamic> data) =>
|
||||
equipmentRepository.createEquipment(equipmentId, data);
|
||||
|
||||
Future<void> updateEquipment(String equipmentId, Map<String, dynamic> data) =>
|
||||
equipmentRepository.updateEquipment(equipmentId, data);
|
||||
|
||||
Future<void> deleteEquipment(String equipmentId, {bool forceDelete = false}) =>
|
||||
equipmentRepository.deleteEquipment(equipmentId, forceDelete: forceDelete);
|
||||
|
||||
Future<void> updateEquipmentStatusOnly({required String equipmentId, String? status, int? availableQuantity}) =>
|
||||
equipmentRepository.updateEquipmentStatusOnly(
|
||||
equipmentId: equipmentId,
|
||||
status: status,
|
||||
availableQuantity: availableQuantity,
|
||||
);
|
||||
|
||||
Future<List<Map<String, dynamic>>> searchEquipmentsForAssistant({required String query, int limit = 12}) =>
|
||||
equipmentRepository.searchEquipmentsForAssistant(query: query, limit: limit);
|
||||
|
||||
Future<Map<String, dynamic>> checkEquipmentAvailabilityForAssistant({
|
||||
required String equipmentId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) =>
|
||||
equipmentRepository.checkEquipmentAvailabilityForAssistant(
|
||||
equipmentId: equipmentId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
Future<Map<String, dynamic>> checkEquipmentAvailability({
|
||||
required String equipmentId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) =>
|
||||
equipmentRepository.checkEquipmentAvailability(
|
||||
equipmentId: equipmentId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
Future<Map<String, dynamic>> getConflictingEquipmentIds({
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
int installationTime = 0,
|
||||
int disassemblyTime = 0,
|
||||
}) =>
|
||||
equipmentRepository.getConflictingEquipmentIds(
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
installationTime: installationTime,
|
||||
disassemblyTime: disassemblyTime,
|
||||
);
|
||||
|
||||
Future<List<Map<String, dynamic>>> getMaintenances({String? equipmentId}) =>
|
||||
equipmentRepository.getMaintenances(equipmentId: equipmentId);
|
||||
|
||||
Future<void> deleteMaintenance(String maintenanceId) =>
|
||||
equipmentRepository.deleteMaintenance(maintenanceId);
|
||||
|
||||
// ============================================================================
|
||||
// CONTAINERS (delegated to ContainerRepository)
|
||||
// ============================================================================
|
||||
Future<List<Map<String, dynamic>>> getContainers() =>
|
||||
containerRepository.getContainers();
|
||||
|
||||
Future<List<Map<String, dynamic>>> getContainersByIds(List<String> containerIds) =>
|
||||
containerRepository.getContainersByIds(containerIds);
|
||||
|
||||
/// Récupère les containers avec pagination et filtrage
|
||||
Future<Map<String, dynamic>> getContainersPaginated({
|
||||
int limit = 20,
|
||||
String? startAfter,
|
||||
@@ -444,267 +199,71 @@ class DataService {
|
||||
String? category,
|
||||
String sortBy = 'id',
|
||||
String sortOrder = 'asc',
|
||||
}) async {
|
||||
try {
|
||||
final params = <String, dynamic>{
|
||||
'limit': limit,
|
||||
'sortBy': sortBy,
|
||||
'sortOrder': sortOrder,
|
||||
};
|
||||
|
||||
if (startAfter != null) params['startAfter'] = startAfter;
|
||||
if (type != null) params['type'] = type;
|
||||
if (status != null) params['status'] = status;
|
||||
if (category != null) params['category'] = category;
|
||||
if (searchQuery != null && searchQuery.isNotEmpty) {
|
||||
params['searchQuery'] = searchQuery;
|
||||
}
|
||||
|
||||
final result = await (_apiService as FirebaseFunctionsApiService).callPaginated(
|
||||
'getContainersPaginated',
|
||||
params,
|
||||
);
|
||||
|
||||
return {
|
||||
'containers': (result['containers'] as List<dynamic>?)
|
||||
?.map((e) => e as Map<String, dynamic>)
|
||||
.toList() ?? [],
|
||||
'hasMore': result['hasMore'] as bool? ?? false,
|
||||
'lastVisible': result['lastVisible'] as String?,
|
||||
'total': result['total'] as int? ?? 0,
|
||||
};
|
||||
} catch (e) {
|
||||
DebugLog.error('[DataService] Error in getContainersPaginated', e);
|
||||
throw Exception('Erreur lors de la récupération paginée des containers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Recherche rapide (autocomplétion)
|
||||
Future<List<Map<String, dynamic>>> quickSearch(
|
||||
String query, {
|
||||
int limit = 10,
|
||||
bool includeEquipments = true,
|
||||
bool includeContainers = true,
|
||||
}) async {
|
||||
try {
|
||||
return await (_apiService as FirebaseFunctionsApiService).quickSearch(
|
||||
query,
|
||||
}) =>
|
||||
containerRepository.getContainersPaginated(
|
||||
limit: limit,
|
||||
includeEquipments: includeEquipments,
|
||||
includeContainers: includeContainers,
|
||||
startAfter: startAfter,
|
||||
type: type,
|
||||
status: status,
|
||||
searchQuery: searchQuery,
|
||||
category: category,
|
||||
sortBy: sortBy,
|
||||
sortOrder: sortOrder,
|
||||
);
|
||||
} catch (e) {
|
||||
DebugLog.error('[DataService] Error in quickSearch', e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// USER - Current User
|
||||
// ============================================================================
|
||||
Future<List<Map<String, dynamic>>> getContainersByEquipment(String equipmentId) =>
|
||||
containerRepository.getContainersByEquipment(equipmentId);
|
||||
|
||||
/// Récupère l'utilisateur actuellement authentifié avec son rôle
|
||||
Future<Map<String, dynamic>> getCurrentUser() async {
|
||||
try {
|
||||
print('[DataService] Calling getCurrentUser API...');
|
||||
final result = await _apiService.call('getCurrentUser', {});
|
||||
print('[DataService] Current user loaded successfully');
|
||||
return result['user'] as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
print('[DataService] Error getting current user: $e');
|
||||
throw Exception('Erreur lors de la récupération de l\'utilisateur actuel: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ALERTS
|
||||
// ============================================================================
|
||||
|
||||
/// Récupère toutes les alertes
|
||||
Future<List<Map<String, dynamic>>> getAlerts() async {
|
||||
try {
|
||||
final result = await _apiService.call('getAlerts', {});
|
||||
final alerts = result['alerts'] as List<dynamic>?;
|
||||
if (alerts == null) return [];
|
||||
return alerts.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des alertes: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Marque une alerte comme lue
|
||||
Future<void> markAlertAsRead(String alertId) async {
|
||||
try {
|
||||
await _apiService.call('markAlertAsRead', {'alertId': alertId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors du marquage de l\'alerte comme lue: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime une alerte
|
||||
Future<void> deleteAlert(String alertId) async {
|
||||
try {
|
||||
await _apiService.call('deleteAlert', {'alertId': alertId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'alerte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EQUIPMENT AVAILABILITY
|
||||
// ============================================================================
|
||||
|
||||
/// Vérifie la disponibilité d'un équipement
|
||||
Future<Map<String, dynamic>> checkEquipmentAvailability({
|
||||
required String equipmentId,
|
||||
Future<Map<String, dynamic>> checkContainerAvailability({
|
||||
required String containerId,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('checkEquipmentAvailability', {
|
||||
'equipmentId': equipmentId,
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
if (excludeEventId != null) 'excludeEventId': excludeEventId,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la vérification de disponibilité: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère tous les IDs d'équipements et conteneurs en conflit pour une période
|
||||
/// Optimisé : une seule requête au lieu d'une par équipement
|
||||
Future<Map<String, dynamic>> getConflictingEquipmentIds({
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
int installationTime = 0,
|
||||
int disassemblyTime = 0,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('getConflictingEquipmentIds', {
|
||||
'startDate': startDate.toIso8601String(),
|
||||
'endDate': endDate.toIso8601String(),
|
||||
if (excludeEventId != null) 'excludeEventId': excludeEventId,
|
||||
'installationTime': installationTime,
|
||||
'disassemblyTime': disassemblyTime,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des équipements en conflit: $e');
|
||||
}
|
||||
}
|
||||
}) =>
|
||||
containerRepository.checkContainerAvailability(
|
||||
containerId: containerId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
// ============================================================================
|
||||
// MAINTENANCES
|
||||
// USERS (delegated to UserRepository)
|
||||
// ============================================================================
|
||||
Future<Map<String, dynamic>> getCurrentUser() => userRepository.getCurrentUser();
|
||||
|
||||
/// Récupère toutes les maintenances
|
||||
Future<List<Map<String, dynamic>>> getMaintenances({String? equipmentId}) async {
|
||||
try {
|
||||
final data = <String, dynamic>{};
|
||||
if (equipmentId != null) data['equipmentId'] = equipmentId;
|
||||
Future<List<Map<String, dynamic>>> getUsers() => userRepository.getUsers();
|
||||
|
||||
final result = await _apiService.call('getMaintenances', data);
|
||||
final maintenances = result['maintenances'] as List<dynamic>?;
|
||||
if (maintenances == null) return [];
|
||||
return maintenances.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des maintenances: $e');
|
||||
}
|
||||
}
|
||||
Future<Map<String, dynamic>> getUser(String userId) => userRepository.getUser(userId);
|
||||
|
||||
/// Supprime une maintenance
|
||||
Future<void> deleteMaintenance(String maintenanceId) async {
|
||||
try {
|
||||
await _apiService.call('deleteMaintenance', {'maintenanceId': maintenanceId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de la maintenance: $e');
|
||||
}
|
||||
}
|
||||
Future<void> deleteUser(String userId) => userRepository.deleteUser(userId);
|
||||
|
||||
/// Récupère les containers contenant un équipement
|
||||
Future<List<Map<String, dynamic>>> getContainersByEquipment(String equipmentId) async {
|
||||
try {
|
||||
final result = await _apiService.call('getContainersByEquipment', {
|
||||
'equipmentId': equipmentId,
|
||||
});
|
||||
final containers = result['containers'] as List<dynamic>?;
|
||||
if (containers == null) return [];
|
||||
return containers.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des containers: $e');
|
||||
}
|
||||
}
|
||||
Future<void> updateUser(String userId, Map<String, dynamic> data) =>
|
||||
userRepository.updateUser(userId, data);
|
||||
|
||||
// ============================================================================
|
||||
// USERS
|
||||
// ============================================================================
|
||||
|
||||
/// Récupère tous les utilisateurs (selon permissions)
|
||||
Future<List<Map<String, dynamic>>> getUsers() async {
|
||||
try {
|
||||
final result = await _apiService.call('getUsers', {});
|
||||
final users = result['users'] as List<dynamic>?;
|
||||
if (users == null) return [];
|
||||
return users.map((e) => e as Map<String, dynamic>).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des utilisateurs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère un utilisateur spécifique
|
||||
Future<Map<String, dynamic>> getUser(String userId) async {
|
||||
try {
|
||||
final result = await _apiService.call('getUser', {'userId': userId});
|
||||
return result['user'] as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Supprime un utilisateur (Auth + Firestore)
|
||||
Future<void> deleteUser(String userId) async {
|
||||
try {
|
||||
await _apiService.call('deleteUser', {'userId': userId});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour un utilisateur
|
||||
Future<void> updateUser(String userId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
await _apiService.call('updateUser', {
|
||||
'userId': userId,
|
||||
'data': data,
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un utilisateur avec invitation par email
|
||||
Future<Map<String, dynamic>> createUserWithInvite({
|
||||
required String email,
|
||||
required String firstName,
|
||||
required String lastName,
|
||||
String? phoneNumber,
|
||||
required String roleId,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _apiService.call('createUserWithInvite', {
|
||||
'email': email,
|
||||
'firstName': firstName,
|
||||
'lastName': lastName,
|
||||
'phoneNumber': phoneNumber ?? '',
|
||||
'roleId': roleId,
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
}) =>
|
||||
userRepository.createUserWithInvite(
|
||||
email: email,
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
phoneNumber: phoneNumber,
|
||||
roleId: roleId,
|
||||
);
|
||||
|
||||
Future<List<Map<String, dynamic>>> getRoles() => userRepository.getRoles();
|
||||
|
||||
// ============================================================================
|
||||
// ALERTS (delegated to AlertRepository)
|
||||
// ============================================================================
|
||||
Future<List<Map<String, dynamic>>> getAlerts() => alertRepository.getAlerts();
|
||||
|
||||
Future<void> markAlertAsRead(String alertId) => alertRepository.markAlertAsRead(alertId);
|
||||
|
||||
Future<void> deleteAlert(String alertId) => alertRepository.deleteAlert(alertId);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
/// Service d'envoi d'emails via Cloud Functions
|
||||
class EmailService {
|
||||
final FirebaseFunctions _functions = FirebaseFunctions.instanceFor(region: 'europe-west9');
|
||||
FirebaseFunctions get _functions => FirebaseFunctions.instanceFor(region: 'europe-west9');
|
||||
|
||||
/// Envoie un email d'alerte à un utilisateur
|
||||
///
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/models/equipment_model.dart';
|
||||
import 'package:em2rp/models/container_model.dart';
|
||||
@@ -66,27 +67,19 @@ class AvailabilityConflict {
|
||||
class EventAvailabilityService {
|
||||
final DataService _dataService = DataService(apiService);
|
||||
|
||||
/// Helper pour récupérer uniquement la liste d'événements
|
||||
Future<List<Map<String, dynamic>>> _getEventsList() async {
|
||||
final result = await _dataService.getEvents();
|
||||
final events = result['events'] as List<dynamic>? ?? [];
|
||||
return events.map((e) => e as Map<String, dynamic>).toList();
|
||||
}
|
||||
|
||||
/// Vérifie si un équipement est disponible pour une plage de dates via Cloud Function
|
||||
Future<List<AvailabilityConflict>> checkEquipmentAvailability({
|
||||
required String equipmentId,
|
||||
required String equipmentName,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId, // Pour exclure l'événement en cours d'édition
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
final conflicts = <AvailabilityConflict>[];
|
||||
|
||||
try {
|
||||
print('[EventAvailabilityService] Checking availability for equipment $equipmentId ($equipmentName)');
|
||||
if (kDebugMode) debugPrint('[EventAvailabilityService] Checking availability for equipment $equipmentId ($equipmentName)');
|
||||
|
||||
// Utiliser la Cloud Function pour vérifier la disponibilité
|
||||
final result = await _dataService.checkEquipmentAvailability(
|
||||
equipmentId: equipmentId,
|
||||
startDate: startDate,
|
||||
@@ -94,20 +87,12 @@ class EventAvailabilityService {
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
print('[EventAvailabilityService] Result for $equipmentId: $result');
|
||||
|
||||
final available = result['available'] as bool? ?? true;
|
||||
print('[EventAvailabilityService] Equipment $equipmentId available: $available');
|
||||
|
||||
if (!available) {
|
||||
final conflictsData = result['conflicts'] as List<dynamic>? ?? [];
|
||||
print('[EventAvailabilityService] Found ${conflictsData.length} conflicts for equipment $equipmentId');
|
||||
|
||||
for (final conflictData in conflictsData) {
|
||||
final conflict = conflictData as Map<String, dynamic>;
|
||||
final eventId = conflict['eventId'] as String;
|
||||
|
||||
// Le backend retourne déjà eventData
|
||||
final eventData = conflict['eventData'] as Map<String, dynamic>?;
|
||||
|
||||
if (eventData != null && eventData.isNotEmpty) {
|
||||
@@ -119,19 +104,16 @@ class EventAvailabilityService {
|
||||
conflictingEvent: event,
|
||||
overlapDays: conflict['overlapDays'] as int? ?? 0,
|
||||
));
|
||||
print('[EventAvailabilityService] Added conflict with event ${event.name}');
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error creating EventModel: $e');
|
||||
print('[EventAvailabilityService] EventData: $eventData');
|
||||
if (kDebugMode) debugPrint('[EventAvailabilityService] Error creating EventModel: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error checking availability: $e');
|
||||
if (kDebugMode) debugPrint('[EventAvailabilityService] Error checking availability: $e');
|
||||
}
|
||||
|
||||
print('[EventAvailabilityService] Returning ${conflicts.length} conflicts for equipment $equipmentId');
|
||||
return conflicts;
|
||||
}
|
||||
|
||||
@@ -159,164 +141,10 @@ class EventAvailabilityService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return allConflicts;
|
||||
}
|
||||
|
||||
/// Vérifie si deux plages de dates se chevauchent
|
||||
bool _datesOverlap(DateTime start1, DateTime end1, DateTime start2, DateTime end2) {
|
||||
// Deux plages se chevauchent si elles ne sont PAS complètement séparées
|
||||
// Elles sont séparées si : end1 < start2 OU end2 < start1
|
||||
// Donc elles se chevauchent si : NOT (end1 < start2 OU end2 < start1)
|
||||
// Équivalent à : end1 >= start2 ET end2 >= start1
|
||||
return !end1.isBefore(start2) && !end2.isBefore(start1);
|
||||
}
|
||||
|
||||
/// Calcule le nombre de jours de chevauchement
|
||||
int _calculateOverlapDays(DateTime start1, DateTime end1, DateTime start2, DateTime end2) {
|
||||
final overlapStart = start1.isAfter(start2) ? start1 : start2;
|
||||
final overlapEnd = end1.isBefore(end2) ? end1 : end2;
|
||||
|
||||
return overlapEnd.difference(overlapStart).inDays + 1;
|
||||
}
|
||||
|
||||
/// Récupère la quantité disponible pour un consommable/câble
|
||||
Future<int> getAvailableQuantity({
|
||||
required EquipmentModel equipment,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
if (!equipment.hasQuantity) {
|
||||
return 1; // Équipement non consommable
|
||||
}
|
||||
|
||||
final totalQuantity = equipment.totalQuantity ?? 0;
|
||||
int reservedQuantity = 0;
|
||||
|
||||
try {
|
||||
// Récupérer tous les événements via Cloud Function
|
||||
final eventsData = await _getEventsList();
|
||||
|
||||
for (var eventData in eventsData) {
|
||||
final eventId = eventData['id'] as String;
|
||||
if (excludeEventId != null && eventId == excludeEventId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
final event = EventModel.fromMap(eventData, eventId);
|
||||
|
||||
// Ignorer les événements annulés
|
||||
if (event.status == EventStatus.canceled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculer les dates réelles avec temps d'installation et démontage
|
||||
final eventRealStartDate = event.startDateTime.subtract(
|
||||
Duration(hours: event.installationTime),
|
||||
);
|
||||
final eventRealEndDate = event.endDateTime.add(
|
||||
Duration(hours: event.disassemblyTime),
|
||||
);
|
||||
|
||||
// Vérifier le chevauchement des dates
|
||||
if (_datesOverlap(startDate, endDate, eventRealStartDate, eventRealEndDate)) {
|
||||
final assignedEquipment = event.assignedEquipment.firstWhere(
|
||||
(eq) => eq.equipmentId == equipment.id,
|
||||
orElse: () => EventEquipment(equipmentId: ''),
|
||||
);
|
||||
|
||||
// Si l'équipement est assigné, réserver la quantité
|
||||
// (peu importe le statut de préparation/retour)
|
||||
if (assignedEquipment.equipmentId.isNotEmpty) {
|
||||
reservedQuantity += assignedEquipment.quantity;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error processing event $eventId for quantity: $e');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error getting available quantity: $e');
|
||||
}
|
||||
|
||||
return totalQuantity - reservedQuantity;
|
||||
}
|
||||
|
||||
/// Vérifie la disponibilité d'un équipement avec gestion des quantités
|
||||
Future<List<AvailabilityConflict>> checkEquipmentAvailabilityWithQuantity({
|
||||
required EquipmentModel equipment,
|
||||
required int requestedQuantity,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
final conflicts = <AvailabilityConflict>[];
|
||||
|
||||
// Si équipement quantifiable (consommable/câble)
|
||||
if (equipment.hasQuantity) {
|
||||
final totalQuantity = equipment.totalQuantity ?? 0;
|
||||
final availableQty = await getAvailableQuantity(
|
||||
equipment: equipment,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
final reservedQty = totalQuantity - availableQty;
|
||||
|
||||
// ✅ Ne créer un conflit que si la quantité est VRAIMENT insuffisante
|
||||
if (availableQty < requestedQuantity) {
|
||||
// Trouver les événements qui réservent cette quantité
|
||||
final eventsData = await _getEventsList();
|
||||
|
||||
for (var eventData in eventsData) {
|
||||
final eventId = eventData['id'] as String;
|
||||
if (excludeEventId != null && eventId == excludeEventId) continue;
|
||||
|
||||
try {
|
||||
final event = EventModel.fromMap(eventData, eventId);
|
||||
|
||||
if (_datesOverlap(startDate, endDate, event.startDateTime, event.endDateTime)) {
|
||||
final assignedEquipment = event.assignedEquipment.firstWhere(
|
||||
(eq) => eq.equipmentId == equipment.id,
|
||||
orElse: () => EventEquipment(equipmentId: ''),
|
||||
);
|
||||
|
||||
if (assignedEquipment.equipmentId.isNotEmpty && !assignedEquipment.isReturned) {
|
||||
conflicts.add(AvailabilityConflict(
|
||||
equipmentId: equipment.id,
|
||||
equipmentName: equipment.name,
|
||||
conflictingEvent: event,
|
||||
overlapDays: _calculateOverlapDays(startDate, endDate, event.startDateTime, event.endDateTime),
|
||||
type: ConflictType.insufficientQuantity,
|
||||
totalQuantity: totalQuantity,
|
||||
availableQuantity: availableQty,
|
||||
requestedQuantity: requestedQuantity,
|
||||
reservedQuantity: reservedQty,
|
||||
));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error processing event $eventId: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Équipement non quantifiable : vérification classique
|
||||
return await checkEquipmentAvailability(
|
||||
equipmentId: equipment.id,
|
||||
equipmentName: equipment.name,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
}
|
||||
|
||||
return conflicts;
|
||||
}
|
||||
|
||||
/// Vérifie la disponibilité d'une boîte et de son contenu
|
||||
/// Vérifie la disponibilité d'une boîte et de son contenu via le backend
|
||||
Future<List<AvailabilityConflict>> checkContainerAvailability({
|
||||
required ContainerModel container,
|
||||
required List<EquipmentModel> containerEquipment,
|
||||
@@ -325,99 +153,62 @@ class EventAvailabilityService {
|
||||
String? excludeEventId,
|
||||
}) async {
|
||||
final conflicts = <AvailabilityConflict>[];
|
||||
final conflictingChildrenIds = <String>[];
|
||||
|
||||
// Vérifier d'abord si la boîte complète est utilisée
|
||||
final eventsData = await _getEventsList();
|
||||
bool isContainerFullyUsed = false;
|
||||
EventModel? containerConflictingEvent;
|
||||
try {
|
||||
final result = await _dataService.checkContainerAvailability(
|
||||
containerId: container.id,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
for (var eventData in eventsData) {
|
||||
final eventId = eventData['id'] as String;
|
||||
if (excludeEventId != null && eventId == excludeEventId) continue;
|
||||
|
||||
try {
|
||||
final event = EventModel.fromMap(eventData, eventId);
|
||||
|
||||
// Ignorer les événements annulés
|
||||
if (event.status == EventStatus.canceled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculer les dates réelles avec temps d'installation et démontage
|
||||
final eventRealStartDate = event.startDateTime.subtract(
|
||||
Duration(hours: event.installationTime),
|
||||
);
|
||||
final eventRealEndDate = event.endDateTime.add(
|
||||
Duration(hours: event.disassemblyTime),
|
||||
);
|
||||
|
||||
// Vérifier si cette boîte est assignée
|
||||
if (event.assignedContainers.contains(container.id)) {
|
||||
if (_datesOverlap(startDate, endDate, eventRealStartDate, eventRealEndDate)) {
|
||||
isContainerFullyUsed = true;
|
||||
containerConflictingEvent = event;
|
||||
break;
|
||||
final isAvailable = result['isAvailable'] as bool? ?? true;
|
||||
if (!isAvailable) {
|
||||
final conflictType = result['conflictType'] as String?;
|
||||
|
||||
if (conflictType == 'complete') {
|
||||
final containerConflicts = result['containerConflicts'] as List<dynamic>? ?? [];
|
||||
for (var conflictData in containerConflicts) {
|
||||
final conflict = conflictData as Map<String, dynamic>;
|
||||
final eventId = conflict['eventId'] as String;
|
||||
final eventName = conflict['eventName'] as String? ?? '';
|
||||
final startDateStr = conflict['startDate'] as String?;
|
||||
final endDateStr = conflict['endDate'] as String?;
|
||||
|
||||
final event = EventModel(
|
||||
id: eventId,
|
||||
name: eventName,
|
||||
description: '',
|
||||
startDateTime: startDateStr != null ? DateTime.tryParse(startDateStr) ?? DateTime.now() : DateTime.now(),
|
||||
endDateTime: endDateStr != null ? DateTime.tryParse(endDateStr) ?? DateTime.now() : DateTime.now(),
|
||||
basePrice: 0.0,
|
||||
installationTime: 0,
|
||||
disassemblyTime: 0,
|
||||
eventTypeId: '',
|
||||
customerId: '',
|
||||
address: '',
|
||||
latitude: 0.0,
|
||||
longitude: 0.0,
|
||||
workforce: const [],
|
||||
documents: const [],
|
||||
);
|
||||
|
||||
conflicts.add(AvailabilityConflict(
|
||||
equipmentId: container.id,
|
||||
equipmentName: container.name,
|
||||
conflictingEvent: event,
|
||||
overlapDays: conflict['overlapDays'] as int? ?? 0,
|
||||
type: ConflictType.containerFullyUsed,
|
||||
containerId: container.id,
|
||||
containerName: container.name,
|
||||
));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('[EventAvailabilityService] Error processing event $eventId: $e');
|
||||
}
|
||||
}
|
||||
|
||||
if (isContainerFullyUsed && containerConflictingEvent != null) {
|
||||
// Boîte complète utilisée
|
||||
conflicts.add(AvailabilityConflict(
|
||||
equipmentId: container.id,
|
||||
equipmentName: container.name,
|
||||
conflictingEvent: containerConflictingEvent,
|
||||
overlapDays: _calculateOverlapDays(
|
||||
startDate,
|
||||
endDate,
|
||||
containerConflictingEvent.startDateTime,
|
||||
containerConflictingEvent.endDateTime,
|
||||
),
|
||||
type: ConflictType.containerFullyUsed,
|
||||
containerId: container.id,
|
||||
containerName: container.name,
|
||||
));
|
||||
} else {
|
||||
// Vérifier chaque équipement enfant individuellement
|
||||
for (var equipment in containerEquipment) {
|
||||
final equipmentConflicts = await checkEquipmentAvailability(
|
||||
equipmentId: equipment.id,
|
||||
equipmentName: equipment.name,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
excludeEventId: excludeEventId,
|
||||
);
|
||||
|
||||
if (equipmentConflicts.isNotEmpty) {
|
||||
conflictingChildrenIds.add(equipment.id);
|
||||
conflicts.addAll(equipmentConflicts);
|
||||
}
|
||||
}
|
||||
|
||||
// Si au moins un enfant en conflit, ajouter un conflit pour la boîte
|
||||
if (conflictingChildrenIds.isNotEmpty && conflicts.isNotEmpty) {
|
||||
conflicts.insert(
|
||||
0,
|
||||
AvailabilityConflict(
|
||||
equipmentId: container.id,
|
||||
equipmentName: container.name,
|
||||
conflictingEvent: conflicts.first.conflictingEvent,
|
||||
overlapDays: conflicts.first.overlapDays,
|
||||
type: ConflictType.containerPartiallyUsed,
|
||||
containerId: container.id,
|
||||
containerName: container.name,
|
||||
conflictingChildrenIds: conflictingChildrenIds,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (kDebugMode) debugPrint('[EventAvailabilityService] Error checking container availability: $e');
|
||||
}
|
||||
|
||||
return conflicts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,44 @@ import 'package:em2rp/services/api_service.dart';
|
||||
class EventPreparationService {
|
||||
final ApiService _apiService = apiService;
|
||||
|
||||
/// Retourne true si l'équipement était absent du flux événementiel.
|
||||
///
|
||||
/// Cas typique: matériel jamais emporté au départ, donc absent au retour,
|
||||
/// mais qui ne doit jamais être classé en [LOST].
|
||||
static bool isEquipmentNotTakenToEvent({
|
||||
required bool isMissingAtReturn,
|
||||
required bool isLoaded,
|
||||
required bool isMissingAtLoading,
|
||||
int? quantityAtLoading,
|
||||
}) {
|
||||
if (!isMissingAtReturn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final loadedQuantity = quantityAtLoading ?? 0;
|
||||
return !isLoaded || isMissingAtLoading || loadedQuantity <= 0;
|
||||
}
|
||||
|
||||
/// Retourne true uniquement si l'équipement doit être classé perdu.
|
||||
static bool shouldMarkEquipmentAsLost({
|
||||
required bool isReturnValidationStep,
|
||||
required bool isMissingAtReturn,
|
||||
required bool isLoaded,
|
||||
required bool isMissingAtLoading,
|
||||
int? quantityAtLoading,
|
||||
}) {
|
||||
if (!isReturnValidationStep || !isMissingAtReturn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !isEquipmentNotTakenToEvent(
|
||||
isMissingAtReturn: isMissingAtReturn,
|
||||
isLoaded: isLoaded,
|
||||
isMissingAtLoading: isMissingAtLoading,
|
||||
quantityAtLoading: quantityAtLoading,
|
||||
);
|
||||
}
|
||||
|
||||
// === PRÉPARATION ===
|
||||
|
||||
/// Valider un équipement individuel en préparation
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/models/event_statistics_models.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class EventStatisticsService {
|
||||
const EventStatisticsService();
|
||||
|
||||
static const double _taxRatio = 1.2;
|
||||
|
||||
EventStatisticsSummary buildSummary({
|
||||
required List<EventModel> events,
|
||||
required EventStatisticsFilter filter,
|
||||
required Map<String, String> eventTypeNames,
|
||||
}) {
|
||||
final filteredEvents =
|
||||
events.where((event) => _matchesFilter(event, filter)).toList();
|
||||
|
||||
if (filteredEvents.isEmpty) {
|
||||
return EventStatisticsSummary.empty;
|
||||
}
|
||||
|
||||
var validatedEvents = 0;
|
||||
var pendingEvents = 0;
|
||||
var canceledEvents = 0;
|
||||
|
||||
var validatedAmount = 0.0;
|
||||
var pendingAmount = 0.0;
|
||||
var canceledAmount = 0.0;
|
||||
|
||||
var baseAmount = 0.0;
|
||||
var optionsAmount = 0.0;
|
||||
|
||||
final eventAmounts = <double>[];
|
||||
final byType = <String, _EventTypeAccumulator>{};
|
||||
final optionStats = <String, _OptionAccumulator>{};
|
||||
|
||||
for (final event in filteredEvents) {
|
||||
final base = _toHtAmount(event.basePrice);
|
||||
final optionTotal = _computeOptionsTotal(event);
|
||||
final amount = base + optionTotal;
|
||||
final isValidated = event.status == EventStatus.confirmed;
|
||||
|
||||
eventAmounts.add(amount);
|
||||
baseAmount += base;
|
||||
optionsAmount += optionTotal;
|
||||
|
||||
switch (event.status) {
|
||||
case EventStatus.confirmed:
|
||||
validatedEvents += 1;
|
||||
validatedAmount += amount;
|
||||
break;
|
||||
case EventStatus.waitingForApproval:
|
||||
pendingEvents += 1;
|
||||
pendingAmount += amount;
|
||||
break;
|
||||
case EventStatus.canceled:
|
||||
canceledEvents += 1;
|
||||
canceledAmount += amount;
|
||||
break;
|
||||
}
|
||||
|
||||
final eventTypeId = event.eventTypeId;
|
||||
final eventTypeName = eventTypeNames[eventTypeId] ?? 'Type inconnu';
|
||||
final typeAccumulator = byType.putIfAbsent(
|
||||
eventTypeId,
|
||||
() => _EventTypeAccumulator(
|
||||
eventTypeId: eventTypeId, eventTypeName: eventTypeName),
|
||||
);
|
||||
typeAccumulator.totalEvents += 1;
|
||||
typeAccumulator.totalAmount += amount;
|
||||
switch (event.status) {
|
||||
case EventStatus.confirmed:
|
||||
typeAccumulator.validatedAmount += amount;
|
||||
break;
|
||||
case EventStatus.waitingForApproval:
|
||||
typeAccumulator.pendingAmount += amount;
|
||||
break;
|
||||
case EventStatus.canceled:
|
||||
typeAccumulator.canceledAmount += amount;
|
||||
break;
|
||||
}
|
||||
|
||||
for (final rawOption in event.options) {
|
||||
final optionPrice = _toHtAmount(_toDouble(rawOption['price']));
|
||||
final optionQuantity = _toInt(rawOption['quantity'], fallback: 1);
|
||||
if (optionPrice == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final optionId = (rawOption['id'] ??
|
||||
rawOption['code'] ??
|
||||
rawOption['name'] ??
|
||||
'option')
|
||||
.toString();
|
||||
final optionLabel = _buildOptionLabel(rawOption, optionId);
|
||||
final optionAmount = optionPrice * optionQuantity;
|
||||
|
||||
final optionAccumulator = optionStats.putIfAbsent(
|
||||
optionId,
|
||||
() =>
|
||||
_OptionAccumulator(optionKey: optionId, optionLabel: optionLabel),
|
||||
);
|
||||
optionAccumulator.usageCount += 1;
|
||||
if (isValidated) {
|
||||
optionAccumulator.validatedUsageCount += 1;
|
||||
}
|
||||
optionAccumulator.quantity += optionQuantity;
|
||||
optionAccumulator.totalAmount += optionAmount;
|
||||
}
|
||||
}
|
||||
|
||||
final byEventType = byType.values
|
||||
.map((accumulator) => EventTypeStatistics(
|
||||
eventTypeId: accumulator.eventTypeId,
|
||||
eventTypeName: accumulator.eventTypeName,
|
||||
totalEvents: accumulator.totalEvents,
|
||||
totalAmount: accumulator.totalAmount,
|
||||
validatedAmount: accumulator.validatedAmount,
|
||||
pendingAmount: accumulator.pendingAmount,
|
||||
canceledAmount: accumulator.canceledAmount,
|
||||
))
|
||||
.toList()
|
||||
..sort((a, b) => b.totalAmount.compareTo(a.totalAmount));
|
||||
|
||||
final topOptions = optionStats.values
|
||||
.map((accumulator) => OptionStatistics(
|
||||
optionKey: accumulator.optionKey,
|
||||
optionLabel: accumulator.optionLabel,
|
||||
usageCount: accumulator.usageCount,
|
||||
validatedUsageCount: accumulator.validatedUsageCount,
|
||||
quantity: accumulator.quantity,
|
||||
totalAmount: accumulator.totalAmount,
|
||||
))
|
||||
.toList()
|
||||
..sort((a, b) {
|
||||
final validatedComparison =
|
||||
b.validatedUsageCount.compareTo(a.validatedUsageCount);
|
||||
if (validatedComparison != 0) {
|
||||
return validatedComparison;
|
||||
}
|
||||
return b.totalAmount.compareTo(a.totalAmount);
|
||||
});
|
||||
|
||||
return EventStatisticsSummary(
|
||||
totalEvents: filteredEvents.length,
|
||||
validatedEvents: validatedEvents,
|
||||
pendingEvents: pendingEvents,
|
||||
canceledEvents: canceledEvents,
|
||||
totalAmount: validatedAmount + pendingAmount + canceledAmount,
|
||||
validatedAmount: validatedAmount,
|
||||
pendingAmount: pendingAmount,
|
||||
canceledAmount: canceledAmount,
|
||||
baseAmount: baseAmount,
|
||||
optionsAmount: optionsAmount,
|
||||
medianAmount: _computeMedian(eventAmounts),
|
||||
byEventType: byEventType,
|
||||
topOptions: topOptions.take(8).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
bool _matchesFilter(EventModel event, EventStatisticsFilter filter) {
|
||||
if (!_overlapsRange(event, filter.period)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!filter.selectedStatuses.contains(event.status)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filter.eventTypeIds.isNotEmpty &&
|
||||
!filter.eventTypeIds.contains(event.eventTypeId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _overlapsRange(EventModel event, DateTimeRange range) {
|
||||
return !event.endDateTime.isBefore(range.start) &&
|
||||
!event.startDateTime.isAfter(range.end);
|
||||
}
|
||||
|
||||
double _computeOptionsTotal(EventModel event) {
|
||||
return event.options.fold<double>(0.0, (sum, option) {
|
||||
final optionPrice = _toHtAmount(_toDouble(option['price']));
|
||||
final optionQuantity = _toInt(option['quantity'], fallback: 1);
|
||||
return sum + (optionPrice * optionQuantity);
|
||||
});
|
||||
}
|
||||
|
||||
double _toHtAmount(double storedAmount) {
|
||||
return storedAmount / _taxRatio;
|
||||
}
|
||||
|
||||
double _toDouble(dynamic value) {
|
||||
if (value == null) {
|
||||
return 0.0;
|
||||
}
|
||||
if (value is num) {
|
||||
return value.toDouble();
|
||||
}
|
||||
return double.tryParse(value.toString()) ?? 0.0;
|
||||
}
|
||||
|
||||
int _toInt(dynamic value, {int fallback = 0}) {
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
}
|
||||
if (value is int) {
|
||||
return value;
|
||||
}
|
||||
if (value is num) {
|
||||
return value.toInt();
|
||||
}
|
||||
return int.tryParse(value.toString()) ?? fallback;
|
||||
}
|
||||
|
||||
String _buildOptionLabel(Map<String, dynamic> option, String fallback) {
|
||||
final code = (option['code'] ?? '').toString().trim();
|
||||
final name = (option['name'] ?? '').toString().trim();
|
||||
|
||||
if (code.isNotEmpty && name.isNotEmpty) {
|
||||
return '$code - $name';
|
||||
}
|
||||
|
||||
if (name.isNotEmpty) {
|
||||
return name;
|
||||
}
|
||||
|
||||
if (code.isNotEmpty) {
|
||||
return code;
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
|
||||
double _computeMedian(List<double> values) {
|
||||
if (values.isEmpty) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
final sorted = [...values]..sort();
|
||||
final middleIndex = sorted.length ~/ 2;
|
||||
|
||||
if (sorted.length.isOdd) {
|
||||
return sorted[middleIndex];
|
||||
}
|
||||
|
||||
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
class _EventTypeAccumulator {
|
||||
final String eventTypeId;
|
||||
final String eventTypeName;
|
||||
int totalEvents = 0;
|
||||
double totalAmount = 0.0;
|
||||
double validatedAmount = 0.0;
|
||||
double pendingAmount = 0.0;
|
||||
double canceledAmount = 0.0;
|
||||
|
||||
_EventTypeAccumulator({
|
||||
required this.eventTypeId,
|
||||
required this.eventTypeName,
|
||||
});
|
||||
}
|
||||
|
||||
class _OptionAccumulator {
|
||||
final String optionKey;
|
||||
final String optionLabel;
|
||||
int usageCount = 0;
|
||||
int validatedUsageCount = 0;
|
||||
int quantity = 0;
|
||||
double totalAmount = 0.0;
|
||||
|
||||
_OptionAccumulator({
|
||||
required this.optionKey,
|
||||
required this.optionLabel,
|
||||
});
|
||||
}
|
||||
@@ -16,7 +16,7 @@ class IcsExportService {
|
||||
Map<String, String>? optionNames,
|
||||
}) async {
|
||||
final now = DateTime.now().toUtc();
|
||||
final timestamp = DateFormat('yyyyMMddTHHmmss').format(now) + 'Z';
|
||||
final timestamp = '${DateFormat('yyyyMMddTHHmmss').format(now)}Z';
|
||||
|
||||
// Récupérer les informations supplémentaires
|
||||
final resolvedEventTypeName = eventTypeName ?? await _getEventTypeName(event.eventTypeId);
|
||||
@@ -238,7 +238,7 @@ END:VCALENDAR''';
|
||||
/// Formate une date au format ICS (yyyyMMddTHHmmssZ)
|
||||
static String _formatDateForIcs(DateTime dateTime) {
|
||||
final utcDate = dateTime.toUtc();
|
||||
return DateFormat('yyyyMMddTHHmmss').format(utcDate) + 'Z';
|
||||
return '${DateFormat('yyyyMMddTHHmmss').format(utcDate)}Z';
|
||||
}
|
||||
|
||||
/// Échappe les caractères spéciaux pour le format ICS
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:qr_flutter/qr_flutter.dart';
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:em2rp/services/cloud_text_to_speech_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Service de synthèse vocale utilisant exclusivement Google Cloud TTS
|
||||
/// Garantit une qualité et une compatibilité maximales sur tous les navigateurs
|
||||
class SmartTextToSpeechService {
|
||||
static bool _initialized = false;
|
||||
|
||||
/// Initialiser le service
|
||||
static Future<void> initialize() async {
|
||||
if (_initialized) return;
|
||||
|
||||
try {
|
||||
DebugLog.info('[SmartTTS] Initializing Cloud TTS only...');
|
||||
|
||||
// Pré-charger les phrases courantes pour Cloud TTS
|
||||
Future.delayed(const Duration(milliseconds: 500), () {
|
||||
CloudTextToSpeechService.preloadCommonPhrases();
|
||||
});
|
||||
|
||||
_initialized = true;
|
||||
DebugLog.info('[SmartTTS] ✓ Initialized (Cloud TTS only)');
|
||||
} catch (e) {
|
||||
DebugLog.error('[SmartTTS] Initialization error', e);
|
||||
_initialized = true; // Continuer quand même
|
||||
}
|
||||
}
|
||||
|
||||
/// Lire un texte à haute voix avec Google Cloud TTS
|
||||
static Future<void> speak(String text) async {
|
||||
if (!_initialized) {
|
||||
await initialize();
|
||||
}
|
||||
|
||||
try {
|
||||
DebugLog.info('[SmartTTS] → Using Cloud TTS');
|
||||
await CloudTextToSpeechService.speak(text);
|
||||
DebugLog.info('[SmartTTS] ✓ Cloud TTS succeeded');
|
||||
} catch (e) {
|
||||
DebugLog.error('[SmartTTS] ✗ Cloud TTS failed', e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Arrêter toute lecture en cours
|
||||
static Future<void> stop() async {
|
||||
try {
|
||||
CloudTextToSpeechService.stopAll();
|
||||
} catch (e) {
|
||||
DebugLog.error('[SmartTTS] Error stopping', e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifier si une lecture est en cours
|
||||
static Future<bool> isSpeaking() async {
|
||||
// Cloud TTS n'a pas de méthode native pour vérifier le statut
|
||||
// Retourner false par défaut (peut être amélioré si nécessaire)
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Obtenir le statut actuel
|
||||
static Map<String, dynamic> getStatus() {
|
||||
return {
|
||||
'initialized': _initialized,
|
||||
'currentStrategy': 'Cloud TTS (exclusive)',
|
||||
};
|
||||
}
|
||||
|
||||
/// Nettoyer les ressources
|
||||
static Future<void> dispose() async {
|
||||
try {
|
||||
CloudTextToSpeechService.clearCache();
|
||||
} catch (e) {
|
||||
DebugLog.error('[SmartTTS] Error disposing', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,244 +0,0 @@
|
||||
import 'dart:js_interop';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Service de synthèse vocale pour lire des textes à haute voix (Web)
|
||||
class TextToSpeechService {
|
||||
static bool _isInitialized = false;
|
||||
static bool _voicesLoaded = false;
|
||||
static List<web.SpeechSynthesisVoice> _cachedVoices = [];
|
||||
|
||||
/// Initialiser le service TTS
|
||||
static Future<void> initialize() async {
|
||||
if (_isInitialized) return;
|
||||
|
||||
try {
|
||||
_isInitialized = true;
|
||||
|
||||
final synthesis = web.window.speechSynthesis;
|
||||
|
||||
// Essayer de charger les voix immédiatement
|
||||
_cachedVoices = synthesis.getVoices().toDart;
|
||||
|
||||
if (_cachedVoices.isNotEmpty) {
|
||||
_voicesLoaded = true;
|
||||
DebugLog.info('[TextToSpeechService] Service initialized with ${_cachedVoices.length} voices');
|
||||
return;
|
||||
}
|
||||
|
||||
// Sur certains navigateurs (Firefox notamment), les voix se chargent de manière asynchrone
|
||||
DebugLog.info('[TextToSpeechService] Waiting for voices to load asynchronously...');
|
||||
|
||||
// Attendre l'événement voiceschanged (si supporté)
|
||||
final voicesLoaded = await _waitForVoices(synthesis);
|
||||
|
||||
if (voicesLoaded) {
|
||||
_cachedVoices = synthesis.getVoices().toDart;
|
||||
_voicesLoaded = true;
|
||||
DebugLog.info('[TextToSpeechService] ✓ Voices loaded asynchronously: ${_cachedVoices.length}');
|
||||
} else {
|
||||
DebugLog.warning('[TextToSpeechService] ⚠ No voices found after initialization');
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLog.error('[TextToSpeechService] Erreur lors de l\'initialisation', e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Attendre le chargement des voix (avec timeout)
|
||||
static Future<bool> _waitForVoices(web.SpeechSynthesis synthesis) async {
|
||||
// Essayer plusieurs fois avec des délais croissants
|
||||
for (int attempt = 0; attempt < 5; attempt++) {
|
||||
await Future.delayed(Duration(milliseconds: 100 * (attempt + 1)));
|
||||
|
||||
final voices = synthesis.getVoices().toDart;
|
||||
if (voices.isNotEmpty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DebugLog.info('[TextToSpeechService] Attempt ${attempt + 1}/5: No voices yet');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Lire un texte à haute voix
|
||||
static Future<void> speak(String text) async {
|
||||
if (!_isInitialized) {
|
||||
await initialize();
|
||||
}
|
||||
|
||||
try {
|
||||
final synthesis = web.window.speechSynthesis;
|
||||
|
||||
DebugLog.info('[TextToSpeechService] Speaking requested: "$text"');
|
||||
|
||||
// Arrêter toute lecture en cours
|
||||
synthesis.cancel();
|
||||
|
||||
// Attendre un peu pour que le cancel soit effectif
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
// Créer une nouvelle utterance
|
||||
final utterance = web.SpeechSynthesisUtterance(text);
|
||||
utterance.lang = 'fr-FR';
|
||||
utterance.rate = 0.7;
|
||||
utterance.pitch = 0.7;
|
||||
utterance.volume = 1.0;
|
||||
|
||||
// Récupérer les voix (depuis le cache ou re-charger)
|
||||
var voices = _cachedVoices;
|
||||
|
||||
// Si le cache est vide, essayer de recharger
|
||||
if (voices.isEmpty) {
|
||||
DebugLog.info('[TextToSpeechService] Cache empty, reloading voices...');
|
||||
voices = synthesis.getVoices().toDart;
|
||||
|
||||
// Sur Firefox/Linux, les voix peuvent ne pas être disponibles immédiatement
|
||||
if (voices.isEmpty && !_voicesLoaded) {
|
||||
DebugLog.info('[TextToSpeechService] Waiting for voices with multiple attempts...');
|
||||
|
||||
// Essayer plusieurs fois avec des délais
|
||||
for (int i = 0; i < 3; i++) {
|
||||
await Future.delayed(Duration(milliseconds: 100 * (i + 1)));
|
||||
voices = synthesis.getVoices().toDart;
|
||||
|
||||
if (voices.isNotEmpty) {
|
||||
DebugLog.info('[TextToSpeechService] ✓ Voices loaded on attempt ${i + 1}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour le cache
|
||||
if (voices.isNotEmpty) {
|
||||
_cachedVoices = voices;
|
||||
_voicesLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
DebugLog.info('[TextToSpeechService] Available voices: ${voices.length}');
|
||||
|
||||
if (voices.isNotEmpty) {
|
||||
web.SpeechSynthesisVoice? selectedVoice;
|
||||
|
||||
// Lister TOUTES les voix françaises pour debug
|
||||
final frenchVoices = <web.SpeechSynthesisVoice>[];
|
||||
for (final voice in voices) {
|
||||
final lang = voice.lang.toLowerCase();
|
||||
if (lang.startsWith('fr')) {
|
||||
frenchVoices.add(voice);
|
||||
DebugLog.info('[TextToSpeechService] French: ${voice.name} (${voice.lang}) ${voice.localService ? 'LOCAL' : 'REMOTE'}');
|
||||
}
|
||||
}
|
||||
|
||||
if (frenchVoices.isEmpty) {
|
||||
DebugLog.warning('[TextToSpeechService] ⚠ NO French voices found!');
|
||||
DebugLog.info('[TextToSpeechService] Available languages:');
|
||||
for (final voice in voices.take(5)) {
|
||||
DebugLog.info('[TextToSpeechService] - ${voice.name} (${voice.lang})');
|
||||
}
|
||||
}
|
||||
|
||||
// Stratégie de sélection: préférer les voix LOCALES (plus fiables sur Linux)
|
||||
for (final voice in frenchVoices) {
|
||||
if (voice.localService) {
|
||||
selectedVoice = voice;
|
||||
DebugLog.info('[TextToSpeechService] ✓ Selected LOCAL French voice: ${voice.name}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Si pas de voix locale, chercher une voix masculine
|
||||
if (selectedVoice == null) {
|
||||
for (final voice in frenchVoices) {
|
||||
final name = voice.name.toLowerCase();
|
||||
if (name.contains('male') ||
|
||||
name.contains('homme') ||
|
||||
name.contains('thomas') ||
|
||||
name.contains('paul') ||
|
||||
name.contains('bernard')) {
|
||||
selectedVoice = voice;
|
||||
DebugLog.info('[TextToSpeechService] Selected male voice: ${voice.name}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: première voix française
|
||||
selectedVoice ??= frenchVoices.isNotEmpty ? frenchVoices.first : null;
|
||||
|
||||
if (selectedVoice != null) {
|
||||
utterance.voice = selectedVoice;
|
||||
utterance.lang = selectedVoice.lang; // Utiliser la langue de la voix
|
||||
DebugLog.info('[TextToSpeechService] Final voice: ${selectedVoice.name} (${selectedVoice.lang})');
|
||||
} else {
|
||||
DebugLog.warning('[TextToSpeechService] No French voice, using default with lang=fr-FR');
|
||||
}
|
||||
} else {
|
||||
DebugLog.warning('[TextToSpeechService] ⚠ NO voices available at all!');
|
||||
DebugLog.warning('[TextToSpeechService] On Linux: install speech-dispatcher and espeak-ng');
|
||||
}
|
||||
|
||||
// Ajouter des événements pour le debug
|
||||
utterance.onstart = (web.SpeechSynthesisEvent event) {
|
||||
DebugLog.info('[TextToSpeechService] ✓ Speech started');
|
||||
}.toJS;
|
||||
|
||||
utterance.onend = (web.SpeechSynthesisEvent event) {
|
||||
DebugLog.info('[TextToSpeechService] ✓ Speech ended');
|
||||
}.toJS;
|
||||
|
||||
utterance.onerror = (web.SpeechSynthesisErrorEvent event) {
|
||||
DebugLog.error('[TextToSpeechService] ✗ Speech error: ${event.error}');
|
||||
|
||||
// Messages spécifiques pour aider au diagnostic
|
||||
if (event.error == 'synthesis-failed') {
|
||||
DebugLog.error('[TextToSpeechService] ⚠ SYNTHESIS FAILED - Common on Linux');
|
||||
DebugLog.error('[TextToSpeechService] Possible causes:');
|
||||
DebugLog.error('[TextToSpeechService] 1. speech-dispatcher not installed/running');
|
||||
DebugLog.error('[TextToSpeechService] 2. espeak or espeak-ng not installed');
|
||||
DebugLog.error('[TextToSpeechService] 3. No TTS engine configured');
|
||||
DebugLog.error('[TextToSpeechService] Fix: sudo apt-get install speech-dispatcher espeak-ng');
|
||||
DebugLog.error('[TextToSpeechService] Then restart browser');
|
||||
} else if (event.error == 'network') {
|
||||
DebugLog.error('[TextToSpeechService] Network error - online voice unavailable');
|
||||
} else if (event.error == 'audio-busy') {
|
||||
DebugLog.error('[TextToSpeechService] Audio system is busy');
|
||||
}
|
||||
}.toJS;
|
||||
|
||||
// Lire le texte
|
||||
synthesis.speak(utterance);
|
||||
DebugLog.info('[TextToSpeechService] Speech command sent');
|
||||
} catch (e) {
|
||||
DebugLog.error('[TextToSpeechService] Erreur lors de la lecture', e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Arrêter la lecture en cours
|
||||
static Future<void> stop() async {
|
||||
try {
|
||||
web.window.speechSynthesis.cancel();
|
||||
} catch (e) {
|
||||
DebugLog.error('[TextToSpeechService] Erreur lors de l\'arrêt', e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Vérifier si le service est en train de lire
|
||||
static Future<bool> isSpeaking() async {
|
||||
try {
|
||||
return web.window.speechSynthesis.speaking;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Nettoyer les ressources
|
||||
static Future<void> dispose() async {
|
||||
try {
|
||||
web.window.speechSynthesis.cancel();
|
||||
} catch (e) {
|
||||
DebugLog.error('[TextToSpeechService] Erreur lors du nettoyage', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import '../views/login_page.dart';
|
||||
import '../utils/colors.dart';
|
||||
import '../providers/local_user_provider.dart';
|
||||
|
||||
/// Gate de démarrage qui attend la restauration Firebase Auth avant
|
||||
/// d'afficher soit le contenu connecté, soit la page de connexion.
|
||||
class AppStartGate extends StatelessWidget {
|
||||
const AppStartGate({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Sur le web, certaines erreurs natives (ex: cookies tiers bloqués)
|
||||
// peuvent faire remonter une FirebaseException sur le stream d'auth.
|
||||
// Pour éviter que StreamBuilder reçoive une erreur qui casse le build
|
||||
// (TypeError JS interop), on "handleError" et on transforme l'erreur
|
||||
// en une valeur nulle (pas d'utilisateur) afin de garder l'app stable.
|
||||
// Accès protégé à `FirebaseAuth.instance` — sur le web certaines erreurs
|
||||
// d'interop JS peuvent produire des TypeError non compatibles. Nous
|
||||
// attrapons toute exception lors de l'accès et fournissons un stream
|
||||
// neutre (pas d'utilisateur) afin de garder l'UI stable.
|
||||
late final Stream<User?> safeAuthStream;
|
||||
try {
|
||||
safeAuthStream = FirebaseAuth.instance
|
||||
.authStateChanges()
|
||||
.handleError((error, stack) {
|
||||
// Log pour debug ; ne rethrow pas
|
||||
debugPrint('[AppStartGate] authStateChanges error: $error');
|
||||
});
|
||||
} catch (e, st) {
|
||||
// Sur certaines configurations web l'accès à FirebaseAuth.instance
|
||||
// peut échouer au niveau JS interop. On log puis on fournit un stream
|
||||
// qui émet une seule valeur nulle pour indiquer "pas d'utilisateur".
|
||||
debugPrint('[AppStartGate] FirebaseAuth.instance access error: $e\n$st');
|
||||
safeAuthStream = Stream<User?>.value(null);
|
||||
}
|
||||
|
||||
return StreamBuilder<User?>(
|
||||
stream: safeAuthStream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const _StartupSplashScreen();
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
// En théorie handleError évite d'arriver ici, mais on garde
|
||||
// une protection supplémentaire.
|
||||
debugPrint('[AppStartGate] snapshot error: ${snapshot.error}');
|
||||
return const _StartupSplashScreen(message: 'Erreur de connexion');
|
||||
}
|
||||
|
||||
if (snapshot.data != null) {
|
||||
return const _AuthenticatedBootstrap();
|
||||
}
|
||||
|
||||
return const LoginPage();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AuthenticatedBootstrap extends StatefulWidget {
|
||||
const _AuthenticatedBootstrap();
|
||||
|
||||
@override
|
||||
State<_AuthenticatedBootstrap> createState() =>
|
||||
_AuthenticatedBootstrapState();
|
||||
}
|
||||
|
||||
class _AuthenticatedBootstrapState extends State<_AuthenticatedBootstrap> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_redirectAfterAuth();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _redirectAfterAuth() async {
|
||||
final fragment = Uri.base.fragment;
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
// Charger les données utilisateur de façon non bloquante
|
||||
unawaited(
|
||||
context.read<LocalUserProvider>().loadUserData().catchError((e) {
|
||||
debugPrint('[AppStartGate] User data bootstrap failed: $e');
|
||||
}),
|
||||
);
|
||||
|
||||
if (fragment.isNotEmpty && fragment != '/' && fragment != '/calendar') {
|
||||
Navigator.of(context).pushReplacementNamed(fragment);
|
||||
} else {
|
||||
Navigator.of(context).pushReplacementNamed('/calendar');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const _StartupSplashScreen();
|
||||
}
|
||||
}
|
||||
|
||||
class _StartupSplashScreen extends StatelessWidget {
|
||||
final String message;
|
||||
|
||||
const _StartupSplashScreen({this.message = 'Démarrage...'});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Image.asset(
|
||||
'assets/logos/RectangleLogoBlack.png',
|
||||
width: 160,
|
||||
height: 160,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return const Icon(
|
||||
Icons.event_available,
|
||||
size: 72,
|
||||
color: AppColors.rouge,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const CircularProgressIndicator(),
|
||||
const SizedBox(height: 16),
|
||||
Text(message),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,48 @@
|
||||
import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/views/login_page.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:em2rp/views/login_page.dart';
|
||||
|
||||
class AuthGuard extends StatelessWidget {
|
||||
final Widget child;
|
||||
final String? requiredPermission;
|
||||
final bool allowWhileLoading;
|
||||
|
||||
const AuthGuard({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.requiredPermission,
|
||||
this.allowWhileLoading = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localAuthProvider = Provider.of<LocalUserProvider>(context);
|
||||
final firebaseUser = FirebaseAuth.instance.currentUser;
|
||||
|
||||
// Log pour débug
|
||||
print('[AuthGuard] Vérification accès - User: ${localAuthProvider.currentUser?.uid}, Permission requise: $requiredPermission');
|
||||
|
||||
// Si Firebase n'a pas encore restauré la session ou si le profil charge,
|
||||
// afficher un écran neutre plutôt que la page de connexion.
|
||||
if (firebaseUser != null &&
|
||||
(localAuthProvider.currentUser == null ||
|
||||
localAuthProvider.isLoadingUserData)) {
|
||||
if (allowWhileLoading) {
|
||||
return child;
|
||||
}
|
||||
|
||||
return const Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Si l'utilisateur n'est pas connecté
|
||||
if (localAuthProvider.currentUser == null) {
|
||||
if (firebaseUser == null || localAuthProvider.currentUser == null) {
|
||||
print('[AuthGuard] Utilisateur non connecté, redirection vers LoginPage');
|
||||
return const LoginPage();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// Utilitaire pour différer l'exécution d'une action après un délai.
|
||||
/// Utilisé principalement pour les champs de recherche afin d'éviter
|
||||
/// des requêtes à chaque frappe clavier.
|
||||
class Debouncer {
|
||||
final Duration delay;
|
||||
Timer? _timer;
|
||||
|
||||
Debouncer({this.delay = const Duration(milliseconds: 400)});
|
||||
|
||||
void call(VoidCallback action) {
|
||||
_timer?.cancel();
|
||||
_timer = Timer(delay, action);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Utilitaires partages pour la suppression d'equipement avec forcage.
|
||||
class EquipmentDeleteUtils {
|
||||
static const String _legacyConflictToken = 'future_event_assignment';
|
||||
static const List<String> _conflictMessageTokens = [
|
||||
'cannot delete equipment because it is assigned to upcoming events',
|
||||
'cannot delete equipment because it is assigned to future events',
|
||||
'assigned to upcoming events',
|
||||
'assigned to future events',
|
||||
];
|
||||
|
||||
static const String deleteDialogTitle = 'Confirmer la suppression';
|
||||
static const String deleteDialogCancelLabel = 'Annuler';
|
||||
static const String deleteDialogConfirmLabel = 'Supprimer';
|
||||
static const String deleteSuccessMessage = 'Équipement supprimé avec succès';
|
||||
|
||||
/// Retourne [name] si renseigne, sinon [id].
|
||||
static String resolveEquipmentLabel({required String id, String? name}) {
|
||||
final trimmedName = name?.trim();
|
||||
if (trimmedName == null || trimmedName.isEmpty) {
|
||||
return id;
|
||||
}
|
||||
return trimmedName;
|
||||
}
|
||||
|
||||
/// Construit le message de confirmation de suppression d'un equipement.
|
||||
static String buildSingleDeleteConfirmationMessage(String equipmentLabel) {
|
||||
return 'Voulez-vous vraiment supprimer "$equipmentLabel" ?\n\n'
|
||||
'Cette action est irréversible.';
|
||||
}
|
||||
|
||||
/// Construit le message de confirmation de suppression multiple.
|
||||
static String buildBulkDeleteConfirmationMessage(int selectedCount) {
|
||||
return 'Voulez-vous vraiment supprimer $selectedCount équipement(s) ?\n\n'
|
||||
'Cette action est irréversible.';
|
||||
}
|
||||
|
||||
/// Construit le message de succes de suppression multiple.
|
||||
static String buildBulkDeleteSuccessMessage(int deletedCount) {
|
||||
return '$deletedCount équipement(s) supprimé(s) avec succès';
|
||||
}
|
||||
|
||||
/// Construit un message d'erreur de suppression homogene.
|
||||
static String buildDeleteErrorMessage(Object error) {
|
||||
return 'Erreur lors de la suppression : $error';
|
||||
}
|
||||
|
||||
/// Indique si l'erreur correspond a un conflit de suppression 409.
|
||||
static bool isFutureAssignmentDeleteConflict(Object error) {
|
||||
if (error is ApiException && !error.isConflict) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final normalizedMessage = _normalizeErrorMessage(error);
|
||||
if (normalizedMessage.contains(_legacyConflictToken)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return _conflictMessageTokens.any(normalizedMessage.contains);
|
||||
}
|
||||
|
||||
/// Affiche la confirmation de suppression forcee.
|
||||
static Future<bool> showForceDeleteDialog(
|
||||
BuildContext context, {
|
||||
required String equipmentLabel,
|
||||
}) async {
|
||||
final shouldForceDelete = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('Équipement utilisé dans un événement à venir'),
|
||||
content: Text(
|
||||
'"$equipmentLabel" est assigné à au moins un événement à venir.\n\n'
|
||||
'Voulez-vous forcer la suppression ?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext, false),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext, true),
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Forcer la suppression'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
return shouldForceDelete == true;
|
||||
}
|
||||
|
||||
/// Execute une suppression, puis propose un forcage en cas de conflit 409.
|
||||
static Future<bool> deleteWithFutureAssignmentCheck({
|
||||
required BuildContext context,
|
||||
required String equipmentLabel,
|
||||
required Future<void> Function({bool forceDelete}) deleteEquipment,
|
||||
}) async {
|
||||
try {
|
||||
await deleteEquipment(forceDelete: false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (!isFutureAssignmentDeleteConflict(error)) {
|
||||
rethrow;
|
||||
}
|
||||
|
||||
if (!context.mounted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final shouldForceDelete = await showForceDeleteDialog(
|
||||
context,
|
||||
equipmentLabel: equipmentLabel,
|
||||
);
|
||||
if (!shouldForceDelete) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await deleteEquipment(forceDelete: true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static String _normalizeErrorMessage(Object error) {
|
||||
if (error is ApiException) {
|
||||
return error.message.toLowerCase();
|
||||
}
|
||||
return error.toString().toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
|
||||
class FirebaseStorageManager {
|
||||
final FirebaseStorage _storage = FirebaseStorage.instance;
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
FirebaseStorage get _storage => FirebaseStorage.instance;
|
||||
final DataService _dataService = DataService(apiService);
|
||||
|
||||
/// Upload ou remplace la photo de profil d'un utilisateur dans Firebase Storage.
|
||||
/// Pour le Web, on fixe l'extension .jpg.
|
||||
|
||||
@@ -59,7 +59,7 @@ class PerformanceMonitor {
|
||||
static void printSummary() {
|
||||
if (!_enabled || _results.isEmpty) return;
|
||||
|
||||
print('\n' + '=' * 60);
|
||||
print('\n${'=' * 60}');
|
||||
print('PERFORMANCE SUMMARY');
|
||||
print('=' * 60);
|
||||
|
||||
@@ -77,7 +77,7 @@ class PerformanceMonitor {
|
||||
Duration.zero,
|
||||
(sum, duration) => sum + duration,
|
||||
);
|
||||
print('${'=' * 60}');
|
||||
print('=' * 60);
|
||||
print('TOTAL: ${total.inMilliseconds}ms');
|
||||
print('=' * 60 + '\n');
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import '../providers/local_user_provider.dart';
|
||||
@@ -33,22 +35,17 @@ class LoginViewModel extends ChangeNotifier {
|
||||
passwordController.text,
|
||||
);
|
||||
|
||||
// --- Étape 2: Charger les données utilisateur depuis Firestore ---
|
||||
await localAuthProvider.loadUserData();
|
||||
// --- Étape 2: Charger les données utilisateur en arrière-plan ---
|
||||
unawaited(
|
||||
localAuthProvider.loadUserData().catchError((e) {
|
||||
debugPrint('Erreur chargement profil après connexion : $e');
|
||||
}),
|
||||
);
|
||||
|
||||
// Vérifier si le contexte est toujours valide
|
||||
if (context.mounted) {
|
||||
// Vérifier si l'utilisateur a bien été chargé dans le provider
|
||||
if (localAuthProvider.currentUser != null) {
|
||||
// Utiliser pushReplacementNamed pour une transition propre
|
||||
Navigator.of(context, rootNavigator: true)
|
||||
.pushReplacementNamed('/calendar');
|
||||
} else {
|
||||
errorMessage =
|
||||
'Erreur inattendue après connexion: Données utilisateur non chargées.';
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
Navigator.of(context, rootNavigator: true)
|
||||
.pushReplacementNamed('/calendar');
|
||||
}
|
||||
} on FirebaseAuthException catch (e) {
|
||||
// Gestion spécifique des erreurs d'authentification (email/mot de passe incorrects, etc.)
|
||||
|
||||
+992
-315
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,9 @@ import 'package:em2rp/providers/container_provider.dart';
|
||||
import 'package:em2rp/providers/equipment_provider.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
import 'package:em2rp/utils/id_generator.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/utils/debouncer.dart';
|
||||
|
||||
class ContainerFormPage extends StatefulWidget {
|
||||
final ContainerModel? container;
|
||||
@@ -32,7 +35,7 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
// Form fields
|
||||
ContainerType _selectedType = ContainerType.flightCase;
|
||||
EquipmentStatus _selectedStatus = EquipmentStatus.available;
|
||||
bool _autoGenerateId = true;
|
||||
final ValueNotifier<bool> _autoGenerateIdNotifier = ValueNotifier<bool>(true);
|
||||
final Set<String> _selectedEquipmentIds = {};
|
||||
|
||||
bool _isEditing = false;
|
||||
@@ -58,11 +61,11 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
_heightController.text = container.height?.toString() ?? '';
|
||||
_notesController.text = container.notes ?? '';
|
||||
_selectedEquipmentIds.addAll(container.equipmentIds);
|
||||
_autoGenerateId = false;
|
||||
_autoGenerateIdNotifier.value = false;
|
||||
}
|
||||
|
||||
void _updateIdFromName() {
|
||||
if (_autoGenerateId && !_isEditing) {
|
||||
if (_autoGenerateIdNotifier.value && !_isEditing) {
|
||||
final name = _nameController.text;
|
||||
if (name.isNotEmpty) {
|
||||
final baseId = IdGenerator.generateContainerId(
|
||||
@@ -75,7 +78,7 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
}
|
||||
|
||||
void _updateIdFromType() {
|
||||
if (_autoGenerateId && !_isEditing) {
|
||||
if (_autoGenerateIdNotifier.value && !_isEditing) {
|
||||
final name = _nameController.text;
|
||||
if (name.isNotEmpty) {
|
||||
final baseId = IdGenerator.generateContainerId(
|
||||
@@ -100,7 +103,6 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
children: [
|
||||
|
||||
// Nom
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
@@ -121,55 +123,58 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// ID
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _idController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Identifiant *',
|
||||
hintText: 'ex: FLIGHTCASE_BEAM',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.qr_code),
|
||||
ValueListenableBuilder<bool>(
|
||||
valueListenable: _autoGenerateIdNotifier,
|
||||
builder: (context, autoGenerateId, child) {
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _idController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Identifiant *',
|
||||
hintText: 'ex: FLIGHTCASE_BEAM',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.qr_code),
|
||||
),
|
||||
enabled: !autoGenerateId || _isEditing,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Veuillez entrer un identifiant';
|
||||
}
|
||||
final validation = IdGenerator.validateContainerId(value);
|
||||
return validation;
|
||||
},
|
||||
),
|
||||
),
|
||||
enabled: !_autoGenerateId || _isEditing,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Veuillez entrer un identifiant';
|
||||
}
|
||||
final validation = IdGenerator.validateContainerId(value);
|
||||
return validation;
|
||||
},
|
||||
),
|
||||
),
|
||||
if (!_isEditing) ...[
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
_autoGenerateId ? Icons.lock : Icons.lock_open,
|
||||
color: _autoGenerateId ? AppColors.rouge : Colors.grey,
|
||||
),
|
||||
tooltip: _autoGenerateId
|
||||
? 'Génération automatique'
|
||||
: 'Saisie manuelle',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_autoGenerateId = !_autoGenerateId;
|
||||
if (_autoGenerateId) {
|
||||
_updateIdFromName();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
if (!_isEditing) ...[
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
autoGenerateId ? Icons.lock : Icons.lock_open,
|
||||
color: autoGenerateId ? AppColors.rouge : Colors.grey,
|
||||
),
|
||||
tooltip: autoGenerateId
|
||||
? 'Génération automatique'
|
||||
: 'Saisie manuelle',
|
||||
onPressed: () {
|
||||
_autoGenerateIdNotifier.value = !autoGenerateId;
|
||||
if (_autoGenerateIdNotifier.value) {
|
||||
_updateIdFromName();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Type
|
||||
DropdownButtonFormField<ContainerType>(
|
||||
value: _selectedType,
|
||||
initialValue: _selectedType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Type de container *',
|
||||
border: OutlineInputBorder(),
|
||||
@@ -194,7 +199,7 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
|
||||
// Statut
|
||||
DropdownButtonFormField<EquipmentStatus>(
|
||||
value: _selectedStatus,
|
||||
initialValue: _selectedStatus,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Statut *',
|
||||
border: OutlineInputBorder(),
|
||||
@@ -257,7 +262,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.scale),
|
||||
),
|
||||
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||
keyboardType:
|
||||
const TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
if (double.tryParse(value) == null) {
|
||||
@@ -279,7 +285,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
labelText: 'Longueur (cm)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
||||
keyboardType:
|
||||
TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
if (double.tryParse(value) == null) {
|
||||
@@ -298,7 +305,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
labelText: 'Largeur (cm)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
||||
keyboardType:
|
||||
TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
if (double.tryParse(value) == null) {
|
||||
@@ -317,7 +325,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
labelText: 'Hauteur (cm)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
||||
keyboardType:
|
||||
TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
if (double.tryParse(value) == null) {
|
||||
@@ -452,6 +461,11 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
Future<void> _selectEquipment() async {
|
||||
final equipmentProvider = context.read<EquipmentProvider>();
|
||||
|
||||
// Toujours charger la liste complète pour éviter d'afficher uniquement
|
||||
// la page paginée active d'un autre écran.
|
||||
await equipmentProvider.loadEquipments();
|
||||
if (!mounted) return;
|
||||
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (context) => _EquipmentSelectorDialog(
|
||||
@@ -460,6 +474,7 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
),
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@@ -535,7 +550,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
equipmentId: equipmentId,
|
||||
);
|
||||
} catch (e) {
|
||||
DebugLog.error('Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
|
||||
DebugLog.error(
|
||||
'Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,7 +589,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
});
|
||||
|
||||
// Gérer les équipements ajoutés
|
||||
final addedEquipment = _selectedEquipmentIds.difference(container.equipmentIds.toSet());
|
||||
final addedEquipment =
|
||||
_selectedEquipmentIds.difference(container.equipmentIds.toSet());
|
||||
for (final equipmentId in addedEquipment) {
|
||||
try {
|
||||
await provider.addEquipmentToContainer(
|
||||
@@ -581,12 +598,14 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
equipmentId: equipmentId,
|
||||
);
|
||||
} catch (e) {
|
||||
DebugLog.error('Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
|
||||
DebugLog.error(
|
||||
'Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Gérer les équipements retirés
|
||||
final removedEquipment = container.equipmentIds.toSet().difference(_selectedEquipmentIds);
|
||||
final removedEquipment =
|
||||
container.equipmentIds.toSet().difference(_selectedEquipmentIds);
|
||||
for (final equipmentId in removedEquipment) {
|
||||
try {
|
||||
await provider.removeEquipmentFromContainer(
|
||||
@@ -594,7 +613,8 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
equipmentId: equipmentId,
|
||||
);
|
||||
} catch (e) {
|
||||
DebugLog.error('Erreur lors du retrait de l\'équipement $equipmentId', e);
|
||||
DebugLog.error(
|
||||
'Erreur lors du retrait de l\'équipement $equipmentId', e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -615,6 +635,7 @@ class _ContainerFormPageState extends State<ContainerFormPage> {
|
||||
_widthController.dispose();
|
||||
_heightController.dispose();
|
||||
_notesController.dispose();
|
||||
_autoGenerateIdNotifier.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -630,28 +651,94 @@ class _EquipmentSelectorDialog extends StatefulWidget {
|
||||
});
|
||||
|
||||
@override
|
||||
State<_EquipmentSelectorDialog> createState() => _EquipmentSelectorDialogState();
|
||||
State<_EquipmentSelectorDialog> createState() =>
|
||||
_EquipmentSelectorDialogState();
|
||||
}
|
||||
|
||||
class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
|
||||
EquipmentCategory? _filterCategory;
|
||||
String _searchQuery = '';
|
||||
late Set<String> _tempSelectedIds;
|
||||
final _searchDebouncer = Debouncer();
|
||||
|
||||
final List<EquipmentModel> _paginatedEquipments = [];
|
||||
bool _isLoadingMore = false;
|
||||
bool _hasMoreEquipments = true;
|
||||
String? _lastEquipmentId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Créer une copie temporaire des IDs sélectionnés
|
||||
_tempSelectedIds = Set<String>.from(widget.selectedIds);
|
||||
_scrollController.addListener(_onScroll);
|
||||
_loadNextPage();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
_scrollController.dispose();
|
||||
_searchDebouncer.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
if (_isLoadingMore) return;
|
||||
if (_scrollController.hasClients &&
|
||||
_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 300) {
|
||||
if (_hasMoreEquipments) {
|
||||
_loadNextPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadNextPage() async {
|
||||
if (_isLoadingMore || !_hasMoreEquipments) return;
|
||||
setState(() => _isLoadingMore = true);
|
||||
|
||||
try {
|
||||
final result = await _dataService.getEquipmentsPaginated(
|
||||
limit: 50,
|
||||
startAfter: _lastEquipmentId,
|
||||
searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null,
|
||||
category: _filterCategory != null ? equipmentCategoryToString(_filterCategory!) : null,
|
||||
sortBy: 'id',
|
||||
sortOrder: 'asc',
|
||||
);
|
||||
|
||||
final newEquipments = (result['equipments'] as List<dynamic>)
|
||||
.map((data) => EquipmentModel.fromMap(data as Map<String, dynamic>, data['id'] as String))
|
||||
.toList();
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_paginatedEquipments.addAll(newEquipments);
|
||||
_hasMoreEquipments = result['hasMore'] as bool? ?? false;
|
||||
_lastEquipmentId = result['lastVisible'] as String?;
|
||||
_isLoadingMore = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() => _isLoadingMore = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _reloadData() async {
|
||||
setState(() {
|
||||
_paginatedEquipments.clear();
|
||||
_lastEquipmentId = null;
|
||||
_hasMoreEquipments = true;
|
||||
});
|
||||
await _loadNextPage();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
@@ -701,6 +788,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
setState(() {
|
||||
_searchQuery = '';
|
||||
});
|
||||
_reloadData();
|
||||
},
|
||||
)
|
||||
: null,
|
||||
@@ -709,6 +797,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
setState(() {
|
||||
_searchQuery = value;
|
||||
});
|
||||
_searchDebouncer(_reloadData);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
@@ -726,10 +815,12 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
setState(() {
|
||||
_filterCategory = null;
|
||||
});
|
||||
_reloadData();
|
||||
},
|
||||
selectedColor: AppColors.rouge,
|
||||
labelStyle: TextStyle(
|
||||
color: _filterCategory == null ? Colors.white : Colors.black,
|
||||
color:
|
||||
_filterCategory == null ? Colors.white : Colors.black,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
@@ -743,10 +834,13 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
setState(() {
|
||||
_filterCategory = selected ? category : null;
|
||||
});
|
||||
_reloadData();
|
||||
},
|
||||
selectedColor: AppColors.rouge,
|
||||
labelStyle: TextStyle(
|
||||
color: _filterCategory == category ? Colors.white : Colors.black,
|
||||
color: _filterCategory == category
|
||||
? Colors.white
|
||||
: Colors.black,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -760,7 +854,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.rouge.withOpacity(0.1),
|
||||
color: AppColors.rouge.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
@@ -778,86 +872,62 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
|
||||
// Liste des équipements
|
||||
Expanded(
|
||||
child: StreamBuilder<List<EquipmentModel>>(
|
||||
stream: widget.equipmentProvider.equipmentStream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('Erreur: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
var equipment = snapshot.data ?? [];
|
||||
|
||||
// Filtrer par catégorie
|
||||
if (_filterCategory != null) {
|
||||
equipment = equipment.where((e) => e.category == _filterCategory).toList();
|
||||
}
|
||||
|
||||
// Filtrer par recherche
|
||||
if (_searchQuery.isNotEmpty) {
|
||||
final query = _searchQuery.toLowerCase();
|
||||
equipment = equipment.where((e) {
|
||||
return e.id.toLowerCase().contains(query) ||
|
||||
(e.brand?.toLowerCase().contains(query) ?? false) ||
|
||||
(e.model?.toLowerCase().contains(query) ?? false);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
if (equipment.isEmpty) {
|
||||
return const Center(
|
||||
child: Text('Aucun équipement trouvé'),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: equipment.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = equipment[index];
|
||||
final isSelected = _tempSelectedIds.contains(item.id);
|
||||
|
||||
return CheckboxListTile(
|
||||
value: isSelected,
|
||||
onChanged: (selected) {
|
||||
setState(() {
|
||||
if (selected == true) {
|
||||
_tempSelectedIds.add(item.id);
|
||||
} else {
|
||||
_tempSelectedIds.remove(item.id);
|
||||
}
|
||||
});
|
||||
},
|
||||
title: Text(
|
||||
item.id,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (item.brand != null || item.model != null)
|
||||
Text('${item.brand ?? ''} ${item.model ?? ''}'),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
_getCategoryLabel(item.category),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
child: _paginatedEquipments.isEmpty && !_isLoadingMore
|
||||
? const Center(child: Text('Aucun équipement trouvé'))
|
||||
: ListView.builder(
|
||||
controller: _scrollController,
|
||||
itemCount: _paginatedEquipments.length + (_isLoadingMore ? 1 : 0),
|
||||
itemBuilder: (context, index) {
|
||||
if (index == _paginatedEquipments.length) {
|
||||
return const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
],
|
||||
),
|
||||
secondary: Icon(
|
||||
_getCategoryIcon(item.category),
|
||||
color: AppColors.rouge,
|
||||
),
|
||||
activeColor: AppColors.rouge,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final item = _paginatedEquipments[index];
|
||||
final isSelected = _tempSelectedIds.contains(item.id);
|
||||
|
||||
return CheckboxListTile(
|
||||
value: isSelected,
|
||||
onChanged: (selected) {
|
||||
setState(() {
|
||||
if (selected == true) {
|
||||
_tempSelectedIds.add(item.id);
|
||||
} else {
|
||||
_tempSelectedIds.remove(item.id);
|
||||
}
|
||||
});
|
||||
},
|
||||
title: Text(
|
||||
item.id,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (item.brand != null || item.model != null)
|
||||
Text('${item.brand ?? ''} ${item.model ?? ''}'),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
_getCategoryLabel(item.category),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
secondary: Icon(
|
||||
_getCategoryIcon(item.category),
|
||||
color: AppColors.rouge,
|
||||
),
|
||||
activeColor: AppColors.rouge,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// Boutons d'action
|
||||
@@ -945,4 +1015,4 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
|
||||
return Icons.category;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/views/widgets/data_management/event_types_management.dart';
|
||||
import 'package:em2rp/views/widgets/data_management/options_management.dart';
|
||||
import 'package:em2rp/views/widgets/data_management/events_export.dart';
|
||||
import 'package:em2rp/views/widgets/data_management/event_statistics_tab.dart';
|
||||
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
||||
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
|
||||
import 'package:em2rp/utils/permission_gate.dart';
|
||||
@@ -32,6 +33,23 @@ class _DataManagementPageState extends State<DataManagementPage> {
|
||||
icon: Icons.file_download,
|
||||
widget: const EventsExport(),
|
||||
),
|
||||
DataCategory(
|
||||
title: 'Statistiques evenements',
|
||||
icon: Icons.bar_chart,
|
||||
widget: const PermissionGate(
|
||||
requiredPermissions: ['generate_reports'],
|
||||
fallback: Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text(
|
||||
'Vous n\'avez pas les permissions necessaires pour voir les statistiques.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: EventStatisticsTab(),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
@@ -78,7 +96,7 @@ class _DataManagementPageState extends State<DataManagementPage> {
|
||||
child: Column(
|
||||
children: [
|
||||
// Menu horizontal en mobile
|
||||
Container(
|
||||
SizedBox(
|
||||
height: 60,
|
||||
child: ListView.builder(
|
||||
scrollDirection: Axis.horizontal,
|
||||
@@ -143,7 +161,7 @@ class _DataManagementPageState extends State<DataManagementPage> {
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.rouge.withOpacity(0.1),
|
||||
color: AppColors.rouge.withValues(alpha: 0.1),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
@@ -177,7 +195,7 @@ class _DataManagementPageState extends State<DataManagementPage> {
|
||||
),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: AppColors.rouge.withOpacity(0.1),
|
||||
selectedTileColor: AppColors.rouge.withValues(alpha: 0.1),
|
||||
onTap: () => setState(() => _selectedIndex = index),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/services/equipment_service.dart';
|
||||
import 'package:em2rp/services/qr_code_service.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/utils/equipment_delete_utils.dart';
|
||||
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
|
||||
import 'package:em2rp/views/equipment_form_page.dart';
|
||||
import 'package:em2rp/views/widgets/equipment/equipment_referencing_containers.dart';
|
||||
@@ -45,7 +46,8 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
|
||||
Future<void> _loadMaintenances() async {
|
||||
try {
|
||||
final maintenances = await _equipmentService.getMaintenancesForEquipment(widget.equipment.id);
|
||||
final maintenances = await _equipmentService
|
||||
.getMaintenancesForEquipment(widget.equipment.id);
|
||||
setState(() {
|
||||
_maintenances = maintenances;
|
||||
_isLoadingMaintenances = false;
|
||||
@@ -57,8 +59,6 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final screenWidth = MediaQuery.of(context).size.width;
|
||||
@@ -103,7 +103,8 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 3. Notes
|
||||
if (widget.equipment.notes != null && widget.equipment.notes!.isNotEmpty) ...[
|
||||
if (widget.equipment.notes != null &&
|
||||
widget.equipment.notes!.isNotEmpty) ...[
|
||||
EquipmentNotesSection(notes: widget.equipment.notes!),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
@@ -185,7 +186,6 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
void _showQRCode() {
|
||||
showDialog(
|
||||
context: context,
|
||||
@@ -249,10 +249,12 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${widget.equipment.brand ?? ''} ${widget.equipment.model ?? ''}'.trim(),
|
||||
'${widget.equipment.brand ?? ''} ${widget.equipment.model ?? ''}'
|
||||
.trim(),
|
||||
style: TextStyle(color: Colors.grey[700]),
|
||||
),
|
||||
if (widget.equipment.subCategory != null && widget.equipment.subCategory!.isNotEmpty) ...[
|
||||
if (widget.equipment.subCategory != null &&
|
||||
widget.equipment.subCategory!.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'📁 ${widget.equipment.subCategory}',
|
||||
@@ -389,7 +391,8 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
if (!hasPermission) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Vous n\'avez pas la permission de gérer les maintenances'),
|
||||
content:
|
||||
Text('Vous n\'avez pas la permission de gérer les maintenances'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
@@ -423,31 +426,50 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
}
|
||||
|
||||
void _deleteEquipment() {
|
||||
final pageContext = context;
|
||||
final equipmentLabel = EquipmentDeleteUtils.resolveEquipmentLabel(
|
||||
id: widget.equipment.id,
|
||||
name: widget.equipment.name,
|
||||
);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Confirmer la suppression'),
|
||||
context: pageContext,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text(EquipmentDeleteUtils.deleteDialogTitle),
|
||||
content: Text(
|
||||
'Voulez-vous vraiment supprimer "${widget.equipment.id}" ?\n\nCette action est irréversible.',
|
||||
EquipmentDeleteUtils.buildSingleDeleteConfirmationMessage(
|
||||
equipmentLabel,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Annuler'),
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogCancelLabel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
// Fermer le dialog
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(dialogContext);
|
||||
|
||||
// Capturer le ScaffoldMessenger avant la suppression
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
final navigator = Navigator.of(context);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(pageContext);
|
||||
final navigator = Navigator.of(pageContext);
|
||||
final provider = pageContext.read<EquipmentProvider>();
|
||||
|
||||
try {
|
||||
await context
|
||||
.read<EquipmentProvider>()
|
||||
.deleteEquipment(widget.equipment.id);
|
||||
final deleted =
|
||||
await EquipmentDeleteUtils.deleteWithFutureAssignmentCheck(
|
||||
context: pageContext,
|
||||
equipmentLabel: equipmentLabel,
|
||||
deleteEquipment: ({bool forceDelete = false}) {
|
||||
return provider.deleteEquipment(
|
||||
widget.equipment.id,
|
||||
forceDelete: forceDelete,
|
||||
);
|
||||
},
|
||||
);
|
||||
if (!deleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Revenir à la page précédente
|
||||
navigator.pop();
|
||||
@@ -455,22 +477,26 @@ class _EquipmentDetailPageState extends State<EquipmentDetailPage> {
|
||||
// Afficher le snackbar (même si le widget est démonté)
|
||||
scaffoldMessenger.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Équipement supprimé avec succès'),
|
||||
content: Text(EquipmentDeleteUtils.deleteSuccessMessage),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
// Afficher l'erreur
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(content: Text('Erreur: $e')),
|
||||
SnackBar(
|
||||
content: Text(
|
||||
EquipmentDeleteUtils.buildDeleteErrorMessage(e),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Supprimer'),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogConfirmLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,12 @@ class _EquipmentFormPageState extends State<EquipmentFormPage> {
|
||||
final provider = Provider.of<EquipmentProvider>(context, listen: false);
|
||||
provider.loadBrands();
|
||||
provider.loadModels();
|
||||
if (widget.equipment != null) {
|
||||
if (_selectedBrand != null && _selectedBrand!.isNotEmpty) {
|
||||
_loadFilteredModels(_selectedBrand!);
|
||||
}
|
||||
_loadFilteredSubCategories(_selectedCategory);
|
||||
}
|
||||
});
|
||||
if (widget.equipment != null) {
|
||||
_populateFields();
|
||||
@@ -84,14 +90,6 @@ class _EquipmentFormPageState extends State<EquipmentFormPage> {
|
||||
});
|
||||
|
||||
DebugLog.info('[EquipmentForm] Populating fields for equipment: ${equipment.id}');
|
||||
|
||||
|
||||
if (_selectedBrand != null && _selectedBrand!.isNotEmpty) {
|
||||
_loadFilteredModels(_selectedBrand!);
|
||||
}
|
||||
|
||||
// Charger les sous-catégories pour la catégorie sélectionnée
|
||||
_loadFilteredSubCategories(_selectedCategory);
|
||||
}
|
||||
|
||||
|
||||
@@ -163,11 +161,11 @@ class _EquipmentFormPageState extends State<EquipmentFormPage> {
|
||||
TextFormField(
|
||||
controller: _identifierController,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Identifiant *',
|
||||
labelText: 'Identifiant (Laissez vide pour auto-génération) *',
|
||||
border: const OutlineInputBorder(),
|
||||
prefixIcon: const Icon(Icons.tag),
|
||||
hintText: isEditing ? null : 'Laissez vide pour générer automatiquement',
|
||||
helperText: isEditing ? 'Non modifiable' : 'Format auto: {Marque4Chars}_{Modèle}',
|
||||
hintText: isEditing ? null : 'Auto-attribué par défaut',
|
||||
helperText: isEditing ? 'Non modifiable' : 'Génération auto recommandée basée sur Marque/Modèle',
|
||||
),
|
||||
enabled: !isEditing,
|
||||
validator: (value) {
|
||||
@@ -271,7 +269,7 @@ class _EquipmentFormPageState extends State<EquipmentFormPage> {
|
||||
children: [
|
||||
Expanded(
|
||||
child: DropdownButtonFormField<EquipmentCategory>(
|
||||
value: _selectedCategory,
|
||||
initialValue: _selectedCategory,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Catégorie *',
|
||||
border: OutlineInputBorder(),
|
||||
@@ -299,7 +297,7 @@ class _EquipmentFormPageState extends State<EquipmentFormPage> {
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: DropdownButtonFormField<EquipmentStatus>(
|
||||
value: _selectedStatus,
|
||||
initialValue: _selectedStatus,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Statut *',
|
||||
border: OutlineInputBorder(),
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
||||
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
|
||||
import 'package:em2rp/providers/equipment_provider.dart';
|
||||
import 'package:em2rp/providers/container_provider.dart';
|
||||
import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/models/equipment_model.dart';
|
||||
import 'package:em2rp/models/container_model.dart';
|
||||
import 'package:em2rp/views/equipment_form_page.dart';
|
||||
@@ -16,9 +17,11 @@ import 'package:em2rp/views/widgets/common/qr_code_scanner_dialog.dart';
|
||||
import 'package:em2rp/views/widgets/common/qr_code_format_selector_dialog.dart';
|
||||
import 'package:em2rp/views/widgets/equipment/equipment_status_badge.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
import 'package:em2rp/utils/equipment_delete_utils.dart';
|
||||
import 'package:em2rp/mixins/selection_mode_mixin.dart';
|
||||
import 'package:em2rp/views/widgets/common/search_actions_bar.dart';
|
||||
import 'package:em2rp/views/widgets/notification_badge.dart';
|
||||
import 'package:em2rp/utils/debouncer.dart';
|
||||
|
||||
class EquipmentManagementPage extends StatefulWidget {
|
||||
const EquipmentManagementPage({super.key});
|
||||
@@ -28,11 +31,11 @@ class EquipmentManagementPage extends StatefulWidget {
|
||||
_EquipmentManagementPageState();
|
||||
}
|
||||
|
||||
|
||||
class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
with SelectionModeMixin<EquipmentManagementPage> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final _searchDebouncer = Debouncer();
|
||||
EquipmentCategory? _selectedCategory;
|
||||
List<EquipmentModel>? _cachedEquipment;
|
||||
bool _isLoadingMore = false; // Flag pour éviter les appels multiples
|
||||
@@ -66,7 +69,6 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
if (_scrollController.hasClients &&
|
||||
_scrollController.position.pixels >=
|
||||
_scrollController.position.maxScrollExtent - 300) {
|
||||
|
||||
// Vérifier qu'on peut charger plus
|
||||
if (provider.hasMore && !provider.isLoadingMore) {
|
||||
// ✅ Pas de setState ici pour éviter les rebuilds pendant le scroll
|
||||
@@ -76,7 +78,8 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
_isLoadingMore = false;
|
||||
}).catchError((error) {
|
||||
_isLoadingMore = false;
|
||||
DebugLog.error('[EquipmentManagementPage] Error loading next page', error);
|
||||
DebugLog.error(
|
||||
'[EquipmentManagementPage] Error loading next page', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -87,6 +90,7 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
_scrollController.removeListener(_onScroll);
|
||||
_scrollController.dispose();
|
||||
_searchController.dispose();
|
||||
_searchDebouncer.dispose();
|
||||
// Désactiver le mode pagination en quittant
|
||||
context.read<EquipmentProvider>().disablePagination();
|
||||
super.dispose();
|
||||
@@ -140,7 +144,7 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
],
|
||||
],
|
||||
)
|
||||
: CustomAppBar(
|
||||
: const CustomAppBar(
|
||||
title: 'Gestion du matériel',
|
||||
),
|
||||
drawer: const MainDrawer(currentPage: '/equipment_management'),
|
||||
@@ -169,9 +173,7 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
SearchActionsBar(
|
||||
controller: _searchController,
|
||||
hintText: 'Rechercher par nom, modèle ou ID...',
|
||||
onChanged: (value) {
|
||||
context.read<EquipmentProvider>().setSearchQuery(value);
|
||||
},
|
||||
onChanged: (value) => _searchDebouncer(() => context.read<EquipmentProvider>().setSearchQuery(value)),
|
||||
onClear: () {
|
||||
_searchController.clear();
|
||||
context.read<EquipmentProvider>().setSearchQuery('');
|
||||
@@ -342,9 +344,7 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
SearchActionsBar(
|
||||
controller: _searchController,
|
||||
hintText: 'Rechercher par nom, modèle ou ID...',
|
||||
onChanged: (value) {
|
||||
context.read<EquipmentProvider>().setSearchQuery(value);
|
||||
},
|
||||
onChanged: (value) => _searchDebouncer(() => context.read<EquipmentProvider>().setSearchQuery(value)),
|
||||
onClear: () {
|
||||
_searchController.clear();
|
||||
context.read<EquipmentProvider>().setSearchQuery('');
|
||||
@@ -456,11 +456,13 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
Widget _buildEquipmentList() {
|
||||
return Consumer<EquipmentProvider>(
|
||||
builder: (context, provider, child) {
|
||||
DebugLog.info('[EquipmentManagementPage] Building list - isLoading: ${provider.isLoading}, equipment count: ${provider.equipment.length}');
|
||||
DebugLog.info(
|
||||
'[EquipmentManagementPage] Building list - isLoading: ${provider.isLoading}, equipment count: ${provider.equipment.length}');
|
||||
|
||||
// Afficher l'indicateur de chargement initial uniquement
|
||||
if (provider.isLoading && provider.equipment.isEmpty) {
|
||||
DebugLog.info('[EquipmentManagementPage] Showing initial loading indicator');
|
||||
DebugLog.info(
|
||||
'[EquipmentManagementPage] Showing initial loading indicator');
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
@@ -490,7 +492,8 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
);
|
||||
}
|
||||
|
||||
DebugLog.info('[EquipmentManagementPage] Building list with ${equipments.length} items');
|
||||
DebugLog.info(
|
||||
'[EquipmentManagementPage] Building list with ${equipments.length} items');
|
||||
|
||||
// Calculer le nombre total d'items (équipements + indicateur de chargement)
|
||||
final itemCount = equipments.length + (provider.hasMore ? 1 : 0);
|
||||
@@ -498,10 +501,7 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
itemCount: itemCount,
|
||||
// ✅ Ajouter une estimation de la hauteur pour améliorer le scroll
|
||||
// Note : À ajuster selon la hauteur réelle de vos cartes
|
||||
// itemExtent: 140, // Décommentez si toutes les cartes ont la même hauteur
|
||||
// ✅ Augmenter le cache pour un scroll plus fluide
|
||||
// ✅ Augmenter le cache pour un scroll plus fluide (prototypeItem retiré car les hauteurs dynamiques varient selon le type d'équipement)
|
||||
cacheExtent: 500, // Précharger 500px en plus
|
||||
itemBuilder: (context, index) {
|
||||
// Dernier élément = indicateur de chargement
|
||||
@@ -528,121 +528,192 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
return RepaintBoundary(
|
||||
key: ValueKey(equipment.id),
|
||||
child: Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
margin: const EdgeInsets.only(bottom: 12, left: 16, right: 16),
|
||||
elevation: 1,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: isSelectionMode && isSelected
|
||||
? AppColors.rouge
|
||||
: Colors.grey.shade200,
|
||||
width: isSelectionMode && isSelected ? 2 : 1,
|
||||
),
|
||||
),
|
||||
color: isSelectionMode && isSelected
|
||||
? AppColors.rouge.withValues(alpha: 0.1)
|
||||
: null,
|
||||
child: ListTile(
|
||||
leading: isSelectionMode
|
||||
? Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (value) => toggleItemSelection(equipment.id),
|
||||
activeColor: AppColors.rouge,
|
||||
)
|
||||
: CircleAvatar(
|
||||
backgroundColor: equipment.category.color.withValues(alpha: 0.2),
|
||||
child: equipment.category.getIcon(
|
||||
size: 20,
|
||||
color: equipment.category.color,
|
||||
),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
equipment.id,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
// Afficher le badge de statut calculé dynamiquement
|
||||
if (equipment.category != EquipmentCategory.consumable &&
|
||||
equipment.category != EquipmentCategory.cable)
|
||||
EquipmentStatusBadge(equipment: equipment),
|
||||
],
|
||||
),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${equipment.brand ?? ''} ${equipment.model ?? ''}'
|
||||
.trim()
|
||||
.isNotEmpty
|
||||
? '${equipment.brand ?? ''} ${equipment.model ?? ''}'.trim()
|
||||
: 'Marque/Modèle non défini',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||
),
|
||||
// Afficher la sous-catégorie si elle existe
|
||||
if (equipment.subCategory != null && equipment.subCategory!.isNotEmpty) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'📁 ${equipment.subCategory}',
|
||||
style: TextStyle(
|
||||
color: Colors.grey[500],
|
||||
fontSize: 12,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
],
|
||||
// Afficher la quantité disponible pour les consommables/câbles
|
||||
if (equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable) ...[
|
||||
const SizedBox(height: 4),
|
||||
_buildQuantityDisplay(equipment),
|
||||
],
|
||||
],
|
||||
),
|
||||
trailing: isSelectionMode
|
||||
? AppColors.rouge.withValues(alpha: 0.05)
|
||||
: Colors.white,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onTap: isSelectionMode
|
||||
? () => toggleItemSelection(equipment.id)
|
||||
: () => _viewEquipmentDetails(equipment),
|
||||
onLongPress: isSelectionMode
|
||||
? null
|
||||
: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Bouton Restock (uniquement pour consommables/câbles avec permission)
|
||||
if (equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.add_shopping_cart,
|
||||
color: AppColors.rouge),
|
||||
tooltip: 'Restock',
|
||||
onPressed: () => _showRestockDialog(equipment),
|
||||
: () {
|
||||
final localUserProvider = Provider.of<LocalUserProvider>(context, listen: false);
|
||||
if (localUserProvider.hasPermission('manage_equipment')) {
|
||||
_editEquipment(equipment);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text("Vous n'avez pas la permission de modifier cet équipement"),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// 1. leading selection or icon
|
||||
if (isSelectionMode)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 12),
|
||||
child: Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (value) => toggleItemSelection(equipment.id),
|
||||
activeColor: AppColors.rouge,
|
||||
),
|
||||
)
|
||||
else
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: equipment.category.color.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Center(
|
||||
child: equipment.category.getIcon(
|
||||
size: 22,
|
||||
color: equipment.category.color,
|
||||
),
|
||||
),
|
||||
),
|
||||
// Bouton QR Code
|
||||
IconButton(
|
||||
icon: const Icon(Icons.qr_code, color: AppColors.rouge),
|
||||
tooltip: 'QR Code',
|
||||
onPressed: () => showDialog(
|
||||
context: context,
|
||||
builder: (context) => QRCodeDialog.forEquipment(equipment),
|
||||
),
|
||||
),
|
||||
// Bouton Modifier (permission required)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.edit, color: AppColors.rouge),
|
||||
tooltip: 'Modifier',
|
||||
onPressed: () => _editEquipment(equipment),
|
||||
),
|
||||
|
||||
// 2. Info details (ID, Brand/Model, Subcategory)
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
equipment.id,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 15,
|
||||
color: AppColors.noir,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${equipment.brand ?? ''} ${equipment.model ?? ''}'
|
||||
.trim()
|
||||
.isNotEmpty
|
||||
? '${equipment.brand ?? ''} ${equipment.model ?? ''}'.trim()
|
||||
: 'Marque/Modèle non défini',
|
||||
style: TextStyle(
|
||||
color: Colors.grey[700],
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
// Sous-catégorie
|
||||
if (equipment.subCategory != null &&
|
||||
equipment.subCategory!.isNotEmpty) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'📁 ${equipment.subCategory}',
|
||||
style: TextStyle(
|
||||
color: Colors.grey[500],
|
||||
fontSize: 11,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
// Bouton Supprimer (permission required)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.delete, color: Colors.red),
|
||||
tooltip: 'Supprimer',
|
||||
onPressed: () => _deleteEquipment(equipment),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 16),
|
||||
|
||||
// 3. Status Badge OR Quantity Display (centered vertically in the row!)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: (equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable)
|
||||
? _buildQuantityDisplay(equipment)
|
||||
: EquipmentStatusBadge(equipment: equipment),
|
||||
),
|
||||
|
||||
// 4. Trailing Action Buttons
|
||||
if (!isSelectionMode)
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Bouton Restock (uniquement pour consommables/câbles avec permission)
|
||||
if (equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.add_shopping_cart,
|
||||
color: AppColors.rouge, size: 20),
|
||||
tooltip: 'Restock',
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
onPressed: () => _showRestockDialog(equipment),
|
||||
),
|
||||
),
|
||||
if (equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable)
|
||||
const SizedBox(width: 8),
|
||||
// Bouton QR Code
|
||||
IconButton(
|
||||
icon: const Icon(Icons.qr_code, color: AppColors.rouge, size: 20),
|
||||
tooltip: 'QR Code',
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
onPressed: () => showDialog(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
QRCodeDialog.forEquipment(equipment),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// Bouton Modifier (permission required)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.edit, color: AppColors.rouge, size: 20),
|
||||
tooltip: 'Modifier',
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
onPressed: () => _editEquipment(equipment),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// Bouton Supprimer (permission required)
|
||||
PermissionGate(
|
||||
requiredPermissions: const ['manage_equipment'],
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.delete, color: Colors.red, size: 20),
|
||||
tooltip: 'Supprimer',
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
onPressed: () => _deleteEquipment(equipment),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: isSelectionMode
|
||||
? () => toggleItemSelection(equipment.id)
|
||||
: () => _viewEquipmentDetails(equipment),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -652,60 +723,26 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
final criticalThreshold = equipment.criticalThreshold ?? 0;
|
||||
final isCritical =
|
||||
criticalThreshold > 0 && availableQty <= criticalThreshold;
|
||||
final color = isCritical ? Colors.red : Colors.grey.shade600;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: isCritical
|
||||
? Colors.red.withOpacity(0.15)
|
||||
: Colors.grey.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: isCritical ? Colors.red : Colors.grey.shade400,
|
||||
width: isCritical ? 2 : 1,
|
||||
),
|
||||
color: color.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: color),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
isCritical ? Icons.warning : Icons.inventory,
|
||||
size: 16,
|
||||
color: isCritical ? Colors.red : Colors.grey[700],
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
'Disponible: $availableQty / $totalQty',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: isCritical ? FontWeight.bold : FontWeight.normal,
|
||||
color: isCritical ? Colors.red : Colors.grey[700],
|
||||
),
|
||||
),
|
||||
if (isCritical) ...[
|
||||
const SizedBox(width: 6),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: const Text(
|
||||
'CRITIQUE',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
child: Text(
|
||||
'$availableQty / $totalQty',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Actions
|
||||
void _createNewEquipment() {
|
||||
Navigator.push(
|
||||
@@ -726,39 +763,64 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
}
|
||||
|
||||
void _deleteEquipment(EquipmentModel equipment) {
|
||||
final pageContext = context;
|
||||
final equipmentLabel = EquipmentDeleteUtils.resolveEquipmentLabel(
|
||||
id: equipment.id,
|
||||
name: equipment.name,
|
||||
);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Confirmer la suppression'),
|
||||
content: Text('Voulez-vous vraiment supprimer "${equipment.name}" ?'),
|
||||
context: pageContext,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text(EquipmentDeleteUtils.deleteDialogTitle),
|
||||
content: Text(
|
||||
EquipmentDeleteUtils.buildSingleDeleteConfirmationMessage(
|
||||
equipmentLabel,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Annuler'),
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogCancelLabel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(dialogContext);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(pageContext);
|
||||
final provider = pageContext.read<EquipmentProvider>();
|
||||
|
||||
try {
|
||||
await context
|
||||
.read<EquipmentProvider>()
|
||||
.deleteEquipment(equipment.id);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Équipement supprimé avec succès')),
|
||||
);
|
||||
final deleted =
|
||||
await EquipmentDeleteUtils.deleteWithFutureAssignmentCheck(
|
||||
context: pageContext,
|
||||
equipmentLabel: equipmentLabel,
|
||||
deleteEquipment: ({bool forceDelete = false}) {
|
||||
return provider.deleteEquipment(
|
||||
equipment.id,
|
||||
forceDelete: forceDelete,
|
||||
);
|
||||
},
|
||||
);
|
||||
if (!deleted) {
|
||||
return;
|
||||
}
|
||||
scaffoldMessenger.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(EquipmentDeleteUtils.deleteSuccessMessage),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Erreur: $e')),
|
||||
);
|
||||
}
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
EquipmentDeleteUtils.buildDeleteErrorMessage(e),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Supprimer'),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogConfirmLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -768,46 +830,78 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
void _deleteSelectedEquipment() async {
|
||||
if (!hasSelection) return;
|
||||
|
||||
final pageContext = context;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Confirmer la suppression'),
|
||||
context: pageContext,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text(EquipmentDeleteUtils.deleteDialogTitle),
|
||||
content: Text(
|
||||
'Voulez-vous vraiment supprimer $selectedCount équipement(s) ?',
|
||||
EquipmentDeleteUtils.buildBulkDeleteConfirmationMessage(
|
||||
selectedCount,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Annuler'),
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogCancelLabel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(dialogContext);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(pageContext);
|
||||
final provider = pageContext.read<EquipmentProvider>();
|
||||
|
||||
try {
|
||||
final provider = context.read<EquipmentProvider>();
|
||||
final equipmentById = {
|
||||
for (final equipment
|
||||
in provider.equipment)
|
||||
equipment.id: equipment,
|
||||
};
|
||||
|
||||
var deletedCount = 0;
|
||||
for (final id in selectedIds) {
|
||||
await provider.deleteEquipment(id);
|
||||
final label = EquipmentDeleteUtils.resolveEquipmentLabel(
|
||||
id: id,
|
||||
name: equipmentById[id]?.name,
|
||||
);
|
||||
final deleted = await EquipmentDeleteUtils
|
||||
.deleteWithFutureAssignmentCheck(
|
||||
context: pageContext,
|
||||
equipmentLabel: label,
|
||||
deleteEquipment: ({bool forceDelete = false}) {
|
||||
return provider.deleteEquipment(
|
||||
id,
|
||||
forceDelete: forceDelete,
|
||||
);
|
||||
},
|
||||
);
|
||||
if (deleted) {
|
||||
deletedCount++;
|
||||
}
|
||||
}
|
||||
disableSelectionMode();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'$selectedCount équipement(s) supprimé(s) avec succès'),
|
||||
backgroundColor: Colors.green,
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
EquipmentDeleteUtils.buildBulkDeleteSuccessMessage(
|
||||
deletedCount,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Erreur: $e')),
|
||||
);
|
||||
}
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
EquipmentDeleteUtils.buildDeleteErrorMessage(e),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Supprimer'),
|
||||
child: const Text(EquipmentDeleteUtils.deleteDialogConfirmLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -853,7 +947,8 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
if (mounted) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => QRCodeDialog.forEquipment(selectedEquipment.first),
|
||||
builder: (context) =>
|
||||
QRCodeDialog.forEquipment(selectedEquipment.first),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
@@ -1046,7 +1141,9 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await context.read<EquipmentProvider>().updateEquipment(updatedEquipment);
|
||||
await context
|
||||
.read<EquipmentProvider>()
|
||||
.updateEquipment(updatedEquipment);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -1184,7 +1281,8 @@ class _EquipmentManagementPageState extends State<EquipmentManagementPage>
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Aucun équipement ou conteneur trouvé avec l\'ID : $scannedCode'),
|
||||
content: Text(
|
||||
'Aucun équipement ou conteneur trouvé avec l\'ID : $scannedCode'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -77,7 +77,8 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
return;
|
||||
}
|
||||
|
||||
final success = await _controller.submitForm(context, existingEvent: widget.event);
|
||||
final success =
|
||||
await _controller.submitForm(context, existingEvent: widget.event);
|
||||
if (success && mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
@@ -158,21 +159,25 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(isEditMode ? 'Modifier un événement' : 'Créer un événement'),
|
||||
title: Text(
|
||||
isEditMode ? 'Modifier un événement' : 'Créer un événement'),
|
||||
),
|
||||
body: Center(
|
||||
child: SingleChildScrollView(
|
||||
child: (isMobile
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 12),
|
||||
child: _buildFormContent(isMobile),
|
||||
)
|
||||
: Card(
|
||||
elevation: 6,
|
||||
margin: const EdgeInsets.all(24),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(18)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 32),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 32, vertical: 32),
|
||||
child: _buildFormContent(isMobile),
|
||||
),
|
||||
)),
|
||||
@@ -186,15 +191,6 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
Widget _buildFormContent(bool isMobile) {
|
||||
return Consumer<EventFormController>(
|
||||
builder: (context, controller, child) {
|
||||
// Trouver le nom du type d'événement pour le passer au sélecteur d'options
|
||||
final selectedEventTypeIndex = controller.selectedEventTypeId != null
|
||||
? controller.eventTypes.indexWhere((et) => et.id == controller.selectedEventTypeId)
|
||||
: -1;
|
||||
final selectedEventType = selectedEventTypeIndex != -1
|
||||
? controller.eventTypes[selectedEventTypeIndex]
|
||||
: null;
|
||||
final selectedEventTypeName = selectedEventType?.name;
|
||||
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
@@ -209,18 +205,22 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
selectedEventTypeId: controller.selectedEventTypeId,
|
||||
startDateTime: controller.startDateTime,
|
||||
endDateTime: controller.endDateTime,
|
||||
onEventTypeChanged: (typeId) => controller.onEventTypeChanged(typeId, context),
|
||||
onEventTypeChanged: (typeId) =>
|
||||
controller.onEventTypeChanged(typeId, context),
|
||||
onStartDateTimeChanged: controller.setStartDateTime,
|
||||
onEndDateTimeChanged: controller.setEndDateTime,
|
||||
onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur
|
||||
onAnyFieldChanged:
|
||||
() {}, // Géré automatiquement par le contrôleur
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
OptionSelectorWidget(
|
||||
eventType: controller.selectedEventTypeId, // Utilise l'ID au lieu du nom
|
||||
eventType: controller
|
||||
.selectedEventTypeId, // Utilise l'ID au lieu du nom
|
||||
selectedOptions: controller.selectedOptions,
|
||||
onChanged: controller.setSelectedOptions,
|
||||
onRemove: (optionId) {
|
||||
final newOptions = List<Map<String, dynamic>>.from(controller.selectedOptions);
|
||||
final newOptions = List<Map<String, dynamic>>.from(
|
||||
controller.selectedOptions);
|
||||
newOptions.removeWhere((o) => o['id'] == optionId);
|
||||
controller.setSelectedOptions(newOptions);
|
||||
},
|
||||
@@ -236,6 +236,7 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
endDate: controller.endDateTime,
|
||||
onChanged: controller.setAssignedEquipment,
|
||||
eventId: widget.event?.id,
|
||||
eventTypeId: controller.selectedEventTypeId,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
EventDetailsSection(
|
||||
@@ -247,7 +248,8 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
contactEmailController: controller.contactEmailController,
|
||||
contactPhoneController: controller.contactPhoneController,
|
||||
isMobile: isMobile,
|
||||
onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur
|
||||
onAnyFieldChanged:
|
||||
() {}, // Géré automatiquement par le contrôleur
|
||||
),
|
||||
EventStaffAndDocumentsSection(
|
||||
allUsers: controller.allUsers,
|
||||
@@ -290,9 +292,10 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
}
|
||||
},
|
||||
onSubmit: _submit,
|
||||
onSetConfirmed: !isEditMode ? () {
|
||||
} : null,
|
||||
onDelete: isEditMode ? _deleteEvent : null, // Ajout du callback de suppression
|
||||
onSetConfirmed: !isEditMode ? () {} : null,
|
||||
onDelete: isEditMode
|
||||
? _deleteEvent
|
||||
: null, // Ajout du callback de suppression
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -11,8 +11,9 @@ import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/services/qr_code_processing_service.dart';
|
||||
import 'package:em2rp/services/audio_feedback_service.dart';
|
||||
import 'package:em2rp/services/text_to_speech_service.dart';
|
||||
import 'package:em2rp/services/smart_text_to_speech_service.dart';
|
||||
import 'package:em2rp/services/equipment_service.dart';
|
||||
import 'package:em2rp/services/event_preparation_service.dart';
|
||||
import 'package:em2rp/views/widgets/equipment/equipment_checklist_item.dart' show EquipmentChecklistItem, ChecklistStep;
|
||||
import 'package:em2rp/views/widgets/equipment/container_checklist_item.dart';
|
||||
import 'package:em2rp/views/widgets/common/qr_code_scanner_dialog.dart';
|
||||
@@ -49,18 +50,18 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
late final DataService _dataService;
|
||||
late final QRCodeProcessingService _qrCodeService;
|
||||
|
||||
Map<String, EquipmentModel> _equipmentCache = {};
|
||||
Map<String, ContainerModel> _containerCache = {};
|
||||
Map<String, int> _returnedQuantities = {};
|
||||
final Map<String, EquipmentModel> _equipmentCache = {};
|
||||
final Map<String, ContainerModel> _containerCache = {};
|
||||
final Map<String, int> _returnedQuantities = {};
|
||||
|
||||
// État local des validations (non sauvegardé jusqu'à la validation finale)
|
||||
Map<String, bool> _localValidationState = {};
|
||||
final Map<String, bool> _localValidationState = {};
|
||||
|
||||
// Gestion des quantités par étape
|
||||
Map<String, int> _quantitiesAtPreparation = {};
|
||||
Map<String, int> _quantitiesAtLoading = {};
|
||||
Map<String, int> _quantitiesAtUnloading = {};
|
||||
Map<String, int> _quantitiesAtReturn = {};
|
||||
final Map<String, int> _quantitiesAtPreparation = {};
|
||||
final Map<String, int> _quantitiesAtLoading = {};
|
||||
final Map<String, int> _quantitiesAtUnloading = {};
|
||||
final Map<String, int> _quantitiesAtReturn = {};
|
||||
|
||||
bool _isLoading = true;
|
||||
bool _isValidating = false;
|
||||
@@ -121,8 +122,8 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
duration: const Duration(milliseconds: 500),
|
||||
);
|
||||
|
||||
// Initialiser le service de synthèse vocale
|
||||
TextToSpeechService.initialize();
|
||||
// Initialiser le service de synthèse vocale hybride
|
||||
SmartTextToSpeechService.initialize();
|
||||
|
||||
// Initialiser et débloquer l'audio (pour éviter les problèmes d'autoplay)
|
||||
AudioFeedbackService.unlockAudio();
|
||||
@@ -164,7 +165,7 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
_animationController.dispose();
|
||||
_manualCodeController.dispose();
|
||||
_manualCodeFocusNode.dispose();
|
||||
TextToSpeechService.stop();
|
||||
SmartTextToSpeechService.stop();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -1097,6 +1098,10 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
|
||||
/// Détermine le statut d'un équipement selon l'étape actuelle
|
||||
String _determineEquipmentStatus(EventEquipment eq) {
|
||||
if (_isNotTakenToEventAtReturn(eq)) {
|
||||
return 'NOT_TAKEN';
|
||||
}
|
||||
|
||||
// Vérifier d'abord si l'équipement est perdu (LOST)
|
||||
if (_shouldMarkAsLost(eq)) {
|
||||
return 'LOST';
|
||||
@@ -1118,14 +1123,31 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
|
||||
/// Vérifie si un équipement doit être marqué comme LOST
|
||||
bool _shouldMarkAsLost(EventEquipment eq) {
|
||||
// Seulement aux étapes de retour
|
||||
if (_currentStep != PreparationStep.return_ &&
|
||||
!(_currentStep == PreparationStep.unloadingReturn && _loadSimultaneously)) {
|
||||
return EventPreparationService.shouldMarkEquipmentAsLost(
|
||||
isReturnValidationStep: _isReturnValidationStep,
|
||||
isMissingAtReturn: eq.isMissingAtReturn,
|
||||
isLoaded: eq.isLoaded,
|
||||
isMissingAtLoading: eq.isMissingAtLoading,
|
||||
quantityAtLoading: eq.quantityAtLoading,
|
||||
);
|
||||
}
|
||||
|
||||
bool _isNotTakenToEventAtReturn(EventEquipment eq) {
|
||||
if (!_isReturnValidationStep) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si manquant maintenant mais PAS manquant à la préparation = LOST
|
||||
return eq.isMissingAtReturn && !eq.isMissingAtPreparation;
|
||||
return EventPreparationService.isEquipmentNotTakenToEvent(
|
||||
isMissingAtReturn: eq.isMissingAtReturn,
|
||||
isLoaded: eq.isLoaded,
|
||||
isMissingAtLoading: eq.isMissingAtLoading,
|
||||
quantityAtLoading: eq.quantityAtLoading,
|
||||
);
|
||||
}
|
||||
|
||||
bool get _isReturnValidationStep {
|
||||
return _currentStep == PreparationStep.return_ ||
|
||||
(_currentStep == PreparationStep.unloadingReturn && _loadSimultaneously);
|
||||
}
|
||||
|
||||
/// Vérifie si un équipement est manquant à l'étape actuelle
|
||||
@@ -1212,9 +1234,9 @@ class _EventPreparationPageState extends State<EventPreparationPage> with Single
|
||||
Future<void> _announceNextItem() async {
|
||||
final nextItem = _findNextItemToScan();
|
||||
if (nextItem != null) {
|
||||
await TextToSpeechService.speak('Prochain item: $nextItem');
|
||||
await SmartTextToSpeechService.speak('Prochain item: $nextItem');
|
||||
} else {
|
||||
await TextToSpeechService.speak('Tous les items sont validés');
|
||||
await SmartTextToSpeechService.speak('Tous les items sont validés');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:em2rp/utils/permission_gate.dart';
|
||||
import 'package:em2rp/views/widgets/data_management/event_statistics_tab.dart';
|
||||
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
|
||||
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class EventStatisticsPage extends StatelessWidget {
|
||||
const EventStatisticsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PermissionGate(
|
||||
requiredPermissions: const ['generate_reports'],
|
||||
fallback: const Scaffold(
|
||||
appBar: CustomAppBar(title: 'Acces refuse'),
|
||||
body: Center(
|
||||
child: Text(
|
||||
'Vous n\'avez pas les permissions necessaires pour acceder aux statistiques.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: const Scaffold(
|
||||
appBar: CustomAppBar(title: 'Statistiques evenements'),
|
||||
drawer: MainDrawer(currentPage: '/event_statistics'),
|
||||
body: EventStatisticsTab(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,14 +75,15 @@ class _MaintenanceManagementPageState extends State<MaintenanceManagementPage> {
|
||||
title: 'Gestion des maintenances',
|
||||
),
|
||||
drawer: const MainDrawer(currentPage: '/maintenance_management'),
|
||||
body: Consumer<MaintenanceProvider>(
|
||||
builder: (context, maintenanceProvider, _) {
|
||||
if (maintenanceProvider.isLoading) {
|
||||
body: Selector<MaintenanceProvider, ({bool isLoading, List<MaintenanceModel> maintenances})>(
|
||||
selector: (context, provider) => (isLoading: provider.isLoading, maintenances: provider.maintenances),
|
||||
builder: (context, data, _) {
|
||||
if (data.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final filteredMaintenances = _getFilteredMaintenances(
|
||||
maintenanceProvider.maintenances,
|
||||
data.maintenances,
|
||||
);
|
||||
|
||||
return Column(
|
||||
@@ -91,7 +92,7 @@ class _MaintenanceManagementPageState extends State<MaintenanceManagementPage> {
|
||||
_buildFilterChips(),
|
||||
|
||||
// Statistiques
|
||||
_buildStatsCards(maintenanceProvider),
|
||||
_buildStatsCards(data.maintenances),
|
||||
|
||||
// Liste des maintenances
|
||||
Expanded(
|
||||
@@ -148,10 +149,10 @@ class _MaintenanceManagementPageState extends State<MaintenanceManagementPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatsCards(MaintenanceProvider provider) {
|
||||
final upcoming = provider.maintenances.where((m) => !m.isCompleted && !m.isOverdue).length;
|
||||
final overdue = provider.maintenances.where((m) => m.isOverdue).length;
|
||||
final completed = provider.maintenances.where((m) => m.isCompleted).length;
|
||||
Widget _buildStatsCards(List<MaintenanceModel> maintenances) {
|
||||
final upcoming = maintenances.where((m) => !m.isCompleted && !m.isOverdue).length;
|
||||
final overdue = maintenances.where((m) => m.isOverdue).length;
|
||||
final completed = maintenances.where((m) => m.isCompleted).length;
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:em2rp/models/user_model.dart';
|
||||
import 'package:em2rp/views/widgets/inputs/styled_text_field.dart';
|
||||
import 'package:em2rp/views/widgets/image/profile_picture_selector.dart';
|
||||
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
|
||||
@@ -17,10 +18,9 @@ class MyAccountPage extends StatelessWidget {
|
||||
title: 'Mon compte',
|
||||
),
|
||||
drawer: const MainDrawer(currentPage: '/my_account'),
|
||||
body: Consumer<LocalUserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
|
||||
body: Selector<LocalUserProvider, UserModel?>(
|
||||
selector: (context, provider) => provider.currentUser,
|
||||
builder: (context, user, child) {
|
||||
if (user == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
@@ -73,7 +73,7 @@ class MyAccountPage extends StatelessWidget {
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
userProvider.updateUserData(
|
||||
context.read<LocalUserProvider>().updateUserData(
|
||||
firstName: firstNameController.text,
|
||||
lastName: lastNameController.text,
|
||||
phoneNumber: phoneController.text,
|
||||
|
||||
@@ -51,12 +51,13 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
||||
title: 'Gestion des utilisateurs',
|
||||
),
|
||||
drawer: const MainDrawer(currentPage: '/account_management'),
|
||||
body: Consumer<UsersProvider>(
|
||||
builder: (context, usersProvider, child) {
|
||||
if (usersProvider.isLoading) {
|
||||
body: Selector<UsersProvider, ({bool isLoading, List<UserModel> users})>(
|
||||
selector: (context, provider) => (isLoading: provider.isLoading, users: provider.users),
|
||||
builder: (context, data, child) {
|
||||
if (data.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final users = usersProvider.users;
|
||||
final users = data.users;
|
||||
if (users.isEmpty) {
|
||||
return const Center(child: Text("Aucun utilisateur trouvé"));
|
||||
}
|
||||
@@ -92,7 +93,7 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
||||
context: context,
|
||||
builder: (_) => EditUserDialog(user: user)),
|
||||
onResetPassword: () => _resetPassword(context, user),
|
||||
onDelete: () => _confirmDeleteUser(context, usersProvider, user),
|
||||
onDelete: () => _confirmDeleteUser(context, context.read<UsersProvider>(), user),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
+1
-1
@@ -133,7 +133,7 @@ class EventDetailsDocuments extends StatelessWidget {
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
child: SizedBox(
|
||||
width: MediaQuery.of(context).size.width * 0.9,
|
||||
height: MediaQuery.of(context).size.height * 0.8,
|
||||
child: Column(
|
||||
|
||||
+9
-7
@@ -7,8 +7,9 @@ import 'package:em2rp/views/event_add_page.dart';
|
||||
import 'package:em2rp/services/ics_export_service.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'dart:html' as html;
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'dart:convert';
|
||||
import 'dart:js_interop';
|
||||
|
||||
class EventDetailsHeader extends StatefulWidget {
|
||||
final EventModel event;
|
||||
@@ -180,12 +181,13 @@ class _EventDetailsHeaderState extends State<EventDetailsHeader> {
|
||||
|
||||
// Créer un blob et télécharger le fichier
|
||||
final bytes = utf8.encode(icsContent);
|
||||
final blob = html.Blob([bytes], 'text/calendar');
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
html.AnchorElement(href: url)
|
||||
..setAttribute('download', fileName)
|
||||
..click();
|
||||
html.Url.revokeObjectUrl(url);
|
||||
final blob = web.Blob([bytes.toJS].toJS, web.BlobPropertyBag(type: 'text/calendar'));
|
||||
final url = web.URL.createObjectURL(blob);
|
||||
final anchor = web.document.createElement('a') as web.HTMLAnchorElement;
|
||||
anchor.href = url;
|
||||
anchor.download = fileName;
|
||||
anchor.click();
|
||||
web.URL.revokeObjectURL(url);
|
||||
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
|
||||
+10
@@ -26,6 +26,16 @@ class _EventStatusButtonState extends State<EventStatusButton> {
|
||||
EventStatus? _optimisticStatus;
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
|
||||
@override
|
||||
void didUpdateWidget(EventStatusButton oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
// Réinitialiser le statut optimiste si on affiche un nouvel événement
|
||||
if (oldWidget.event.id != widget.event.id) {
|
||||
_optimisticStatus = null;
|
||||
_loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _changeStatus(EventStatus newStatus) async {
|
||||
if ((widget.event.status == newStatus) || _loading) return;
|
||||
setState(() {
|
||||
|
||||
@@ -5,6 +5,11 @@ import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/utils/calendar_utils.dart';
|
||||
|
||||
class MonthView extends StatelessWidget {
|
||||
static const double _calendarPadding = 8.0;
|
||||
static const double _headerHeight = 52.0;
|
||||
static const double _headerVerticalPadding = 16.0;
|
||||
static const double _daysOfWeekHeight = 16.0;
|
||||
|
||||
final DateTime focusedDay;
|
||||
final DateTime? selectedDay;
|
||||
final CalendarFormat calendarFormat;
|
||||
@@ -30,11 +35,17 @@ class MonthView extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final rowHeight = (constraints.maxHeight - 100) / 6;
|
||||
final rowCount = _computeRowCount(focusedDay);
|
||||
final availableHeight = constraints.maxHeight -
|
||||
(_calendarPadding * 2) -
|
||||
_headerHeight -
|
||||
_headerVerticalPadding -
|
||||
_daysOfWeekHeight;
|
||||
final rowHeight = availableHeight / rowCount;
|
||||
|
||||
return Container(
|
||||
height: constraints.maxHeight,
|
||||
padding: const EdgeInsets.all(8),
|
||||
padding: const EdgeInsets.all(_calendarPadding),
|
||||
child: TableCalendar(
|
||||
firstDay: DateTime.utc(2020, 1, 1),
|
||||
lastDay: DateTime.utc(2030, 12, 31),
|
||||
@@ -42,6 +53,7 @@ class MonthView extends StatelessWidget {
|
||||
calendarFormat: calendarFormat,
|
||||
startingDayOfWeek: StartingDayOfWeek.monday,
|
||||
locale: 'fr_FR',
|
||||
daysOfWeekHeight: _daysOfWeekHeight,
|
||||
availableCalendarFormats: const {
|
||||
CalendarFormat.month: 'Mois',
|
||||
CalendarFormat.week: 'Semaine',
|
||||
@@ -132,10 +144,9 @@ class MonthView extends StatelessWidget {
|
||||
|
||||
Widget _buildDayCell(DateTime day, bool isSelected, {bool isToday = false}) {
|
||||
final dayEvents = CalendarUtils.getEventsForDay(day, events);
|
||||
final statusCounts = _getStatusCounts(dayEvents);
|
||||
final textColor =
|
||||
isSelected ? Colors.white : (isToday ? AppColors.rouge : null);
|
||||
final badgeColor = isSelected ? Colors.white : AppColors.rouge;
|
||||
final badgeTextColor = isSelected ? AppColors.rouge : Colors.white;
|
||||
|
||||
BoxDecoration decoration;
|
||||
if (isSelected) {
|
||||
@@ -161,56 +172,125 @@ class MonthView extends StatelessWidget {
|
||||
return Container(
|
||||
margin: const EdgeInsets.all(4),
|
||||
decoration: decoration,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
top: 4,
|
||||
left: 4,
|
||||
child: Text(
|
||||
day.day.toString(),
|
||||
style: TextStyle(color: textColor),
|
||||
),
|
||||
),
|
||||
if (dayEvents.isNotEmpty)
|
||||
Positioned(
|
||||
top: 4,
|
||||
right: 4,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: badgeColor,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
day.day.toString(),
|
||||
style: TextStyle(color: textColor),
|
||||
),
|
||||
child: Text(
|
||||
dayEvents.length.toString(),
|
||||
style: TextStyle(
|
||||
color: badgeTextColor,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.topRight,
|
||||
child: Wrap(
|
||||
spacing: 4,
|
||||
runSpacing: 2,
|
||||
alignment: WrapAlignment.end,
|
||||
children: _buildStatusBadges(statusCounts),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (dayEvents.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: dayEvents
|
||||
.map((event) => _buildEventItem(event, isSelected, day))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (dayEvents.isNotEmpty)
|
||||
Positioned(
|
||||
bottom: 2,
|
||||
left: 2,
|
||||
right: 2,
|
||||
top: 28,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: dayEvents
|
||||
.map((event) => _buildEventItem(event, isSelected, day))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Map<EventStatus, int> _getStatusCounts(List<EventModel> dayEvents) {
|
||||
final counts = <EventStatus, int>{
|
||||
EventStatus.confirmed: 0,
|
||||
EventStatus.waitingForApproval: 0,
|
||||
EventStatus.canceled: 0,
|
||||
};
|
||||
|
||||
for (final event in dayEvents) {
|
||||
counts[event.status] = (counts[event.status] ?? 0) + 1;
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
List<Widget> _buildStatusBadges(Map<EventStatus, int> statusCounts) {
|
||||
final badges = <Widget>[];
|
||||
|
||||
void addBadge({
|
||||
required EventStatus status,
|
||||
required Color backgroundColor,
|
||||
required Color textColor,
|
||||
required String tooltipLabel,
|
||||
}) {
|
||||
final count = statusCounts[status] ?? 0;
|
||||
if (count <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
badges.add(
|
||||
Tooltip(
|
||||
message: '$count $tooltipLabel',
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: backgroundColor,
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
),
|
||||
child: Text(
|
||||
count.toString(),
|
||||
style: TextStyle(
|
||||
color: textColor,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
addBadge(
|
||||
status: EventStatus.confirmed,
|
||||
backgroundColor: Colors.green,
|
||||
textColor: Colors.white,
|
||||
tooltipLabel:
|
||||
'validé${(statusCounts[EventStatus.confirmed] ?? 0) > 1 ? 's' : ''}',
|
||||
);
|
||||
addBadge(
|
||||
status: EventStatus.waitingForApproval,
|
||||
backgroundColor: Colors.amber,
|
||||
textColor: Colors.black,
|
||||
tooltipLabel: 'en attente',
|
||||
);
|
||||
addBadge(
|
||||
status: EventStatus.canceled,
|
||||
backgroundColor: Colors.red,
|
||||
textColor: Colors.white,
|
||||
tooltipLabel:
|
||||
'annulé${(statusCounts[EventStatus.canceled] ?? 0) > 1 ? 's' : ''}',
|
||||
);
|
||||
|
||||
return badges;
|
||||
}
|
||||
|
||||
Widget _buildEventItem(
|
||||
EventModel event, bool isSelected, DateTime currentDay) {
|
||||
Color color;
|
||||
@@ -228,7 +308,6 @@ class MonthView extends StatelessWidget {
|
||||
icon = Icons.close;
|
||||
break;
|
||||
case EventStatus.waitingForApproval:
|
||||
default:
|
||||
color = Colors.amber;
|
||||
textColor = Colors.black;
|
||||
icon = Icons.hourglass_empty;
|
||||
@@ -243,7 +322,8 @@ class MonthView extends StatelessWidget {
|
||||
margin: const EdgeInsets.only(bottom: 2),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected ? color.withAlpha(220) : color.withOpacity(0.18),
|
||||
color:
|
||||
isSelected ? color.withAlpha(220) : color.withValues(alpha: 0.18),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
@@ -282,4 +362,13 @@ class MonthView extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Calcule le nombre de rangées affichées pour le mois de [focusedDay]
|
||||
/// (calendrier commençant le lundi : offset = weekday - 1)
|
||||
int _computeRowCount(DateTime focusedDay) {
|
||||
final firstOfMonth = DateTime(focusedDay.year, focusedDay.month, 1);
|
||||
final daysInMonth = DateTime(focusedDay.year, focusedDay.month + 1, 0).day;
|
||||
final offset = (firstOfMonth.weekday - 1) % 7; // 0 = lundi, 6 = dimanche
|
||||
return ((daysInMonth + offset) / 7).ceil();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ class _UserFilterDropdownState extends State<UserFilterDropdown> {
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:em2rp/services/audio_feedback_service.dart';
|
||||
import 'package:em2rp/services/text_to_speech_service.dart';
|
||||
import 'package:em2rp/services/smart_text_to_speech_service.dart';
|
||||
import 'package:em2rp/utils/debug_log.dart';
|
||||
|
||||
/// Bouton de diagnostic pour tester l'audio et le TTS
|
||||
@@ -59,11 +59,11 @@ class AudioDiagnosticButton extends StatelessWidget {
|
||||
DebugLog.info('[AudioDiagnostic] Platform: ${web.window.navigator.platform}');
|
||||
DebugLog.info('[AudioDiagnostic] Language: ${web.window.navigator.language}');
|
||||
|
||||
await TextToSpeechService.initialize();
|
||||
await SmartTextToSpeechService.initialize();
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
DebugLog.info('[AudioDiagnostic] Speaking test phrase...');
|
||||
await TextToSpeechService.speak('Test de synthèse vocale. Un, deux, trois.');
|
||||
await SmartTextToSpeechService.speak('Test de synthèse vocale. Un, deux, trois.');
|
||||
|
||||
DebugLog.info('[AudioDiagnostic] ========== TTS TEST END ==========');
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
class StartupSplashScreen extends StatelessWidget {
|
||||
final String message;
|
||||
const StartupSplashScreen({super.key, this.message = 'Démarrage...'});
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Image.asset(
|
||||
'assets/logos/RectangleLogoBlack.png',
|
||||
width: 160,
|
||||
height: 160,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return const Icon(
|
||||
Icons.event_available,
|
||||
size: 72,
|
||||
color: AppColors.rouge,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppColors.rouge),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
message,
|
||||
style: const TextStyle(
|
||||
color: AppColors.noir,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,715 @@
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/models/event_statistics_models.dart';
|
||||
import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/event_statistics_service.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
enum _AmountDisplayMode { ht, ttc }
|
||||
|
||||
enum _DatePreset { currentMonth, previousMonth, currentYear, previousYear }
|
||||
|
||||
class EventStatisticsTab extends StatefulWidget {
|
||||
const EventStatisticsTab({super.key});
|
||||
|
||||
@override
|
||||
State<EventStatisticsTab> createState() => _EventStatisticsTabState();
|
||||
}
|
||||
|
||||
class _EventStatisticsTabState extends State<EventStatisticsTab> {
|
||||
final DataService _dataService = DataService(FirebaseFunctionsApiService());
|
||||
final EventStatisticsService _statisticsService =
|
||||
const EventStatisticsService();
|
||||
|
||||
final NumberFormat _currencyFormat =
|
||||
NumberFormat.currency(locale: 'fr_FR', symbol: 'EUR ');
|
||||
final NumberFormat _percentFormat = NumberFormat.percentPattern('fr_FR');
|
||||
|
||||
DateTimeRange _selectedPeriod = _initialPeriod();
|
||||
final Set<String> _selectedEventTypeIds = {};
|
||||
final Set<EventStatus> _selectedStatuses = {
|
||||
EventStatus.confirmed,
|
||||
EventStatus.waitingForApproval,
|
||||
};
|
||||
_AmountDisplayMode _amountDisplayMode = _AmountDisplayMode.ht;
|
||||
|
||||
bool _isLoading = true;
|
||||
String? _errorMessage;
|
||||
|
||||
List<EventModel> _events = [];
|
||||
Map<String, String> _eventTypeNames = {};
|
||||
EventStatisticsSummary _summary = EventStatisticsSummary.empty;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadStatistics();
|
||||
}
|
||||
|
||||
static DateTimeRange _initialPeriod() {
|
||||
final now = DateTime.now();
|
||||
return DateTimeRange(
|
||||
start: DateTime(now.year, now.month, 1),
|
||||
end: DateTime(now.year, now.month + 1, 0, 23, 59, 59),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _loadStatistics() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final localUserProvider =
|
||||
Provider.of<LocalUserProvider>(context, listen: false);
|
||||
final userId = localUserProvider.uid;
|
||||
|
||||
final results = await Future.wait([
|
||||
_dataService.getEvents(userId: userId),
|
||||
_dataService.getEventTypes(),
|
||||
]);
|
||||
|
||||
final eventsResult = results[0] as Map<String, dynamic>;
|
||||
final eventTypesResult = results[1] as List<Map<String, dynamic>>;
|
||||
final eventsData = eventsResult['events'] as List<Map<String, dynamic>>;
|
||||
|
||||
final parsedEvents = <EventModel>[];
|
||||
for (final eventData in eventsData) {
|
||||
try {
|
||||
parsedEvents
|
||||
.add(EventModel.fromMap(eventData, eventData['id'] as String));
|
||||
} catch (_) {
|
||||
// Ignore malformed rows and continue to keep the dashboard available.
|
||||
}
|
||||
}
|
||||
|
||||
final eventTypeNames = <String, String>{};
|
||||
for (final eventType in eventTypesResult) {
|
||||
final id = (eventType['id'] ?? '').toString();
|
||||
if (id.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
eventTypeNames[id] = (eventType['name'] ?? id).toString();
|
||||
}
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_events = parsedEvents;
|
||||
_eventTypeNames = eventTypeNames;
|
||||
_isLoading = false;
|
||||
});
|
||||
|
||||
_rebuildSummary();
|
||||
} catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_errorMessage = 'Erreur lors du chargement des statistiques: $error';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _rebuildSummary() {
|
||||
final filter = EventStatisticsFilter(
|
||||
period: _selectedPeriod,
|
||||
eventTypeIds: _selectedEventTypeIds,
|
||||
includeCanceled: _selectedStatuses.contains(EventStatus.canceled),
|
||||
selectedStatuses: _selectedStatuses,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_summary = _statisticsService.buildSummary(
|
||||
events: _events,
|
||||
filter: filter,
|
||||
eventTypeNames: _eventTypeNames,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _selectDateRange() async {
|
||||
final selectedRange = await showDateRangePicker(
|
||||
context: context,
|
||||
firstDate: DateTime(2020),
|
||||
lastDate: DateTime(2035),
|
||||
initialDateRange: _selectedPeriod,
|
||||
locale: const Locale('fr', 'FR'),
|
||||
builder: (context, child) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: ColorScheme.light(
|
||||
primary: AppColors.rouge,
|
||||
onPrimary: Colors.white,
|
||||
),
|
||||
),
|
||||
child: child ?? const SizedBox.shrink(),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (selectedRange == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_selectedPeriod = DateTimeRange(
|
||||
start: selectedRange.start,
|
||||
end: DateTime(
|
||||
selectedRange.end.year,
|
||||
selectedRange.end.month,
|
||||
selectedRange.end.day,
|
||||
23,
|
||||
59,
|
||||
59,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
_rebuildSummary();
|
||||
}
|
||||
|
||||
void _resetFilters() {
|
||||
setState(() {
|
||||
_selectedPeriod = _initialPeriod();
|
||||
_selectedEventTypeIds.clear();
|
||||
_selectedStatuses.clear();
|
||||
_selectedStatuses.addAll({
|
||||
EventStatus.confirmed,
|
||||
EventStatus.waitingForApproval,
|
||||
});
|
||||
_amountDisplayMode = _AmountDisplayMode.ht;
|
||||
});
|
||||
_rebuildSummary();
|
||||
}
|
||||
|
||||
String _formatCurrency(double value) => _currencyFormat.format(value);
|
||||
|
||||
String _formatPercent(double value) => _percentFormat.format(value);
|
||||
|
||||
String get _amountUnitLabel =>
|
||||
_amountDisplayMode == _AmountDisplayMode.ht ? 'HT' : 'TTC';
|
||||
|
||||
double _toDisplayAmount(double htAmount) {
|
||||
if (_amountDisplayMode == _AmountDisplayMode.ttc) {
|
||||
return htAmount * 1.2;
|
||||
}
|
||||
return htAmount;
|
||||
}
|
||||
|
||||
String _formatAmount(double htAmount) =>
|
||||
_formatCurrency(_toDisplayAmount(htAmount));
|
||||
|
||||
String _presetLabel(_DatePreset preset) {
|
||||
switch (preset) {
|
||||
case _DatePreset.currentMonth:
|
||||
return 'Ce mois-ci';
|
||||
case _DatePreset.previousMonth:
|
||||
return 'Mois dernier';
|
||||
case _DatePreset.currentYear:
|
||||
return 'Cette année';
|
||||
case _DatePreset.previousYear:
|
||||
return 'Année dernière';
|
||||
}
|
||||
}
|
||||
|
||||
DateTimeRange _rangeForMonth(int year, int month) {
|
||||
return DateTimeRange(
|
||||
start: DateTime(year, month, 1),
|
||||
end: DateTime(year, month + 1, 0, 23, 59, 59),
|
||||
);
|
||||
}
|
||||
|
||||
DateTimeRange _rangeForYear(int year) {
|
||||
return DateTimeRange(
|
||||
start: DateTime(year, 1, 1),
|
||||
end: DateTime(year, 12, 31, 23, 59, 59),
|
||||
);
|
||||
}
|
||||
|
||||
DateTimeRange _rangeForPreset(_DatePreset preset) {
|
||||
final now = DateTime.now();
|
||||
|
||||
switch (preset) {
|
||||
case _DatePreset.currentMonth:
|
||||
return _rangeForMonth(now.year, now.month);
|
||||
case _DatePreset.previousMonth:
|
||||
return _rangeForMonth(now.year, now.month - 1);
|
||||
case _DatePreset.currentYear:
|
||||
return _rangeForYear(now.year);
|
||||
case _DatePreset.previousYear:
|
||||
return _rangeForYear(now.year - 1);
|
||||
}
|
||||
}
|
||||
|
||||
bool _isPresetSelected(_DatePreset preset) {
|
||||
final presetRange = _rangeForPreset(preset);
|
||||
return _selectedPeriod.start == presetRange.start &&
|
||||
_selectedPeriod.end == presetRange.end;
|
||||
}
|
||||
|
||||
void _applyDatePreset(_DatePreset preset) {
|
||||
setState(() {
|
||||
_selectedPeriod = _rangeForPreset(preset);
|
||||
});
|
||||
_rebuildSummary();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (_errorMessage != null) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_errorMessage!,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _loadStatistics,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _loadStatistics,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
_buildFiltersCard(),
|
||||
const SizedBox(height: 16),
|
||||
_buildSummaryCards(),
|
||||
const SizedBox(height: 16),
|
||||
_buildByTypeSection(),
|
||||
const SizedBox(height: 16),
|
||||
_buildTopOptionsSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFiltersCard() {
|
||||
final dateFormat = DateFormat('dd/MM/yyyy');
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.filter_alt, color: AppColors.rouge),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Filtres',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
ToggleButtons(
|
||||
isSelected: [
|
||||
_amountDisplayMode == _AmountDisplayMode.ht,
|
||||
_amountDisplayMode == _AmountDisplayMode.ttc,
|
||||
],
|
||||
onPressed: (index) {
|
||||
setState(() {
|
||||
_amountDisplayMode = index == 0
|
||||
? _AmountDisplayMode.ht
|
||||
: _AmountDisplayMode.ttc;
|
||||
});
|
||||
},
|
||||
children: const [
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Text('HT'),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Text('TTC'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
TextButton.icon(
|
||||
onPressed: _resetFilters,
|
||||
icon: const Icon(Icons.restart_alt),
|
||||
label: const Text('Réinitialiser'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed: _selectDateRange,
|
||||
icon: const Icon(Icons.date_range),
|
||||
label: Text(
|
||||
'${dateFormat.format(_selectedPeriod.start)} - ${dateFormat.format(_selectedPeriod.end)}',
|
||||
),
|
||||
),
|
||||
..._DatePreset.values.map(
|
||||
(preset) => ChoiceChip(
|
||||
label: Text(_presetLabel(preset)),
|
||||
selected: _isPresetSelected(preset),
|
||||
onSelected: (_) => _applyDatePreset(preset),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'Statuts d\'événements',
|
||||
style: TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
FilterChip(
|
||||
label: const Text('Validés'),
|
||||
selected: _selectedStatuses.contains(EventStatus.confirmed),
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
if (value) {
|
||||
_selectedStatuses.add(EventStatus.confirmed);
|
||||
} else {
|
||||
_selectedStatuses.remove(EventStatus.confirmed);
|
||||
}
|
||||
});
|
||||
_rebuildSummary();
|
||||
},
|
||||
),
|
||||
FilterChip(
|
||||
label: const Text('En attente'),
|
||||
selected: _selectedStatuses.contains(EventStatus.waitingForApproval),
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
if (value) {
|
||||
_selectedStatuses.add(EventStatus.waitingForApproval);
|
||||
} else {
|
||||
_selectedStatuses.remove(EventStatus.waitingForApproval);
|
||||
}
|
||||
});
|
||||
_rebuildSummary();
|
||||
},
|
||||
),
|
||||
FilterChip(
|
||||
label: const Text('Annulés'),
|
||||
selected: _selectedStatuses.contains(EventStatus.canceled),
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
if (value) {
|
||||
_selectedStatuses.add(EventStatus.canceled);
|
||||
} else {
|
||||
_selectedStatuses.remove(EventStatus.canceled);
|
||||
}
|
||||
});
|
||||
_rebuildSummary();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_eventTypeNames.isNotEmpty) ...[
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'Types d\'événements',
|
||||
style: TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: _eventTypeNames.entries.map((entry) {
|
||||
final selected = _selectedEventTypeIds.contains(entry.key);
|
||||
return FilterChip(
|
||||
label: Text(entry.value),
|
||||
selected: selected,
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
if (value) {
|
||||
_selectedEventTypeIds.add(entry.key);
|
||||
} else {
|
||||
_selectedEventTypeIds.remove(entry.key);
|
||||
}
|
||||
});
|
||||
_rebuildSummary();
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSummaryCards() {
|
||||
final metrics = <_MetricConfig>[
|
||||
_MetricConfig(
|
||||
title: 'Total événements',
|
||||
value: _summary.totalEvents.toString(),
|
||||
subtitle: 'Sur la période sélectionnée',
|
||||
icon: Icons.event,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Montant total',
|
||||
value: _formatAmount(_summary.totalAmount),
|
||||
subtitle: 'Base + options ($_amountUnitLabel)',
|
||||
icon: Icons.account_balance_wallet,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Montant validé',
|
||||
value: _formatAmount(_summary.validatedAmount),
|
||||
subtitle: '${_summary.validatedEvents} événement(s) ($_amountUnitLabel)',
|
||||
icon: Icons.verified,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Montant non validé',
|
||||
value: _formatAmount(_summary.pendingAmount),
|
||||
subtitle: '${_summary.pendingEvents} événement(s) ($_amountUnitLabel)',
|
||||
icon: Icons.hourglass_top,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Montant annulé',
|
||||
value: _formatAmount(_summary.canceledAmount),
|
||||
subtitle: '${_summary.canceledEvents} événement(s) ($_amountUnitLabel)',
|
||||
icon: Icons.cancel,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Panier moyen',
|
||||
value: _formatAmount(_summary.averageAmount),
|
||||
subtitle: 'Par événement ($_amountUnitLabel)',
|
||||
icon: Icons.trending_up,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Panier médian',
|
||||
value: _formatAmount(_summary.medianAmount),
|
||||
subtitle: 'Par événement ($_amountUnitLabel)',
|
||||
icon: Icons.timeline,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Pourcentage de validation',
|
||||
value: _formatPercent(_summary.validationRate),
|
||||
subtitle:
|
||||
'${_summary.validatedEvents} validés sur ${_summary.totalEvents}',
|
||||
icon: Icons.pie_chart,
|
||||
),
|
||||
_MetricConfig(
|
||||
title: 'Base vs options',
|
||||
value:
|
||||
'${_formatPercent(_summary.baseContributionRate)} / ${_formatPercent(_summary.optionsContributionRate)}',
|
||||
subtitle:
|
||||
'Base: ${_formatAmount(_summary.baseAmount)} - Options: ${_formatAmount(_summary.optionsAmount)}',
|
||||
icon: Icons.stacked_bar_chart,
|
||||
),
|
||||
];
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'KPI période',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: metrics
|
||||
.map(
|
||||
(metric) => _buildMetricCard(
|
||||
title: metric.title,
|
||||
value: metric.value,
|
||||
subtitle: metric.subtitle,
|
||||
icon: metric.icon,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMetricCard({
|
||||
required String title,
|
||||
required String value,
|
||||
required String subtitle,
|
||||
required IconData icon,
|
||||
}) {
|
||||
return SizedBox(
|
||||
width: 280,
|
||||
child: Card(
|
||||
elevation: 1,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(14),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, color: AppColors.rouge),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
title,
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
value,
|
||||
style:
|
||||
const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
subtitle,
|
||||
style: TextStyle(color: Colors.grey.shade700),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildByTypeSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Répartition par type d\'événement',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (_summary.byEventType.isEmpty)
|
||||
const Text(
|
||||
'Aucune donnée pour la période et les filtres sélectionnés.')
|
||||
else
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: DataTable(
|
||||
columns: [
|
||||
const DataColumn(label: Text('Type')),
|
||||
const DataColumn(label: Text('Nb')),
|
||||
const DataColumn(label: Text('Validé')),
|
||||
const DataColumn(label: Text('Non validé')),
|
||||
const DataColumn(label: Text('Annulé')),
|
||||
DataColumn(label: Text('Total $_amountUnitLabel')),
|
||||
],
|
||||
rows: _summary.byEventType
|
||||
.map(
|
||||
(row) => DataRow(
|
||||
cells: [
|
||||
DataCell(Text(row.eventTypeName)),
|
||||
DataCell(Text(row.totalEvents.toString())),
|
||||
DataCell(Text(_formatAmount(row.validatedAmount))),
|
||||
DataCell(Text(_formatAmount(row.pendingAmount))),
|
||||
DataCell(Text(_formatAmount(row.canceledAmount))),
|
||||
DataCell(Text(_formatAmount(row.totalAmount))),
|
||||
],
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTopOptionsSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Top options',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (_summary.topOptions.isEmpty)
|
||||
const Text('Aucune option valorisée sur la période sélectionnée.')
|
||||
else
|
||||
..._summary.topOptions.map(
|
||||
(option) => ListTile(
|
||||
dense: true,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: const Icon(Icons.add_chart, color: AppColors.rouge),
|
||||
title: Text(option.optionLabel),
|
||||
subtitle: Text(
|
||||
'Validées ${option.validatedUsageCount} fois',
|
||||
),
|
||||
trailing: Text(
|
||||
_formatAmount(option.totalAmount),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MetricConfig {
|
||||
final String title;
|
||||
final String value;
|
||||
final String subtitle;
|
||||
final IconData icon;
|
||||
|
||||
const _MetricConfig({
|
||||
required this.title,
|
||||
required this.value,
|
||||
required this.subtitle,
|
||||
required this.icon,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ class _EquipmentConflictDialogState extends State<EquipmentConflictDialog> {
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
}),
|
||||
|
||||
// Boutons d'action par équipement
|
||||
if (!isRemoved)
|
||||
|
||||
@@ -3,10 +3,11 @@ import 'package:flutter/material.dart';
|
||||
import 'package:em2rp/models/equipment_model.dart';
|
||||
import 'package:em2rp/models/container_model.dart';
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/services/event_availability_service.dart';
|
||||
import 'package:em2rp/services/data_service.dart';
|
||||
import 'package:em2rp/services/api_service.dart';
|
||||
import 'package:em2rp/services/event_availability_service.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/utils/debouncer.dart';
|
||||
|
||||
/// Type de sélection dans le dialog
|
||||
enum SelectionType { equipment, container }
|
||||
@@ -35,7 +36,7 @@ class ContainerConflictInfo {
|
||||
if (status == ContainerConflictStatus.complete) {
|
||||
return 'Tous les équipements sont déjà utilisés';
|
||||
}
|
||||
return '${conflictingEquipmentIds.length}/${totalChildren} équipement(s) déjà utilisé(s)';
|
||||
return '${conflictingEquipmentIds.length}/$totalChildren équipement(s) déjà utilisé(s)';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,18 +88,17 @@ class EquipmentSelectionDialog extends StatefulWidget {
|
||||
class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController(); // Préserve la position de scroll
|
||||
final EventAvailabilityService _availabilityService = EventAvailabilityService();
|
||||
final DataService _dataService = DataService(apiService);
|
||||
|
||||
EquipmentCategory? _selectedCategory;
|
||||
|
||||
Map<String, SelectedItem> _selectedItems = {};
|
||||
final ValueNotifier<int> _selectionChangeNotifier = ValueNotifier<int>(0); // Pour notifier les changements de sélection sans setState
|
||||
Map<String, int> _availableQuantities = {}; // Pour consommables
|
||||
Map<String, List<ContainerModel>> _recommendedContainers = {}; // Recommandations
|
||||
Map<String, List<AvailabilityConflict>> _equipmentConflicts = {}; // Conflits de disponibilité (détaillés)
|
||||
Map<String, ContainerConflictInfo> _containerConflicts = {}; // Conflits des conteneurs
|
||||
Set<String> _expandedContainers = {}; // Conteneurs dépliés dans la liste
|
||||
final Map<String, int> _availableQuantities = {}; // Pour consommables
|
||||
final Map<String, List<ContainerModel>> _recommendedContainers = {}; // Recommandations
|
||||
final Map<String, List<AvailabilityConflict>> _equipmentConflicts = {}; // Conflits de disponibilité (détaillés)
|
||||
final Map<String, ContainerConflictInfo> _containerConflicts = {}; // Conflits des conteneurs
|
||||
final Set<String> _expandedContainers = {}; // Conteneurs dépliés dans la liste
|
||||
|
||||
// NOUVEAU : IDs en conflit récupérés en batch
|
||||
Set<String> _conflictingEquipmentIds = {};
|
||||
@@ -108,6 +108,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
|
||||
bool _isLoadingConflicts = false;
|
||||
String _searchQuery = '';
|
||||
final _searchDebouncer = Debouncer();
|
||||
|
||||
// Nouvelles options d'affichage
|
||||
bool _showConflictingItems = false; // Afficher les équipements/boîtes en conflit
|
||||
@@ -119,12 +120,12 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
bool _hasMoreContainers = true;
|
||||
String? _lastEquipmentId;
|
||||
String? _lastContainerId;
|
||||
List<EquipmentModel> _paginatedEquipments = [];
|
||||
List<ContainerModel> _paginatedContainers = [];
|
||||
final List<EquipmentModel> _paginatedEquipments = [];
|
||||
final List<ContainerModel> _paginatedContainers = [];
|
||||
|
||||
// Cache pour éviter les rebuilds inutiles
|
||||
List<ContainerModel> _cachedContainers = [];
|
||||
List<EquipmentModel> _cachedEquipment = [];
|
||||
final List<ContainerModel> _cachedContainers = [];
|
||||
final List<EquipmentModel> _cachedEquipment = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -172,7 +173,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialise la sélection avec le matériel déjà assigné
|
||||
/// Initialise la slection avec le matriel dj assign
|
||||
Future<void> _initializeAlreadyAssigned() async {
|
||||
final Map<String, SelectedItem> initialSelection = {};
|
||||
|
||||
@@ -250,7 +251,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
|
||||
try {
|
||||
final result = await _dataService.getEquipmentsPaginated(
|
||||
limit: 25,
|
||||
limit: 50,
|
||||
startAfter: _lastEquipmentId,
|
||||
searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null,
|
||||
category: _selectedCategory != null ? equipmentCategoryToString(_selectedCategory!) : null,
|
||||
@@ -276,8 +277,13 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
|
||||
DebugLog.info('[EquipmentSelectionDialog] Loaded ${newEquipments.length} equipments, total: ${_paginatedEquipments.length}, hasMore: $_hasMoreEquipments');
|
||||
|
||||
// Charger les quantités pour les consommables/câbles de cette page
|
||||
await _loadAvailableQuantities(newEquipments);
|
||||
// Charger les quantites pour les consommables/cbles de cette page
|
||||
_loadAvailableQuantities(newEquipments);
|
||||
|
||||
// Vrifier si on doit charger d'autres lments (ex: tout a t filtr)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_checkIfMoreItemsNeeded();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLog.error('[EquipmentSelectionDialog] Error loading equipment page', e);
|
||||
@@ -295,7 +301,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
|
||||
try {
|
||||
final result = await _dataService.getContainersPaginated(
|
||||
limit: 25,
|
||||
limit: 50,
|
||||
startAfter: _lastContainerId,
|
||||
searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null,
|
||||
category: _selectedCategory?.name, // Filtre par catégorie d'équipements
|
||||
@@ -358,8 +364,12 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
DebugLog.info('[EquipmentSelectionDialog] Loaded ${newContainers.length} containers, total: ${_paginatedContainers.length}, hasMore: $_hasMoreContainers');
|
||||
DebugLog.info('[EquipmentSelectionDialog] Cached ${allEquipmentsToCache.length} equipment(s) from containers, total cache: ${_cachedEquipment.length}');
|
||||
|
||||
// Mettre à jour les statuts de conflit pour les nouveaux containers
|
||||
// Mettre jour les statuts de conflit pour les nouveaux containers
|
||||
await _updateContainerConflictStatus();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_checkIfMoreItemsNeeded();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLog.error('[EquipmentSelectionDialog] Error loading container page', e);
|
||||
@@ -387,16 +397,51 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
}
|
||||
}
|
||||
|
||||
void _checkIfMoreItemsNeeded() {
|
||||
if (!mounted || _isLoadingMore) return;
|
||||
|
||||
int visibleItems = 0;
|
||||
if (_displayType == SelectionType.equipment) {
|
||||
visibleItems = _paginatedEquipments.where((eq) {
|
||||
return _showConflictingItems || !_conflictingEquipmentIds.contains(eq.id);
|
||||
}).length;
|
||||
|
||||
if (visibleItems < 15 && _hasMoreEquipments) {
|
||||
_loadNextEquipmentPage();
|
||||
} else if (_scrollController.hasClients && _scrollController.position.maxScrollExtent <= 0 && _hasMoreEquipments) {
|
||||
_loadNextEquipmentPage();
|
||||
}
|
||||
} else {
|
||||
visibleItems = _paginatedContainers.where((container) {
|
||||
if (!_showConflictingItems) {
|
||||
if (_conflictingContainerIds.contains(container.id)) return false;
|
||||
final hasConflictingChildren = container.equipmentIds.any(
|
||||
(eqId) => _conflictingEquipmentIds.contains(eqId),
|
||||
);
|
||||
if (hasConflictingChildren) return false;
|
||||
}
|
||||
return true;
|
||||
}).length;
|
||||
|
||||
if (visibleItems < 15 && _hasMoreContainers) {
|
||||
_loadNextContainerPage();
|
||||
} else if (_scrollController.hasClients && _scrollController.position.maxScrollExtent <= 0 && _hasMoreContainers) {
|
||||
_loadNextContainerPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
_scrollController.dispose(); // Nettoyer le ScrollController
|
||||
_selectionChangeNotifier.dispose(); // Nettoyer le ValueNotifier
|
||||
_searchDebouncer.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Charge les quantités disponibles pour les consommables/câbles d'une liste d'équipements
|
||||
Future<void> _loadAvailableQuantities(List<EquipmentModel> equipments) async {
|
||||
void _loadAvailableQuantities(List<EquipmentModel> equipments) {
|
||||
if (!mounted) return;
|
||||
|
||||
try {
|
||||
@@ -407,12 +452,13 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
for (var eq in consumables) {
|
||||
// Ne recharger que si on n'a pas déjà la quantité
|
||||
if (!_availableQuantities.containsKey(eq.id)) {
|
||||
final available = await _availabilityService.getAvailableQuantity(
|
||||
equipment: eq,
|
||||
startDate: widget.startDate,
|
||||
endDate: widget.endDate,
|
||||
excludeEventId: widget.excludeEventId,
|
||||
);
|
||||
int available = eq.totalQuantity ?? 0;
|
||||
if (_equipmentQuantities.containsKey(eq.id)) {
|
||||
final qtyInfo = _equipmentQuantities[eq.id];
|
||||
if (qtyInfo != null && qtyInfo['availableQuantity'] != null) {
|
||||
available = (qtyInfo['availableQuantity'] as num).toInt();
|
||||
}
|
||||
}
|
||||
_availableQuantities[eq.id] = available;
|
||||
}
|
||||
}
|
||||
@@ -1047,7 +1093,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Container(
|
||||
child: SizedBox(
|
||||
width: dialogWidth.clamp(600.0, 1200.0),
|
||||
height: dialogHeight.clamp(500.0, 900.0),
|
||||
child: Column(
|
||||
@@ -1076,20 +1122,23 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: ValueListenableBuilder<int>(
|
||||
valueListenable: _selectionChangeNotifier,
|
||||
builder: (context, _, __) => _buildSelectionPanel(),
|
||||
),
|
||||
),
|
||||
if (_hasRecommendations)
|
||||
Container(
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: Colors.grey.shade300),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: Colors.grey.shade300),
|
||||
),
|
||||
),
|
||||
child: _buildRecommendationsPanel(),
|
||||
),
|
||||
child: _buildRecommendationsPanel(),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -1166,7 +1215,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
onChanged: (value) {
|
||||
setState(() => _searchQuery = value.toLowerCase());
|
||||
// Recharger depuis le début avec le nouveau filtre
|
||||
_reloadData();
|
||||
_searchDebouncer(_reloadData);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -1358,60 +1407,85 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
return ListView(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
// Header
|
||||
_buildSectionHeader(
|
||||
_displayType == SelectionType.equipment ? 'Équipements' : 'Containers',
|
||||
_displayType == SelectionType.equipment ? Icons.inventory_2 : Icons.inventory,
|
||||
itemWidgets.length,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Items
|
||||
...itemWidgets,
|
||||
|
||||
// Indicateur de chargement en bas
|
||||
if (_isLoadingMore)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(color: AppColors.rouge),
|
||||
return NotificationListener<ScrollNotification>(
|
||||
onNotification: (ScrollNotification scrollInfo) {
|
||||
if (!_isLoadingMore && scrollInfo.metrics.pixels >= scrollInfo.metrics.maxScrollExtent - 300) {
|
||||
if (_displayType == SelectionType.equipment && _hasMoreEquipments) {
|
||||
_loadNextEquipmentPage();
|
||||
} else if (_displayType == SelectionType.container && _hasMoreContainers) {
|
||||
_loadNextContainerPage();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
// ✅ Construction de la liste complète avant le ListView.builder
|
||||
// pour permettre l'utilisation de ListView.builder (lazy rendering)
|
||||
// à la place de ListView(children:[]) qui construit tous les widgets
|
||||
// en mémoire d'un coup.
|
||||
// Pas d'itemExtent : les cards ont des hauteurs très variables
|
||||
// (80px sans conflit, jusqu'à 300px+ avec détails de conflits).
|
||||
final List<Widget> allChildren = [
|
||||
// Header
|
||||
_buildSectionHeader(
|
||||
_displayType == SelectionType.equipment ? 'Équipements' : 'Containers',
|
||||
_displayType == SelectionType.equipment ? Icons.inventory_2 : Icons.inventory,
|
||||
itemWidgets.length,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Message si fin de liste
|
||||
if (!_isLoadingMore && !(_displayType == SelectionType.equipment ? _hasMoreEquipments : _hasMoreContainers))
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Fin de la liste',
|
||||
style: TextStyle(color: Colors.grey.shade600, fontSize: 14),
|
||||
// Items
|
||||
...itemWidgets,
|
||||
|
||||
// Indicateur de chargement en bas
|
||||
if (_isLoadingMore)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(color: AppColors.rouge),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Message si rien trouvé
|
||||
if (itemWidgets.isEmpty && !_isLoadingMore)
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(Icons.search_off, size: 64, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Aucun résultat trouvé',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey.shade600),
|
||||
// Message si fin de liste
|
||||
if (!_isLoadingMore && !(_displayType == SelectionType.equipment ? _hasMoreEquipments : _hasMoreContainers))
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Fin de la liste',
|
||||
style: TextStyle(color: Colors.grey.shade600, fontSize: 14),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
// Message si rien trouvé
|
||||
if (itemWidgets.isEmpty && !_isLoadingMore)
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(Icons.search_off, size: 64, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Aucun résultat trouvé',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey.shade600),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: allChildren.length,
|
||||
itemBuilder: (context, index) => allChildren[index],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -1458,66 +1532,6 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Header de section repliable
|
||||
Widget _buildCollapsibleSectionHeader(
|
||||
String title,
|
||||
IconData icon,
|
||||
int count,
|
||||
bool isExpanded,
|
||||
Function(bool) onToggle,
|
||||
) {
|
||||
return InkWell(
|
||||
onTap: () => onToggle(!isExpanded),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.rouge.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: AppColors.rouge.withValues(alpha: 0.3),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
isExpanded ? Icons.keyboard_arrow_down : Icons.keyboard_arrow_right,
|
||||
color: AppColors.rouge,
|
||||
size: 24,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Icon(icon, color: AppColors.rouge, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.rouge,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.rouge,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
'$count',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEquipmentCard(EquipmentModel equipment, {Key? key}) {
|
||||
final isSelected = _selectedItems.containsKey(equipment.id);
|
||||
final isConsumable = equipment.category == EquipmentCategory.consumable ||
|
||||
@@ -1734,7 +1748,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'• $eventName',
|
||||
'ÔÇó $eventName',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.orange.shade800,
|
||||
@@ -1795,8 +1809,8 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Widget pour le sélecteur de quantité
|
||||
@@ -1809,7 +1823,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
}) {
|
||||
final displayQuantity = isSelected ? selectedItem.quantity : 0;
|
||||
|
||||
return Container(
|
||||
return SizedBox(
|
||||
width: 120,
|
||||
child: Row(
|
||||
children: [
|
||||
@@ -2100,7 +2114,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2369,7 +2383,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
}
|
||||
|
||||
// Cache local pour les équipements des conteneurs
|
||||
Map<String, List<String>> _containerEquipmentCache = {};
|
||||
final Map<String, List<String>> _containerEquipmentCache = {};
|
||||
|
||||
Widget _buildSelectedContainerTile(String id, SelectedItem item) {
|
||||
final isExpanded = _expandedContainers.contains(id);
|
||||
@@ -2425,7 +2439,7 @@ class _EquipmentSelectionDialogState extends State<EquipmentSelectionDialog> {
|
||||
return _buildSelectedChildEquipmentTile(equipmentId, childItem);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
}).toList(),
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user