feat: add endpoint and logic for retrieving transaction chart data, including SQL queries, models, and service integration
This commit is contained in:
@@ -253,3 +253,50 @@ func (q *Queries) GetTotalComponentStats(ctx context.Context, warehouseID pgtype
|
||||
err := row.Scan(&i.TotalTypes, &i.TotalQuantity)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getTransactionChartData = `-- name: GetTransactionChartData :many
|
||||
SELECT DATE(st.created_at) AS date, st.transaction_type, COALESCE(SUM(st.quantity), 0)::bigint AS total_quantity
|
||||
FROM stock_transactions st
|
||||
JOIN containers con ON st.container_id = con.id
|
||||
JOIN shelves s ON con.shelf_id = s.id
|
||||
JOIN cabinets cab ON s.cabinet_id = cab.id
|
||||
JOIN rooms r ON cab.room_id = r.id
|
||||
WHERE st.transaction_type IN ('import', 'export')
|
||||
AND st.created_at >= $1::timestamptz
|
||||
AND st.created_at < $2::timestamptz
|
||||
AND ($3::bigint IS NULL OR r.warehouse_id = $3::bigint)
|
||||
GROUP BY DATE(st.created_at), st.transaction_type
|
||||
ORDER BY DATE(st.created_at) ASC
|
||||
`
|
||||
|
||||
type GetTransactionChartDataParams struct {
|
||||
StartDate time.Time `db:"start_date" json:"startDate"`
|
||||
EndDate time.Time `db:"end_date" json:"endDate"`
|
||||
WarehouseID pgtype.Int8 `db:"warehouse_id" json:"warehouseId"`
|
||||
}
|
||||
|
||||
type GetTransactionChartDataRow struct {
|
||||
Date pgtype.Date `db:"date" json:"date"`
|
||||
TransactionType TransactionTypeEnum `db:"transaction_type" json:"transactionType"`
|
||||
TotalQuantity int64 `db:"total_quantity" json:"totalQuantity"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTransactionChartData(ctx context.Context, arg GetTransactionChartDataParams) ([]GetTransactionChartDataRow, error) {
|
||||
rows, err := q.db.Query(ctx, getTransactionChartData, arg.StartDate, arg.EndDate, arg.WarehouseID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTransactionChartDataRow
|
||||
for rows.Next() {
|
||||
var i GetTransactionChartDataRow
|
||||
if err := rows.Scan(&i.Date, &i.TransactionType, &i.TotalQuantity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ type Querier interface {
|
||||
GetStockAlerts(ctx context.Context) ([]GetStockAlertsRow, error)
|
||||
GetTodayInvoiceCounts(ctx context.Context) ([]GetTodayInvoiceCountsRow, error)
|
||||
GetTotalComponentStats(ctx context.Context, warehouseID pgtype.Int8) (GetTotalComponentStatsRow, error)
|
||||
GetTransactionChartData(ctx context.Context, arg GetTransactionChartDataParams) ([]GetTransactionChartDataRow, error)
|
||||
GetUserByEmail(ctx context.Context, email string) (User, error)
|
||||
GetUserByID(ctx context.Context, id uuid.UUID) (User, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (User, error)
|
||||
|
||||
Reference in New Issue
Block a user