feat: add endpoint and logic for retrieving top exported components, including SQL queries, models, and service integration

This commit is contained in:
Tran Anh Tuan
2026-05-14 11:02:09 +07:00
parent 96bc22942b
commit cee0186225
12 changed files with 431 additions and 4 deletions

View File

@@ -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
}