Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alt phone number and qualificationspec #2767

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Jacobjeevan
Copy link
Contributor

@Jacobjeevan Jacobjeevan commented Jan 21, 2025

Proposed Changes

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

Release Notes

  • New Features

    • Added support for user qualifications with a new QualificationSpec class
    • Introduced alternative phone number field for users
  • Improvements

    • Updated user model to use JSON field for qualifications
    • Enhanced user data model with more detailed qualification tracking
  • Database Changes

    • Renamed existing user fields to preserve legacy data
    • Added new fields to support richer user profile information

The release introduces more comprehensive user profile management with improved data flexibility and structure.

@Jacobjeevan Jacobjeevan requested a review from a team as a code owner January 21, 2025 12:20
Copy link

coderabbitai bot commented Jan 21, 2025

📝 Walkthrough

Walkthrough

This pull request introduces significant changes across multiple files in the care/emr and care/users modules. The modifications primarily focus on enhancing user specifications, introducing a new PeriodSpec class for date validation, and updating the user model to use JSON fields for qualifications. The changes involve restructuring data models, adding new validation mechanisms, and preparing for more flexible user information storage.

Changes

File Change Summary
care/emr/resources/common/period.py Added PeriodSpec class with datetime validation
care/emr/resources/encounter/spec.py Removed PeriodSpec class and related imports
care/emr/resources/user/spec.py Added QualificationSpec class, new attributes to UserBaseSpec (alt phone number, qualifications)
care/users/migrations/0022_migrate_users_qualification.py Created migration to rename and add new user fields
care/users/models.py Updated User model fields, converted qualification to JSONField

Sequence Diagram

sequenceDiagram
    participant User
    participant UserModel
    participant QualificationSpec
    participant PeriodSpec

    User->>UserModel: Create/Update User
    UserModel->>QualificationSpec: Validate Qualification
    QualificationSpec->>PeriodSpec: Validate Period
    PeriodSpec-->>QualificationSpec: Return Validated Period
    QualificationSpec-->>UserModel: Return Validated Qualification
    UserModel-->>User: Save User Data
Loading

Possibly related PRs

Suggested reviewers

  • vigneshhari

Poem

🕰️ In fields of data, changes bloom
Validations dance, dispelling gloom
JSON whispers, models reborn
Qualifications no longer forlorn
Code evolves with gentle might 🌱

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🔭 Outside diff range comments (1)
care/emr/resources/encounter/spec.py (1)

Line range hint 33-34: Avoid using mutable default values for Pydantic fields

Assigning mutable default values like {} to Pydantic model fields can lead to unexpected behavior because the same object is shared across all instances. Instead, use PeriodSpec() and HospitalizationSpec() to create new instances with default values.

Apply this diff to fix the issue:

-period: PeriodSpec = {}
-hospitalization: HospitalizationSpec | None = {}
+period: PeriodSpec = PeriodSpec()
+hospitalization: HospitalizationSpec | None = HospitalizationSpec()
🧹 Nitpick comments (4)
care/users/migrations/0022_migrate_users_qualification.py (1)

8-31: Replace single quotes with double quotes for consistency

The static analysis tool suggests that double quotes are preferred over single quotes in this codebase. Updating the quotes will enhance consistency with the project's style guidelines.

Apply this diff to replace single quotes with double quotes:

-            ('users', '0021_rename_gender_user_old_gender_user_geo_organization_and_more'),
+            ("users", "0021_rename_gender_user_old_gender_user_geo_organization_and_more"),

Please update the remaining strings in the migration file similarly.

🧰 Tools
🪛 Ruff (0.8.2)

8-8: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


8-8: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


13-13: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


14-14: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


15-15: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


18-18: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


19-19: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


20-20: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


23-23: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


24-24: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


25-25: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


28-28: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


29-29: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)

care/emr/resources/user/spec.py (2)

32-35: Consider adding validation for the identifier field.

While the structure follows FHIR Practitioner.qualification, the identifier field could benefit from some constraints.

 class QualificationSpec(EMRResource):
-    identifier: str
+    identifier: str = Field(min_length=1, max_length=50)
     code: Coding
     period: PeriodSpec

36-37: Remove unnecessary empty lines.

There are two consecutive empty lines after the class definition.

care/users/models.py (1)

308-324: Consider adding FHIR validation middleware.

Since we're aligning with HL7 FHIR standards, it might be worth adding middleware to validate the qualification structure against the FHIR schema. This would ensure:

  1. Consistent data structure
  2. Compatibility with external FHIR systems
  3. Easier future migrations
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 35577b2 and 25ee9d1.

