findDupsByTitle method

TwoLists<CalibreWeebi<ArticleRawAbstract>> findDupsByTitle({
  1. required List<CalibreWeebi<ArticleRawAbstract>> newList,
})

Implementation

TwoLists<CalibreWeebi> findDupsByTitle(
    {required List<CalibreWeebi> newList}) {
  final noDups = <CalibreWeebi>[];
  final dups = <CalibreWeebi>[];

  if (isEmpty) {
    noDups.addAll(newList);
    return TwoLists<CalibreWeebi>(dups: dups, noDups: noDups);
  }
  Map<int, CalibreWeebi> mapOldTitles = {};
  for (var p in this) {
    mapOldTitles[p.titleHash] = p;
  }
  for (var calibre in newList) {
    if (mapOldTitles.keys.contains(calibre.titleHash)) {
      // We make sure that we update the dup list with the old values missing from files
      // it is only relevant here because we built updating objects with dummy values
      final originalCalibre = mapOldTitles[calibre.titleHash]!;
      if (calibre.isBasket) {
        final article = (calibre.articles.first as ArticleBasket).copyWith(
          calibreId: originalCalibre.id,
          creationDate: originalCalibre.creationDate,
          updateDate: DateTime.now(),
          proxies: (originalCalibre.articles.first as ArticleBasket).proxies,
          discountAmountSalesOnly:
              (originalCalibre.articles.first as ArticleBasket)
                  .discountAmountSalesOnly,
          status: (originalCalibre.articles.first as ArticleBasket).status,
          statusUpdateDate: (originalCalibre.articles.first as ArticleBasket)
              .statusUpdateDate,
        );
        calibre = calibre.copyWith(
            id: mapOldTitles[calibre.titleHash]!.id,
            creationDate: mapOldTitles[calibre.titleHash]!.creationDate,
            articles: [article]);
        dups.add(calibre);
      } else {
        final updatedArticles = <ArticleRetail>[];
        for (var i = 0; i < calibre.articles.length; i++) {
          final article = (calibre.articles[i] as ArticleRetail).copyWith(
            calibreId: originalCalibre.id,
            creationDate:
                (originalCalibre.articles[i] as ArticleRetail).creationDate,
            updateDate: DateTime.now(),
            unitsInOnePiece: (originalCalibre.articles[i] as ArticleRetail)
                .unitsInOnePiece,
            articleCode:
                (originalCalibre.articles[i] as ArticleRetail).articleCode,
            status: (originalCalibre.articles[i] as ArticleRetail).status,
            statusUpdateDate: (originalCalibre.articles[i] as ArticleRetail)
                .statusUpdateDate,
          );
          updatedArticles.add(article);
        }
        calibre = calibre.copyWith(
            id: mapOldTitles[calibre.titleHash]!.id,
            creationDate: mapOldTitles[calibre.titleHash]!.creationDate,
            articles: updatedArticles);
        dups.add(calibre);
      }
    } else {
      noDups.add(calibre);
    }
  }
  return TwoLists<CalibreWeebi>(dups: dups, noDups: noDups);
}