from sqlalchemy.orm import Session from datetime import datetime from typing import Optional from fastapi import HTTPException from app.modules.branches.models import Branches from app.modules.branches.schema import class BranchesService: @staticmethod def create_branches(db: Session, data: ): new_branches = Branches(**data.dict()) db.add(new_branches) db.commit() db.refresh(new_branches) return new_branches @staticmethod def get_branches(db: Session, branches_id: int, current_user): branches = db.query(Branches).filter(Branches.id == branches_id).first() if not branches: raise ValueError("branches no encontrado") return branches @staticmethod def update_branches(db: Session, branches_id: int, data: , current_user): branches = db.query(Branches).filter(Branches.id == branches_id).first() if not branches: raise ValueError("branches no encontrado") if current_user.role not in ["ROOT", "ADMIN"]: raise ValueError("No tienes permisos para actualizar este branches") update_data = data.dict(exclude_unset=True) for key, value in update_data.items(): setattr(branches, key, value) db.commit() db.refresh(branches) return branches @staticmethod def delete_branches(db: Session, branches_id: int, current_user): branches = db.query(Branches).filter(Branches.id == branches_id).first() if not branches: raise ValueError("branches no encontrado") if current_user.role not in ["ROOT", "ADMIN"]: raise ValueError("No tienes permisos para eliminar este branches") try: branches.is_active = False branches.deleted_at = datetime.utcnow() branches.deleted_by = current_user.id db.commit() db.refresh(branches) except Exception as e: db.rollback() raise ValueError(f"Error al eliminar el branches: {e}") return {"message": "branches eliminado correctamente"}