diff --git a/api/src/api/v1/projects.py b/api/src/api/v1/projects.py index 120bd24..8017e95 100644 --- a/api/src/api/v1/projects.py +++ b/api/src/api/v1/projects.py @@ -33,6 +33,20 @@ router = APIRouter(tags=["forms"]) FORM3_ALLOWED_SECTIONS = {"q1", "q2", "q3", "q4", "year"} +def _parse_org_units_csv(org_units: Optional[str]) -> Optional[list[int]]: + if org_units is None: + return None + + values = [value.strip() for value in org_units.split(",") if value.strip()] + if not values: + return None + + try: + return [int(value) for value in values] + except ValueError as exc: + raise HTTPException(400, "org_units должен содержать ID через запятую") from exc + + def _parse_sections(sections: Optional[str]) -> Optional[list[str]]: if not sections: return None @@ -74,7 +88,7 @@ async def get_projects( @router.get("/projects/with-reports") async def get_projects_with_reports( year: Optional[int] = None, - branch_id: Optional[int] = None, + branch_id: Optional[str] = None, limit: Optional[int] = None, offset: Optional[int] = None, status_in: Optional[str] = None, @@ -82,6 +96,9 @@ async def get_projects_with_reports( db: AsyncSession = Depends(get_db), current_user: AppUser = Depends(get_current_active_user_with_set_db), ) -> BaseListResponse[dict]: + + branch_id = _parse_org_units_csv(branch_id) + project_service = ProjectService(db) status_in = status_in.split(",") if status_in is not None else None count, projects = await project_service.get_list_with_reports( diff --git a/api/src/repository/project_repository.py b/api/src/repository/project_repository.py index 3a9fc3a..a43fc67 100644 --- a/api/src/repository/project_repository.py +++ b/api/src/repository/project_repository.py @@ -42,7 +42,7 @@ class ProjectRepository: def _apply_project_filters( query, year: int | None, - branch_id: int | None, + branch_id: int | list[int] | None, org_unit_ids: list[int] | None, status_in: list[str] | None = None, search: str | None = None, @@ -60,7 +60,11 @@ class ProjectRepository: ) ) if branch_id is not None: - query = query.where(Project.org_unit_id == branch_id) + if isinstance(branch_id, int): + query = query.where(Project.org_unit_id == branch_id) + else: + query = query.where(Project.org_unit_id.in_(branch_id)) + if status_in is not None: query = query.where(Project.status.in_(status_in)) if search is not None and (normalized_search := search.strip()): diff --git a/api/src/services/project_service.py b/api/src/services/project_service.py index 2590e38..1f55470 100644 --- a/api/src/services/project_service.py +++ b/api/src/services/project_service.py @@ -39,7 +39,7 @@ class ProjectService: self, user: AppUser, year: int | None = None, - branch_id: int | None = None, + branch_id: int | list[int] | None = None, offset: int | None = None, limit: int | None = None, with_count: bool = False,