From b328afb52ddec17043b957e73527fe090cec0ab7 Mon Sep 17 00:00:00 2001 From: tsygankoviva Date: Tue, 9 Jun 2026 12:06:54 +0300 Subject: [PATCH] =?UTF-8?q?vsp-fix-back:=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B3=D0=BE=D0=B2=20=D0=BF=D0=BE=20=D0=B2=D1=81?= =?UTF-8?q?=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/src/api/v1/users.py | 15 ++++++++++++--- api/src/db/models/vsp.py | 14 ++++++++++++-- api/src/services/vsp_service.py | 14 ++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/api/src/api/v1/users.py b/api/src/api/v1/users.py index 741862a..aeba3ce 100644 --- a/api/src/api/v1/users.py +++ b/api/src/api/v1/users.py @@ -24,9 +24,18 @@ from src.services.user_service import UserService router = APIRouter(prefix="/users", tags=["users"]) -@router.get("/me", response_model=UserSchema) -async def get_current_user_info(current_user: AppUser = Depends(get_current_active_user)): - return current_user +@router.get("/me", response_model=UserAdminListResponse) +async def get_current_user_info( + 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("/") diff --git a/api/src/db/models/vsp.py b/api/src/db/models/vsp.py index ff82815..ed24d10 100644 --- a/api/src/db/models/vsp.py +++ b/api/src/db/models/vsp.py @@ -2,7 +2,7 @@ from __future__ import annotations 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 src.db.base import Base @@ -10,7 +10,17 @@ from src.db.base import Base class Vsp(Base): __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) branch_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.org_unit.id")) diff --git a/api/src/services/vsp_service.py b/api/src/services/vsp_service.py index b19f060..efadb8a 100644 --- a/api/src/services/vsp_service.py +++ b/api/src/services/vsp_service.py @@ -39,7 +39,7 @@ class VSPService: return vsp 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() return vsp @@ -129,6 +129,11 @@ class VSPService: ) return created 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 async def export_xlsx( @@ -232,7 +237,7 @@ class VSPService: if user.role_id != UserRoleEnum.ADMIN: 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() if not set(data.keys()).issubset(self.EXECUTOR_EDITABLE_FIELDS): raise AccessDeniedException() @@ -246,6 +251,11 @@ class VSPService: vsp=vsp, data=data, updated_by=user.id, load_org_unit=load_org_unit, ) 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 async def logical_delete(self, vsp_id: int, user: AppUser) -> bool: