From 0a8a0b0a6ddbb925022cb55afa5b6390868212f1 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 9 Jul 2024 19:52:38 +0700 Subject: [PATCH] TF-2907 Handle error correctly when perform reply event action --- .../data/network/calendar_event_api.dart | 18 ++++++++++--- .../exceptions/calendar_event_exceptions.dart | 9 +++++++ .../controller/single_email_controller.dart | 27 ++++++++++++++----- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/features/email/data/network/calendar_event_api.dart b/lib/features/email/data/network/calendar_event_api.dart index 002ef5086..91c4ddb1c 100644 --- a/lib/features/email/data/network/calendar_event_api.dart +++ b/lib/features/email/data/network/calendar_event_api.dart @@ -74,7 +74,11 @@ class CalendarEventAPI { throw NotAcceptableCalendarEventException(); } - return calendarEventAcceptResponse; + if (calendarEventAcceptResponse.accepted?.isNotEmpty == true) { + return calendarEventAcceptResponse; + } else { + throw CannotReplyCalendarEventException(mapErrors: calendarEventAcceptResponse.notAccepted); + } } Future maybeEventInvitation( @@ -102,7 +106,11 @@ class CalendarEventAPI { throw NotMaybeableCalendarEventException(); } - return calendarEventMaybeResponse; + if (calendarEventMaybeResponse.maybe?.isNotEmpty == true) { + return calendarEventMaybeResponse; + } else { + throw CannotReplyCalendarEventException(mapErrors: calendarEventMaybeResponse.notMaybe); + } } Future rejectEventInvitation( @@ -130,6 +138,10 @@ class CalendarEventAPI { throw NotRejectableCalendarEventException(); } - return calendarEventRejectResponse; + if (calendarEventRejectResponse.rejected?.isNotEmpty == true) { + return calendarEventRejectResponse; + } else { + throw CannotReplyCalendarEventException(mapErrors: calendarEventRejectResponse.notRejected); + } } } \ No newline at end of file diff --git a/lib/features/email/domain/exceptions/calendar_event_exceptions.dart b/lib/features/email/domain/exceptions/calendar_event_exceptions.dart index e6d15a375..a85d1c22e 100644 --- a/lib/features/email/domain/exceptions/calendar_event_exceptions.dart +++ b/lib/features/email/domain/exceptions/calendar_event_exceptions.dart @@ -1,4 +1,7 @@ +import 'package:jmap_dart_client/jmap/core/error/set_error.dart'; +import 'package:jmap_dart_client/jmap/core/id.dart'; + class NotFoundCalendarEventException implements Exception {} class NotParsableCalendarEventException implements Exception {} @@ -8,3 +11,9 @@ class NotAcceptableCalendarEventException implements Exception {} class NotMaybeableCalendarEventException implements Exception {} class NotRejectableCalendarEventException implements Exception {} + +class CannotReplyCalendarEventException implements Exception { + final Map? mapErrors; + + CannotReplyCalendarEventException({this.mapErrors}); +} \ No newline at end of file diff --git a/lib/features/email/presentation/controller/single_email_controller.dart b/lib/features/email/presentation/controller/single_email_controller.dart index 49f05d331..72f922a86 100644 --- a/lib/features/email/presentation/controller/single_email_controller.dart +++ b/lib/features/email/presentation/controller/single_email_controller.dart @@ -32,6 +32,7 @@ import 'package:tmail_ui_user/features/base/mixin/app_loader_mixin.dart'; import 'package:tmail_ui_user/features/base/state/button_state.dart'; import 'package:tmail_ui_user/features/composer/presentation/extensions/email_action_type_extension.dart'; import 'package:tmail_ui_user/features/destination_picker/presentation/model/destination_picker_arguments.dart'; +import 'package:tmail_ui_user/features/email/domain/exceptions/calendar_event_exceptions.dart'; import 'package:tmail_ui_user/features/email/domain/exceptions/email_exceptions.dart'; import 'package:tmail_ui_user/features/email/domain/extensions/list_attachments_extension.dart'; import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart'; @@ -255,7 +256,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin { _showMessageWhenEmailPrintingFailed(failure); } else if (failure is CalendarEventReplyFailure || failure is StoreEventAttendanceStatusFailure) { - _calendarEventFailure(); + _calendarEventFailure(failure); } } @@ -1752,12 +1753,26 @@ class SingleEmailController extends BaseController with AppLoaderMixin { success.eventActionType.getToastMessageSuccess(currentContext!)); } - void _calendarEventFailure() { - if (currentOverlayContext != null && currentContext != null) { - appToast.showToastErrorMessage( - currentOverlayContext!, - AppLocalizations.of(currentContext!).eventReplyWasSentUnsuccessfully); + void _calendarEventFailure(Failure failure) { + if (currentOverlayContext == null || currentContext == null) { + return; } + + if (failure is CalendarEventReplyFailure && failure.exception is CannotReplyCalendarEventException) { + final replyEventException = failure.exception as CannotReplyCalendarEventException; + + if (replyEventException.mapErrors?.isNotEmpty == true) { + appToast.showToastErrorMessage( + currentOverlayContext!, + replyEventException.mapErrors!.values.first.description + ?? AppLocalizations.of(currentContext!).eventReplyWasSentUnsuccessfully); + return; + } + } + + appToast.showToastErrorMessage( + currentOverlayContext!, + AppLocalizations.of(currentContext!).eventReplyWasSentUnsuccessfully); } void _downloadMessageAsEML(PresentationEmail presentationEmail) {