Skip to content

Commit

Permalink
Merge pull request #174 from prefeitura-rio/feat/mocked-frontend-endp…
Browse files Browse the repository at this point in the history
…oints

Feat/mocked frontend endpoints
  • Loading branch information
TanookiVerde committed Jul 29, 2024
2 parents 666cf0f + 42fd64f commit ad43545
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 14 deletions.
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ RUN pip install --no-cache-dir -U poetry && \
COPY poetry.lock pyproject.toml ./
RUN poetry install --no-dev --no-interaction --no-ansi

# Install dos2unix
RUN apt-get update && apt-get install -y dos2unix


# Copy the project files into the working directory
COPY ./app /app
COPY ./scripts .
COPY ./data /data
COPY ./migrations /migrations
COPY ./compose-entrypoint.sh /compose-entrypoint.sh

RUN dos2unix /compose-entrypoint.sh

# Grant execution permissions
RUN chmod +x /compose-entrypoint.sh

# Inicializa banco, roda migrations e sobe servidor
ENTRYPOINT ["sh", "/compose-entrypoint.sh"]
ENTRYPOINT ["sh", "compose-entrypoint.sh"]
2 changes: 1 addition & 1 deletion app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from app import config
from app.models import User
from app.pydantic_models import TokenData
from app.types.pydantic_models import TokenData
from jwt import PyJWTError

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
Expand Down
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from app import config
from app.db import TORTOISE_ORM
from app.routers import auth, entities_mrg, entities_raw, entities_std, entities
from app.routers import auth, entities_mrg, entities_raw, entities_std, entities, frontend

logger.remove()
logger.add(sys.stdout, level=config.LOG_LEVEL)
Expand Down Expand Up @@ -47,6 +47,7 @@
app.include_router(entities_mrg.router)
app.include_router(entities.router)
app.include_router(auth.router)
app.include_router(frontend.router)

register_tortoise(
app,
Expand Down
4 changes: 3 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,13 @@ class Meta:
class User(Model):
id = fields.IntField(pk=True)
username = fields.CharField(max_length=255, unique=True)
name = fields.CharField(max_length=255, null=True)
cpf = fields.CharField(max_length=11, unique=True, null=True, validators=[CPFValidator()])
email = fields.CharField(max_length=255, unique=True)
data_source = fields.ForeignKeyField("app.DataSource", related_name="users", null=True)
password = fields.CharField(max_length=255)
is_active = fields.BooleanField(default=True)
is_superuser = fields.BooleanField(default=False)
data_source = fields.ForeignKeyField("app.DataSource", related_name="users", null=True)
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)

Expand Down
4 changes: 2 additions & 2 deletions app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from app import config
from app.models import User
from app.pydantic_models import Token
from app.types.pydantic_models import Token
from app.utils import authenticate_user, create_access_token


Expand Down Expand Up @@ -40,4 +40,4 @@ async def login_for_access_token(
return {
"access_token": access_token,
"token_type": "bearer"
}
}
2 changes: 1 addition & 1 deletion app/routers/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tortoise.contrib.pydantic import pydantic_model_creator

