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

@@ -77,3 +77,21 @@ WHERE st.transaction_type IN ('import', 'export')
AND (sqlc.narg('warehouse_id')::bigint IS NULL OR r.warehouse_id = sqlc.narg('warehouse_id')::bigint)
GROUP BY DATE(st.created_at), st.transaction_type
ORDER BY DATE(st.created_at) ASC;
-- name: GetTopExportedComponents :many
SELECT c.id, c.name, c.unit, ct.name AS component_type_name,
COALESCE(SUM(st.quantity), 0)::bigint AS total_exported
FROM stock_transactions st
JOIN components c ON c.id = st.component_id
LEFT JOIN component_types ct ON c.component_type_id = ct.id
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 = 'export'
AND st.created_at >= sqlc.arg('start_date')::timestamptz
AND st.created_at < sqlc.arg('end_date')::timestamptz
AND (sqlc.narg('warehouse_id')::bigint IS NULL OR r.warehouse_id = sqlc.narg('warehouse_id')::bigint)
GROUP BY c.id, c.name, c.unit, ct.name
ORDER BY total_exported DESC
LIMIT sqlc.arg('limit_count')::int;