refactor: Remplacement de l'accès direct à Firestore par des Cloud Functions
Migration complète du backend pour utiliser des Cloud Functions comme couche API sécurisée, en remplacement des appels directs à Firestore depuis le client.
**Backend (Cloud Functions):**
- **Centralisation CORS :** Ajout d'un middleware `withCors` et d'une configuration `httpOptions` pour gérer uniformément les en-têtes CORS et les requêtes `OPTIONS` sur toutes les fonctions.
- **Nouvelles Fonctions de Lecture (GET) :**
- `getEquipments`, `getContainers`, `getEvents`, `getUsers`, `getOptions`, `getEventTypes`, `getRoles`, `getMaintenances`, `getAlerts`.
- Ces fonctions gèrent les permissions côté serveur, masquant les données sensibles (ex: prix des équipements) pour les utilisateurs non-autorisés.
- `getEvents` retourne également une map des utilisateurs (`usersMap`) pour optimiser le chargement des données de la main d'œuvre.
- **Nouvelle Fonction de Recherche :**
- `getContainersByEquipment` : Endpoint dédié pour trouver efficacement tous les containers qui contiennent un équipement spécifique.
- **Nouvelles Fonctions d'Écriture (CRUD) :**
- Fonctions CRUD complètes pour `eventTypes` (`create`, `update`, `delete`), incluant la validation (unicité du nom, vérification des événements futurs avant suppression).
- **Mise à jour de Fonctions Existantes :**
- Toutes les fonctions CRUD existantes (`create/update/deleteEquipment`, `create/update/deleteContainer`, etc.) sont wrappées avec le nouveau gestionnaire CORS.
**Frontend (Flutter):**
- **Introduction du `DataService` :** Nouveau service centralisant tous les appels aux Cloud Functions, servant d'intermédiaire entre l'UI/Providers et l'API.
- **Refactorisation des Providers :**
- `EquipmentProvider`, `ContainerProvider`, `EventProvider`, `UsersProvider`, `MaintenanceProvider` et `AlertProvider` ont été refactorisés pour utiliser le `DataService` au lieu d'accéder directement à Firestore.
- Les `Stream` Firestore sont remplacés par des chargements de données via des méthodes `Future` (`loadEquipments`, `loadEvents`, etc.).
- **Gestion des Relations Équipement-Container :**
- Le modèle `EquipmentModel` ne stocke plus `parentBoxIds`.
- La relation est maintenant gérée par le `ContainerModel` qui contient `equipmentIds`.
- Le `ContainerEquipmentService` est introduit pour utiliser la nouvelle fonction `getContainersByEquipment`.
- L'affichage des boîtes parentes (`EquipmentParentContainers`) et le formulaire d'équipement (`EquipmentFormPage`) ont été mis à jour pour refléter ce nouveau modèle de données, synchronisant les ajouts/suppressions d'équipements dans les containers.
- **Amélioration de l'UI :**
- Nouveau widget `ParentBoxesSelector` pour une sélection améliorée et visuelle des boîtes parentes dans le formulaire d'équipement.
- Refonte visuelle de `EquipmentParentContainers` pour une meilleure présentation.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:em2rp/models/container_model.dart';
|
||||
import 'package:em2rp/models/equipment_model.dart';
|
||||
import 'package:em2rp/services/container_service.dart';
|
||||
import 'package:em2rp/services/container_equipment_service.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/views/container_detail_page.dart';
|
||||
|
||||
/// Widget pour afficher les containers qui référencent un équipement
|
||||
class EquipmentReferencingContainers extends StatefulWidget {
|
||||
/// Utilise le nouveau système : interroge Firestore via Cloud Function
|
||||
class EquipmentReferencingContainers extends StatelessWidget {
|
||||
final String equipmentId;
|
||||
|
||||
const EquipmentReferencingContainers({
|
||||
@@ -14,191 +14,161 @@ class EquipmentReferencingContainers extends StatefulWidget {
|
||||
required this.equipmentId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EquipmentReferencingContainers> createState() => _EquipmentReferencingContainersState();
|
||||
}
|
||||
|
||||
class _EquipmentReferencingContainersState extends State<EquipmentReferencingContainers> {
|
||||
final ContainerService _containerService = ContainerService();
|
||||
List<ContainerModel> _referencingContainers = [];
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadReferencingContainers();
|
||||
}
|
||||
|
||||
Future<void> _loadReferencingContainers() async {
|
||||
try {
|
||||
final containers = await _containerService.findContainersWithEquipment(widget.equipmentId);
|
||||
setState(() {
|
||||
_referencingContainers = containers;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_referencingContainers.isEmpty && !_isLoading) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return FutureBuilder<List<ContainerModel>>(
|
||||
future: containerEquipmentService.getContainersByEquipment(equipmentId),
|
||||
builder: (context, snapshot) {
|
||||
// Ne rien afficher si vide et pas en chargement
|
||||
if (snapshot.connectionState == ConnectionState.done &&
|
||||
(!snapshot.hasData || snapshot.data!.isEmpty)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Card(
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return Card(
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return const SizedBox.shrink(); // Masquer en cas d'erreur
|
||||
}
|
||||
|
||||
final containers = snapshot.data ?? [];
|
||||
|
||||
return Card(
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Icon(Icons.inventory_2, color: AppColors.rouge),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Containers contenant cet équipement',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.inventory_2, color: AppColors.rouge),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Boîtes contenant cet équipement (${containers.length})',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Divider(height: 24),
|
||||
_buildContainersGrid(context, containers),
|
||||
],
|
||||
),
|
||||
const Divider(height: 24),
|
||||
if (_isLoading)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
else if (_referencingContainers.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Cet équipement n\'est dans aucun container',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
_buildContainersGrid(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContainersGrid() {
|
||||
final screenWidth = MediaQuery.of(context).size.width;
|
||||
final isMobile = screenWidth < 800;
|
||||
final isTablet = screenWidth < 1200;
|
||||
|
||||
final crossAxisCount = isMobile ? 1 : (isTablet ? 2 : 3);
|
||||
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: crossAxisCount,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 8,
|
||||
childAspectRatio: 7.5,
|
||||
),
|
||||
itemCount: _referencingContainers.length,
|
||||
itemBuilder: (context, index) {
|
||||
final container = _referencingContainers[index];
|
||||
return _buildContainerCard(container);
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContainerCard(ContainerModel container) {
|
||||
Widget _buildContainersGrid(BuildContext context, List<ContainerModel> containers) {
|
||||
return Column(
|
||||
children: containers.map((container) {
|
||||
return _buildContainerCard(context, container);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContainerCard(BuildContext context, ContainerModel container) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
elevation: 1,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(color: Colors.grey.shade200, width: 1),
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ContainerDetailPage(container: container),
|
||||
builder: (_) => ContainerDetailPage(container: container),
|
||||
),
|
||||
);
|
||||
},
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
// Icône du type de container
|
||||
container.type.getIcon(size: 28, color: AppColors.rouge),
|
||||
const SizedBox(width: 10),
|
||||
// Infos textuelles
|
||||
CircleAvatar(
|
||||
backgroundColor: AppColors.rouge.withValues(alpha: 0.1),
|
||||
radius: 28,
|
||||
child: container.type.getIconForAvatar(
|
||||
size: 28,
|
||||
color: AppColors.rouge,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
|
||||
// Informations du container
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
container.id,
|
||||
container.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 1.0,
|
||||
fontSize: 16,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
if (container.notes != null && container.notes!.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: Text(
|
||||
container.notes!,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: Colors.grey[600],
|
||||
height: 1.0,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
)
|
||||
else
|
||||
Text(
|
||||
container.name,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.grey[600],
|
||||
height: 1.0,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
container.type.label,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
|
||||
// Badges d'information
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
_buildInfoChip(
|
||||
icon: Icons.inventory,
|
||||
label: '${container.itemCount} équipement${container.itemCount > 1 ? 's' : ''}',
|
||||
color: Colors.blue,
|
||||
),
|
||||
if (container.weight != null)
|
||||
_buildInfoChip(
|
||||
icon: Icons.scale,
|
||||
label: '${container.weight!.toStringAsFixed(1)} kg',
|
||||
color: Colors.orange,
|
||||
),
|
||||
_buildInfoChip(
|
||||
icon: Icons.tag,
|
||||
label: container.id,
|
||||
color: Colors.grey,
|
||||
isCompact: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// Badges compacts
|
||||
Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
_buildStatusBadge(_getStatusLabel(container.status), _getStatusColor(container.status)),
|
||||
if (container.itemCount > 0)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: _buildCountBadge(container.itemCount),
|
||||
),
|
||||
],
|
||||
|
||||
// Icône de navigation
|
||||
Icon(
|
||||
Icons.chevron_right,
|
||||
color: AppColors.rouge,
|
||||
size: 28,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -207,82 +177,44 @@ class _EquipmentReferencingContainersState extends State<EquipmentReferencingCon
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusBadge(String label, Color color) {
|
||||
Widget _buildInfoChip({
|
||||
required IconData icon,
|
||||
required String label,
|
||||
required Color color,
|
||||
bool isCompact = false,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: color.withValues(alpha: 0.4), width: 0.5),
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: isCompact ? 6 : 8,
|
||||
vertical: isCompact ? 2 : 4,
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 9,
|
||||
color: color,
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 1.0,
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: color.withValues(alpha: 0.3),
|
||||
width: 1,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: isCompact ? 12 : 14,
|
||||
color: color.withValues(alpha: 0.8),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: isCompact ? 10 : 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color.withValues(alpha: 0.9),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCountBadge(int count) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.green.withValues(alpha: 0.4), width: 0.5),
|
||||
),
|
||||
child: Text(
|
||||
'$count article${count > 1 ? 's' : ''}',
|
||||
style: const TextStyle(
|
||||
fontSize: 9,
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 1.0,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getStatusLabel(EquipmentStatus status) {
|
||||
switch (status) {
|
||||
case EquipmentStatus.available:
|
||||
return 'Disponible';
|
||||
case EquipmentStatus.inUse:
|
||||
return 'En prestation';
|
||||
case EquipmentStatus.rented:
|
||||
return 'Loué';
|
||||
case EquipmentStatus.lost:
|
||||
return 'Perdu';
|
||||
case EquipmentStatus.outOfService:
|
||||
return 'HS';
|
||||
case EquipmentStatus.maintenance:
|
||||
return 'En maintenance';
|
||||
}
|
||||
}
|
||||
|
||||
Color _getStatusColor(EquipmentStatus status) {
|
||||
switch (status) {
|
||||
case EquipmentStatus.available:
|
||||
return Colors.green;
|
||||
case EquipmentStatus.inUse:
|
||||
return Colors.blue;
|
||||
case EquipmentStatus.rented:
|
||||
return Colors.orange;
|
||||
case EquipmentStatus.lost:
|
||||
return Colors.red;
|
||||
case EquipmentStatus.outOfService:
|
||||
return Colors.red;
|
||||
case EquipmentStatus.maintenance:
|
||||
return Colors.yellow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user