feat: add endpoint and logic for retrieving top exported components, including SQL queries, models, and service integration
This commit is contained in:
@@ -66,3 +66,13 @@ func ToDomainTransactionChartRow(r db.GetTransactionChartDataRow) models.Transac
|
||||
TotalQuantity: r.TotalQuantity,
|
||||
}
|
||||
}
|
||||
|
||||
func ToDomainTopExportedComponent(r db.GetTopExportedComponentsRow) models.TopExportedComponent {
|
||||
return models.TopExportedComponent{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Unit: r.Unit,
|
||||
ComponentTypeName: r.ComponentTypeName.String,
|
||||
TotalExported: r.TotalExported,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,3 +68,11 @@ type TransactionChartItem struct {
|
||||
type TransactionChartData struct {
|
||||
Items []TransactionChartItem `json:"items"`
|
||||
}
|
||||
|
||||
type TopExportedComponent struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Unit string `json:"unit"`
|
||||
ComponentTypeName string `json:"componentTypeName"`
|
||||
TotalExported int64 `json:"totalExported"`
|
||||
}
|
||||
|
||||
@@ -125,3 +125,20 @@ func GetTransactionChartData(ctx context.Context, queries *db.Queries, startDate
|
||||
|
||||
return models.TransactionChartData{Items: items}, nil
|
||||
}
|
||||
|
||||
func GetTopExportedComponents(ctx context.Context, queries *db.Queries, startDate, endDate time.Time, warehouseID pgtype.Int8, limitCount int32) ([]models.TopExportedComponent, error) {
|
||||
results, err := queries.GetTopExportedComponents(ctx, db.GetTopExportedComponentsParams{
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
WarehouseID: warehouseID,
|
||||
LimitCount: limitCount,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]models.TopExportedComponent, 0, len(results))
|
||||
for _, r := range results {
|
||||
items = append(items, mapper.ToDomainTopExportedComponent(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -159,6 +159,7 @@ func NewRouter() *gin.Engine {
|
||||
dashboard.GET("/stock-alerts", utils.AsyncHandler(services.DashboardStockAlerts))
|
||||
dashboard.GET("/anomalies", utils.AsyncHandler(services.DashboardAnomalies))
|
||||
dashboard.GET("/transactions-chart", utils.AsyncHandler(services.DashboardTransactionsChart))
|
||||
dashboard.GET("/top-components", utils.AsyncHandler(services.DashboardTopComponents))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,3 +146,68 @@ func DashboardTransactionsChart(c *gin.Context) error {
|
||||
response.Ok(c, "Success", data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// @Summary Get top exported components
|
||||
// @Description Retrieve top components by export quantity for chart display
|
||||
// @Tags dashboard
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param period query string false "Time period: today, 7d, 30d, this_month" default(30d)
|
||||
// @Param warehouse_id query int false "Filter by warehouse ID"
|
||||
// @Param component_quantities query int false "Number of components to return" default(10)
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.TopExportedComponent}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/dashboard/top-components [get]
|
||||
func DashboardTopComponents(c *gin.Context) error {
|
||||
period := c.DefaultQuery("period", "30d")
|
||||
|
||||
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}
|
||||
}
|
||||
|
||||
var limitCount int32 = 10
|
||||
if raw := c.Query("component_quantities"); raw != "" {
|
||||
val, err := strconv.ParseInt(raw, 10, 32)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid component_quantities")
|
||||
return nil
|
||||
}
|
||||
limitCount = int32(val)
|
||||
}
|
||||
|
||||
data, err := repositories.GetTopExportedComponents(c.Request.Context(), global.Queries, startDate, endDate, warehouseID, limitCount)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Error when Get Top Exported Components")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to get top exported components")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", data)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user