feat: nested department sections, attaching department sections in the user editor
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
from sqlalchemy import select, update, delete, insert, and_
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from backend import config
|
||||
from external.s3_uploader.uploader import S3Uploader
|
||||
from models import User, user_position, user_pay_rate, PassportImage
|
||||
from models import User, user_position, user_pay_rate, PassportImage, DepartmentSection, UserDepartmentSection
|
||||
from services.base import BaseService
|
||||
from schemas.user import *
|
||||
|
||||
@@ -11,6 +12,9 @@ class UserService(BaseService):
|
||||
async def get_all(self) -> GetAllUsersResponse:
|
||||
stmt = (
|
||||
select(User)
|
||||
.options(
|
||||
selectinload(User.department_sections),
|
||||
)
|
||||
.order_by(User.id.desc())
|
||||
.where(User.is_deleted == False)
|
||||
)
|
||||
@@ -99,6 +103,36 @@ class UserService(BaseService):
|
||||
except Exception as e:
|
||||
return UpdateUserResponse(ok=False, message=str(e))
|
||||
|
||||
async def update_department_sections(
|
||||
self,
|
||||
user_id: int,
|
||||
request: UpdateUserDepartmentSectionsRequest
|
||||
) -> UpdateUserDepartmentSectionsResponse:
|
||||
user = await self.get_by_id(user_id)
|
||||
if not user:
|
||||
return UpdateUserDepartmentSectionsResponse(ok=False, message=f"Пользователь с ID: {user_id} не найден")
|
||||
|
||||
stmt_delete = delete(UserDepartmentSection).where(UserDepartmentSection.user_id == user_id)
|
||||
await self.session.execute(stmt_delete)
|
||||
|
||||
for section_schema in request.department_sections:
|
||||
section = await self.session.get(DepartmentSection, section_schema.section_id)
|
||||
if not section:
|
||||
await self.session.rollback()
|
||||
return UpdateUserDepartmentSectionsResponse(
|
||||
ok=False, message=f"Отдел с ID: {section_schema.section_id} не найден"
|
||||
)
|
||||
user_section = UserDepartmentSection(
|
||||
user_id=user_id,
|
||||
is_chief=section_schema.is_chief,
|
||||
section_id=section_schema.section_id
|
||||
)
|
||||
user.department_sections.append(user_section)
|
||||
|
||||
await self.session.commit()
|
||||
|
||||
return UpdateUserDepartmentSectionsResponse(ok=True, message="Отделы пользователя успешно обновлены")
|
||||
|
||||
async def upload_passport_image(self, user_id: int, file_bytes: bytes) -> UploadPassportImageResponse:
|
||||
try:
|
||||
user: Optional[User] = await self.session.get(User, user_id)
|
||||
|
||||
Reference in New Issue
Block a user