care icon indicating copy to clipboard operation
care copied to clipboard

User and facility flag viewset

Open praffq opened this issue 7 months ago • 2 comments

Proposed Changes

  • Added viewset for facility and user flag , only accessible to super user

Associated Issue

  • fixes - https://github.com/ohcnetwork/roadmap/issues/96

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 facility- and user-scoped feature flag management with CRUD APIs and endpoints to list available flags; restricted to superusers.
    • Flags are registered/unregistered centrally and include cache-backed lookups for faster reads.
  • Bug Fixes

    • Validation prevents creating duplicate flags for the same facility or user; safe cleanup unregisters unused flags after deletions.
  • Chores

    • Consolidated flag models and logic into the EMR module and updated model references and migrations for consistency.

✏️ Tip: You can customize this high-level summary in your review settings.

praffq avatar May 15 '25 18:05 praffq

📝 Walkthrough

Walkthrough

This update migrates the FacilityFlag and UserFlag models and their related logic from the facility and users apps into the emr app. It introduces new API endpoints and Pydantic specs for managing these flags, centralizes flag model logic, and updates all references and migrations accordingly. Router and admin configurations are also updated.

Changes

File(s) Change Summary
care/emr/api/viewsets/facility.py, care/emr/api/viewsets/user.py Added FacilityFlagViewSet and UserFlagViewSet with full CRUD, filtering, registry sync, cache invalidation, and new available-flags endpoints.
care/emr/migrations/0030_facilityflag_userflag.py Introduced new models FacilityFlag and UserFlag in the emr app with all fields, constraints, and relations.
care/emr/models/base.py Added new abstract BaseFlag model with caching, validation, and flag logic.
care/emr/models/facility.py, care/emr/models/user.py Updated to use new BaseFlag; FacilityFlag and new UserFlag now reside in emr.models; constraint and import adjustments.
care/emr/models/file_upload.py, care/emr/models/patient.py, care/emr/models/scheduling/booking.py Changed User foreign key references to string-based lazy references.
care/emr/resources/facility/spec.py, care/emr/resources/user/spec.py Added Pydantic specs for FacilityFlag and UserFlag with validation and serialization logic.
care/facility/migrations/0477_delete_facilityflag.py, care/users/migrations/0024_delete_userflag.py Deleted old FacilityFlag and UserFlag models from facility and users apps.
care/facility/models/init.py, care/facility/models/facility.py Removed or updated imports for FacilityFlag to point to new location.
care/users/admin.py, care/users/models.py Updated UserFlag import to new emr.models.user location; removed old model definition.
care/utils/models/base.py Removed old BaseFlag and related flag logic from utils.
config/api_router.py Registered new FacilityFlagViewSet and UserFlagViewSet in the API router.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant FacilityFlag
    participant FlagRegistry
    participant Cache

    Client->>API: POST /facility_flags/ (create flag)
    API->>FacilityFlag: Create instance in DB
    API->>FlagRegistry: Register flag (within transaction)
    API->>Cache: Invalidate relevant cache
    API-->>Client: Return created flag

    Client->>API: DELETE /facility_flags/{id}/
    API->>FacilityFlag: Mark instance as deleted in DB
    API->>Cache: Invalidate cache for flag/facility
    API->>FlagRegistry: Unregister flag if unused (after commit)
    API-->>Client: Return success

    Client->>API: GET /facility_flags/available-flags/
    API->>FlagRegistry: List all registered facility flags
    API-->>Client: Return available flag list
sequenceDiagram
    participant Client
    participant API
    participant UserFlag
    participant FlagRegistry
    participant Cache

    Client->>API: POST /user_flags/ (create flag)
    API->>UserFlag: Create instance in DB
    API->>FlagRegistry: Register flag (within transaction)
    API->>Cache: Invalidate relevant cache
    API-->>Client: Return created flag

    Client->>API: DELETE /user_flags/{id}/
    API->>UserFlag: Mark instance as deleted in DB
    API->>Cache: Invalidate cache for flag/user
    API->>FlagRegistry: Unregister flag if unused (after commit)
    API-->>Client: Return success

    Client->>API: GET /user_flags/available-flags/
    API->>FlagRegistry: List all registered user flags
    API-->>Client: Return available flag list

Suggested labels

waiting-for-review

Suggested reviewers

  • vigneshhari

Poem

The flags have packed their bags, you see,
And moved to EMR, quite merrily!
With cache in hand and specs anew,
They left old homes (a thing to rue?).
APIs now wave their banners high—
Facility and User flags unified!
(Migration scripts sigh, "About time, guys!")


📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7597a7f992a7fd564200c7484a4ca1339d0a5f7c and f5f56d6284b2efcf4f88f7095fc34394316a5bb8.

📒 Files selected for processing (2)
  • care/facility/models/facility.py (1 hunks)
  • config/api_router.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • care/facility/models/facility.py
  • config/api_router.py
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test / Test
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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 May 15 '25 18:05 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 63.54167% with 70 lines in your changes missing coverage. Please review.

Project coverage is 51.56%. Comparing base (1fa63f2) to head (7597a7f).

Files with missing lines Patch % Lines
care/emr/api/viewsets/facility.py 54.28% 16 Missing :warning:
care/emr/api/viewsets/user.py 52.94% 16 Missing :warning:
care/emr/models/base.py 66.66% 12 Missing :warning:
care/emr/resources/user/spec.py 60.00% 12 Missing :warning:
care/emr/resources/facility/spec.py 63.33% 11 Missing :warning:
care/emr/models/user.py 85.00% 3 Missing :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3044      +/-   ##
===========================================
+ Coverage    51.40%   51.56%   +0.16%     
===========================================
  Files          248      250       +2     
  Lines        11157    11323     +166     
  Branches      1241     1247       +6     
===========================================
+ Hits          5735     5839     +104     
- Misses        5403     5465      +62     
  Partials        19       19              

: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 May 15 '25 18:05 codecov[bot]