Decision helpers: turn a Middesk business report into a compact, deterministic record an agent (or a rule) can act on.

summarize gives what passed, what failed, the warnings, every review task, the TIN result, watchlist hits, registration and record counts, the orders, and a needs-review flag. The same report always gives the same record: lists are sorted, and nothing depends on time or ordering in the response.

The summary never carries the TIN, SSNs, birth dates or document links. It does carry what Middesk wrote in task messages (business names, addresses, and sometimes officer names).

Decisions use the same vocabulary as ::persona::decisions: approved | declined | needs-review | pending | failed.

Functions

reasons

fn (summary: Map): Vec<Str>

Short reasons for a summary, sorted: one per failed or warning task ("failure tin: TIN Match - Not Found"), plus "watchlist hits: N" when there are hits.

Example

::middesk::decisions/reasons({
  failed: [{key: "tin", label: "TIN Match", sub_label: "Not Found"}],
  warnings: [],
  watchlist-hits: 1
})
// ["failure tin: TIN Match - Not Found", "watchlist hits: 1"]

registration-summary

fn (business: Map): Map

Secretary of State registrations: {count, active, inactive, states} with states sorted and distinct. Exposed for testing.

Example

::middesk::decisions/registration-summary({registrations: [
  {status: "active", state: "TX"},
  {status: "inactive", state: "CA"}
]})
// {count: 2, active: 1, inactive: 1, states: ["CA", "TX"]}

review-tasks

fn (business: Map): Vec

The business's review tasks as {key, category, label, sub_label, status, message} (sources dropped), sorted by category then key. Exposed for testing.

Example

tasks ::middesk::decisions/review-tasks({review: {tasks: [
  {key: "tin", category: "tin", label: "TIN Match", status: "success", sources: []},
  {key: "name", category: "name", label: "Business Name", status: "failure", sources: []}
]}})
map(tasks, %.key)  // ["name", "tin"]

status-decision

fn (status: Any): Str

Normalize a business status: approved → approved, rejected → declined, in_review → needs-review, open/pending/in_audit → pending, anything else → needs-review (a new status is never read as an approval).

Example

::middesk::decisions/status-decision("rejected")  // "declined"
::middesk::decisions/status-decision("in_audit")  // "pending"
::middesk::decisions/status-decision("something_new")  // "needs-review"

summarize

fn (business: Map): Map

A compact, deterministic record of a business report:

  • id, name, status, decision (see status-decision), final (approved or rejected)
  • needs-review: true when the status is in_review, or when the business is not yet approved or rejected and any task failed or warned, or a watchlist hit exists; also true for an unknown status. False once you have approved or rejected the business.
  • passed: the keys of success tasks (sorted)
  • failed, warnings: {key, label, sub_label, message} for failure and warning tasks
  • review-tasks: every task (see review-tasks)
  • tin: see tin-result; watchlist-hits; registrations: see registration-summary
  • records: counts of liens, litigations, bankruptcies
  • orders: [{product, status}]

Example

s ::middesk::decisions/summarize(::middesk::businesses/get-business("biz_..."))
s.needs-review  // true
s.failed        // [{key: "tin", label: "TIN Match", sub_label: "Not Found", message: "..."}]

tin-result

fn (business: Map): Map?

The TIN match without the TIN: {verified, mismatch, unknown, issued, error} (null fields dropped), or null when no TIN was checked. Exposed for testing.

Example

::middesk::decisions/tin-result({tin: {tin: "123410000", verified: true, mismatch: false}})
// {verified: true, mismatch: false}

::middesk::decisions/tin-result({})  // null

watchlist-hits

fn (business: Map): Int

The number of watchlist hits: watchlist.hit_count when present, else the total results across watchlist.lists. 0 when there is no watchlist. Exposed for testing.

Example

::middesk::decisions/watchlist-hits({watchlist: {hit_count: 2}})  // 2
::middesk::decisions/watchlist-hits({watchlist: {lists: [{results: [{}, {}, {}]}]}})  // 3
::middesk::decisions/watchlist-hits({})  // 0