0d30c58bf0
Snapshot of the working tree on srv1071960 (branch main), taken during server cleanup. .env files, node_modules, builds, logs and media were excluded.
83 lines
3.2 KiB
Python
83 lines
3.2 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, Request, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.auth.dependencies import client_ip, get_current_user, require_role
|
|
from app.common.enums import Role
|
|
from app.database.session import get_db
|
|
from app.doctors.schemas import RejectMaster
|
|
from app.stockists.models import Stockist
|
|
from app.stockists.schemas import StockistCreate, StockistRead, StockistUpdate
|
|
from app.stockists.service import StockistService
|
|
from app.users.models import User
|
|
|
|
router = APIRouter(prefix="/stockists", tags=["stockists"])
|
|
|
|
|
|
@router.get("", response_model=list[StockistRead])
|
|
async def list_stockists(
|
|
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
|
) -> list[StockistRead]:
|
|
service = StockistService(db)
|
|
stockists = await service.list_for(current_user)
|
|
names = await service.mr_names_by_hq({s.headquarters_id for s in stockists})
|
|
return [
|
|
StockistRead.model_validate(s).model_copy(update={"assigned_mr_name": names.get(s.headquarters_id)})
|
|
for s in stockists
|
|
]
|
|
|
|
|
|
@router.post("", response_model=StockistRead, status_code=status.HTTP_201_CREATED)
|
|
async def create_stockist(
|
|
payload: StockistCreate,
|
|
request: Request,
|
|
current_user: User = Depends(require_role(Role.ADMIN, Role.MR)),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Stockist:
|
|
"""An Administrator (listed at once), or an MR within their own headquarters (waits for the
|
|
Area Business Manager)."""
|
|
return await StockistService(db).create(payload, current_user, client_ip(request))
|
|
|
|
|
|
@router.patch("/{stockist_id}", response_model=StockistRead)
|
|
async def update_stockist(
|
|
stockist_id: uuid.UUID,
|
|
payload: StockistUpdate,
|
|
request: Request,
|
|
current_user: User = Depends(require_role(Role.ADMIN)),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Stockist:
|
|
return await StockistService(db).update(stockist_id, payload, current_user, client_ip(request))
|
|
|
|
|
|
|
|
@router.get("/requests", response_model=list[StockistRead])
|
|
async def list_stockist_requests(
|
|
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
|
) -> list[Stockist]:
|
|
"""Stockists an MR added that wait for the Area Business Manager: an MR sees their own (with any
|
|
rejected and why), an Area Business Manager what waits for them, the administrator and AGM all."""
|
|
return await StockistService(db).requests_for(current_user)
|
|
|
|
|
|
@router.post("/{stockist_id}/approve", response_model=StockistRead)
|
|
async def approve_stockist(
|
|
stockist_id: uuid.UUID,
|
|
request: Request,
|
|
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Stockist:
|
|
return await StockistService(db).review(stockist_id, True, None, current_user, client_ip(request))
|
|
|
|
|
|
@router.post("/{stockist_id}/reject", response_model=StockistRead)
|
|
async def reject_stockist(
|
|
stockist_id: uuid.UUID,
|
|
payload: RejectMaster,
|
|
request: Request,
|
|
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Stockist:
|
|
return await StockistService(db).review(stockist_id, False, payload.reason, current_user, client_ip(request))
|