HR-ATS-Portal/backend/job/cost/views.py

45 lines
1.7 KiB
Python

from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from job.cost.models import HiringCosts
from job.cost.serializers import serialize_hiring_cost
class HiringCost:
def __init__(self,session:AsyncSession):
self.session=session
async def list_costs(self,job_post_id=None,from_date=None,to_date=None,top=None,skip=0):
rows,total=await HiringCosts.fetch_costs(
self.session,
job_post_id=job_post_id,
from_date=from_date,
to_date=to_date,
top=top,
skip=skip,
)
return [serialize_hiring_cost(r) for r in rows],total
async def create_cost(self,payload,current_user):
cost_type=payload.get("cost_type")
amount=payload.get("amount")
if not cost_type or amount is None:
raise HTTPException(status_code=422,detail="cost_type and amount are required")
created_by=HiringCosts._as_uuid(
current_user.get("id") if isinstance(current_user,dict) else None
)
if not created_by:
raise HTTPException(status_code=422,detail="created_by is required")
fields={
"job_post_id":HiringCosts._as_uuid(payload.get("job_post_id")),
"cost_type":cost_type,
"amount":float(amount),
"currency":payload.get("currency") or "USD",
"description":payload.get("description"),
"created_by":created_by,
}
if payload.get("incurred_at") is not None:
fields["incurred_at"]=payload["incurred_at"]
row=await HiringCosts.insert_cost(self.session,fields)
return serialize_hiring_cost(row)