Fix : Mise a jour et création d'un événement

This commit is contained in:
ElPoyo
2026-01-13 17:26:09 +01:00
parent 2bcd1ca4c3
commit 0f7a886cf7
5 changed files with 37 additions and 10 deletions

View File

@@ -505,7 +505,7 @@ exports.createEvent = onRequest(httpOptions, withCors(async (req, res) => {
const eventData = req.body.data; const eventData = req.body.data;
const dataToSave = helpers.deserializeTimestamps(eventData, [ const dataToSave = helpers.deserializeTimestamps(eventData, [
'startDateTime', 'endDateTime', 'createdAt', 'updatedAt' 'StartDateTime', 'EndDateTime', 'createdAt', 'updatedAt'
]); ]);
const docRef = await db.collection('events').add(dataToSave); const docRef = await db.collection('events').add(dataToSave);
@@ -529,9 +529,13 @@ exports.updateEvent = onRequest(httpOptions, withCors(async (req, res) => {
} }
const requestData = req.body.data; const requestData = req.body.data;
logger.info(`Update event - requestData keys: ${Object.keys(requestData || {}).join(', ')}`);
const eventId = requestData.eventId; const eventId = requestData.eventId;
logger.info(`Update event - eventId: ${eventId}`);
if (!eventId) { if (!eventId) {
logger.error('Event ID is missing from request');
res.status(400).json({ error: 'Event ID is required' }); res.status(400).json({ error: 'Event ID is required' });
return; return;
} }
@@ -548,7 +552,7 @@ exports.updateEvent = onRequest(httpOptions, withCors(async (req, res) => {
data.updatedAt = admin.firestore.Timestamp.now(); data.updatedAt = admin.firestore.Timestamp.now();
const dataToSave = helpers.deserializeTimestamps(data, [ const dataToSave = helpers.deserializeTimestamps(data, [
'startDateTime', 'endDateTime' 'StartDateTime', 'EndDateTime'
]); ]);
await db.collection('events').doc(eventId).update(dataToSave); await db.collection('events').doc(eventId).update(dataToSave);

View File

@@ -395,8 +395,14 @@ class EventFormController extends ChangeNotifier {
); );
final eventId = await EventFormService.createEvent(newEvent); final eventId = await EventFormService.createEvent(newEvent);
final newFiles = await EventFormService.moveFilesToEvent(_uploadedFiles, eventId);
await EventFormService.updateEventDocuments(eventId, newFiles); // Déplacer et mettre à jour les fichiers uniquement s'il y en a
if (_uploadedFiles.isNotEmpty) {
final newFiles = await EventFormService.moveFilesToEvent(_uploadedFiles, eventId);
if (newFiles.isNotEmpty) {
await EventFormService.updateEventDocuments(eventId, newFiles);
}
}
// Reload events // Reload events
final localUserProvider = Provider.of<LocalUserProvider>(context, listen: false); final localUserProvider = Provider.of<LocalUserProvider>(context, listen: false);

View File

@@ -77,6 +77,9 @@ class FirebaseFunctionsApiService implements ApiService {
// Convertir les Timestamps avant l'envoi // Convertir les Timestamps avant l'envoi
final convertedData = _convertTimestamps(data) as Map<String, dynamic>; final convertedData = _convertTimestamps(data) as Map<String, dynamic>;
// Log pour débogage
print('[API] Calling $functionName with eventId: ${convertedData['eventId']}');
final response = await http.post( final response = await http.post(
url, url,
headers: headers, headers: headers,

View File

@@ -87,7 +87,7 @@ class DataService {
/// Met à jour un événement /// Met à jour un événement
Future<void> updateEvent(String eventId, Map<String, dynamic> data) async { Future<void> updateEvent(String eventId, Map<String, dynamic> data) async {
try { try {
final requestData = {'eventId': eventId, ...data}; final requestData = {'eventId': eventId, 'data': data};
await _apiService.call('updateEvent', requestData); await _apiService.call('updateEvent', requestData);
} catch (e) { } catch (e) {
throw Exception('Erreur lors de la mise à jour de l\'événement: $e'); throw Exception('Erreur lors de la mise à jour de l\'événement: $e');

View File

@@ -118,10 +118,17 @@ class EventFormService {
static Future<void> updateEvent(EventModel event) async { static Future<void> updateEvent(EventModel event) async {
try { try {
await _apiService.call('updateEvent', { if (event.id.isEmpty) {
'eventId': event.id, throw Exception("Cannot update event: Event ID is empty");
'data': event.toMap(), }
});
developer.log('Updating event with ID: ${event.id}', name: 'EventFormService');
final eventData = event.toMap();
eventData['eventId'] = event.id;
await _apiService.call('updateEvent', eventData);
developer.log('Event updated successfully', name: 'EventFormService');
} catch (e) { } catch (e) {
developer.log('Error updating event', name: 'EventFormService', error: e); developer.log('Error updating event', name: 'EventFormService', error: e);
rethrow; rethrow;
@@ -172,12 +179,19 @@ class EventFormService {
} }
static Future<void> updateEventDocuments(String eventId, List<Map<String, String>> documents) async { static Future<void> updateEventDocuments(String eventId, List<Map<String, String>> documents) async {
// Utiliser l'API pour mettre à jour les documents
try { try {
if (eventId.isEmpty) {
throw Exception("Event ID cannot be empty");
}
developer.log('Updating event documents for ID: $eventId (${documents.length} documents)', name: 'EventFormService');
await _apiService.call('updateEvent', { await _apiService.call('updateEvent', {
'eventId': eventId, 'eventId': eventId,
'documents': documents, 'documents': documents,
}); });
developer.log('Event documents updated successfully', name: 'EventFormService');
} catch (e) { } catch (e) {
developer.log('Error updating event documents', name: 'EventFormService', error: e); developer.log('Error updating event documents', name: 'EventFormService', error: e);
throw Exception("Could not update event documents."); throw Exception("Could not update event documents.");