TF-3752 Better error handling when updating recipients in Forwarding
Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:forward/forward/forward_id.dart';
|
||||
import 'package:forward/forward/get/get_forward_method.dart';
|
||||
import 'package:forward/forward/get/get_forward_response.dart';
|
||||
import 'package:forward/forward/set/set_forward_method.dart';
|
||||
import 'package:forward/forward/set/set_forward_response.dart';
|
||||
import 'package:forward/forward/tmail_forward.dart';
|
||||
import 'package:jmap_dart_client/http/http_client.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/jmap_request.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/handle_error_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/exceptions/set_method_exception.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/exceptions/forward_exception.dart';
|
||||
|
||||
class ForwardingAPI {
|
||||
class ForwardingAPI with HandleSetErrorMixin {
|
||||
final HttpClient _httpClient;
|
||||
|
||||
ForwardingAPI(this._httpClient);
|
||||
@@ -31,7 +35,7 @@ class ForwardingAPI {
|
||||
getForwardInvocation.methodCallId,
|
||||
GetForwardResponse.deserialize);
|
||||
|
||||
final tMailForwardResult = result?.list.first;
|
||||
final tMailForwardResult = result?.list.firstOrNull;
|
||||
if (tMailForwardResult == null) {
|
||||
throw NotFoundForwardException();
|
||||
}
|
||||
@@ -40,14 +44,15 @@ class ForwardingAPI {
|
||||
}
|
||||
|
||||
Future<TMailForward> updateForward(AccountId accountId, TMailForward forward) async {
|
||||
log('ForwardingAPI::updateForward: ${forward.toJson()}');
|
||||
final setForwardMethod = SetForwardMethod(accountId)
|
||||
..addUpdatesSingleton({
|
||||
ForwardIdSingleton.forwardIdSingleton.id : forward
|
||||
});
|
||||
|
||||
final processingInvocation = ProcessingInvocation();
|
||||
final requestBuilder = JmapRequestBuilder(_httpClient, processingInvocation)
|
||||
..invocation(setForwardMethod);
|
||||
final requestBuilder = JmapRequestBuilder(_httpClient, processingInvocation);
|
||||
final setForwardInvocation = requestBuilder.invocation(setForwardMethod);
|
||||
|
||||
final getForwardMethod = GetForwardMethod(accountId)
|
||||
..addIds({ForwardIdSingleton.forwardIdSingleton.id});
|
||||
@@ -58,11 +63,27 @@ class ForwardingAPI {
|
||||
.build()
|
||||
.execute();
|
||||
|
||||
final setForwardResponse = response.parse<SetForwardResponse>(
|
||||
setForwardInvocation.methodCallId,
|
||||
SetForwardResponse.deserialize,
|
||||
);
|
||||
|
||||
final mapErrors = handleSetResponse([setForwardResponse]);
|
||||
if (mapErrors.isNotEmpty) {
|
||||
throw SetMethodException(mapErrors);
|
||||
}
|
||||
|
||||
final updatedForward = setForwardResponse?.updated;
|
||||
if (updatedForward?.isNotEmpty != true) {
|
||||
throw UpdateForwardException();
|
||||
}
|
||||
|
||||
final getForwardResponse = response.parse<GetForwardResponse>(
|
||||
getForwardInvocation.methodCallId,
|
||||
GetForwardResponse.deserialize);
|
||||
GetForwardResponse.deserialize,
|
||||
);
|
||||
|
||||
final newForward = getForwardResponse?.list.first;
|
||||
final newForward = getForwardResponse?.list.firstOrNull;
|
||||
if (newForward == null) {
|
||||
throw NotFoundForwardException();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
class NotFoundForwardException implements Exception {}
|
||||
|
||||
class UpdateForwardException implements Exception {}
|
||||
|
||||
class RecipientListIsEmptyException implements Exception {}
|
||||
|
||||
class RecipientListWithInvalidEmailsException implements Exception {}
|
||||
@@ -11,11 +11,14 @@ class AddRecipientInForwardingRequest with EquatableMixin {
|
||||
});
|
||||
|
||||
TMailForward get newTMailForward {
|
||||
final newListRecipients = currentForward.forwards
|
||||
.where((recipient) => !listRecipientAdded.contains(recipient))
|
||||
.toSet();
|
||||
newListRecipients.addAll(listRecipientAdded);
|
||||
return currentForward.copyWith(forwards: newListRecipients);
|
||||
final currentRecipients = currentForward.forwards ?? {};
|
||||
|
||||
final updatedRecipients = {
|
||||
...currentRecipients,
|
||||
...listRecipientAdded,
|
||||
};
|
||||
|
||||
return currentForward.copyWith(forwards: updatedRecipients);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
+4
-4
@@ -11,11 +11,11 @@ class DeleteRecipientInForwardingRequest with EquatableMixin {
|
||||
});
|
||||
|
||||
TMailForward get newTMailForward {
|
||||
final newListRecipients = currentForward.forwards
|
||||
.where((recipient) => !listRecipientDeleted.contains(recipient))
|
||||
.toSet();
|
||||
final currentRecipients = currentForward.forwards ?? {};
|
||||
|
||||
return currentForward.copyWith(forwards: newListRecipients);
|
||||
final updatedRecipients = currentRecipients.difference(listRecipientDeleted.toSet());
|
||||
|
||||
return currentForward.copyWith(forwards: updatedRecipients);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -7,7 +7,7 @@ extension TMailForwardExtension on TMailForward {
|
||||
|
||||
List<RecipientForward> get listRecipientForward {
|
||||
return forwards
|
||||
.map((value) => RecipientForward(EmailAddress(value, value)))
|
||||
.toList();
|
||||
?.map((value) => RecipientForward(EmailAddress(value, value)))
|
||||
.toList() ?? [];
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/state/add_recipient_in_forwarding_state.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/state/delete_recipient_in_forwarding_state.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/state/edit_local_copy_in_forwarding_state.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/forward/forward_controller.dart';
|
||||
|
||||
extension HandleErrorWhenUpdateForwardFailExtension on ForwardController {
|
||||
void handleErrorWhenUpdateForwardFail(Failure failure) {
|
||||
logError(
|
||||
'HandleErrorWhenUpdateForwardFailExtension::handleErrorWhenUpdateForwardFail: $failure',
|
||||
);
|
||||
if (failure is AddRecipientsInForwardingFailure) {
|
||||
_handleAddRecipientsInForwardingFailure(failure);
|
||||
} else if (failure is DeleteRecipientInForwardingFailure) {
|
||||
_handleDeleteRecipientInForwardingFailure(failure);
|
||||
} else if (failure is EditLocalCopyInForwardingFailure) {
|
||||
_handleEditLocalCopyInForwardingFailure(failure);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleAddRecipientsInForwardingFailure(
|
||||
AddRecipientsInForwardingFailure failure,
|
||||
) {
|
||||
toastManager.showMessageFailure(failure);
|
||||
}
|
||||
|
||||
void _handleDeleteRecipientInForwardingFailure(
|
||||
DeleteRecipientInForwardingFailure failure,
|
||||
) {
|
||||
cancelSelectionMode();
|
||||
toastManager.showMessageFailure(failure);
|
||||
}
|
||||
|
||||
void _handleEditLocalCopyInForwardingFailure(
|
||||
EditLocalCopyInForwardingFailure failure,
|
||||
) {
|
||||
toastManager.showMessageFailure(failure);
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_forwar
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/action/dashboard_setting_action.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/extensions/tmail_forward_extension.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/forward/controller/forward_recipient_controller.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/forward/extensions/handle_error_when_update_forward_fail_extension.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/manage_account_dashboard_controller.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/recipient_forward.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
@@ -85,7 +86,6 @@ class ForwardController extends BaseController {
|
||||
|
||||
@override
|
||||
void handleSuccessViewState(Success success) {
|
||||
super.handleSuccessViewState(success);
|
||||
if (success is GetForwardSuccess) {
|
||||
currentForward.value = success.forward;
|
||||
listRecipientForward.value = currentForward.value!.listRecipientForward;
|
||||
@@ -96,14 +96,19 @@ class ForwardController extends BaseController {
|
||||
_handleAddRecipientsSuccess(success);
|
||||
} else if (success is EditLocalCopyInForwardingSuccess) {
|
||||
_handleEditLocalCopySuccess(success);
|
||||
} else {
|
||||
super.handleSuccessViewState(success);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
super.handleFailureViewState(failure);
|
||||
if (failure is DeleteRecipientInForwardingFailure) {
|
||||
cancelSelectionMode();
|
||||
if (failure is AddRecipientsInForwardingFailure ||
|
||||
failure is DeleteRecipientInForwardingFailure ||
|
||||
failure is EditLocalCopyInForwardingFailure) {
|
||||
handleErrorWhenUpdateForwardFail(failure);
|
||||
} else {
|
||||
super.handleFailureViewState(failure);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +267,7 @@ class ForwardController extends BaseController {
|
||||
accountId,
|
||||
EditLocalCopyInForwardingRequest(
|
||||
currentForward: currentForward.value!,
|
||||
keepLocalCopy: !currentForward.value!.localCopy)));
|
||||
keepLocalCopy: currentForward.value!.localCopy != true)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,7 +275,7 @@ class ForwardController extends BaseController {
|
||||
if (currentOverlayContext != null && currentContext != null) {
|
||||
appToast.showToastSuccessMessage(
|
||||
currentOverlayContext!,
|
||||
success.forward.localCopy
|
||||
success.forward.localCopy == true
|
||||
? AppLocalizations.of(currentContext!).toastMessageLocalCopyEnable
|
||||
: AppLocalizations.of(currentContext!).toastMessageLocalCopyDisable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user