Sub-Tenant Routing

In a tenant with enable_sub_tenant on, every prescription is assigned a sub-tenant by the time it’s PROFILED (SWFT-2363). resolve_prescription_sub_tenant in swifty_integrations/prescriptions/sub_tenants.py picks it, in this order:

  1. the linked patient’s assigned_sub_tenant

  2. the prescription’s own assigned_sub_tenant, when its patient has none

  3. routing rules (SWFT-2452, see below)

  4. the active SubTenantRouting rule marked is_default

  5. the tenant’s default_sub_tenant

Only a sub-tenant of the tenant is ever used; anything else is logged as a configuration error and skipped.

Status: rules are evaluated (step 3), and their caps enforced, behind the sub_tenant_routing_rules flag and the tenant switch (SWFT-2452).

Routing rules

Rules are SubTenantRouting rows in the tenant schema, edited in the tenant’s Django admin. They are evaluated by priority, lowest first (then created_on), and the first rule that matches routes the prescription to its sub-tenant. A rule with is_last set stops evaluation once it’s reached, whether or not it matched.

Rules are evaluated only when all of these are true:

  • the sub_tenant_routing_rules feature flag is on (the rollout switch);

  • the tenant’s enable_sub_tenant_routing_rules is on;

  • the rule’s is_active is on.

With the tenant switch off, only the is_default rule and the tenant’s default_sub_tenant route. An inactive rule is skipped, is_default or not. Rules run before the is_default rule whatever their priority.

A rule that can’t be evaluated is logged as a configuration error (with the rule’s UUID) and treated as no match: rule_json that isn’t valid (saved before validation, or outside the admin), or a sub-tenant that isn’t one of the tenant’s. Conditions about the patient don’t match a prescription without a patient.

rule_json

A rule is one condition, or conditions combined with all, any and not. Each condition or combination is an object with exactly one key:

{"all": [
  {"resolved_gpi_contains": ["27250050007520"]},
  {"prescribed_ndc_contains": ["00002771501"]},
  {"preferred_ndc_contains": ["00002771501"]},
  {"is_greater_than_or_equal": {"field": "days_supply", "value": 90}},
  {"patient_has_insurance_on_profile": true},
  {"patient_does_not_have_external_id_slug": "mccpd"}
]}

Condition

Argument

Matches when

all

non-empty list of conditions

every one matches

any

non-empty list of conditions

at least one matches

not

one condition

it doesn’t match

resolved_gpi_contains

non-empty list of 14-digit MGPIs (spaces and hyphens ignored)

the medication’s drug DB code with the MGPI qualifier is one of them; no MGPI code means no match

prescribed_ndc_contains

non-empty list of NDCs

the prescribed NDC is one of them

preferred_ndc_contains

non-empty list of NDCs

the preferred NDC is one of them

is_greater_than_or_equal

{"field": ..., "value": <number>}; field is days_supply

the field is at least value

patient_has_insurance_on_profile

true or false

the patient has (or hasn’t) any PatientInsurance row

patient_does_not_have_external_id_slug

an external system slug

the patient has no PatientExternalIdentifier with that external_system_slug

NDCs are compared in their 11-digit form. Write them as 11 digits (with or without 5-4-2 dashes), or as a dashed 10-digit NDC (4-4-2, 5-3-2 or 5-4-1). A 10-digit NDC without dashes is rejected: it could be any of three layouts.

An is_default rule isn’t evaluated, so its rule_json must be empty ({}). A tenant has at most one active is_default rule.

The admin rejects an active rule it can’t evaluate: an unknown condition, a wrong type, an empty list, a number that isn’t finite, or more than one key in an object. An inactive rule’s rule_json isn’t checked, so a rule saved before this format existed can always be switched off, then fixed. The format and its validation live in swifty_integrations/prescriptions/sub_tenant_rules.py.

Caps

SubTenantRoutingLimit caps how many prescriptions routing rules send to a sub-tenant in a rolling window: max_count per hour (the last 60 minutes) or per day (the last 24 hours). A sub-tenant has at most one cap per period, so it can have, say, 10 an hour and 25 a day. max_count 0 stops rules routing there.

  • Every prescription any rule routed to the sub-tenant counts, once however often it was routed there, and whether or not the rule still exists. Patient assignments, the is_default rule and the tenant’s default sub-tenant don’t count, and aren’t capped.

  • A rule whose sub-tenant is at a cap is treated as not matching, and the next rule is tried. A capped rule with is_last still stops evaluation.

  • Each routing by a rule is recorded as a SubTenantRoutingAssignment (rule, prescription, sub-tenant, routed_on), shown read-only in the admin. That’s both the audit trail and what’s counted. It’s recorded when the routing is saved: on the move to PROFILED (assign_prescription_sub_tenant) and when a prescription is re-linked to a patient without a sub-tenant (prescription_patient_changed).

  • Intake’s pre-save resolve (linked_prescription_sub_tenant), resolve_prescription_sub_tenant and the sub-tenant backfill check caps as they stand, but never record a routing, so they never use one up.

  • So that concurrent routings can’t go over a cap, a routing that will be recorded locks the tenant’s SubTenantRoutingLimit rows (SELECT ... FOR UPDATE) at its first cap check, until its transaction ends. It locks all of them, in primary key order, rather than only the sub-tenant’s: a routing can check several sub-tenants in turn, and taking each one’s rows as it went could deadlock with another routing taking them in another order. Only a routing where a rule matches takes the lock, and a tenant without limits has nothing to lock.

  • The lock comes after the patient and prescription rows the routing’s transaction already holds, and lasts until that transaction ends. Other rule routings in the tenant wait for it. A transaction that routes one prescription by a rule and then locks another (a patient merge) can deadlock with a move to PROFILED holding that one; Postgres aborts one of them.

  • Intake expands a new prescription’s sig before saving it, for the sub-tenant it predicts without locking the caps. If a cap is reached meanwhile and the prescription is routed elsewhere, intake expands the sig again with that sub-tenant’s codes (expand_sig_again_if_routed_elsewhere).

Logging

Routing logs identifiers only (rule id, prescription UUID, condition name), never patient or prescription data.