149 lines
5.5 KiB
Go
149 lines
5.5 KiB
Go
package services
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
"wm-backend/global"
|
|
"wm-backend/internal/repositories"
|
|
"wm-backend/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// @Summary Get dashboard summary
|
|
// @Description Retrieve dashboard summary with key statistics
|
|
// @Tags dashboard
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param warehouse_id query int false "Filter by warehouse ID"
|
|
// @Success 200 {object} response.SuccessResponse{data=models.DashboardSummary}
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/dashboard/summary [get]
|
|
func DashboardSummary(c *gin.Context) error {
|
|
var warehouseID pgtype.Int8
|
|
if raw := c.Query("warehouse_id"); raw != "" {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
response.BadRequestError(c, http.StatusBadRequest, "Invalid warehouse_id")
|
|
return nil
|
|
}
|
|
warehouseID = pgtype.Int8{Int64: id, Valid: true}
|
|
}
|
|
|
|
summary, err := repositories.GetDashboardSummary(c.Request.Context(), global.Queries, warehouseID)
|
|
if err != nil {
|
|
log.Err(err).Msg("Error when Get Dashboard Summary")
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to get dashboard summary")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", summary)
|
|
return nil
|
|
}
|
|
|
|
// @Summary Get stock alerts
|
|
// @Description Retrieve list of components that are low on stock (total_quantity <= min_quantity)
|
|
// @Tags dashboard
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.SuccessResponse{data=[]models.StockAlert}
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/dashboard/stock-alerts [get]
|
|
func DashboardStockAlerts(c *gin.Context) error {
|
|
alerts, err := repositories.GetStockAlerts(c.Request.Context(), global.Queries)
|
|
if err != nil {
|
|
log.Err(err).Msg("Error when Get Stock Alerts")
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to get stock alerts")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", alerts)
|
|
return nil
|
|
}
|
|
|
|
// @Summary Get anomaly items
|
|
// @Description Retrieve list of component items with abnormal status (damaged, expired, long_unused, pending_inspection)
|
|
// @Tags dashboard
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param warehouse_id query int false "Filter by warehouse ID"
|
|
// @Success 200 {object} response.SuccessResponse{data=[]models.AnomalyItem}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/dashboard/anomalies [get]
|
|
func DashboardAnomalies(c *gin.Context) error {
|
|
var warehouseID pgtype.Int8
|
|
if raw := c.Query("warehouse_id"); raw != "" {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
response.BadRequestError(c, http.StatusBadRequest, "Invalid warehouse_id")
|
|
return nil
|
|
}
|
|
warehouseID = pgtype.Int8{Int64: id, Valid: true}
|
|
}
|
|
|
|
anomalies, err := repositories.GetAnomalyItems(c.Request.Context(), global.Queries, warehouseID)
|
|
if err != nil {
|
|
log.Err(err).Msg("Error when Get Anomaly Items")
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to get anomaly items")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", anomalies)
|
|
return nil
|
|
}
|
|
|
|
// @Summary Get transactions chart data
|
|
// @Description Retrieve import/export transaction quantities grouped by date for chart display
|
|
// @Tags dashboard
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param period query string false "Time period: today, 7d, 30d, this_month" default(7d)
|
|
// @Param warehouse_id query int false "Filter by warehouse ID"
|
|
// @Success 200 {object} response.SuccessResponse{data=models.TransactionChartData}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/dashboard/transactions-chart [get]
|
|
func DashboardTransactionsChart(c *gin.Context) error {
|
|
period := c.DefaultQuery("period", "7d")
|
|
|
|
now := time.Now()
|
|
var startDate, endDate time.Time
|
|
switch period {
|
|
case "today":
|
|
startDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
endDate = startDate.AddDate(0, 0, 1)
|
|
case "7d":
|
|
startDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -6)
|
|
endDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
|
|
case "30d":
|
|
startDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -29)
|
|
endDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
|
|
case "this_month":
|
|
startDate = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
endDate = startDate.AddDate(0, 1, 0)
|
|
default:
|
|
response.BadRequestError(c, http.StatusBadRequest, "Invalid period. Use: today, 7d, 30d, this_month")
|
|
return nil
|
|
}
|
|
|
|
var warehouseID pgtype.Int8
|
|
if raw := c.Query("warehouse_id"); raw != "" {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
response.BadRequestError(c, http.StatusBadRequest, "Invalid warehouse_id")
|
|
return nil
|
|
}
|
|
warehouseID = pgtype.Int8{Int64: id, Valid: true}
|
|
}
|
|
|
|
data, err := repositories.GetTransactionChartData(c.Request.Context(), global.Queries, startDate, endDate, warehouseID)
|
|
if err != nil {
|
|
log.Err(err).Msg("Error when Get Transaction Chart Data")
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to get transaction chart data")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", data)
|
|
return nil
|
|
}
|