care icon indicating copy to clipboard operation
care copied to clipboard

fix for entered in error choices

Open praffq opened this issue 6 months ago โ€ข 2 comments

Proposed Changes

  • added validation to specs and removed delete endpoints from viewsets

Associated Issue

  • Fixes https://github.com/ohcnetwork/roadmap/issues/112

Architecture changes

  • Remove this section if not used

Merge Checklist

  • [ ] Tests added/fixed
  • [ ] Update docs in /docs
  • [ ] Linting Complete
  • [ ] Any other necessary step

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • New Features

    • Added post-update validation to prevent edits to records marked "entered in error" across multiple resource types (conditions, consents, devices, encounters, medication records, allergy records, and bookings).
  • Refactor

    • Modularized EMR viewsets into smaller mixins for clearer composition.
    • Removed device deletion support from the device management interface.
  • Bug Fixes

    • Enforced validation to block updates on error-marked records.
  • Tests

    • Updated tests to set explicit statuses and removed deletion-related tests.

praffq avatar Jun 03 '25 14:06 praffq

๐Ÿ“ Walkthrough

Walkthrough

Refactors multiple EMR viewsets to use explicit EMR mixins instead of a monolithic base class and adds model-level validators across many resource update specs to block updates when status is entered_in_error. Some delete/destroy logic and related delete tests were removed.

Changes

Cohort / File(s) Change Summary
Viewset mixin refactor
care/emr/api/viewsets/...
Replaced EMRModelViewSet with explicit imports and class inheritance using EMRBaseViewSet, EMRCreateMixin, EMRRetrieveMixin, EMRUpdateMixin, EMRListMixin, EMRUpsertMixin, and other existing mixins across multiple viewsets (condition, consent, device, medication_*).
Device destroy removal
care/emr/api/viewsets/device.py
Removed authorize_destroy and perform_destroy methods, eliminating device deletion handling.
Resource update validators
care/emr/resources/.../spec.py
Added Pydantic post-init (@model_validator(mode="after")) validators to various UpdateSpec classes to raise on updates when existing object's status/verification_status == entered_in_error.
Tests: status explicit + delete tests removed
care/emr/tests/*.py
Modified tests to create records with explicit status/verification_status values for update tests; removed delete-related test cases for affected resources.
Small formatting/logic condense
care/facility/models/asset.py, care/facility/models/mixins/permissions/patient.py
Condensed boolean return expressions in has_write_permission methods without changing semantics.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API_ViewSet
    participant PydanticValidator

    Client->>API_ViewSet: PATCH/PUT update request
    API_ViewSet->>PydanticValidator: Run model post-validator (info.context has existing model)
    PydanticValidator-->>API_ViewSet: Raise ValueError if existing.status == "entered_in_error"
    alt validator raised
        API_ViewSet-->>Client: 4xx error (update rejected)
    else
        API_ViewSet-->>Client: Proceed with update
    end

Estimated code review effort

๐ŸŽฏ 3 (Moderate) | โฑ๏ธ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent updates to any resource with entered_in_error status (#112) โœ…
Remove delete endpoints and related logic for resources where entered_in_error is used (#112) โ“ The PR removes device destroy methods and deletes related tests; other viewsets were refactored but explicit removal of destroy endpoints across all resources is unclear from diff.

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Refactor to explicit EMR mixins across many viewsets (care/emr/api/viewsets/...) Structural refactor not explicitly required by #112; it's broader in scope though it supports finer-grained behavior control.

Possibly related PRs

  • ohcnetwork/care#3004 โ€” Refactors adding/adjusting EMR mixins and EMRBaseViewSet, highly related to the viewset decomposition here.
  • ohcnetwork/care#2769 โ€” Modifies MedicationAdministrationViewSet inheritance; overlaps with this PR's changes to that file.
  • ohcnetwork/care#2778 โ€” Updates allergy_intolerance specs/tests; related to the added validators and test changes.

Suggested labels

waiting-for-review

Suggested reviewers

  • sainak
  • vigneshhari

Poem

In EMR halls the rules are clear and sheer,
"entered_in_error" now gets no volunteer.
Mixins line up, monoliths politely shooed,
Deletes gone quiet โ€” tests made less rude.
Status locked tight; the validators stood.

[!TIP]

๐Ÿ”Œ Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

โœจ Finishing Touches
  • [ ] ๐Ÿ“ Generate Docstrings
๐Ÿงช Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
  • [ ] Commit unit tests in branch prafful/bugs/fix-for-entered-in-error-choices

๐Ÿชง Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jun 03 '25 14:06 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 41.30435% with 54 lines in your changes missing coverage. Please review.

Project coverage is 54.83%. Comparing base (da5d37b) to head (f0314ba).

Files with missing lines Patch % Lines
care/emr/resources/allergy_intolerance/spec.py 40.00% 6 Missing :warning:
care/emr/resources/condition/spec.py 40.00% 6 Missing :warning:
care/emr/resources/consent/spec.py 33.33% 6 Missing :warning:
care/emr/resources/device/spec.py 40.00% 6 Missing :warning:
care/emr/resources/encounter/spec.py 40.00% 6 Missing :warning:
...re/emr/resources/medication/administration/spec.py 33.33% 6 Missing :warning:
care/emr/resources/medication/request/spec.py 33.33% 6 Missing :warning:
care/emr/resources/medication/statement/spec.py 40.00% 6 Missing :warning:
care/emr/resources/scheduling/slot/spec.py 40.00% 6 Missing :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3061      +/-   ##
===========================================
- Coverage    54.93%   54.83%   -0.10%     
===========================================
  Files          377      377              
  Lines        16251    16324      +73     
  Branches      1696     1704       +8     
===========================================
+ Hits          8927     8952      +25     
- Misses        7297     7345      +48     
  Partials        27       27              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jun 03 '25 15:06 codecov[bot]