Backend

Backend design, models, APIs, and processing flow for Toxicology.

👤 Rushikesh Sakharwade📅 Updated: May 7, 2026📁 Toxicology

Backend

Architecture Overview

Toxicology backend behavior is implemented across livehealthapp and crelio-app. These services own the APIs, validation, persistence, and mapping behavior for drug, panel, and brand master data.

The same backend surface also persists toxicology report/test configuration. Once a report is created with test type Toxicology, the report parameter configuration can store toxicology components such as Screening, Confirmation, Prescription, Summary, History, Image, and Clinical Notes.

For runtime report behavior in livehealthapp, toxicology passes through the generic billing and report-submit flows. billDefaultController and SubmitReport are not toxicology-specific endpoints; they are shared paths where toxicology-specific branches/config handling must be understood.

System Design Diagram

Storage & Models

Important storage note: toxicology report values are not stored in the MySQL report-value tables. Runtime toxicology component values are stored in MongoDB/DocumentDB in the ReportValue collection, keyed against the lab report ID. This applies to submitted component values such as Screening, Confirmation, Prescription, Summary, History, Image, and Clinical Notes. Master data and report configuration still follow their respective relational/configuration storage paths.

Primary model areas:

  • Drug master
  • Panel master
  • Brand master
  • Drug-to-panel mappings
  • Drug-to-brand mappings
  • Toxicology report/test metadata
  • Toxicology report parameter components
  • Screening selected-field configuration
  • Screening meta configuration: group by, sort by, order by
  • Screening defaults: drugs, panels, and brands added by default
  • Screening billing availability configuration
  • Confirmation reflex configuration
  • Prescription selected drug/panel/brand configuration
  • Summary linked component and summary type configuration
  • History linked component, report-count, display preference, date format, date filter, and ordering configuration
  • Image grid rows, columns, and default upload configuration
  • Clinical Notes linked component, field visibility, and drug-scope configuration

Key model locations:

  • livehealthapp: runtime billing/report paths where toxicology logic passes through generic flows.
  • crelio-app: PY-3 surfaces where migrated master/config APIs exist.
  • Exact file paths should be added after source mapping in the local checkout.

Core Backend Responsibilities

ConcernBackend responsibility
Source of truthPersist drug, panel, and brand master records
ValidationEnforce required names/codes and required drug selection for collections
Permissions / scopeRestrict master-data actions to authorized users/labs
PersistenceMaintain panel-to-drug and brand-to-drug relationships
Audit / loggingTBD
Downstream syncMake prerequisite master data available to toxicology configuration and reporting flows
Report componentsPersist Screening, Confirmation, Prescription, Summary, History, Image, and Clinical Notes configuration
Screening behaviorPersist selected fields, defaults, billing availability, mandatory-drug behavior, and meta ordering
Confirmation behaviorPersist Screening-style configuration plus Reflex settings
Prescription behaviorPersist prescribed-drug configuration for patient medication history
Summary behaviorPersist linked Screening/Confirmation component and selected summary classifications
History behaviorPersist historical-summary settings and linked result component
Image behaviorPersist image-grid dimensions and default image attachments
Clinical Notes behaviorPersist linked result component, note fields, hidden flags, and all/selected-drug scope
Billing defaultsApply toxicology-specific defaults and orderables during billing
Report submissionAdd toxicology-specific component parsing inside the generic report-submit flow and persist submitted values into DocumentDB ReportValue records for the lab report

Runtime Engine / Processing Flow

  1. Frontend submits create/update data for drug, panel, or brand.
  2. Backend resolves lab/user context and validates the payload.
  3. Backend persists the master record.
  4. For panels and brands, backend persists the selected drug mappings.
  5. Backend returns the updated record/list response to the frontend.

Toxicology report setup flow:

  1. Frontend creates or updates a report/test with test type Toxicology.
  2. Frontend adds toxicology report components from the Report Parameters tab.
  3. Backend persists each component with its title and referring list.
  4. For Screening, backend persists selected report-entry fields, labels, editability, and hidden behavior.
  5. Backend persists Screening meta settings for grouping, sorting, and ordering.
  6. Backend persists Screening defaults so selected drugs/panels/brands can be added when the test is billed.
  7. If Available during Billing is enabled, backend exposes the selected orderable drugs/panels/brands to billing workflows.
  8. For Confirmation, backend persists Reflex settings for screening reflex, prescription reflex, and bill-entry activation.
  9. For Prescription, backend persists selected drugs/panels/brands used to track patient prescribed medications.
  10. For Summary, backend persists the linked Screening/Confirmation component and selected summary types.
  11. For History, backend persists the linked component, previous-report count, display type, summary types, date format, date filter, and ordering.
  12. For Image, backend persists grid dimensions and uploaded/default image metadata.
  13. For Clinical Notes, backend persists linked component, visible fields, labels, hidden flags, and all-drugs/selected-drugs scope.

