TF-1923 Fix duplicated store new email from push notification

(cherry picked from commit 076bbe249cc870b944a2dc40bc31012e061e0897)
This commit is contained in:
dab246
2023-06-21 01:53:38 +07:00
committed by Dat H. Pham
parent 5faab2c306
commit ad5136b117
3 changed files with 28 additions and 9 deletions
@@ -5,4 +5,6 @@ class NotFoundStoredNewEmailException implements Exception {}
class NotFoundStoredEmailException implements Exception {} class NotFoundStoredEmailException implements Exception {}
class OpenedEmailAlreadyStoredException implements Exception {} class OpenedEmailAlreadyStoredException implements Exception {}
class NewEmailAlreadyStoredException implements Exception {}
@@ -5,6 +5,7 @@ import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.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/core/session/session.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.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/model/detailed_email.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.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'; import 'package:tmail_ui_user/features/email/domain/state/store_new_email_state.dart';
@@ -22,14 +23,31 @@ class StoreNewEmailInteractor {
) async* { ) async* {
try { try {
yield Right<Failure, Success>(StoreNewEmailLoading()); yield Right<Failure, Success>(StoreNewEmailLoading());
await Future.wait([ final isNewEmailExist = await _isNewEmailAlreadyStored(session, accountId, detailedEmail);
_emailRepository.storeEmail(session, accountId, email), log('StoreNewEmailInteractor::execute():isNewEmailExist: $isNewEmailExist');
_emailRepository.storeDetailedNewEmail(session, accountId, detailedEmail), if (!isNewEmailExist) {
]); await Future.wait([
log('StoreNewEmailInteractor::execute():Store Success | EMAIL: ${detailedEmail.htmlEmailContent} | TIME: ${detailedEmail.createdTime}'); _emailRepository.storeEmail(session, accountId, email),
yield Right<Failure, Success>(StoreNewEmailSuccess()); _emailRepository.storeDetailedNewEmail(session, accountId, detailedEmail),
], eagerError: true);
log('StoreNewEmailInteractor::execute():Store Success | EMAIL: ${detailedEmail.emailId} | TIME: ${detailedEmail.createdTime}');
yield Right<Failure, Success>(StoreNewEmailSuccess());
} else {
log('StoreNewEmailInteractor::execute():Store Failure | EMAIL: ${detailedEmail.emailId} | TIME: ${detailedEmail.createdTime}');
yield Left<Failure, Success>(StoreNewEmailFailure(NewEmailAlreadyStoredException()));
}
} catch (e) { } catch (e) {
yield Left<Failure, Success>(StoreNewEmailFailure(e)); yield Left<Failure, Success>(StoreNewEmailFailure(e));
} }
} }
Future<bool> _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;
}
}
} }
@@ -36,8 +36,7 @@ class StoreOpenedEmailInteractor {
Future<bool> _isOpenedEmailAlreadyStored(Session session, AccountId accountId, DetailedEmail detailedEmail) async { Future<bool> _isOpenedEmailAlreadyStored(Session session, AccountId accountId, DetailedEmail detailedEmail) async {
try { try {
final storedOpenEmail = await _emailRepository.getStoredOpenedEmail(session, accountId, detailedEmail.emailId); await _emailRepository.getStoredOpenedEmail(session, accountId, detailedEmail.emailId);
log('StoreOpenedEmailInteractor::isOpenedEmailAlreadyStored():storedOpenEmail: $storedOpenEmail');
return true; return true;
} catch (err) { } catch (err) {
logError('StoreOpenedEmailInteractor::isOpenedEmailAlreadyStored():EXCEPTION: $err'); logError('StoreOpenedEmailInteractor::isOpenedEmailAlreadyStored():EXCEPTION: $err');