1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Testing workable

This commit is contained in:
MoonTestUse1
2025-02-07 00:43:33 +06:00
parent 8543d7fe88
commit 6db95a5eb0
37 changed files with 911 additions and 454 deletions

View File

@@ -1,6 +1,6 @@
"""Employee CRUD operations"""
from sqlalchemy.orm import Session
from typing import List, Optional
from typing import Optional, List
from ..models.employee import Employee
from ..schemas.employee import EmployeeCreate, EmployeeUpdate
from ..utils.loggers import auth_logger
@@ -13,42 +13,58 @@ def get_employee(db: Session, employee_id: int) -> Optional[Employee]:
"""Get employee by ID"""
return db.query(Employee).filter(Employee.id == employee_id).first()
def get_employee_by_credentials(db: Session, first_name: str, last_name: str) -> Optional[Employee]:
"""Get employee by first name and last name"""
return db.query(Employee).filter(
Employee.first_name == first_name,
Employee.last_name == last_name
).first()
def get_employee_by_email(db: Session, email: str) -> Optional[Employee]:
"""Get employee by email"""
return db.query(Employee).filter(Employee.email == email).first()
def create_employee(db: Session, employee: EmployeeCreate, hashed_password: str) -> Employee:
"""Create new employee"""
db_employee = Employee(
first_name=employee.first_name,
last_name=employee.last_name,
department=employee.department,
office=employee.office,
hashed_password=hashed_password,
is_admin=employee.is_admin
)
db.add(db_employee)
db.commit()
db.refresh(db_employee)
return db_employee
def update_employee(db: Session, employee_id: int, employee: EmployeeUpdate) -> Optional[Employee]:
"""Update employee data"""
db_employee = get_employee(db, employee_id)
if db_employee:
for key, value in employee.dict(exclude_unset=True).items():
setattr(db_employee, key, value)
try:
db_employee = Employee(
email=employee.email,
full_name=employee.full_name,
hashed_password=hashed_password,
is_active=employee.is_active,
is_admin=employee.is_admin,
department=employee.department
)
db.add(db_employee)
db.commit()
db.refresh(db_employee)
return db_employee
return db_employee
except Exception as e:
auth_logger.error(f"Error creating employee: {e}")
db.rollback()
raise
def update_employee(db: Session, employee_id: int, employee: EmployeeUpdate) -> Optional[Employee]:
"""Update employee"""
db_employee = get_employee(db, employee_id)
if not db_employee:
return None
for field, value in employee.model_dump(exclude_unset=True).items():
setattr(db_employee, field, value)
try:
db.commit()
db.refresh(db_employee)
return db_employee
except Exception as e:
auth_logger.error(f"Error updating employee: {e}")
db.rollback()
raise
def delete_employee(db: Session, employee_id: int) -> Optional[Employee]:
"""Delete employee"""
db_employee = get_employee(db, employee_id)
if db_employee:
db.delete(db_employee)
db.commit()
return db_employee
try:
db.delete(db_employee)
db.commit()
return db_employee
except Exception as e:
auth_logger.error(f"Error deleting employee: {e}")
db.rollback()
raise
return None

View File

@@ -8,7 +8,13 @@ from . import employees
def create_request(db: Session, request: RequestCreate, employee_id: int) -> Request:
"""Create new request"""
# Получаем данные сотрудника
employee = employees.get_employee(db, employee_id)
if not employee:
raise ValueError("Employee not found")
db_request = Request(
department=employee.department, # Берем отдел из данных сотрудника
request_type=request.request_type,
description=request.description,
priority=request.priority,
@@ -24,6 +30,27 @@ def get_request(db: Session, request_id: int) -> Optional[Request]:
"""Get request by ID"""
return db.query(Request).filter(Request.id == request_id).first()
def get_request_details(db: Session, request_id: int) -> Optional[Dict]:
"""Get detailed request information including employee data"""
request = get_request(db, request_id)
if not request:
return None
employee = employees.get_employee(db, request.employee_id)
if not employee:
return None
return {
"id": request.id,
"request_type": request.request_type,
"description": request.description,
"priority": request.priority,
"status": request.status,
"department": request.department,
"created_at": request.created_at.isoformat(),
"employee_full_name": employee.full_name
}
def get_employee_requests(db: Session, employee_id: int) -> list[Request]:
"""Get employee's requests"""
return db.query(Request).filter(Request.employee_id == employee_id).all()
@@ -53,6 +80,12 @@ def get_statistics(db: Session) -> Dict:
func.count(Request.id)
).group_by(Request.status).all()
)
# Добавляем статусы с нулевым количеством
for status in RequestStatus:
if status not in by_status:
by_status[status] = 0
return {
"total": total,
"by_status": by_status

View File

@@ -3,9 +3,9 @@ from sqlalchemy.orm import Session
from typing import Optional
from ..models.token import Token
def create_token(db: Session, token: str, employee_id: int) -> Token:
def create_token(db: Session, token: str, user_id: int) -> Token:
"""Create new token"""
db_token = Token(token=token, employee_id=employee_id)
db_token = Token(token=token, user_id=user_id)
db.add(db_token)
db.commit()
db.refresh(db_token)
@@ -24,8 +24,8 @@ def delete_token(db: Session, token: str) -> bool:
return True
return False
def delete_employee_tokens(db: Session, employee_id: int) -> bool:
"""Delete all tokens for an employee"""
db.query(Token).filter(Token.employee_id == employee_id).delete()
def delete_user_tokens(db: Session, user_id: int) -> bool:
"""Delete all tokens for a user"""
db.query(Token).filter(Token.user_id == user_id).delete()
db.commit()
return True