downgradeItem method

Future<ApiCallback<void>> downgradeItem(
  1. {@required String skuId,
  2. @required int quantity,
  3. bool isGift = false,
  4. String cartId,
  5. String channel,
  6. bool saveAsDefault = true}
)

Implementation

Future<ApiCallback<void>> downgradeItem(
    {@required String skuId,
    @required int quantity,
    bool isGift = false,
    String cartId,
    String channel,
    bool saveAsDefault = true}) async {
  try {
    if (cartId == null) cartId = await _getDefaultCartId();

    var queryParameters = {
      'skuId': skuId,
      'quantity': quantity,
    };

    // Set custom channel header if needed
    var headers = Map<String, dynamic>();
    if (channel != null) {
      headers['channel'] = channel;
    }
    var options = Options(responseType: ResponseType.json, headers: headers);

    var response = await _requester.delete(
        (isGift ? UPDATE_GIFT_ITEM : UPDATE_CART_ITEM)
            .replaceAll(PARAMETER_CART_ID, cartId),
        queryParameters: queryParameters,
        options: options);

    if (saveAsDefault) {
      // Parse the new cart
      var parsedCart = Cart.fromJson(response.data);
      // Check if we have a current cart
      var currentCart = await CartDao().get();
      // If we have a current cart, update the data based on the current existing cart
      if (currentCart != null) parsedCart.isUpdatingFrom(currentCart);
      // Save it locally
      await CartDao().insertOrUpdate(parsedCart);
    }

    return ApiCallback(isSuccessful: true);
  } on DioError catch (e) {
    return ApiCallback<void>().defaultErrorCallback(e);
  }
}