79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
|
|
from typing import List
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from ..db import get_session
|
||
|
|
from ..dependencies import AuthUser, require_admin
|
||
|
|
from ..models import AuditAction, AuditResourceType, Customer
|
||
|
|
from ..schemas import CustomerCreate, CustomerOut, CustomerUpdate
|
||
|
|
from ..utils.audit import create_audit_log
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api/v1/customers", tags=["customers"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("", response_model=List[CustomerOut])
|
||
|
|
async def list_customers(
|
||
|
|
session: AsyncSession = Depends(get_session),
|
||
|
|
auth_user: AuthUser = Depends(require_admin),
|
||
|
|
) -> List[CustomerOut]:
|
||
|
|
customers = (await session.scalars(select(Customer))).all()
|
||
|
|
return [CustomerOut.model_validate(c) for c in customers]
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("", response_model=CustomerOut, status_code=status.HTTP_201_CREATED)
|
||
|
|
async def create_customer(
|
||
|
|
payload: CustomerCreate,
|
||
|
|
request: Request,
|
||
|
|
session: AsyncSession = Depends(get_session),
|
||
|
|
auth_user: AuthUser = Depends(require_admin),
|
||
|
|
) -> CustomerOut:
|
||
|
|
customer = Customer(**payload.model_dump())
|
||
|
|
session.add(customer)
|
||
|
|
await session.commit()
|
||
|
|
await session.refresh(customer)
|
||
|
|
await create_audit_log(
|
||
|
|
session,
|
||
|
|
user_id=auth_user.user.id,
|
||
|
|
customer_id=None,
|
||
|
|
action=AuditAction.CUSTOMER_CREATE,
|
||
|
|
resource_type=AuditResourceType.CUSTOMER,
|
||
|
|
resource_id=customer.id,
|
||
|
|
description=f"Create customer {customer.name}",
|
||
|
|
request=request,
|
||
|
|
)
|
||
|
|
await session.commit()
|
||
|
|
return CustomerOut.model_validate(customer)
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/{customer_id}", response_model=CustomerOut)
|
||
|
|
async def update_customer(
|
||
|
|
customer_id: int,
|
||
|
|
payload: CustomerUpdate,
|
||
|
|
request: Request,
|
||
|
|
session: AsyncSession = Depends(get_session),
|
||
|
|
auth_user: AuthUser = Depends(require_admin),
|
||
|
|
) -> CustomerOut:
|
||
|
|
customer = await session.get(Customer, customer_id)
|
||
|
|
if not customer:
|
||
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Customer not found")
|
||
|
|
|
||
|
|
for field, value in payload.model_dump(exclude_unset=True).items():
|
||
|
|
setattr(customer, field, value)
|
||
|
|
await session.commit()
|
||
|
|
await session.refresh(customer)
|
||
|
|
await create_audit_log(
|
||
|
|
session,
|
||
|
|
user_id=auth_user.user.id,
|
||
|
|
customer_id=customer.id,
|
||
|
|
action=AuditAction.CUSTOMER_UPDATE,
|
||
|
|
resource_type=AuditResourceType.CUSTOMER,
|
||
|
|
resource_id=customer.id,
|
||
|
|
description=f"Update customer {customer.name}",
|
||
|
|
payload=payload.model_dump(exclude_unset=True),
|
||
|
|
request=request,
|
||
|
|
)
|
||
|
|
await session.commit()
|
||
|
|
return CustomerOut.model_validate(customer)
|