52 lines
2.0 KiB
Python
52 lines
2.0 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")
|
|
source_channel_id=payload.get("source_channel_id")
|
|
if source_channel_id is not None:
|
|
try:
|
|
source_channel_id=int(source_channel_id)
|
|
except (TypeError,ValueError):
|
|
raise HTTPException(status_code=422,detail="source_channel_id must be an integer")
|
|
fields={
|
|
"job_post_id":HiringCosts._as_uuid(payload.get("job_post_id")),
|
|
"source_channel_id":source_channel_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)
|