import bson
import fastapi
import pydantic

from app.storage.mongo import tasks


class RequestBody(pydantic.BaseModel):
    id: str


router = fastapi.APIRouter()


@router.post('/api/v1/finish', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={'404': {'description': 'Not found'}})
async def execute(body: RequestBody):
    if await tasks.finish_task(bson.ObjectId(body.id)):
        return
    raise fastapi.HTTPException(404)