Billing-time toxicology behavior:

  1. Billing opens or prepares the toxicology test.
  2. Billing reads the test/report parameter configuration.
  3. Screening defaults are resolved from configured drugs, panels, or brands.
  4. If Available during Billing is enabled, configured orderable drugs/panels/brands are exposed to the billing modal.
  5. If Drugs Mandatory is enabled, billing must enforce drug selection before continuing.
  6. Confirmation reflex settings decide whether confirmation drugs should be added automatically or only when requested at bill entry.
  7. Prescription configuration can expose prescribed/orderable drugs, panels, or brands for patient medication history capture.
  8. The resulting toxicology component defaults/orderables are attached to the bill/order context for report entry.

Report-entry toxicology behavior in the generic submit flow:

  1. Report entry loads the billed toxicology test and component configuration.
  2. User-entered values for Screening, Confirmation, Prescription, Summary, History, Image, and Clinical Notes are submitted.
  3. The generic submit flow receives the structured component payload.
  4. Screening/Confirmation rows persist drug-level values such as cut off, result, interpretation, upper limit, name, and reflex-related fields into the DocumentDB ReportValue collection for the lab report.
  5. Prescription persists prescribed drug context.
  6. Summary persists linked-component summary classifications.
  7. History persists generated or submitted historical-summary configuration/output.
  8. Image persists uploaded image metadata or report image references.
  9. Clinical Notes persists drug-level notes linked to the configured component.
  10. Report status, audit fields, and downstream report rendering data are updated as part of the normal report submission flow.

Shared Flow Touchpoints

Generic flowStackWhen it runsToxicology-specific behavior
billDefaultControllerlivehealthappBilling / bill preparationToxicology config contributes default drugs/panels/brands, billing-available orderables, mandatory-drug behavior, and reflex-at-billing behavior
SubmitReportlivehealthappReport entry submitToxicology payload contains component values and linked component data that must be parsed and persisted correctly

Toxicology behavior in billDefaultController

billDefaultController is a generic billing default path. The toxicology-specific concern is what gets added to the bill/report-entry context when the billed test has test type Toxicology.

Developer checklist:

  • Load the billed test and identify whether it is a toxicology test.
  • Read report parameter configuration for Screening, Confirmation, Prescription, and related components.
  • Resolve Screening Defaults into bill/report-entry rows.
  • Expand configured panels/brands into their mapped drugs where required.
  • Respect Available during Billing by exposing only configured orderable drugs/panels/brands.
  • Respect Drugs Mandatory by blocking or flagging incomplete billing input where configured.
  • Apply Confirmation reflex options:
    • Screening Reflex
    • Prescription reflex
    • Ask at Bill Entry to Enable Reflex
  • Carry enough metadata forward so the generic report-submit flow can map submitted values back to the correct toxicology component.

Toxicology behavior in SubmitReport

SubmitReport is a generic report-entry persistence path. The toxicology-specific concern is that toxicology components are structured payloads with linked components, defaults, drug rows, summary classifications, image grids, and clinical-note scopes.

Developer checklist:

  • Resolve the report/test and confirm toxicology component configuration.
  • Map submitted rows back to their component: Screening, Confirmation, Prescription, Summary, History, Image, or Clinical Notes.
  • Preserve linked component relationships, especially:
    • Summary -> Screening/Confirmation
    • History -> Screening/Confirmation
    • Clinical Notes -> Screening/Confirmation
  • Persist drug-level values with the configured labels/visibility semantics.
  • Persist uploaded image metadata for Image grids.
  • Preserve summary classifications:
    • Consistent
    • Inconsistent
    • Prescribed but Consistent
    • Prescribed but Inconsistent
  • Keep report rendering output consistent with component Meta settings such as group by, sort by, order by, date format, and date filter.

