adision de bases para trabajo completo de back-v1.0.0
This commit is contained in:
Binary file not shown.
BIN
app/modules/suppliers/__pycache__/route.cpython-311.pyc
Normal file
BIN
app/modules/suppliers/__pycache__/route.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/modules/suppliers/__pycache__/schema.cpython-311.pyc
Normal file
BIN
app/modules/suppliers/__pycache__/schema.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/modules/suppliers/__pycache__/service.cpython-311.pyc
Normal file
BIN
app/modules/suppliers/__pycache__/service.cpython-311.pyc
Normal file
Binary file not shown.
@@ -7,7 +7,7 @@ import enum
|
||||
|
||||
class SupplierType(str, enum.Enum):
|
||||
NACIONAL = "nacional"
|
||||
EXTRANJERO = "exttranjero"
|
||||
EXTRANJERO = "extranjero"
|
||||
GLOBAL = "global"
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Header, status
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional, List
|
||||
#
|
||||
from database import get_db
|
||||
from app.core.auth import get_current_user
|
||||
from app.modules.users.models import Users # Modelo base de usuario
|
||||
from app.modules.suppliers.models import Suppliers
|
||||
from app.modules.suppliers.schema import SupplierCreate, SupplierResponse, SupplierUpdate, messageResponse
|
||||
from app.modules.suppliers.service import SuppliersService
|
||||
|
||||
router = APIRouter(prefix="/suppliers", tags=["suppliers"])
|
||||
security = HTTPBearer()
|
||||
|
||||
#================ Create ====================
|
||||
@router.post("/create", response_model=SupplierResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_suppliers(
|
||||
data: SupplierCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Users = Depends(get_current_user)
|
||||
):
|
||||
"""Create a suppliers - Requires authentication"""
|
||||
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="User not found - invalid token")
|
||||
|
||||
if current_user.tipo_usuario not in ["root", "admin"]:
|
||||
raise HTTPException(status_code=403, detail="You don't have permission to create suppliers")
|
||||
|
||||
try:
|
||||
result = SuppliersService.create_suppliers(db=db, data=data)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
#================ Get ====================
|
||||
@router.get("/", response_model=List[SupplierResponse])
|
||||
def get_supplierss(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Users = Depends(get_current_user),
|
||||
skip: int = Query(0, ge=0, description="Number of records to skip"),
|
||||
limit: int = Query(100, ge=1, le=1000, description="Max number of records to return")
|
||||
):
|
||||
"""Get list with pagination - requires authentication"""
|
||||
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
return SuppliersService.get_suppliers(db=db, skip=skip, limit=limit)
|
||||
|
||||
#================ Get by ID ====================
|
||||
@router.get("/{{{entity_name}}_id}", response_model=SupplierResponse)
|
||||
def get_suppliers_by_id(
|
||||
suppliers_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Users = Depends(get_current_user)
|
||||
):
|
||||
"""Get suppliers by ID - requires authentication"""
|
||||
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
try:
|
||||
result = SuppliersService.get_suppliers(db=db, suppliers_id=suppliers_id, current_user=current_user)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
#================ Update ====================
|
||||
@router.patch("/update/{{{entity_name}}_id}", response_model=SupplierResponse)
|
||||
async def update_suppliers(
|
||||
suppliers_id: int,
|
||||
data: SupplierUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Users = Depends(get_current_user)
|
||||
):
|
||||
"""Update existing suppliers - requires authentication"""
|
||||
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
if current_user.tipo_usuario not in ["root", "admin"]:
|
||||
raise HTTPException(status_code=403, detail="You don't have permission to update suppliers")
|
||||
|
||||
try:
|
||||
result = SuppliersService.update_suppliers(
|
||||
db=db,
|
||||
suppliers_id=suppliers_id,
|
||||
data=data,
|
||||
current_user=current_user
|
||||
)
|
||||
return result
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
#================ Delete ====================
|
||||
@router.delete("/delete/{{{entity_name}}_id}", response_model=messageResponse, status_code=status.HTTP_200_OK)
|
||||
async def delete_suppliers(
|
||||
suppliers_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Users = Depends(get_current_user)
|
||||
):
|
||||
"""Delete suppliers by ID - requires authentication"""
|
||||
|
||||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
if current_user.tipo_usuario not in ["root", "admin"]:
|
||||
raise HTTPException(status_code=403, detail="You don't have permission to delete suppliers")
|
||||
|
||||
try:
|
||||
result = SuppliersService.delete_suppliers(db=db, suppliers_id=suppliers_id, current_user=current_user)
|
||||
return {"message": "suppliers deleted successfully"}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
@@ -0,0 +1,42 @@
|
||||
from pydantic import BaseModel, EmailStr, Field, validator, ConfigDict
|
||||
from datetime import datetime
|
||||
from typing import Optional, Dict, Any
|
||||
#
|
||||
import re
|
||||
from enum import Enum
|
||||
#
|
||||
|
||||
|
||||
|
||||
class SupplierType(Enum):
|
||||
NACIONAL = "nacional"
|
||||
EXTRANJERO = "extranjero"
|
||||
GLOBAL = "global"
|
||||
|
||||
class SuppliersBase(BaseModel):
|
||||
rfc: str
|
||||
email : EmailStr
|
||||
short_name : str
|
||||
razon_social : str
|
||||
fiscal_number : str
|
||||
cellphone: str
|
||||
supplier_type : SupplierType
|
||||
is_active : bool = True
|
||||
|
||||
class SupplierCreate(SuppliersBase):
|
||||
location_id: int
|
||||
client_id: int
|
||||
|
||||
class SupplierUpdate(SuppliersBase):
|
||||
pass
|
||||
|
||||
class SupplierResponse(SupplierCreate):
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: Optional[datetime]
|
||||
deleted_at: Optional[datetime]
|
||||
created_by: int
|
||||
updated_by: Optional[int]
|
||||
deleted_by: Optional[int]
|
||||
class messageResponse(BaseModel):
|
||||
message : str
|
||||
@@ -3,4 +3,68 @@ from datetime import datetime
|
||||
from typing import Optional
|
||||
#
|
||||
import re
|
||||
from enum import Enum
|
||||
from enum import Enum
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from fastapi import HTTPException
|
||||
from app.modules.suppliers.models import Suppliers
|
||||
from app.modules.suppliers.schema import SupplierCreate, SupplierUpdate
|
||||
|
||||
|
||||
class SuppliersService:
|
||||
@staticmethod
|
||||
def create_suppliers(db: Session, data:SupplierCreate ):
|
||||
new_suppliers = Suppliers(**data.dict())
|
||||
db.add(new_suppliers)
|
||||
db.commit()
|
||||
db.refresh(new_suppliers)
|
||||
return new_suppliers
|
||||
|
||||
@staticmethod
|
||||
def get_suppliers(db: Session, suppliers_id: int, current_user):
|
||||
suppliers = db.query(Suppliers).filter(Suppliers.id == suppliers_id).first()
|
||||
if not suppliers:
|
||||
raise ValueError("suppliers no encontrado")
|
||||
return suppliers
|
||||
|
||||
@staticmethod
|
||||
def update_suppliers(db: Session, suppliers_id: int, data:SupplierUpdate , current_user):
|
||||
suppliers = db.query(Suppliers).filter(Suppliers.id == suppliers_id).first()
|
||||
if not suppliers:
|
||||
raise ValueError("suppliers no encontrado")
|
||||
|
||||
if current_user.role not in ["ROOT", "ADMIN"]:
|
||||
raise ValueError("No tienes permisos para actualizar este suppliers")
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(suppliers, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(suppliers)
|
||||
return suppliers
|
||||
|
||||
@staticmethod
|
||||
def delete_suppliers(db: Session, suppliers_id: int, current_user):
|
||||
suppliers = db.query(Suppliers).filter(Suppliers.id == suppliers_id).first()
|
||||
if not suppliers:
|
||||
raise ValueError("suppliers no encontrado")
|
||||
|
||||
if current_user.role not in ["ROOT", "ADMIN"]:
|
||||
raise ValueError("No tienes permisos para eliminar este suppliers")
|
||||
|
||||
try:
|
||||
suppliers.is_active = False
|
||||
suppliers.deleted_at = datetime.utcnow()
|
||||
suppliers.deleted_by = current_user.id
|
||||
db.commit()
|
||||
db.refresh(suppliers)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise ValueError(f"Error al eliminar el suppliers: {e}")
|
||||
|
||||
return {"message": "suppliers eliminado correctamente"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user