deleteArticleForever<A extends ArticleRawAbstract> method

  1. @action
Future<ObservableList<CalibreWeebi<ArticleRawAbstract>>> deleteArticleForever<A extends ArticleRawAbstract>(
  1. A articleData
)

Implementation

@action
Future<ObservableList<CalibreWeebi>>
    deleteArticleForever<A extends ArticleRawAbstract>(A articleData) async {
  // first remove it from any basket
  if (articleData.kind == ArticleKind.retail && baskets.isNotEmpty) {
    try {
      await _propagateArticleRetailDeletionInBaskets(
          articleData.calibreId, articleData.id);
    } catch (e) {
      print(
          '_propagateArticleRetailDeletionInBaskets of ${articleData.id} ${articleData.designation} failed because of $e\n delete it anyway');
    }
  }

  // delete the photo
  if (photos.any((p) =>
      p.calibreId == articleData.calibreId && p.id == articleData.id)) {
    final temp = [
      ...photos
          .where((p) =>
              p.calibreId == articleData.calibreId && p.id == articleData.id)
          .map((e) => ArticlePhoto.fromMap(e.toMap()))
    ];
    for (final photo in temp) {
      await deletePhoto(photo, isLocalFileToBeDeleted: true);
    }
  }

  final calibre =
      calibresFull.firstWhereOrNull((c) => c.id == articleData.calibreId);
  if (calibre != null) {
    if (calibre.articles.length > 1) {
      final articleCool =
          calibre.articles.firstWhereOrNull((a) => a.id == articleData.id);
      calibre.articles.remove(articleCool);
      await updateCalibre(calibre);
    } else {
      await _deleteCalibreServer(calibre);
      // remove calibre if only one article and server deleted it
      await deleteCalibreForever(calibre);
    }
  }
  return calibresFull;
}