feat: add endpoints for retrieving stock alerts and anomaly items, including database queries and models

This commit is contained in:
Tran Anh Tuan
2026-05-13 18:10:34 +07:00
parent 383bed757d
commit 0a56dfeb61
11 changed files with 695 additions and 0 deletions

View File

@@ -32,3 +32,29 @@ func ToDomainContainerStats(r db.GetContainerStatsRow) models.ContainerStats {
EmptyContainers: int64(r.EmptyContainers),
}
}
func ToDomainStockAlert(r db.GetStockAlertsRow) models.StockAlert {
return models.StockAlert{
ID: r.ID,
Name: r.Name,
Unit: r.Unit,
TotalQuantity: r.TotalQuantity,
MinQuantity: r.MinQuantity,
ComponentTypeID: r.ComponentTypeID,
ComponentTypeName: r.ComponentTypeName.String,
}
}
func ToDomainAnomalyItem(r db.GetAnomalyItemsRow) models.AnomalyItem {
return models.AnomalyItem{
ID: r.ID,
ComponentID: r.ComponentID,
ContainerID: r.ContainerID,
Quantity: r.Quantity,
Status: string(r.Status),
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
ComponentName: r.ComponentName,
ComponentUnit: r.ComponentUnit,
}
}

View File

@@ -1,5 +1,7 @@
package models
import "time"
type TotalComponentStats struct {
TotalTypes int64 `json:"totalTypes"`
TotalQuantity int64 `json:"totalQuantity"`
@@ -28,3 +30,25 @@ type DashboardSummary struct {
TodayInvoices []TodayInvoiceCount `json:"todayInvoices"`
EmptyContainers ContainerStats `json:"emptyContainers"`
}
type StockAlert struct {
ID int64 `json:"id"`
Name string `json:"name"`
Unit string `json:"unit"`
TotalQuantity int32 `json:"totalQuantity"`
MinQuantity int32 `json:"minQuantity"`
ComponentTypeID int64 `json:"componentTypeId"`
ComponentTypeName string `json:"componentTypeName"`
}
type AnomalyItem struct {
ID int64 `json:"id"`
ComponentID int64 `json:"componentId"`
ContainerID int64 `json:"containerId"`
Quantity int32 `json:"quantity"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ComponentName string `json:"componentName"`
ComponentUnit string `json:"componentUnit"`
}

View File

@@ -59,3 +59,27 @@ func GetDashboardSummary(ctx context.Context, queries *db.Queries, warehouseID p
EmptyContainers: mapper.ToDomainContainerStats(containerStats),
}, nil
}
func GetStockAlerts(ctx context.Context, queries *db.Queries) ([]models.StockAlert, error) {
results, err := queries.GetStockAlerts(ctx)
if err != nil {
return nil, err
}
items := make([]models.StockAlert, 0, len(results))
for _, r := range results {
items = append(items, mapper.ToDomainStockAlert(r))
}
return items, nil
}
func GetAnomalyItems(ctx context.Context, queries *db.Queries, warehouseID pgtype.Int8) ([]models.AnomalyItem, error) {
results, err := queries.GetAnomalyItems(ctx, warehouseID)
if err != nil {
return nil, err
}
items := make([]models.AnomalyItem, 0, len(results))
for _, r := range results {
items = append(items, mapper.ToDomainAnomalyItem(r))
}
return items, nil
}

View File

@@ -156,6 +156,8 @@ func NewRouter() *gin.Engine {
dashboard := protected.Group(constants.API_GROUP_DASHBOARD)
{
dashboard.GET("/summary", utils.AsyncHandler(services.DashboardSummary))
dashboard.GET("/stock-alerts", utils.AsyncHandler(services.DashboardStockAlerts))
dashboard.GET("/anomalies", utils.AsyncHandler(services.DashboardAnomalies))
}
}
}

View File

@@ -41,3 +41,53 @@ func DashboardSummary(c *gin.Context) error {
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
}