@@ -0,0 +1,57 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from sqlalchemy import select
|
||||
|
||||
from app import db
|
||||
from app.models import ComponentStock
|
||||
from app.routes.auth_decorators import require_auth
|
||||
|
||||
bp = Blueprint("sklad", __name__, url_prefix="/api/sklad")
|
||||
|
||||
|
||||
def _error(message: str, status_code: int = 400):
|
||||
return jsonify({"error": True, "message": message}), status_code
|
||||
|
||||
|
||||
@bp.get("/ping")
|
||||
def sklad_ping():
|
||||
"""Временный health-check эндпоинт для модуля склада."""
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
|
||||
@bp.get("/components")
|
||||
@require_auth
|
||||
def list_component_stock():
|
||||
"""Список складских остатков с пагинацией."""
|
||||
try:
|
||||
limit = int(request.args.get("limit", 100))
|
||||
offset = int(request.args.get("offset", 0))
|
||||
except ValueError:
|
||||
return _error("Некорректные параметры пагинации", 400)
|
||||
|
||||
rows = db.session.execute(
|
||||
select(ComponentStock)
|
||||
.where(ComponentStock.is_deleted.is_(False))
|
||||
.order_by(
|
||||
ComponentStock.sort_order.asc(),
|
||||
ComponentStock.updated_at.desc(),
|
||||
)
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
).scalars().all()
|
||||
return jsonify(
|
||||
[
|
||||
{
|
||||
"id": s.id,
|
||||
"component_id": s.component_id,
|
||||
"component_name": s.component_name,
|
||||
"total_kg": s.total_kg,
|
||||
"inflow_kg": s.inflow_kg,
|
||||
"baseline_consumed_kg": s.baseline_consumed_kg,
|
||||
"stocktake_at": s.stocktake_at.isoformat() if s.stocktake_at else None,
|
||||
"updated_at": s.updated_at.isoformat() if s.updated_at else None,
|
||||
"updated_by": s.updated_by,
|
||||
}
|
||||
for s in rows
|
||||
]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user