addToCart method

dynamic addToCart(
  1. BuildContext context,
  2. AddToCartRequest request,
  3. ProductContentModel offerList
)

Implementation

addToCart(BuildContext context, AddToCartRequest request,
    ProductContentModel offerList) async {
  Helper.progressDialog(context, "Please wait..");
  String requestTime = DateTime.now().toString();
  try {
    List relatedParty = [];
    request.relatedParty?.forEach((element) {
      relatedParty.add(element.toJson());
    });
    List selectedCartoffers = [];
    request.selectedCartoffers?.forEach((element) {
      selectedCartoffers.add(element.toJson());
    });
    // List adhocTax = [];
    // request.adhocTax?.forEach((element) {
    //   adhocTax.add(element.toJson());
    // });
    // printLogs(dio);
    dio
        .post(
      '${ApiConstant.posBaseUrl}pos-cart-service-svc/generic-carts',
      data: json.encode({
        'category': request.category,
        'contextParameters': request.contextParameters?.toJson(),
        'relatedParty': relatedParty,
        'selectedCartoffers': selectedCartoffers
        //'adhocTax': adhocTax
      }),
      options: Options(
        headers: {
          'x-request-txn-id': UDID.uDID,
          'X-User-Id': SecureStorageService.readSecureData(
            SecureStorageService.xUserId,
          ),
          'X-Roles': SecureStorageService.readSecureData(
            SecureStorageService.xUserId,
          ),
          'Authorization': "Bearer ${SecureStorageService.readSecureData(
            SecureStorageService.accessToken,
          )}",
        },
      ),
    )
        .then((response) {
      if (response.data != null) {
        ConditionalLogs().customLog('addToCart${response.data}');
        CartModel model = CartModel();
        Map<String, dynamic> creditData = response.data;
        model = CartModel.fromJson(creditData);
        UDID.setTraceId(response.headers.map[Constants.traceIdKey]?[0] ?? "");

        Helper.logEvent(
          "RESPONSE_EVENT",
          success: true,
          endPoint: "pos-cart-service-svc/generic-carts",
          responseDate: DateTime.now().toString(),
          screenName: "orderStock",
          requestDate: requestTime,
          transactionId: model.data?.cartId,
        );
        if (model.status == "POS200") {
          cart_id.value = model.data!.cartId!;
          discountList.value = model.data!.cartDiscountsList!;
          updateSellThruOrder(model);
          Get.back();
          Helper.showCustomSnackbar('Item added successfully.');
        } else {
          Get.back();
          Helper.messageDialog(
            Get.context!,
            model.errors?[0].errorItem?.errorCode ?? "Try Again!!",
            model.errors?[0].errorItem?.errorMessage ?? "Unknown issue",
          );
        }
      }
    }).catchError((error) {
      ConditionalLogs().customLog("$error");
      Get.back();
      if (error is DioException) {
        UDID.setTraceId(
          error.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
        );
        Helper.logEvent(
          "ERROR_EVENT",
          failure: true,
          endPoint: "pos-cart-service-svc/generic-carts",
          requestDate: requestTime,
          responseDate: DateTime.now().toString(),
          screenName: "orderStock",
          error: error,
        );
      }
      MainController mainController = Get.put(MainController());
      mainController.showErrorPopup();
    });
  } catch (e) {
    Helper.close();
    if (e is DioException) {
      UDID.setTraceId(
        e.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "ERROR_EVENT",
        failure: true,
        endPoint: "pos-cart-service-svc/generic-carts",
        requestDate: requestTime,
        responseDate: DateTime.now().toString(),
        screenName: "orderStock",
        error: e,
      );
    }
  }
}