Important Toxicology Snippets (Backend)

Add focused snippets here after the exact livehealthapp and crelio-app files are mapped.

High-value snippets to include:

  • generic billing default branch that identifies toxicology tests.
  • toxicology logic that resolves Screening defaults.
  • toxicology logic that expands panels/brands into drugs.
  • toxicology logic for Available during Billing and Drugs Mandatory.
  • toxicology logic for Confirmation reflex toggles.
  • generic report-submit parsing for toxicology components.
  • toxicology persistence for Screening/Confirmation drug-level rows.
  • toxicology persistence for Summary, History, Image, and Clinical Notes linked components.

API / URL Touchpoints

This table intentionally lists only URL patterns verified in the local livehealthapp / crelio-app checkout. Component fields such as Screening defaults, billing availability, Summary types, History settings, Image grid settings, and Clinical Notes visibility are configuration payload fields, not separate endpoints.

StackURL patternMethod / viewSourceToxicology-specific purpose
livehealthapp^billing/$billDefaultControllerlivehealthapp/livehealthapp/urls.pyShared billing path where toxicology defaults/orderables are applied when a toxicology test is billed
livehealthapp^get-lab-report/$SubmitReportlivehealthapp/reports/urls.pyShared report-submit path where toxicology component payloads are submitted
livehealthapp^dataPartialFromToxDevice/$GetToxDataForPending.as_view()livehealthapp/livehealthapp/urls.py, livehealthapp/devices/views.pyReceives pending toxicology device data and maps it into report values
livehealthapp^integration/tox/add_drugs_into_report/$add_mirco_tox_params_in_reportlivehealthapp/livehealthapp/urls.py, livehealthapp/labs/integration_functions.pyIntegration path to add toxicology drugs into report values
crelio-appreport/<int:report_id>/drugs/defaultDrugReportValuesUpdateViewcrelio-app/report/urls.py, crelio-app/report/views/drug_report_values.pyFetches existing Screening, Confirmation, and Prescription drug values from DocumentDB
crelio-appreport/<int:report_id>/drugs/updateDrugReportValuesUpdateViewcrelio-app/report/urls.py, crelio-app/report/views/drug_report_values.pyUpdates default drugs in DocumentDB for unfilled toxicology reports
crelio-apprelated-drugs/<int:primary_drug_id>FetchRelatedDrugsViewcrelio-app/report/urls.pyFetches related/reflex drugs for toxicology
crelio-apprelated-drugs/newRelatedDrugsViewcrelio-app/report/urls.pyCreates related/reflex drug mapping
crelio-apprelated-drugs/updateRelatedDrugsViewcrelio-app/report/urls.pyUpdates related/reflex drug mapping
crelio-apprelated-drugs/delete/<int:related_drug_id>RelatedDrugsViewcrelio-app/report/urls.pyDeletes related/reflex drug mapping
crelio-appdevice-results-validation/tox/newToxDeviceResultsForValidationcrelio-app/interfacing/urls.pySaves toxicology device results for validation
crelio-appdevice-results-validation/tox/<int:device_id>FetchToxDeviceResultsForValidationcrelio-app/interfacing/urls.pyFetches toxicology device results for validation
crelio-appdevice-results-validation/release-tox-parametersToxParameterReleaseViewcrelio-app/interfacing/urls.pyReleases reviewed toxicology device parameters
crelio-appsupport/tox-cache/clear/<int:lab_id>ClearToxicologyCacheViewcrelio-app/support/urls.pyClears toxicology cache for a lab

Entity Creation / Side Effects

Saving panels and brands creates or updates collection mappings to drug records.

Saving Screening defaults creates report-parameter configuration that can auto-add selected drugs or panels once the toxicology test is billed. Enabling Available during Billing makes selected drugs, panels, or brands available on the billing modal. Any audit logs, activity logs, queueing, or system-default request behavior should be documented after backend source paths are confirmed.

Confirmation reflex configuration can add confirmation drugs from screening or prescription context. Prescription configuration tracks prescribed patient drugs. Summary configuration depends on a linked Screening or Confirmation component so it can summarize the correct result set.

History configuration also depends on a linked component and generates historical context across previous reports. Image configuration stores report image-grid behavior. Clinical Notes configuration links note fields to a result component and controls whether notes apply to all drugs or selected drugs.

On this page