📒 Files selected for processing (5)
  • care/emr/resources/common/period.py (1 hunks)
  • care/emr/resources/encounter/spec.py (1 hunks)
  • care/emr/resources/user/spec.py (3 hunks)
  • care/users/migrations/0022_migrate_users_qualification.py (1 hunks)
  • care/users/models.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
care/users/migrations/0022_migrate_users_qualification.py

8-8: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


8-8: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


13-13: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


14-14: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


15-15: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


18-18: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


19-19: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


20-20: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


23-23: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


24-24: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


25-25: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


28-28: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


29-29: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test / test
🔇 Additional comments (3)
care/emr/resources/encounter/spec.py (1)

10-10: Importing PeriodSpec from the common module looks good

Importing PeriodSpec from care.emr.resources.common.period promotes reusability and maintains a cleaner codebase.

care/emr/resources/common/period.py (1)

6-14: PeriodSpec class implementation is well-structured

The PeriodSpec class is correctly defined with appropriate validation to ensure that the start date is not after the end date.

care/users/models.py (1)

308-315: Verify migration strategy for qualification data.

Setting blank=False on both new and old qualification fields might cause issues during migration. Consider the following:

  1. Existing records might not have qualification data
  2. The migration path from TextField to JSONField needs validation
✅ Verification successful

Actually, the migration strategy looks quite well thought out

The concerns about blank=False are unwarranted because:

  • The new qualification field has default=list, which prevents any blank value issues
  • The qualification_old field allows null=True, maintaining compatibility with existing data from the original doctor_qualification field
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for existing records with empty qualifications
echo "Checking for records with empty qualifications..."
rg -l "qualification\s*=\s*models\.TextField" | xargs -I {} rg "blank\s*=\s*True" {}

Length of output: 199


Script:

#!/bin/bash
# Search for any qualification field definition historically
echo "Searching for qualification field definitions..."
rg "qualification.*=.*models\." -A 3

echo -e "\nChecking for migration files..."
fd ".*migrations.*\.py$" care/users/

echo -e "\nLooking for qualification field usage..."
rg "qualification.*=" care/users/

Length of output: 979

Comment on lines +47 to +48
alt_phone_number: str = Field(max_length=14)
qualification: list[QualificationSpec]
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add phone number format validation.

The alt_phone_number field could benefit from the same validation as phone_number.

-    alt_phone_number: str = Field(max_length=14)
+    alt_phone_number: str = Field(max_length=14, pattern=r'^\+?1?\d{9,15}$')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
alt_phone_number: str = Field(max_length=14)
qualification: list[QualificationSpec]
alt_phone_number: str = Field(max_length=14, pattern=r'^\+?1?\d{9,15}$')
qualification: list[QualificationSpec]

Comment on lines +316 to 324
doctor_experience_commenced_on_old = models.DateField(
default=None,
blank=False,
null=True,
)
doctor_medical_council_registration = models.CharField(
doctor_medical_council_registration_old = models.CharField(
max_length=255,
default=None,
blank=False,
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Your migration seems to be having a lovely identity crisis

The migration file shows incomplete implementation that could lead to data loss:

  • Fields are renamed with _old suffix ✓
  • New JSONField is added ✓
  • But... where's the data migration logic? 🤔
  • And the serializers are still living in the past, validating old fields

We should:

  1. Add data migration logic to transform existing doctor experience data into the new JSON structure
  2. Update serializer validation to use the new JSON structure
  3. Document the new JSON schema for qualification data
  4. Add a rollback strategy in case of migration issues
🔗 Analysis chain

Clarify the migration path for doctor experience data.

The fields are marked with _old suffix, but I don't see corresponding new fields. Are these being moved to the qualification JSONField? If so, we should:

  1. Document the new structure
  2. Ensure the migration script handles the data transformation
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for migration files handling these fields
echo "Checking for related migrations..."
fd -e py -e yaml . | xargs rg "doctor_experience_commenced_on|doctor_medical_council_registration"

Length of output: 2611


Script:

#!/bin/bash
# Look for qualification field and related migrations
echo "Searching for qualification field..."
rg -A 5 "qualification.*JSONField|qualification.*Field.*JSON" care/users/models.py

echo -e "\nChecking migration files for qualification data structure..."
rg -A 10 "qualification.*=.*{" care/users/migrations/

Length of output: 612


Script:

#!/bin/bash
# Check the content of the migration file
echo "Examining qualification migration content..."
cat care/users/migrations/0022_migrate_users_qualification.py

echo -e "\nLooking for any documentation..."
fd README.md | xargs rg -i "qualification|doctor.*experience"

Length of output: 1283

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.

1 participant