vsp-fix-back: фикс багов по всп

This commit is contained in:
tsygankoviva 2026-06-09 12:06:54 +03:00
parent b5a6114389
commit b328afb52d
3 changed files with 36 additions and 7 deletions

View File

@ -24,9 +24,18 @@ from src.services.user_service import UserService
router = APIRouter(prefix="/users", tags=["users"]) router = APIRouter(prefix="/users", tags=["users"])
@router.get("/me", response_model=UserSchema) @router.get("/me", response_model=UserAdminListResponse)
async def get_current_user_info(current_user: AppUser = Depends(get_current_active_user)): async def get_current_user_info(
return current_user current_user: AppUser = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db),
load_orgs: bool = False,
):
if load_orgs:
user_service = UserService(db)
result = await user_service.get(user_id=current_user.id, load_orgs=True)
return UserAdminListResponse.model_validate(result)
else:
return current_user
@router.get("/") @router.get("/")

View File

@ -2,7 +2,7 @@ from __future__ import annotations
from datetime import date, datetime from datetime import date, datetime
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, Numeric, String, Text, func from sqlalchemy import Boolean, CheckConstraint, Date, DateTime, ForeignKey, Integer, Numeric, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.orm import Mapped, mapped_column, relationship
from src.db.base import Base from src.db.base import Base
@ -10,7 +10,17 @@ from src.db.base import Base
class Vsp(Base): class Vsp(Base):
__tablename__ = "vsp" __tablename__ = "vsp"
__table_args__ = {"schema": "v3"} __table_args__ = (
UniqueConstraint(
"reg_number", name="vsp_reg_number_unique",
),
CheckConstraint(
"closed_at IS NULL OR opened_at IS NULL OR closed_at >= opened_at",
name="chk_closed_after_opened",
),
{"schema": "v3"},
)
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True)
branch_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.org_unit.id")) branch_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.org_unit.id"))

View File

@ -39,7 +39,7 @@ class VSPService:
return vsp return vsp
ssp_ids = await self._get_scoped_ssp_ids(user=user) ssp_ids = await self._get_scoped_ssp_ids(user=user)
if not ssp_ids or vsp.ssp_id not in set(ssp_ids): if not ssp_ids or vsp.branch_id not in set(ssp_ids):
raise AccessDeniedException() raise AccessDeniedException()
return vsp return vsp
@ -129,6 +129,11 @@ class VSPService:
) )
return created return created
except IntegrityError as exc: except IntegrityError as exc:
message = str(getattr(exc, "orig", exc) or exc).strip()
if "vsp_reg_number_unique" in message:
raise ValidationException(description="Указанный номер уже существует")
if "chk_closed_after_opened" in message:
raise ValidationException(description="Дата закрытия не может быть меньше даты открытия")
raise ValidationException(description="Некорректные данные для записи ВСП") from exc raise ValidationException(description="Некорректные данные для записи ВСП") from exc
async def export_xlsx( async def export_xlsx(
@ -232,7 +237,7 @@ class VSPService:
if user.role_id != UserRoleEnum.ADMIN: if user.role_id != UserRoleEnum.ADMIN:
ssp_ids = await self._get_scoped_ssp_ids(user=user) ssp_ids = await self._get_scoped_ssp_ids(user=user)
if not ssp_ids or vsp.ssp_id not in set(ssp_ids): if not ssp_ids or vsp.branch_id not in set(ssp_ids):
raise AccessDeniedException() raise AccessDeniedException()
if not set(data.keys()).issubset(self.EXECUTOR_EDITABLE_FIELDS): if not set(data.keys()).issubset(self.EXECUTOR_EDITABLE_FIELDS):
raise AccessDeniedException() raise AccessDeniedException()
@ -246,6 +251,11 @@ class VSPService:
vsp=vsp, data=data, updated_by=user.id, load_org_unit=load_org_unit, vsp=vsp, data=data, updated_by=user.id, load_org_unit=load_org_unit,
) )
except IntegrityError as exc: except IntegrityError as exc:
message = str(getattr(exc, "orig", exc) or exc).strip()
if "vsp_reg_number_unique" in message:
raise ValidationException(description="Указанный номер уже существует")
if "chk_closed_after_opened" in message:
raise ValidationException(description="Дата закрытия не может быть меньше даты открытия")
raise ValidationException(description="Некорректные данные для записи ВСП") from exc raise ValidationException(description="Некорректные данные для записи ВСП") from exc
async def logical_delete(self, vsp_id: int, user: AppUser) -> bool: async def logical_delete(self, vsp_id: int, user: AppUser) -> bool: