Conversation
There was a problem hiding this comment.
Analytics SQL Review — Laboratory service requests mapped to charge items (SSMM)
Ticket: No linked ticket — JIRA lookup for ENG-972 returned HTTP 404/401 (see prep step). Branch name should resolve to a real ticket; without it I can't verify this query matches an actual requirement (metric, grain, facility scope). Continuing with Lenses 2–4.
Numbers — not yet trustworthy for the _ssmm dashboard:
- Critical: No
facility_idfilter on either table despite the_ssmmsuffix — this returns/aggregates across every facility, not just SSMM. That's a cross-tenant leak, not just an inefficiency. - High:
emr_chargeitem.status(independent from the service request's status) isn't filtered — anentered_in_error/voided charge item will still appear as a valid mapping. Only the service request's status is checked. - Minor:
deleted = FALSEis missing on both tables (low-signal since it's rarely set, but free and matches repo convention).
No join-fan-out risk here since there's no aggregate/money rollup, and the DISTINCT is reasonable given the intended 1-service-request-to-many-charge-items grain the Purpose describes.
Docs: Notes section correctly explains the lab-only filter; Parameters table is fine as-is (no params). Once the facility scope is added, document the facility id there too.
Hygiene: Correct domain folder (Care/Clinical/), filename convention followed, one file for one PR — no issues.
Verdict: Hold before publishing — the missing facility scope on a facility-suffixed query is the kind of bug that quietly leaks data across tenants on a shared dashboard.
Generated by Analytics SQL Reviewer for #158 · auto · 44.1 AIC · ⌖ 4.75 AIC · ⊞ 14.7K
| emr_chargeitem.title AS charge_item_title | ||
| FROM emr_servicerequest | ||
| JOIN emr_chargeitem | ||
| ON emr_chargeitem.service_resource = 'service_request' |
There was a problem hiding this comment.
[High] emr_chargeitem.status is not filtered.
ChargeItem has its own status field (see care/emr/models/charge_item.py) independent of the service request's status. The query excludes entered_in_error on emr_servicerequest but not on emr_chargeitem — a charge item entered in error (or voided/cancelled) will still show up here as if it were a valid charge tied to the lab request. Add AND emr_chargeitem.status != 'entered_in_error' (and any other terminal status this report should exclude) alongside the existing service-request filter.
Also, neither table filters deleted = FALSE. It's rarely set, but it's free and matches repo convention — worth adding on both tables.
No description provided.