feat: fix stock check and add admin order status update
This commit is contained in:
@@ -278,11 +278,13 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
var inventory models.Inventory
|
||||
if err := utils.DB.Where("product_id = ?", cart.ProductID).First(&inventory).Error; err == nil {
|
||||
if inventory.Quantity < cart.Quantity {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Insufficient stock for " + cart.Product.Name})
|
||||
return
|
||||
}
|
||||
if err := utils.DB.Where("product_id = ?", cart.ProductID).First(&inventory).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No inventory record for " + cart.Product.Name})
|
||||
return
|
||||
}
|
||||
if inventory.Quantity < cart.Quantity {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Insufficient stock for " + cart.Product.Name + ". Available: " + strconv.Itoa(inventory.Quantity)})
|
||||
return
|
||||
}
|
||||
|
||||
subtotal += cart.Product.Price * float64(cart.Quantity)
|
||||
@@ -723,3 +725,52 @@ func (h *OrderHandler) ConfirmReceipt(c *gin.Context) {
|
||||
utils.DB.Model(&order).Update("status", models.OrderStatusCompleted)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Order confirmed successfully"})
|
||||
}
|
||||
|
||||
func (h *OrderHandler) AdminUpdateStatus(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var order models.Order
|
||||
if err := utils.DB.First(&order, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Order not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Status string `json:"status" binding:"required"`
|
||||
TrackingNumber string `json:"tracking_number"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
validStatuses := []string{
|
||||
models.OrderStatusPendingPayment,
|
||||
models.OrderStatusPendingConfirm,
|
||||
models.OrderStatusPendingShip,
|
||||
models.OrderStatusShipped,
|
||||
models.OrderStatusCompleted,
|
||||
models.OrderStatusRefunding,
|
||||
models.OrderStatusRefunded,
|
||||
models.OrderStatusCancelled,
|
||||
}
|
||||
valid := false
|
||||
for _, s := range validStatuses {
|
||||
if s == req.Status {
|
||||
valid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid status"})
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{"status": req.Status}
|
||||
if req.TrackingNumber != "" {
|
||||
updates["tracking_number"] = req.TrackingNumber
|
||||
}
|
||||
|
||||
utils.DB.Model(&order).Updates(updates)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Order status updated successfully"})
|
||||
}
|
||||
|
||||
@@ -188,9 +188,10 @@ func SetupRoutes(r *gin.Engine) {
|
||||
adminOrders := admin.Group("/orders")
|
||||
{
|
||||
adminOrders.GET("", orderHandler.AdminList)
|
||||
adminOrders.GET("/:id", orderHandler.AdminGetByID)
|
||||
adminOrders.GET("/:id", orderHandler.AdminGetByID)
|
||||
adminOrders.GET("/export", orderHandler.Export)
|
||||
adminOrders.PUT("/:id/refund", orderHandler.ProcessRefund)
|
||||
adminOrders.PUT("/:id/status", orderHandler.AdminUpdateStatus)
|
||||
}
|
||||
|
||||
adminLotteries := admin.Group("/lotteries")
|
||||
|
||||
Reference in New Issue
Block a user