80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
import os
|
|
|
|
import httpx
|
|
from dotenv import load_dotenv
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from job.job_post.models import JobPosts
|
|
from job.job_post.plugins import (
|
|
BufferError,
|
|
create_buffer_post,
|
|
list_buffer_channels,
|
|
local_status,
|
|
parse_buffer_datetime,
|
|
render_job_post,
|
|
)
|
|
from job.job_post.serializers import serialize_job_post
|
|
|
|
load_dotenv()
|
|
|
|
|
|
class JobPost:
|
|
def __init__(self,session:AsyncSession):
|
|
self.session=session
|
|
self.buffer_api=os.getenv("BUFFER_API")
|
|
self.channel_id=os.getenv("BUFFER_CHANNEL_ID")
|
|
|
|
async def post_job(self,payload,current_user):
|
|
channel_id=payload.get("channel_id") or self.channel_id
|
|
if not channel_id:
|
|
raise HTTPException(status_code=500,detail="BUFFER_CHANNEL_ID is not configured")
|
|
|
|
text=render_job_post(payload)
|
|
row=await JobPosts.insert_job_post(self.session,{
|
|
"title":payload.get("title"),
|
|
"employment_type":payload.get("employment_type"),
|
|
"location":payload.get("location"),
|
|
"experience_min":payload.get("experience_min"),
|
|
"experience_max":payload.get("experience_max"),
|
|
"requirements":list(payload.get("requirements") or []),
|
|
"optional_skills":list(payload.get("optional_skills") or []),
|
|
"salary":payload.get("salary") or "Anonymous",
|
|
"description":payload.get("description"),
|
|
"post_text":text,
|
|
"channel_id":channel_id,
|
|
"status":"draft",
|
|
"created_by":current_user["id"],
|
|
})
|
|
|
|
try:
|
|
post=await create_buffer_post(
|
|
text,
|
|
channel_id,
|
|
mode=payload.get("mode") or "addToQueue",
|
|
due_at=payload.get("due_at"),
|
|
)
|
|
except (httpx.HTTPError,BufferError,RuntimeError) as e:
|
|
await JobPosts.mark_failed(self.session,str(row.id),str(e))
|
|
raise HTTPException(status_code=502,detail="Failed to publish job post to Buffer") from e
|
|
|
|
# Buffer accepting the mutation is not the same as the network publishing it:
|
|
# the default addToQueue mode returns `scheduled`, so the row only reads
|
|
# "published" once Buffer reports `sent`.
|
|
saved=await JobPosts.mark_buffer_result(
|
|
self.session,
|
|
str(row.id),
|
|
buffer_post_id=post["id"],
|
|
status=local_status(post.get("status")),
|
|
external_link=post.get("externalLink"),
|
|
sent_at=parse_buffer_datetime(post.get("sentAt")),
|
|
platform=post.get("channelService"),
|
|
)
|
|
return serialize_job_post(saved)
|
|
|
|
async def list_channels(self):
|
|
try:
|
|
return await list_buffer_channels()
|
|
except (httpx.HTTPError,BufferError,RuntimeError) as e:
|
|
raise HTTPException(status_code=502,detail="Failed to list Buffer channels") from e
|