/, GET /api/roles|companies|applications, POST /api/companies/{key}/affinity
and /api/applications[/{id}/status]. The Jinja2 template renders an empty
shell; static/app.js fills it and drives Save, affinity and status changes.
<desk-jobs> from pipeline.web mounts at the bottom so Pull can be started
from the page.
Store.roles gains q, function, remote, days and hide_muted, and company/
place move from a key match to a substring on the text a board wrote.
hide_muted filters in SQL before LIMIT: filtering after it shipped first and
broke immediately on real data, since Databricks' freshly pulled 884 roles
were the newest 200-plus rows in the table and muting it left nothing to
show.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
125 lines
4.2 KiB
Python
125 lines
4.2 KiB
Python
"""The JSON routes the page uses: roles, companies, applications.
|
|
|
|
`/api/jobs*`, `/_jobs/jobs.js` and `/ws/jobs` are `pipeline.web`'s, mounted by
|
|
`connect` in `app.py`. This is roledesk's own API, over the same store.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from pydantic import BaseModel
|
|
|
|
from roledesk.models import AFFINITY, Application, Company, Role, Status
|
|
from roledesk.web.deps import StoreDep
|
|
|
|
router = APIRouter(prefix="/api", tags=["roledesk"])
|
|
|
|
|
|
# ------------------------------------------------------------------- roles
|
|
|
|
@router.get("/roles")
|
|
def list_roles(
|
|
store: StoreDep,
|
|
q: str = "",
|
|
company: str = "",
|
|
place: str = "",
|
|
function: str = "",
|
|
remote: str = "",
|
|
days: int | None = None,
|
|
show_muted: bool = False,
|
|
limit: Annotated[int, Query(ge=1, le=1000)] = 200,
|
|
) -> list[Role]:
|
|
"""Open roles, newest first. See `Store.roles` for what each filter does.
|
|
|
|
A muted company's roles are left out unless `show_muted` is set."""
|
|
return store.roles(limit=limit, q=q or None, company=company or None,
|
|
place=place or None, function=function or None,
|
|
remote=remote or None, days=days, hide_muted=not show_muted)
|
|
|
|
|
|
# --------------------------------------------------------------- companies
|
|
|
|
class CompanyOut(BaseModel):
|
|
"""A company row plus the two things only the store can join in: the ats
|
|
host it is read from, and how many of its roles are still open."""
|
|
|
|
key: str
|
|
name: str
|
|
board: str
|
|
affinity: int
|
|
open_roles: int
|
|
|
|
|
|
@router.get("/companies")
|
|
def list_companies(store: StoreDep) -> list[CompanyOut]:
|
|
boards = store.company_boards()
|
|
counts = store.open_role_counts()
|
|
return [CompanyOut(key=c.key, name=c.name, board=boards.get(c.key, ""),
|
|
affinity=c.affinity, open_roles=counts.get(c.key, 0))
|
|
for c in store.companies()]
|
|
|
|
|
|
class AffinityBody(BaseModel):
|
|
affinity: int
|
|
|
|
|
|
@router.post("/companies/{key}/affinity")
|
|
def set_affinity(key: str, body: AffinityBody, store: StoreDep) -> Company:
|
|
if body.affinity not in AFFINITY:
|
|
raise HTTPException(422, "affinity is -1, 0, 1 or 2")
|
|
try:
|
|
return store.update_company(key, affinity=body.affinity)
|
|
except KeyError:
|
|
raise HTTPException(404, f"no such company: {key}") from None
|
|
|
|
|
|
# ----------------------------------------------------------- applications
|
|
|
|
class SaveBody(BaseModel):
|
|
role_uid: str
|
|
status: Status = "saved"
|
|
|
|
|
|
@router.post("/applications", status_code=201)
|
|
def save_role(body: SaveBody, store: StoreDep) -> Application:
|
|
"""Track one role. Pressing Save twice does not double-track it: the
|
|
existing row comes back rather than a second one starting at `saved`
|
|
over whatever status it has since moved to."""
|
|
existing = next((a for a in store.applications() if a.role_uid == body.role_uid), None)
|
|
if existing is not None:
|
|
return existing
|
|
role = store.role(body.role_uid)
|
|
if role is None:
|
|
raise HTTPException(404, f"no such role: {body.role_uid}")
|
|
next_date = (store.clock() + timedelta(days=2)).date().isoformat()
|
|
application = Application(
|
|
role_uid=role.uid, company_key=role.company_key, role_title=role.title,
|
|
url=role.original_url, status=body.status, source=role.source,
|
|
next_action="Read the posting and decide", next_date=next_date)
|
|
return store.save_application(application)
|
|
|
|
|
|
@router.get("/applications")
|
|
def list_applications(store: StoreDep) -> list[Application]:
|
|
"""Every tracked application, with the role title and company key it was
|
|
saved with — enough for the page to list them without a join; the
|
|
company's full name comes from `/api/companies`, already on the page."""
|
|
return store.applications()
|
|
|
|
|
|
class StatusBody(BaseModel):
|
|
status: Status
|
|
|
|
|
|
@router.post("/applications/{app_id}/status")
|
|
def set_status(app_id: str, body: StatusBody, store: StoreDep) -> Application:
|
|
try:
|
|
return store.set_application_status(app_id, body.status)
|
|
except ValueError as exc:
|
|
raise HTTPException(422, str(exc)) from exc
|
|
except KeyError:
|
|
raise HTTPException(404, f"no such application: {app_id}") from None
|