Every way a company gets a board is set_board, and an unreadable host keeps its slug
`app.jobs.sources.set_board` attaches a board to an existing company,
replaces the one it has (closing the old board's open roles) or creates
the company, with row and subscription changed in one transaction by
`Store.set_company_board`. A board that lists zero roles is kept. `add`,
`discover --add`, the seed importer (moved out of app/cli.py) and the new
`POST|DELETE /api/companies/{key}/board` all go through it; a seed re-run
attaches a board to a row that never had one. A company's subscription is
rewritten in place, and detaching hides it.
`companies.ats_slug` (migration 9) keeps the slug of a host the app
cannot read yet, and discovery returns such a host before probing slugs.
The Companies rows get Set board / Change, the host select is rendered
from `plugins.ats.HOSTS`, and a row with nothing found reads "board not
found" and is in the "Not readable yet" filter.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0558ef19ed
commit
3b209d628a
18 files changed
+1038
-178
No files matched your search
@@ -94,7 +94,9 @@ CREATE TABLE IF NOT EXISTS role_tags (
|
||||
-- ats is the host the careers site runs on, readable by the ats plugin or
|
||||
-- not (workday, successfactors, ...): what discovery found, '' unknown. A
|
||||
-- company with an ats and no board subscription is one the app cannot read
|
||||
-- yet. country is an ISO code and city the head office's.
|
||||
-- yet. country is an ISO code and city the head office's. ats_slug is the
|
||||
-- name that host knows the company by (the acme of acme.teamtailor.com),
|
||||
-- '' where its URL carries none, kept for the parser written later.
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
key TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
@@ -106,7 +108,8 @@ CREATE TABLE IF NOT EXISTS companies (
|
||||
updated_at TEXT NOT NULL,
|
||||
ats TEXT NOT NULL DEFAULT '',
|
||||
country TEXT NOT NULL DEFAULT '',
|
||||
city TEXT NOT NULL DEFAULT ''
|
||||
city TEXT NOT NULL DEFAULT '',
|
||||
ats_slug TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
-- The targets desk's runner hands to plugins. newsdesk's shape, with its two
|
||||
@@ -337,6 +340,9 @@ ALTER TABLE companies ADD COLUMN city TEXT NOT NULL DEFAULT '';
|
||||
"""),
|
||||
(8, "agents.limits: how deep into its ranking the model stages read is each agent's own", """
|
||||
ALTER TABLE agents ADD COLUMN limits TEXT NOT NULL DEFAULT '{}';
|
||||
"""),
|
||||
(9, "companies.ats_slug: the slug of a host the app cannot read yet is kept", """
|
||||
ALTER TABLE companies ADD COLUMN ats_slug TEXT NOT NULL DEFAULT '';
|
||||
"""),
|
||||
)
|
||||
|
||||
|
||||
+103
-14
@@ -69,7 +69,8 @@ _VOTED_SCORE = ("MIN(1.0, s.score * CASE v.vote WHEN -1 THEN 0.5 "
|
||||
#: nothing, so `vote` and `voted_at` are NULL: one query shape either way.
|
||||
_WITH_VOTES = "LEFT JOIN role_votes v ON v.uid = roles.uid AND v.agent_id = ?"
|
||||
|
||||
_COMPANY_FIELDS = ("name", "domain", "status", "careers_url", "notes", "ats", "country", "city")
|
||||
_COMPANY_FIELDS = ("name", "domain", "status", "careers_url", "notes", "ats", "country", "city",
|
||||
"ats_slug")
|
||||
|
||||
#: A company as the store serves it: the row and the asked-for agent's affinity.
|
||||
_COMPANY_SELECT = """SELECT c.*, COALESCE(ac.affinity, 0) AS affinity FROM companies c
|
||||
@@ -979,24 +980,31 @@ class Store:
|
||||
def add_company(self, name: str, *, key: str = "", domain: str = "",
|
||||
status: str = "researching", careers_url: str = "",
|
||||
notes: str = "", ats: str = "", country: str = "",
|
||||
city: str = "") -> Company:
|
||||
city: str = "", ats_slug: str = "") -> Company:
|
||||
"""Raises ValueError on a key already taken."""
|
||||
name = name.strip()
|
||||
key = key or company_key(name)
|
||||
if not key:
|
||||
raise ValueError("a company needs a name")
|
||||
stamp = self.now()
|
||||
try:
|
||||
self._write(
|
||||
"""INSERT INTO companies (key, name, domain, status, careers_url, notes,
|
||||
ats, country, city, created_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(key, name, domain, status, careers_url, notes, ats, country, city,
|
||||
stamp, stamp))
|
||||
with self.transaction() as conn:
|
||||
self._insert_company(conn, key, name, {
|
||||
"domain": domain, "status": status, "careers_url": careers_url,
|
||||
"notes": notes, "ats": ats, "country": country, "city": city,
|
||||
"ats_slug": ats_slug})
|
||||
except sqlite3.IntegrityError as exc:
|
||||
raise ValueError(f"company {key!r} already exists") from exc
|
||||
return self._company(key)
|
||||
|
||||
def _insert_company(self, conn: sqlite3.Connection, key: str, name: str,
|
||||
fields: dict[str, Any]) -> None:
|
||||
stamp = self.now()
|
||||
columns = ["key", "name", *fields, "created_at", "updated_at"]
|
||||
conn.execute(
|
||||
f"INSERT INTO companies ({', '.join(columns)}) "
|
||||
f"VALUES ({', '.join('?' * len(columns))})",
|
||||
(key, name, *fields.values(), stamp, stamp))
|
||||
|
||||
def company(self, key: str, agent_id: int | None = None) -> Company | None:
|
||||
"""One company, with `affinity` as `agent_id` set it; 0 without an agent."""
|
||||
row = self._row(f"{_COMPANY_SELECT} WHERE c.key=?", (agent_id, key))
|
||||
@@ -1029,8 +1037,8 @@ class Store:
|
||||
return self._company(key, agent_id)
|
||||
|
||||
def update_company(self, key: str, **fields: Any) -> Company:
|
||||
"""Change any of name, domain, status, careers_url, notes. Affinity is
|
||||
per agent: `set_affinity`."""
|
||||
"""Change any of `_COMPANY_FIELDS`. Affinity is per agent:
|
||||
`set_affinity`."""
|
||||
unknown = set(fields) - set(_COMPANY_FIELDS)
|
||||
if unknown:
|
||||
raise ValueError(f"not a company field: {', '.join(sorted(unknown))}")
|
||||
@@ -1072,13 +1080,94 @@ class Store:
|
||||
"""`company_key -> ats host`, read off each company's own
|
||||
subscription. `Subscription.company` is the display name it and the
|
||||
company row were both added with, so `company_key` of one finds the
|
||||
other; a company added by hand with no subscription is simply
|
||||
absent."""
|
||||
other; a company with no subscription, or a detached one (`show` 0,
|
||||
see `set_company_board`), is simply absent."""
|
||||
boards: dict[str, str] = {}
|
||||
for sub in self.subscriptions(source="ats"):
|
||||
boards.setdefault(company_key(sub.company), sub.board)
|
||||
if sub.show:
|
||||
boards.setdefault(company_key(sub.company), sub.board)
|
||||
return boards
|
||||
|
||||
def company_subscription(self, key: str) -> Subscription | None:
|
||||
"""Company `key`'s own `ats` subscription, detached or not; None when
|
||||
it never had one. A company has at most one: `set_company_board`
|
||||
rewrites it in place rather than adding a second."""
|
||||
with closing(self._connect()) as conn:
|
||||
return self._company_subscription(conn, key)
|
||||
|
||||
@staticmethod
|
||||
def _company_subscription(conn: sqlite3.Connection, key: str) -> Subscription | None:
|
||||
rows = conn.execute("SELECT * FROM subscriptions WHERE source = 'ats' "
|
||||
"ORDER BY show DESC, id").fetchall()
|
||||
return next((Subscription.from_row(r) for r in rows
|
||||
if company_key(r["company"]) == key), None)
|
||||
|
||||
def set_company_board(self, key: str, target: str, *, host: str = "", name: str = "",
|
||||
fields: dict[str, Any] | None = None,
|
||||
) -> tuple[Company, Subscription | None, int]:
|
||||
"""Point company `key` at the `ats` board `target`, or at none when
|
||||
`target` is "", in one transaction, so a clash on either table leaves
|
||||
nothing half written.
|
||||
|
||||
A missing company is created from `name` and `fields`; an existing one
|
||||
has `fields` written over it. Its subscription is rewritten in place:
|
||||
a new target closes the old target's open roles, as switching a board
|
||||
off does, and "" detaches it (off, hidden, its roles closed) while the
|
||||
row stays for the next board. Returns the company, its subscription
|
||||
(None when there is none) and how many roles closed. Raises KeyError
|
||||
for a missing company with no board, ValueError on a UNIQUE clash."""
|
||||
fields = dict(fields or {})
|
||||
unknown = set(fields) - set(_COMPANY_FIELDS)
|
||||
if unknown:
|
||||
raise ValueError(f"not a company field: {', '.join(sorted(unknown))}")
|
||||
stamp = self.now()
|
||||
closed = 0
|
||||
try:
|
||||
with self.transaction() as conn:
|
||||
row = conn.execute("SELECT name FROM companies WHERE key=?", (key,)).fetchone()
|
||||
if row is None:
|
||||
name = fields.pop("name", "") or name.strip()
|
||||
if not target:
|
||||
raise KeyError(key)
|
||||
if not name:
|
||||
raise ValueError("a company needs a name")
|
||||
self._insert_company(conn, key, name, fields)
|
||||
else:
|
||||
name = fields.get("name") or row["name"]
|
||||
if fields:
|
||||
sets = ", ".join(f"{f}=?" for f in fields)
|
||||
conn.execute(f"UPDATE companies SET {sets}, updated_at=? WHERE key=?",
|
||||
(*fields.values(), stamp, key))
|
||||
sub = self._company_subscription(conn, key)
|
||||
sub_id = sub.id if sub else None
|
||||
if sub is not None and sub.target != target:
|
||||
closed = conn.execute(
|
||||
"UPDATE roles SET closed_at = ? "
|
||||
"WHERE subscription_id = ? AND closed_at IS NULL",
|
||||
(stamp, sub.id)).rowcount
|
||||
if not target:
|
||||
if sub is not None:
|
||||
conn.execute("UPDATE subscriptions SET subscribed=0, fetch=0, show=0 "
|
||||
"WHERE id=?", (sub.id,))
|
||||
elif sub is not None:
|
||||
conn.execute(
|
||||
"""UPDATE subscriptions
|
||||
SET target=?, board=?, label=?, company=?, subscribed=1,
|
||||
fetch=1, show=1,
|
||||
target_id=CASE WHEN target=? THEN target_id ELSE '' END
|
||||
WHERE id=?""",
|
||||
(target, host, name, name, target, sub.id))
|
||||
else:
|
||||
sub_id = conn.execute(
|
||||
"""INSERT INTO subscriptions (source, target, label, company, board,
|
||||
params, added_at)
|
||||
VALUES ('ats',?,?,?,?,'{}',?)""",
|
||||
(target, name, name, host, stamp)).lastrowid
|
||||
except sqlite3.IntegrityError as exc:
|
||||
raise ValueError(f"another board already has {target!r} or the label "
|
||||
f"{name!r}") from exc
|
||||
return (self._company(key), self.subscription(sub_id) if sub_id else None, closed)
|
||||
|
||||
# -------------------------------------------------------- subscriptions
|
||||
|
||||
def subscriptions(self, source: str | None = None,
|
||||
|
||||
Reference in new issue
Block a user