From 28afc334c341700905441783092c4fab599ac673 Mon Sep 17 00:00:00 2001 From: Wolf Date: Tue, 29 Sep 2026 17:02:19 +0200 Subject: [PATCH] Add, edit, check and delete a company by hand through the API, and show the by-hand fields in the list Co-Authored-By: Claude Sonnet 5.5 --- app/web/api.py | 122 ++++++++++++++++++++++++++++++----- tests/test_web.py | 161 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 259 insertions(+), 24 deletions(-) diff --git a/app/web/api.py b/app/web/api.py index 229f8b6..e27a3e2 100644 --- a/app/web/api.py +++ b/app/web/api.py @@ -10,6 +10,7 @@ This is roledesk's own API, over the same store. Every refusal is from __future__ import annotations from typing import Annotated, Any, Literal +from urllib.parse import urlsplit from fastapi import APIRouter, HTTPException, Query, Response from pydantic import BaseModel, Field @@ -17,7 +18,7 @@ from pydantic import BaseModel, Field from app import jobs from app.models import ( AFFINITY, Agent, Application, Company, Gate, Limits, Prompts, Role, RoleRow, - ScoredRole, Status, Subscription, Weights, company_key, + HandReason, ScoredRole, Status, Subscription, Weights, company_key, ) from app.storage import Store from app.web.deps import ConfigDep, StoreDep @@ -303,11 +304,17 @@ class CompanyOut(BaseModel): the board has failed on every fetch and why it last did, both empty while it answers. `from_search` marks a row that exists only because a search listed its roles and somebody judged it (`Store.set_affinity`): - no board, not even a detached one, and no `ats`, `careers_url` or - `domain`, which every way of following a company writes one of.""" + no board, not even a detached one, and no `ats`, `careers_url`, `domain` + or `hand_reason`, which every way of following a company or listing it by + hand writes one of (`jobs.sources.known_from_search_only`). `by_hand` + marks a company on the list checked by hand: no board shown and not + `from_search`. `hand_reason` says why (`app.models.HAND_REASONS`) and + `checked_at` when a person last looked at its careers page, "" for never.""" key: str name: str + domain: str = "" + notes: str = "" board: str subscription_id: int | None = None on: bool | None = None @@ -320,6 +327,9 @@ class CompanyOut(BaseModel): failing_since: str = "" failing_reason: str = "" from_search: bool = False + hand_reason: str = "" + checked_at: str = "" + by_hand: bool = False def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]: @@ -337,29 +347,109 @@ def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]: return boards -@router.get("/companies") -def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut]: - """Every company, with `affinity` as `agent` (the default agent when - absent) has it; 0 throughout when there is no agent.""" +def _company_outs(store: Store, agent: int | None) -> list[CompanyOut]: boards = _company_boards(store) ever_boarded = {company_key(s.company) for s in store.subscriptions(source="ats")} counts = store.open_role_counts() failing = jobs.pull.failing_boards(store) out = [] - for c in store.companies(_agent_id(store, agent)): + for c in store.companies(agent): sub_id, host, on, target = boards.get(c.key, (None, "", None, "")) since, reason = failing.get(target, ("", "")) - out.append(CompanyOut(key=c.key, name=c.name, board=host, - subscription_id=sub_id, on=on, affinity=c.affinity, - open_roles=counts.get(c.key, 0), ats=c.ats, - ats_slug=c.ats_slug, careers_url=c.careers_url, - country=c.country, - failing_since=since, failing_reason=reason, - from_search=not (c.key in ever_boarded or c.ats - or c.careers_url or c.domain))) + from_search = jobs.sources.known_from_search_only(c, ever_boarded) + out.append(CompanyOut(key=c.key, name=c.name, domain=c.domain, notes=c.notes, + board=host, subscription_id=sub_id, on=on, + affinity=c.affinity, open_roles=counts.get(c.key, 0), + ats=c.ats, ats_slug=c.ats_slug, careers_url=c.careers_url, + country=c.country, failing_since=since, failing_reason=reason, + from_search=from_search, hand_reason=c.hand_reason, + checked_at=c.checked_at, + by_hand=c.key not in boards and not from_search)) return out +def _one_company(store: Store, key: str) -> CompanyOut: + """`key` as the list shows it, with the default agent's affinity.""" + return next(c for c in _company_outs(store, _agent_id(store, None)) if c.key == key) + + +@router.get("/companies") +def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut]: + """Every company, with `affinity` as `agent` (the default agent when + absent) has it; 0 throughout when there is no agent.""" + return _company_outs(store, _agent_id(store, agent)) + + +class NewCompanyBody(BaseModel): + name: str + careers_url: str = "" + notes: str = "" + hand_reason: HandReason = "added" + + +@router.post("/companies", status_code=201) +def add_company(body: NewCompanyBody, store: StoreDep) -> CompanyOut: + """A company put on the list checked by hand: no board, `hand_reason` + `added` unless it says another. 409 when its key, from the name, is taken.""" + try: + company = jobs.sources.add_by_hand(store, body.name, careers_url=body.careers_url, + notes=body.notes, hand_reason=body.hand_reason) + except jobs.sources.Conflict as exc: + raise HTTPException(409, str(exc)) from exc + except ValueError as exc: + raise HTTPException(422, str(exc)) from exc + return _one_company(store, company.key) + + +class CompanyPatch(BaseModel): + name: str | None = None + careers_url: str | None = None + notes: str | None = None + hand_reason: HandReason | None = None + + +@router.patch("/companies/{key}") +def edit_company(key: str, body: CompanyPatch, store: StoreDep) -> CompanyOut: + """Change any of a company's name, careers link, notes and reason; a field + left out stays. The key never changes, and `name` only for a company + without a board: 409 for one with a subscription, shown or not (see + `jobs.sources.edit_company`).""" + fields = body.model_dump(exclude_unset=True) + if any(v is None for v in fields.values()): + raise HTTPException(422, "A field is left out, not null.") + try: + jobs.sources.edit_company(store, key, **fields) + except KeyError: + raise HTTPException(404, f"no such company: {key}") from None + except jobs.sources.Conflict as exc: + raise HTTPException(409, str(exc)) from exc + except ValueError as exc: + raise HTTPException(422, str(exc)) from exc + return _one_company(store, key) + + +@router.post("/companies/{key}/checked") +def company_checked(key: str, store: StoreDep) -> CompanyOut: + """A person has just looked at the company's careers page.""" + if store.company(key) is None: + raise HTTPException(404, f"no such company: {key}") + store.update_company(key, checked_at=store.now()) + return _one_company(store, key) + + +@router.delete("/companies/{key}", status_code=204) +def remove_company(key: str, store: StoreDep) -> Response: + """Delete a company and every agent's stance on it. 409 for one with a + board shown or with open roles; detach the board first.""" + try: + jobs.sources.remove_company(store, key) + except KeyError: + raise HTTPException(404, f"no such company: {key}") from None + except ValueError as exc: + raise HTTPException(409, str(exc)) from exc + return Response(status_code=204) + + class CompanyBoardBody(BaseModel): board: str diff --git a/tests/test_web.py b/tests/test_web.py index 975b72a..2a6066f 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -421,24 +421,27 @@ def test_companies_lists_board_and_open_role_count( store.add_company("Acme", key="acme", ats="greenhouse", country="DK") store.upsert_role(posting("1", company="Acme"), sub) store.add_company("Other", key="other", domain="other.example") # no board found - store.add_company("Novo", key="novo", ats="successfactors", # followed, not readable + store.add_company("Novo", key="novo", ats="taleo", # followed, not readable careers_url="https://novo.example/careers") res = client.get("/api/companies") assert res.status_code == 200 [row] = [c for c in res.json() if c["key"] == "acme"] - assert row == {"key": "acme", "name": "Acme", "board": "greenhouse", - "subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1, - "ats": "greenhouse", "ats_slug": "", "careers_url": "", - "country": "DK", "failing_since": "", "failing_reason": "", - "from_search": False} + assert row == {"key": "acme", "name": "Acme", "domain": "", "notes": "", + "board": "greenhouse", "subscription_id": sub.id, "on": True, + "affinity": 0, "open_roles": 1, "ats": "greenhouse", "ats_slug": "", + "careers_url": "", "country": "DK", "failing_since": "", + "failing_reason": "", "from_search": False, "hand_reason": "", + "checked_at": "", "by_hand": False} [other] = [c for c in res.json() if c["key"] == "other"] assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == ( "", None, None, "") [novo] = [c for c in res.json() if c["key"] == "novo"] assert (novo["board"], novo["subscription_id"], novo["ats"], novo["careers_url"]) == ( - "", None, "successfactors", "https://novo.example/careers") + "", None, "taleo", "https://novo.example/careers") assert not any(c["from_search"] for c in res.json()) + # Companies with no board shown, unread or not, are the list checked by hand. + assert {c["key"] for c in res.json() if c["by_hand"]} == {"other", "novo"} def test_judging_a_company_known_only_from_a_search_creates_its_row( @@ -1214,10 +1217,152 @@ def test_a_companys_board_is_set_replaced_and_detached( assert client.post("/api/companies/nope/board", json={"board": "lever:acme"}).status_code == 404 assert client.delete("/api/companies/nope/board").status_code == 404 - bad = client.post("/api/companies/acme/board", json={"board": "workday:acme"}) + bad = client.post("/api/companies/acme/board", json={"board": "taleo:acme"}) assert bad.status_code == 422 and "host:slug" in bad.json()["detail"] +def test_a_company_is_added_by_hand_with_a_reason_and_a_link( + client: TestClient, store: Store) -> None: + res = client.post("/api/companies", json={ + "name": "Revolut Ltd", "careers_url": " https://revolut.example/careers ", + "notes": "wants a login"}) + assert res.status_code == 201, res.text + assert res.json() == { + "key": "revolut", "name": "Revolut Ltd", "domain": "", "notes": "wants a login", + "board": "", "subscription_id": None, "on": None, "affinity": 0, "open_roles": 0, + "ats": "", "ats_slug": "", "careers_url": "https://revolut.example/careers", + "country": "", "failing_since": "", "failing_reason": "", "from_search": False, + "hand_reason": "added", "checked_at": "", "by_hand": True} + assert store.company("revolut").hand_reason == "added" # type: ignore[union-attr] + + other = client.post("/api/companies", json={"name": "Meta", "hand_reason": "blocked"}) + assert other.status_code == 201 and other.json()["hand_reason"] == "blocked" + # The key comes from the name, so a legal suffix or case is the same company. + assert client.post("/api/companies", json={"name": "REVOLUT"}).status_code == 409 + assert store.company("revolut").notes == "wants a login" # type: ignore[union-attr] + + +def test_adding_a_company_by_hand_refuses_a_bad_link_reason_or_name( + client: TestClient, store: Store) -> None: + for link in ("javascript:alert(1)", "ftp://x.example/jobs", "revolut.example/careers", + "https://"): + res = client.post("/api/companies", json={"name": "Revolut", "careers_url": link}) + assert res.status_code == 422, link + assert client.post("/api/companies", json={"name": "Revolut", "careers_url": ""} + ).status_code == 201 + assert client.post("/api/companies", json={"name": "Meta", "hand_reason": "lazy"} + ).status_code == 422 + assert client.post("/api/companies", json={"name": " "}).status_code == 422 + assert client.post("/api/companies", json={}).status_code == 422 + assert [c.key for c in store.companies()] == ["revolut"] + + +def test_a_company_from_a_search_only_is_no_longer_one_once_it_has_a_reason( + client: TestClient, store: Store) -> None: + store.save_agent("A") + search = store.add_subscription("search", "jobindex:cph", label="cph", board="jobindex") + item = posting("1", company="Novo") + item.source = "search" + store.upsert_role(item, search) + client.post("/api/companies/novo/affinity", json={"affinity": 1}) + [row] = client.get("/api/companies").json() + assert (row["from_search"], row["by_hand"]) == (True, False) + + res = client.patch("/api/companies/novo", json={"hand_reason": "unread"}) + assert res.status_code == 200 + assert (res.json()["from_search"], res.json()["by_hand"]) == (False, True) + + +def test_a_company_is_edited_and_only_the_fields_sent_change( + client: TestClient, store: Store) -> None: + store.add_company("Acme", key="acme", careers_url="https://acme.example/jobs", + notes="old", hand_reason="unread") + res = client.patch("/api/companies/acme", json={"notes": "new", "name": "Acme Labs"}) + assert res.status_code == 200, res.text + body = res.json() + assert (body["key"], body["name"], body["notes"], body["careers_url"], + body["hand_reason"]) == ("acme", "Acme Labs", "new", "https://acme.example/jobs", + "unread") + # A link and a reason can be cleared, since "" is a value. + res = client.patch("/api/companies/acme", json={"careers_url": "", "hand_reason": ""}) + assert (res.json()["careers_url"], res.json()["hand_reason"]) == ("", "") + assert client.patch("/api/companies/acme", json={}).status_code == 200 + + assert client.patch("/api/companies/acme", json={"careers_url": "javascript:x"} + ).status_code == 422 + assert client.patch("/api/companies/acme", json={"hand_reason": "lazy"}).status_code == 422 + assert client.patch("/api/companies/acme", json={"name": " "}).status_code == 422 + assert client.patch("/api/companies/acme", json={"notes": None}).status_code == 422 + assert client.patch("/api/companies/nope", json={"notes": "x"}).status_code == 404 + assert store.company("acme").name == "Acme Labs" # type: ignore[union-attr] + + +def test_a_company_with_a_board_keeps_its_name_and_takes_the_other_edits( + client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: + import plugins.ats as ats + + monkeypatch.setattr(ats, "fetch_board", lambda target: ([], True)) + store.add_company("Acme", key="acme") + assert client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"} + ).status_code == 200 + + res = client.patch("/api/companies/acme", json={"name": "Acme Labs"}) + assert res.status_code == 409 and "detach" in res.json()["detail"] + assert client.patch("/api/companies/acme", json={"name": "Acme"}).status_code == 200 + assert client.patch("/api/companies/acme", json={"notes": "hi"}).status_code == 200 + # Detached, the subscription is still there and the name stays fixed. + client.delete("/api/companies/acme/board") + assert client.patch("/api/companies/acme", json={"name": "Acme Labs"}).status_code == 409 + assert store.company("acme").name == "Acme" # type: ignore[union-attr] + + +def test_checking_a_company_stamps_now(client: TestClient, store: Store) -> None: + clock = Clock() + store.clock = clock + store.add_company("Acme", key="acme", hand_reason="unread") + res = client.post("/api/companies/acme/checked") + assert res.status_code == 200 + assert res.json()["checked_at"] == "2026-09-23T09:00:00+00:00" + clock.tick(48) + assert client.post("/api/companies/acme/checked").json()["checked_at"] == ( + "2026-09-25T09:00:00+00:00") + assert client.post("/api/companies/nope/checked").status_code == 404 + + +def test_a_company_is_deleted_unless_it_is_read( + client: TestClient, store: Store, monkeypatch: pytest.MonkeyPatch) -> None: + import plugins.ats as ats + + monkeypatch.setattr(ats, "fetch_board", lambda target: ([], True)) + agent = store.save_agent("A") + store.add_company("Meta", key="meta", hand_reason="blocked") + store.set_affinity(agent.id, "meta", 1) + res = client.delete("/api/companies/meta") + assert res.status_code == 204 and res.content == b"" + assert store.company("meta") is None + assert store.company("meta", agent.id) is None + assert client.delete("/api/companies/meta").status_code == 404 + assert "meta" not in {c["key"] for c in client.get("/api/companies").json()} + + # A board shown: refused until it is detached; a detached board is no board. + store.add_company("Acme", key="acme") + sub_id = client.post("/api/companies/acme/board", json={"board": "greenhouse:acme"} + ).json()["source"]["id"] + res = client.delete("/api/companies/acme") + assert res.status_code == 409 and "greenhouse:acme" in res.json()["detail"] + store.upsert_role(posting("1", company="Acme"), store.subscription(sub_id)) + client.delete("/api/companies/acme/board") + assert client.delete("/api/companies/acme").status_code == 204 + + # Open roles hold a company even where no board is on: they would be + # orphaned from the list. + store.add_company("Beta", key="beta") + store.upsert_role(posting("2", company="Beta")) + res = client.delete("/api/companies/beta") + assert res.status_code == 409 and "open roles" in res.json()["detail"] + assert store.company("beta") is not None + + def test_the_pull_a_source_switch_queues_is_refused_quietly_while_one_runs( app: FastAPI, store: Store) -> None: """The four actions that queue a `pull` (switching a source on, following