saveDocument method

Future<void> saveDocument({
  1. required Map<String, dynamic> data,
})

Saves a document to Firestore.

This method logs the progress, performs the save operation, and handles errors. The logger also records the completion time.

data: The document data to be saved as a Map.

Returns a Future that completes when the document is saved.

Implementation

Future<void> saveDocument({required Map<String, dynamic> data}) async {
  // Log the start of the save operation
  final now = DateTime.now();
  loggerService?.log("⌛ Saving document: $data in progress");

  try {
    // Save document to Firestore
    await firestoreWriteService.saveDocument(collection, data);
    // Log the success of the operation
    loggerService?.log(
        '✅ Document ${data.containsKey('id') ? data['id'] : "New document"} saved successfully');
  } catch (e) {
    // Log any error encountered during the save
    const errorMessage = 'Error saving document';
    loggerService?.logError(errorMessage, e.toString());
    // Rethrow the error for further handling
    rethrow;
  } finally {
    // Log the completion time of the save operation
    loggerService?.logCompletionTime(now, 'Saving document');
  }
}