Skip to content

Commit

Permalink
Add net fee properties and calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddharthPant committed Apr 2, 2024
1 parent 246db30 commit 01f069e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
16 changes: 15 additions & 1 deletion server/api/routes/claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
from fastapi import APIRouter, HTTPException, status
from sqlmodel import func, select

from server.models import Claim, ClaimCreate, ClaimOut, ClaimsOut, Record
from server.models import (
Claim,
ClaimCreate,
ClaimOut,
ClaimsOut,
NetFeeClaimOut,
Record,
)

from ..deps import SessionDep

Expand Down Expand Up @@ -44,3 +51,10 @@ def add_claim(session: SessionDep, claim_in: ClaimCreate) -> Any:
session.commit()
session.refresh(new_claim)
return new_claim


@router.get("/top_net_fee", response_model=list[NetFeeClaimOut])
def read_top_net_fee(session: SessionDep) -> list[NetFeeClaimOut]:
top_10_claims = session.query(Claim).order_by(Claim.total_net_fee).limit(10).all()

return top_10_claims
36 changes: 22 additions & 14 deletions server/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any
from typing import Any, ClassVar

from pydantic import ValidationError, field_validator
from sqlalchemy import text
Expand Down Expand Up @@ -39,14 +39,15 @@ class ClaimBase(SQLModel):
pass


def _total_net_fee(self) -> float:
net_fees = [record.net_fee for record in self.records]
return sum(net_fees)


class Claim(TimestampModel, ClaimBase, table=True):
id: int | None = Field(default=None, primary_key=True)
records: list["Record"] = Relationship(back_populates="claim")

@hybrid_property
def total_net_fee(self) -> float:
net_fees = [record.net_fee for record in self.records]
return sum(net_fees)
total_net_fee: ClassVar[float] = hybrid_property(_total_net_fee)


class RecordBase(SQLModel):
Expand All @@ -62,19 +63,21 @@ class RecordBase(SQLModel):
member_copay: float


def _net_fee(self) -> float:
return (
self.provider_fees
+ self.member_coinsurance
+ self.member_copay
- self.allowed_fees
)


class Record(RecordBase, table=True):
id: int | None = Field(default=None, primary_key=True)
claim_id: int | None = Field(default=None, foreign_key="claim.id")
claim: Claim | None = Relationship(back_populates="records")

@hybrid_property
def net_fee(self) -> float:
return (
self.provider_fees
+ self.member_coinsurance
+ self.member_copay
- self.allowed_fees
)
net_fee: ClassVar[float] = hybrid_property(_net_fee)


class RecordOut(RecordBase):
Expand All @@ -93,6 +96,11 @@ class ClaimsOut(SQLModel):
count: int


class NetFeeClaimOut(SQLModel):
id: int
total_net_fee: float


# Unfortunately there is a bug currently in versions of SQLModel > 0.0.13 that prevents
# alias parameters from working. We can't go back as 0.0.13 will need to revert back
# to pydantic v1 syntax which will cause too many breaking changes.
Expand Down

0 comments on commit 01f069e

Please sign in to comment.