Ejemplo n.º 1
0
        /// <summary>Update order details</summary>
        /// <param name="orderId"></param>
        public async static Task <string> UpdatePaymentDetails(long orderId)
        {
            try
            {
                PaymentMethod     paymentDetails = new PaymentMethod();
                List <StatusCode> statusCodeList = new List <StatusCode>();
                var orderData = BsonSerializer.Deserialize <OrderInfo>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", orderId), "OrderDB", "OrderInfo").Result);
                foreach (var detail in orderData.PaymentDetails.Status)
                {
                    statusCodeList.Add(detail);
                }
                statusCodeList.Add(new StatusCode {
                    StatusId = 2, Description = "Payment Received", Date = DateTime.UtcNow
                });
                paymentDetails.Status = statusCodeList;
                var updatePaymentDetails = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", orderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentDetails", paymentDetails));

                return("Success");
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("MongoHelper", "UpdatePaymentDetails", ex.Message);
                return(null);
            }
        }
        public ActionResult Delete(string productSKU)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("Product_SKU", productSKU);

                var product = MH.GetSingleObject(filter, "ProductDB", "Product").Result;
                if (product != null)
                {
                    var authCollection = _db.GetCollection <Product>("Product");
                    var response       = authCollection.DeleteOneAsync(product);
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Deleted",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Product Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("ProductController", "Delete", "Delete", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
        public ActionResult Delete(string productFor, string productType)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("ProductFor", productFor) & Builders <BsonDocument> .Filter.Eq("ProductType", productType);

                var product = MH.GetSingleObject(filter, "ProductDB", "Category").Result;
                if (product != null)
                {
                    var response = MH.DeleteSingleObject(filter, "ProductDB", "Category");
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Deleted",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Category Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CategoryController", "Delete", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
        public async Task <ActionResult> ReturnProduct([FromBody] OrderInfo data, string request, string username, string productSKU)
        {
            try
            {
                if (request == "refund" || request == "replace")
                {
                    var orders = GH.GetOrders(username, order_db).Result;
                    if (orders == null)
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "404",
                            Message = "No orders found",
                            Data = null
                        }));
                    }
                    else
                    {
                        IAsyncCursor <OrderInfo> orderCursor = await order_db.GetCollection <OrderInfo>("OrderInfo").FindAsync(Builders <OrderInfo> .Filter.Eq("UserName", username) & Builders <OrderInfo> .Filter.Eq("OrderId", data.OrderId));

                        var order = orderCursor.FirstOrDefaultAsync().Result;
                        if (order == null)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "404",
                                Message = "Order Not Found",
                                Data = null
                            }));
                        }
                        foreach (var productDetails in order.ProductDetails)
                        {
                            if (productDetails.ProductSKU == productSKU)
                            {
                                var productData = BsonSerializer.Deserialize <Product>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", productSKU), "ProductDB", "Product").Result);
                                if (productDetails.Status != "Delivered")
                                {
                                    return(BadRequest(new ResponseData
                                    {
                                        Code = "401",
                                        Message = "Order Return Request Failed",
                                        Data = null
                                    }));
                                }
                                else
                                {
                                    if (request == "refund")
                                    {
                                        if (productData.RefundApplicable == false)
                                        {
                                            return(BadRequest(new ResponseData
                                            {
                                                Code = "402",
                                                Message = "Refund not applicable for this product",
                                                Data = null
                                            }));
                                        }
                                    }
                                    if (request == "replace")
                                    {
                                        if (productData.ReplacementApplicable == false)
                                        {
                                            return(BadRequest(new ResponseData
                                            {
                                                Code = "403",
                                                Message = "Replacement not applicable for this product",
                                                Data = null
                                            }));
                                        }
                                    }
                                    PaymentMethod paymentMethod = new PaymentMethod();
                                    paymentMethod.Method = order.PaymentMethod;
                                    List <StatusCode> paymentList = new List <StatusCode>();
                                    int i = 1;
                                    foreach (var status in order.PaymentDetails.Status)
                                    {
                                        paymentList.Add(status);
                                        i++;
                                    }
                                    StatusCode paymentStatus = new StatusCode();
                                    paymentStatus.StatusId = i;
                                    paymentStatus.Date     = DateTime.UtcNow;
                                    if (request == "refund")
                                    {
                                        paymentStatus.Description = "Refund Initiated";
                                    }
                                    if (request == "replace")
                                    {
                                        paymentStatus.Description = "Replacement Initiated";
                                    }
                                    paymentList.Add(paymentStatus);
                                    paymentMethod.Status = paymentList;
                                    var paymentUpdate = Builders <BsonDocument> .Update.Set("PaymentDetails", paymentMethod);

                                    var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", paymentUpdate).Result;
                                    List <ProductDetails> productDetailsList = new List <ProductDetails>();
                                    foreach (var product in order.ProductDetails)
                                    {
                                        if (product.ProductSKU == productSKU)
                                        {
                                            ProductDetails details = new ProductDetails();
                                            details.ProductSKU = productSKU;
                                            details.Status     = "Refund Initiated";
                                            List <StatusCode> productStatusList = new List <StatusCode>();
                                            int j = 1;
                                            foreach (var status in product.StatusCode)
                                            {
                                                productStatusList.Add(status);
                                                j++;
                                            }
                                            StatusCode productStatus = new StatusCode();
                                            productStatus.StatusId = j;
                                            productStatus.Date     = DateTime.UtcNow;
                                            if (request == "refund")
                                            {
                                                productStatus.Description = "Refund Initiated";
                                            }
                                            if (request == "replace")
                                            {
                                                productStatus.Description = "Replacement Initiated";
                                            }
                                            productStatusList.Add(productStatus);
                                            details.StatusCode    = productStatusList;
                                            details.ProductInCart = product.ProductInCart;
                                            productDetailsList.Add(details);
                                        }
                                        else
                                        {
                                            productDetailsList.Add(product);
                                        }
                                    }
                                    var productUpdate = Builders <BsonDocument> .Update.Set("ProductDetails", productDetailsList);

                                    var responce = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", productUpdate).Result;
                                }
                            }
                        }
                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Success",
                            Data = null
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "405",
                        Message = "Invalid Request",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("OrderController", "ReturnProduct", "ReturnProduct", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }