feat: add dashboard summary endpoint and related models, queries, and services
This commit is contained in:
61
internal/repositories/dashboard_repository.go
Normal file
61
internal/repositories/dashboard_repository.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func GetDashboardSummary(ctx context.Context, queries *db.Queries, warehouseID pgtype.Int8) (models.DashboardSummary, error) {
|
||||
totalStats, err := queries.GetTotalComponentStats(ctx, warehouseID)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
pendingInvoices, err := queries.CountPendingInvoices(ctx)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
lowStockCount, err := queries.CountLowStockComponents(ctx)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
abnormalRows, err := queries.GetAbnormalItemCounts(ctx, warehouseID)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
todayInvoiceRows, err := queries.GetTodayInvoiceCounts(ctx)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
containerStats, err := queries.GetContainerStats(ctx, warehouseID)
|
||||
if err != nil {
|
||||
return models.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
abnormalAlerts := make([]models.AbnormalAlert, 0, len(abnormalRows))
|
||||
for _, r := range abnormalRows {
|
||||
abnormalAlerts = append(abnormalAlerts, mapper.ToDomainAbnormalAlert(r))
|
||||
}
|
||||
|
||||
todayInvoices := make([]models.TodayInvoiceCount, 0, len(todayInvoiceRows))
|
||||
for _, r := range todayInvoiceRows {
|
||||
todayInvoices = append(todayInvoices, mapper.ToDomainTodayInvoiceCount(r))
|
||||
}
|
||||
|
||||
return models.DashboardSummary{
|
||||
TotalComponents: mapper.ToDomainTotalComponentStats(totalStats),
|
||||
PendingInvoices: pendingInvoices,
|
||||
LowStockComponents: lowStockCount,
|
||||
AbnormalAlerts: abnormalAlerts,
|
||||
TodayInvoices: todayInvoices,
|
||||
EmptyContainers: mapper.ToDomainContainerStats(containerStats),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user