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

@@ -7,6 +7,7 @@ package db
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
@@ -72,6 +73,62 @@ func (q *Queries) GetAbnormalItemCounts(ctx context.Context, warehouseID pgtype.
return items, nil
}
const getAnomalyItems = `-- name: GetAnomalyItems :many
SELECT ci.id, ci.component_id, ci.container_id, ci.quantity, ci.status, ci.created_at, ci.updated_at,
c.name AS component_name, c.unit AS component_unit
FROM component_items ci
JOIN components c ON ci.component_id = c.id
JOIN containers con ON ci.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 ci.status != 'normal'
AND ($1::bigint IS NULL OR r.warehouse_id = $1::bigint)
ORDER BY ci.updated_at DESC
`
type GetAnomalyItemsRow struct {
ID int64 `db:"id" json:"id"`
ComponentID int64 `db:"component_id" json:"componentId"`
ContainerID int64 `db:"container_id" json:"containerId"`
Quantity int32 `db:"quantity" json:"quantity"`
Status ComponentItemStatusEnum `db:"status" json:"status"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
ComponentName string `db:"component_name" json:"componentName"`
ComponentUnit string `db:"component_unit" json:"componentUnit"`
}
func (q *Queries) GetAnomalyItems(ctx context.Context, warehouseID pgtype.Int8) ([]GetAnomalyItemsRow, error) {
rows, err := q.db.Query(ctx, getAnomalyItems, warehouseID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAnomalyItemsRow
for rows.Next() {
var i GetAnomalyItemsRow
if err := rows.Scan(
&i.ID,
&i.ComponentID,
&i.ContainerID,
&i.Quantity,
&i.Status,
&i.CreatedAt,
&i.UpdatedAt,
&i.ComponentName,
&i.ComponentUnit,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getContainerStats = `-- name: GetContainerStats :one
SELECT
COUNT(*) AS total_containers,
@@ -96,6 +153,53 @@ func (q *Queries) GetContainerStats(ctx context.Context, warehouseID pgtype.Int8
return i, err
}
const getStockAlerts = `-- name: GetStockAlerts :many
SELECT c.id, c.name, c.unit, c.total_quantity, c.min_quantity, c.component_type_id,
ct.name AS component_type_name
FROM components c
LEFT JOIN component_types ct ON c.component_type_id = ct.id
WHERE c.total_quantity <= c.min_quantity
ORDER BY (c.total_quantity - c.min_quantity) ASC
`
type GetStockAlertsRow struct {
ID int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Unit string `db:"unit" json:"unit"`
TotalQuantity int32 `db:"total_quantity" json:"totalQuantity"`
MinQuantity int32 `db:"min_quantity" json:"minQuantity"`
ComponentTypeID int64 `db:"component_type_id" json:"componentTypeId"`
ComponentTypeName pgtype.Text `db:"component_type_name" json:"componentTypeName"`
}
func (q *Queries) GetStockAlerts(ctx context.Context) ([]GetStockAlertsRow, error) {
rows, err := q.db.Query(ctx, getStockAlerts)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetStockAlertsRow
for rows.Next() {
var i GetStockAlertsRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Unit,
&i.TotalQuantity,
&i.MinQuantity,
&i.ComponentTypeID,
&i.ComponentTypeName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getTodayInvoiceCounts = `-- name: GetTodayInvoiceCounts :many
SELECT type, COUNT(*) AS count
FROM invoices