6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit e6e5b9a9
Changed files
.../repository/hotel_information_repository.dart | 32 ++++++++---- .../lib/profile/profile_repository.dart | 34 ++++++++---- .../interceptors/response_handle_interceptor.dart | 60 +++++++++++----------- .../lib/up_sales/up_sales_repository.dart | 33 ++++++++---- 4 files changed, 97 insertions(+), 62 deletions(-)
Diff
diff --git a/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart b/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
index 61a95653..5a7398f4 100644
--- a/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
+++ b/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
@@ -10,23 +10,33 @@ class HotelInformationRepository {
Future<Hotel> fetchHotelInformation(String hotelCode) async {
try {
- final hotel = await locator<ComwellDatabase>()
- .hotelInformationDAO
- .getHotelByCode(hotelCode);
+ final hotel = await _checkIfHotelInformationExists(hotelCode);
debugPrint("Hotel information fetched from database: $hotel");
return hotel;
} catch (e) {
try {
- final response = await api.getHotelInfo(hotelCode);
- final data = response.data as Json;
- final hotelInfo = Hotel.fromJson(data);
- await locator<ComwellDatabase>().hotelInformationDAO.saveHotel(hotelInfo);
- debugPrint("Hotel information saved to database: $hotelInfo");
- return hotelInfo;
- } catch (dbError) {
- debugPrint("Database error while saving hotel information: $dbError");
+ final newHotel = await _fetchAndSaveHotelInformationToDatabase(hotelCode);
+ return newHotel;
+ } catch (e) {
+ debugPrint("Error fetching hotel information: $e");
rethrow;
}
}
}
+
+ Future<Hotel> _fetchAndSaveHotelInformationToDatabase(String hotelCode) async {
+ final response = await api.getHotelInfo(hotelCode);
+ final data = response.data as Json;
+ final hotelInfo = Hotel.fromJson(data);
+ await locator<ComwellDatabase>().hotelInformationDAO.saveHotel(hotelInfo);
+ debugPrint("Hotel information saved to database: $hotelInfo");
+ return hotelInfo;
+ }
+
+ Future<Hotel> _checkIfHotelInformationExists(String hotelCode) async {
+ final hotel = await locator<ComwellDatabase>()
+ .hotelInformationDAO
+ .getHotelByCode(hotelCode);
+ return hotel;
+ }
}
diff --git a/comwell_key_app/lib/profile/profile_repository.dart b/comwell_key_app/lib/profile/profile_repository.dart
index 79232ee7..9d35a84a 100644
--- a/comwell_key_app/lib/profile/profile_repository.dart
+++ b/comwell_key_app/lib/profile/profile_repository.dart
@@ -56,25 +56,37 @@ class ProfileRepository {
Future<Booking> getBookingDetails(String bookingId) async {
user = await fetchProfileSettings();
try {
- final booking = await locator<ComwellDatabase>()
- .bookingsDao
- .getBookingDetails(bookingId, user.id, []);
+ final booking = await _checkIfBookingDetailsExists(bookingId);
return booking;
} catch (e) {
try {
- final response = await api.getBookingDetails(bookingId);
- await locator<ComwellDatabase>().bookingsDao.insertBookings(
- BookingsDTO(current: [response!], past: [], cancelled: []));
- final booking = response.toBooking(user.id, BookingStatus.current, []);
- debugPrint("Booking saved to database: ${booking.confirmationId}");
- return booking;
- } catch (dbError) {
- debugPrint("Database error while saving booking: $dbError");
+ final newBooking =
+ await _fetchAndSaveBookingDetailsToDatabase(bookingId);
+ return newBooking;
+ } catch (e) {
+ debugPrint("Error fetching booking details: $e");
rethrow;
}
}
}
+ Future<Booking> _checkIfBookingDetailsExists(String bookingId) async {
+ final booking = await locator<ComwellDatabase>()
+ .bookingsDao
+ .getBookingDetails(bookingId, user.id, []);
+ return booking;
+ }
+
+ Future<Booking> _fetchAndSaveBookingDetailsToDatabase(
+ String bookingId) async {
+ final response = await api.getBookingDetails(bookingId);
+ await locator<ComwellDatabase>().bookingsDao.insertBookings(
+ BookingsDTO(current: [response!], past: [], cancelled: []));
+ final booking = response.toBooking(user.id, BookingStatus.current, []);
+ debugPrint("Booking saved to database: ${booking.confirmationId}");
+ return booking;
+ }
+
Future<User> fetchProfileSettings() async {
try {
final user = await locator<ComwellDatabase>().userDAO.getUser();
diff --git a/comwell_key_app/lib/services/interceptors/response_handle_interceptor.dart b/comwell_key_app/lib/services/interceptors/response_handle_interceptor.dart
index 427307d7..2ed56702 100644
--- a/comwell_key_app/lib/services/interceptors/response_handle_interceptor.dart
+++ b/comwell_key_app/lib/services/interceptors/response_handle_interceptor.dart
@@ -36,9 +36,9 @@ class ResponseHandleInterceptor extends Interceptor {
error: 'Token not found');
return handler.reject(error);
}
-
+
options.headers.addAll({
- 'Authorization':accessToken,
+ 'Authorization': accessToken,
'Ocp-Apim-Subscription-Key': dotenv.env['OCP_APIM_SUBSCRIPTION_KEY']!,
});
return handler.next(options);
@@ -70,35 +70,35 @@ class ResponseHandleInterceptor extends Interceptor {
case 401:
retryCount++;
try {
- final identifier = await _secureStorageService.read(key: constants.identifier);
- final authResult = await _authenticationRepository.acquireTokenSilent(identifier ?? '');
- await _secureStorageService.write(key: constants.accessToken, value: authResult.accessToken);
-
- if (retryCount < 3) {
-
- if (authResult.accessToken.isNotEmpty) {
- // Retry the original request with the new token
- final options = response.requestOptions;
- options.headers['Authorization'] = authResult.accessToken;
- final opts = Options(
- method: response.requestOptions.method,
- headers: response.requestOptions.headers);
- final retryRequest = await _dio.request<dynamic>(
- response.requestOptions.path,
- options: opts,
- data: response.requestOptions.data,
- queryParameters: response.requestOptions.queryParameters);
+ final identifier =
+ await _secureStorageService.read(key: constants.identifier);
+ final authResult = await _authenticationRepository
+ .acquireTokenSilent(identifier ?? '');
+ await _secureStorageService.write(
+ key: constants.accessToken, value: authResult.accessToken);
+
+ if (retryCount < 3) {
+ if (authResult.accessToken.isNotEmpty) {
+ // Retry the original request with the new token
+ final options = response.requestOptions;
+ options.headers['Authorization'] = authResult.accessToken;
+ final opts = Options(
+ method: response.requestOptions.method,
+ headers: response.requestOptions.headers);
+ final retryRequest = await _dio.request<dynamic>(
+ response.requestOptions.path,
+ options: opts,
+ data: response.requestOptions.data,
+ queryParameters: response.requestOptions.queryParameters);
- return handler.resolve(retryRequest);
-
+ return handler.resolve(retryRequest);
+ }
+ } else {
+ retryCount = 0;
+ _authenticationRepository.logOut(forced: true);
+ return handler.reject(err);
}
-
- } else {
- retryCount = 0;
- _authenticationRepository.logOut(forced: true);
- return handler.reject(err);
- }
- } catch (e){
+ } catch (e) {
debugPrint('Error acquiring token silently: $e');
}
break;
@@ -146,4 +146,4 @@ class ResponseHandleInterceptor extends Interceptor {
retryCount = 0;
return handler.next(response);
}
-}
\ No newline at end of file
+}
diff --git a/comwell_key_app/lib/up_sales/up_sales_repository.dart b/comwell_key_app/lib/up_sales/up_sales_repository.dart
index a2157f56..6a209c3a 100644
--- a/comwell_key_app/lib/up_sales/up_sales_repository.dart
+++ b/comwell_key_app/lib/up_sales/up_sales_repository.dart
@@ -19,24 +19,37 @@ class UpSalesRepository {
Future<UpSales> getUpSales(String confirmationId, String hotelCode) async {
try {
- final upsales = await locator<ComwellDatabase>()
- .upsalesDAO
- .getUpsaleByConfirmationNumber(confirmationId);
+ final upsales = await _checkIfUpSalesExists(confirmationId, hotelCode);
return upsales;
} catch (e) {
try {
- final response = await api.fetchUpSales(confirmationId, hotelCode);
- await locator<ComwellDatabase>().upsalesDAO.insertUpsale(response);
- final upsales = response.toUpSales();
- debugPrint("Up sales saved to database: $upsales");
- return upsales;
- } catch (dbError) {
- debugPrint("Error saving up sales to database: $dbError");
+ final newUpsales =
+ await _fetchAndSaveUpSalesToDatabase(confirmationId, hotelCode);
+ return newUpsales;
+ } catch (e) {
+ debugPrint("Error fetching up sales: $e");
rethrow;
}
}
}
+ Future<UpSales> _fetchAndSaveUpSalesToDatabase(
+ String confirmationId, String hotelCode) async {
+ final response = await api.fetchUpSales(confirmationId, hotelCode);
+ await locator<ComwellDatabase>().upsalesDAO.insertUpsale(response);
+ final upsales = response.toUpSales();
+ debugPrint("Up sales saved to database: $upsales");
+ return upsales;
+ }
+
+ Future<UpSales> _checkIfUpSalesExists(
+ String confirmationId, String hotelCode) async {
+ final upsales = await locator<ComwellDatabase>()
+ .upsalesDAO
+ .getUpsaleByConfirmationNumber(confirmationId);
+ return upsales;
+ }
+
Future<void> addUpSalesToBooking(String confirmationId, String hotelCode,
String roomType, List<AddOnList> selectedUpSales) async {
await api.addUpSalesToBooking(