from app.dependencies import get_current_active_user
from app.pydantic_models import UserRegisterInputModel, UserRegisterOutputModel
from app.types.pydantic_models import UserRegisterInputModel, UserRegisterOutputModel
from app.utils import password_hash
from app.models import (
User, DataSource
Expand Down
2 changes: 1 addition & 1 deletion app/routers/entities_mrg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tortoise.exceptions import ValidationError

from app.dependencies import get_current_active_user
from app.pydantic_models import (
from app.types.pydantic_models import (
CompletePatientModel,
MergedPatient as PydanticMergedPatient,
MergedPatientCns as PydanticMergedPatientCns,
Expand Down
2 changes: 1 addition & 1 deletion app/routers/entities_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from loguru import logger
from tortoise.exceptions import ValidationError

from app.pydantic_models import RawDataListModel, BulkInsertOutputModel, RawDataModel
from app.types.pydantic_models import RawDataListModel, BulkInsertOutputModel, RawDataModel
from app.dependencies import get_current_active_user
from app.models import User, RawPatientRecord, RawPatientCondition, DataSource, RawEncounter

Expand Down
2 changes: 1 addition & 1 deletion app/routers/entities_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tortoise.exceptions import ValidationError, DoesNotExist

from app.dependencies import get_current_active_user
from app.pydantic_models import (
from app.types.pydantic_models import (
PatientMergeableRecord, StandardizedPatientRecordModel, StandardizedPatientConditionModel,
BulkInsertOutputModel, MergeableRecord, Page
)
Expand Down
121 changes: 121 additions & 0 deletions app/routers/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
from typing import Annotated, List

from fastapi import APIRouter, Depends

from app.dependencies import get_current_active_user
from app.models import User
from app.types.frontend import (
PatientHeader,
PatientSummary,
Encounter,
UserInfo,
)


router = APIRouter(prefix="/frontend", tags=["Frontend Application"])


@router.get("/user")
async def get_user_info(
user: Annotated[User, Depends(get_current_active_user)],
) -> UserInfo:
if user.cpf:
cpf = user.cpf
cpf = f"{cpf[:3]}.{cpf[3:6]}.{cpf[6:9]}-{cpf[9:]}"
else:
cpf = None

return {
"name": user.name,
"email": user.email,
"username": user.username,
"cpf": cpf,
}


@router.get("/patient/header/{cpf}")
async def get_patient_header(
_: Annotated[User, Depends(get_current_active_user)],
cpf: str,
) -> PatientHeader:
return {
"registration_name": "José da Silva Xavier",
"social_name": None,
"cpf": f"{cpf[:3]}.{cpf[3:6]}.{cpf[6:9]}-{cpf[9:]}",
"cns": "123456789012345",
"birth_date": "1972-08-01",
"gender": "masculino",
"race": "parda",
"phone": "(21) 99999-9999",
"family_clinic": {"cnes": "1234567", "name": "Clinica da Familia XXX"},
"family_health_team": {"ine_code": "1234567", "name": "Equipe Roxo"},
"medical_responsible": "Roberta dos Santos",
"nursing_responsible": "Pedro da Nobrega",
"validated": True,
}


@router.get("/patient/summary/{cpf}")
async def get_patient_summary(
_: Annotated[User, Depends(get_current_active_user)],
cpf: str,
) -> PatientSummary:
return {
"allergies": [
"Sulfonamidas",
"Ácaros do pó",
"Penicilina",
"Medicamentos anticonvulsivantes",
"Gatos",
"Gramíneas",
"Picadas de abelhas",
"Picadas de vespas",
"Preservativos",
"Luvas de látex",
],
"continuous_use_medications": [
"Losartana potássica",
"Enalapril maleato",
"Besilato de anlodipino",
"Captopril",
"Clonazepam",
"Enalapril",
],
}


@router.get("/patient/encounters/{cpf}")
async def get_patient_encounters(
_: Annotated[User, Depends(get_current_active_user)],
cpf: str,
) -> List[Encounter]:
return [
{
"entry_datetime": "2023-09-01T10:00:00",
"exit_datetime": "2023-09-01T12:00:00",
"location": "UPA 24h Magalhães Bastos",
"type": "Consulta",
"subtype": "Emergência",
"active_cids": ["A10.2", "B02.5"],
"responsible": {"name": "Dr. João da Silva", "role": "Médico(a)"},
"description": "Lorem ipsum dolor sit amet consectetur.",
"filter_tags": ["UPA"],
},
{
"entry_datetime": "2021-09-01T10:00:00",
"exit_datetime": "2021-09-01T12:00:00",
"location": "UPA 24h Magalhães Bastos",
"type": "Consulta",
"subtype": "Emergência",
"active_cids": ["A10.2"],
"responsible": {"name": "Dr. João da Silva", "role": "Médico(a)"},
"description": (
"Lorem ipsum dolor sit amet consectetur. Sed vel suscipit id pulvinar"
"sed nam libero eu. Leo arcu sit lacus nisl nullam eget et dignissim sed."
"Fames pretium cursus viverra posuere arcu tortor sit lectus congue. Velit"
"tempor ultricies pulvinar magna pulvinar ridiculus consequat nibh..."
),
"filter_tags": ["UPA", "Emergência"],
},
]
63 changes: 63 additions & 0 deletions app/types/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
from typing import Optional, List
from pydantic import BaseModel


# Clinic Family model
class FamilyClinic(BaseModel):
cnes: str
name: str


# Family Health Team model
class FamilyHealthTeam(BaseModel):
ine_code: str
name: str


# Medical Conditions model
class PatientSummary(BaseModel):
allergies: List[str]
continuous_use_medications: List[str]


# Responsible model
class Responsible(BaseModel):
name: str
role: str


# Medical Visit model
class Encounter(BaseModel):
entry_datetime: str
exit_datetime: str
location: str
type: str
subtype: Optional[str]
active_cids: List[str]
responsible: Responsible
description: Optional[str]
filter_tags: List[str]


class UserInfo(BaseModel):
name: Optional[str]
cpf: Optional[str]
username: str
email: str


class PatientHeader(BaseModel):
registration_name: str
social_name: Optional[str]
cpf: Optional[str]
cns: Optional[str]
birth_date: Optional[str]
gender: Optional[str]
race: Optional[str]
phone: Optional[str]
family_clinic: FamilyClinic
family_health_team: FamilyHealthTeam
medical_responsible: Optional[str]
nursing_responsible: Optional[str]
validated: bool
Loading

0 comments on commit ad43545

Please sign in to comment.