validateCart method

Future<ApiCallback<void>> validateCart()

Implementation

Future<ApiCallback<void>> validateCart() async {
  // Get the current cart
  Cart cart = await CartDao().get();
  // Check if we already have one
  if (cart != null) {
    //If yes just we need do update the cart
    try {
      String channel = LoriApi.operatingSystem;
      await getCart(cartId: cart.id, channel: channel);
      return ApiCallback(isSuccessful: true);
    } on DioError catch (e) {
      return ApiCallback<void>().defaultErrorCallback(e);
    }
  } else {
    // If no, make sure to create one
    ApiCallback createCartResult = await getCart();
    if (createCartResult.isSuccessful) {
      return ApiCallback(isSuccessful: true);
    } else {
      // We can't continue if we don't succeed on creating a cart
      return ApiCallback(isSuccessful: false);
    }
  }
}