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

@@ -42,3 +42,24 @@ JOIN cabinets cab ON s.cabinet_id = cab.id
JOIN rooms r ON cab.room_id = r.id
LEFT JOIN component_items ci ON c.id = ci.container_id
WHERE sqlc.narg('warehouse_id')::bigint IS NULL OR r.warehouse_id = sqlc.narg('warehouse_id')::bigint;
-- 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;
-- 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 (sqlc.narg('warehouse_id')::bigint IS NULL OR r.warehouse_id = sqlc.narg('warehouse_id')::bigint)
ORDER BY ci.updated_at DESC;