Skip to content

Add documentation for Patients with Billable Charge Item and Encounter/Appointment Details query - #147

Open
sonzsara wants to merge 1 commit into
mainfrom
ENG-910
Open

Add documentation for Patients with Billable Charge Item and Encounter/Appointment Details query#147
sonzsara wants to merge 1 commit into
mainfrom
ENG-910

Conversation

@sonzsara

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Markdown documentation page describing the “Patients with Billable Charge Item and Encounter/Appointment Details” SSMM query so analysts can understand intent, parameters, and the exact SQL used.

Changes:

  • Introduces a new SSMM query documentation file with Purpose/Parameters/Notes sections.
  • Documents the SQL query that returns encounters with associated token bookings and billable charge items.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@amjithtitus09
amjithtitus09 marked this pull request as draft August 17, 2026 19:46
@amjithtitus09
amjithtitus09 marked this pull request as ready for review August 17, 2026 19:46

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analytics SQL Review — billable charge item + encounter/appointment report (ENG-910)

Ticket ask: report billable charge items with associated patient, encounter, and appointment details. The query delivers the right shape (patient → encounter → token booking → charge item, filtered to status = 'billable' and total_price > 0) and column set matches the ask, so requirement fidelity is fine at the grain level.

Numbers are not yet trustworthy — two correctness issues found:

  1. Critical — no facility scoping. Despite the _ssmm filename, no join or WHERE clause constrains to a single facility. This returns billable charge items across every facility, not just SSMM.
  2. High — no entered_in_error/deleted exclusion on emr_encounter, emr_tokenbooking, or emr_patientidentifier. Only the charge item's own status is checked; a mistakenly-created encounter/booking can still surface with a real charge item attached.

Documentation: Purpose/Notes/Parameters are accurate to the SQL and the earlier reviewer's "Purpose vs actual columns" comment was addressed. One gap remains: the template's ### Output column table is missing entirely (Medium).

Hygiene: correct domain folder, correct filename convention with _ssmm suffix, one file per PR, magic config_id = 21 documented in Notes — all fine.

Verdict: not yet safe to publish — fix the facility scope (Critical) and add status/deleted filters on the joined tables (High) before this feeds the dashboard. Output table doc gap is a quick follow-up, not a blocker for correctness.

Generated by Analytics SQL Reviewer for #147 · auto · 37.3 AIC · ⌖ 3.2 AIC · ⊞ 14.7K

ON emr_tokenbooking.associated_encounter_id = emr_encounter.id
JOIN emr_chargeitem
ON emr_chargeitem.id = emr_tokenbooking.charge_item_id
WHERE emr_chargeitem.status = 'billable'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] No facility scoping — the _ssmm suffix promises a single-facility query, but nothing in the SQL enforces it.

Facility boundaries are normally enforced by the application, not by Postgres — a raw query has to add it explicitly (care-sql-code-review skill, inversion 2). This query has no facility_id predicate anywhere: not on emr_encounter, not on emr_tokenbooking, not on emr_chargeitem. As written it returns billable charge items across every facility in the deployment, not just SSMM, which is both a wrong-number bug for a per-facility dashboard and a cross-tenant data exposure.

Fix — scope through the encounter (or another table that carries facility_id):

WHERE emr_chargeitem.status = 'billable'
  AND emr_chargeitem.total_price > 0
  AND emr_encounter.facility_id = <ssmm_facility_id>
  ...

If the intent is genuinely "all facilities, but named _ssmm for another reason," please say so in ## Notes — as it stands the filename and the SQL disagree.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is only one facility and thats ssmm

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood — thanks for confirming there's only one facility in this deployment today.

One thing worth flagging even so: this repo's convention for other single-facility assumptions (e.g. the config_id = 21 identifier mapping right below) is to say so explicitly in ## Notes, precisely because these facts aren't visible from the SQL itself and can silently stop being true (a second facility gets provisioned, a new encounter type is added elsewhere, etc.) without anyone noticing until the dashboard numbers are wrong. Right now ## Notes documents the identifier mapping but says nothing about the facility assumption.

Suggest adding a line such as:

Facility scope: This deployment currently has only one facility (SSMM); no explicit facility_id filter is applied. If a second facility is ever added to this instance, add AND emr_encounter.facility_id = <ssmm_facility_id> to keep this report scoped correctly.

That turns a query-level assumption into a documented, checkable one rather than an implicit fact a future reader has to rediscover. Not blocking — resolving this thread since the underlying data-scope concern is a conscious, confirmed decision for the current environment.

Generated by Analytics SQL Reviewer for #147 · auto · 30.9 AIC · ⌖ 2.38 AIC · ⊞ 14.7K

--[[AND {{encounter_status}}]]
--[[AND emr_patientidentifier.value = {{ssmm_id}}]]
ORDER BY emr_encounter.created_date DESC, emr_patient.name;
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] Missing ### Output section required by TEMPLATE.md.

The template calls for an ### Output table (column/type/description) directly under ## Query, documenting the 8 selected columns for the dashboard consumer. This doc jumps straight from the SQL fence to ## Notes — please add the output table before merging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outputs are not needed, they are optional.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough — ### Output is indeed optional per TEMPLATE.md conventions in this repo (only ## Purpose, ## Parameters, and ## Query are consistently required across existing docs). Not blocking on this. Resolving.

Generated by Analytics SQL Reviewer for #147 · auto · 30.9 AIC · ⌖ 2.38 AIC · ⊞ 14.7K

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants