diff --git a/lib/features/email/domain/exceptions/email_cache_exceptions.dart b/lib/features/email/domain/exceptions/email_cache_exceptions.dart index 5e2a9e9bd..414a25f4c 100644 --- a/lib/features/email/domain/exceptions/email_cache_exceptions.dart +++ b/lib/features/email/domain/exceptions/email_cache_exceptions.dart @@ -5,4 +5,6 @@ class NotFoundStoredNewEmailException implements Exception {} class NotFoundStoredEmailException implements Exception {} -class OpenedEmailAlreadyStoredException implements Exception {} \ No newline at end of file +class OpenedEmailAlreadyStoredException implements Exception {} + +class NewEmailAlreadyStoredException implements Exception {} \ No newline at end of file diff --git a/lib/features/email/domain/usecases/store_new_email_interator.dart b/lib/features/email/domain/usecases/store_new_email_interator.dart index 51a6e0d1d..1b9002145 100644 --- a/lib/features/email/domain/usecases/store_new_email_interator.dart +++ b/lib/features/email/domain/usecases/store_new_email_interator.dart @@ -5,6 +5,7 @@ import 'package:dartz/dartz.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; +import 'package:tmail_ui_user/features/email/domain/exceptions/email_cache_exceptions.dart'; import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart'; import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart'; import 'package:tmail_ui_user/features/email/domain/state/store_new_email_state.dart'; @@ -22,14 +23,31 @@ class StoreNewEmailInteractor { ) async* { try { yield Right(StoreNewEmailLoading()); - await Future.wait([ - _emailRepository.storeEmail(session, accountId, email), - _emailRepository.storeDetailedNewEmail(session, accountId, detailedEmail), - ]); - log('StoreNewEmailInteractor::execute():Store Success | EMAIL: ${detailedEmail.htmlEmailContent} | TIME: ${detailedEmail.createdTime}'); - yield Right(StoreNewEmailSuccess()); + final isNewEmailExist = await _isNewEmailAlreadyStored(session, accountId, detailedEmail); + log('StoreNewEmailInteractor::execute():isNewEmailExist: $isNewEmailExist'); + if (!isNewEmailExist) { + await Future.wait([ + _emailRepository.storeEmail(session, accountId, email), + _emailRepository.storeDetailedNewEmail(session, accountId, detailedEmail), + ], eagerError: true); + log('StoreNewEmailInteractor::execute():Store Success | EMAIL: ${detailedEmail.emailId} | TIME: ${detailedEmail.createdTime}'); + yield Right(StoreNewEmailSuccess()); + } else { + log('StoreNewEmailInteractor::execute():Store Failure | EMAIL: ${detailedEmail.emailId} | TIME: ${detailedEmail.createdTime}'); + yield Left(StoreNewEmailFailure(NewEmailAlreadyStoredException())); + } } catch (e) { yield Left(StoreNewEmailFailure(e)); } } + + Future _isNewEmailAlreadyStored(Session session, AccountId accountId, DetailedEmail detailedEmail) async { + try { + await _emailRepository.getStoredNewEmail(session, accountId, detailedEmail.emailId); + return true; + } catch (err) { + logError('StoreNewEmailInteractor::_isNewEmailAlreadyStored():EXCEPTION: $err'); + return false; + } + } } \ No newline at end of file diff --git a/lib/features/email/domain/usecases/store_opened_email_interactor.dart b/lib/features/email/domain/usecases/store_opened_email_interactor.dart index 568f5f369..50897fbdb 100644 --- a/lib/features/email/domain/usecases/store_opened_email_interactor.dart +++ b/lib/features/email/domain/usecases/store_opened_email_interactor.dart @@ -36,8 +36,7 @@ class StoreOpenedEmailInteractor { Future _isOpenedEmailAlreadyStored(Session session, AccountId accountId, DetailedEmail detailedEmail) async { try { - final storedOpenEmail = await _emailRepository.getStoredOpenedEmail(session, accountId, detailedEmail.emailId); - log('StoreOpenedEmailInteractor::isOpenedEmailAlreadyStored():storedOpenEmail: $storedOpenEmail'); + await _emailRepository.getStoredOpenedEmail(session, accountId, detailedEmail.emailId); return true; } catch (err) { logError('StoreOpenedEmailInteractor::isOpenedEmailAlreadyStored():EXCEPTION: $err');