deleteDocument method

  1. @override
Future<bool> deleteDocument(
  1. String documentId
)
override

Deletes a document.

documentId - The ID of the document to delete

Returns true if the document was deleted, false if it was not found.

Throws DeskDataSourceException if the operation fails. Throws DeskAuthenticationException if authentication is required.

Implementation

@override
Future<bool> deleteDocument(String documentId) async {
  final doc = _documents[documentId];
  if (doc == null) return false;

  final wasDefault = doc.isDefault;
  final docType = doc.documentType;

  // Remove document and all its versions
  final versionIds = _versions.remove(documentId)?.keys.toList() ?? [];
  for (final vid in versionIds) {
    _versionData.remove(vid);
  }
  _documents.remove(documentId);

  // Auto-assign default to the sole remaining document if needed
  if (wasDefault) {
    final remaining = _documents.values
        .where((d) => d.documentType == docType)
        .toList();
    if (remaining.length == 1) {
      final newDefault = remaining.first;
      _documents[newDefault.id!] = newDefault.copyWith(isDefault: true);
    }
  }

  return true;
}