Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/prefeitura-rio/histo…
Browse files Browse the repository at this point in the history
…rico-clinico-integrado into development
  • Loading branch information
TanookiVerde committed Oct 29, 2024
2 parents 77a5d19 + d33df8c commit 2257f44
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class LoginErrorEnum(str, Enum):
INACTIVE_EMPLOYEE = "inactive_employee"
REQUIRE_2FA = "require_2fa"

class AcceptTermsEnum(str, Enum):
SUCCESS = "success"
FAILURE = "failure"


class AccessErrorEnum(str, Enum):
NOT_FOUND = "NOT_FOUND"
Expand Down
3 changes: 3 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class User(Model):
is_2fa_required = fields.BooleanField(default=False)
is_2fa_activated = fields.BooleanField(default=False)
is_ergon_validation_required = fields.BooleanField(default=False)
# Terms of use
is_use_terms_accepted = fields.BooleanField(default=False)
use_terms_accepted_at = fields.DatetimeField(null=True)
# Metadata
is_active = fields.BooleanField(default=True)
is_superuser = fields.BooleanField(default=False)
Expand Down
42 changes: 41 additions & 1 deletion app/routers/frontend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import asyncio
import datetime
from typing import Annotated, List
from fastapi import APIRouter, Depends, Request
from fastapi_limiter.depends import RateLimiter
from fastapi.responses import JSONResponse

from app.decorators import router_request
from app.dependencies import assert_user_is_active, assert_cpf_is_valid
Expand All @@ -13,6 +15,7 @@
Encounter,
UserInfo,
)
from app.types.errors import AcceptTermsEnum
from app.utils import read_bq, validate_user_access_to_patient_data
from app.config import (
BIGQUERY_PROJECT,
Expand All @@ -23,7 +26,8 @@
REQUEST_LIMIT_WINDOW_SIZE,
)
from app.types.errors import (
AccessErrorModel
AccessErrorModel,
TermAcceptanceErrorModel
)

router = APIRouter(prefix="/frontend", tags=["Frontend Application"])
Expand All @@ -45,10 +49,46 @@ async def get_user_info(
"role": user.role.job_title if user.role else None,
"email": user.email,
"username": user.username,
"is_use_terms_accepted": user.is_use_terms_accepted,
"cpf": cpf,
}


@router_request(
method="POST",
router=router,
path="/user/accept-terms/",
response_model=TermAcceptanceErrorModel,
responses={
500: {"model": TermAcceptanceErrorModel},
},
)
async def accept_use_terms(
user: Annotated[User, Depends(assert_user_is_active)],
request: Request,
) -> TermAcceptanceErrorModel:

try:
user.is_use_terms_accepted = True
user.use_terms_accepted_at = datetime.datetime.now()
await user.save()
return JSONResponse(
status_code=200,
content={
"message": "Success",
"type": AcceptTermsEnum.SUCCESS,
},
)
except Exception:
return JSONResponse(
status_code=500,
content={
"message": "Patient not found",
"type": AcceptTermsEnum.FAILURE,
},
)


@router_request(
method="GET",
router=router,
Expand Down
10 changes: 8 additions & 2 deletions app/types/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from app.enums import (
LoginErrorEnum,
AccessErrorEnum
AccessErrorEnum,
AcceptTermsEnum
)


Expand All @@ -14,4 +15,9 @@ class AuthenticationErrorModel(BaseModel):

class AccessErrorModel(BaseModel):
message: str
type: AccessErrorEnum
type: AccessErrorEnum


class TermAcceptanceErrorModel(BaseModel):
message: str
type: AcceptTermsEnum
14 changes: 14 additions & 0 deletions migrations/app/35_20241029140029_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "user" ADD "use_terms_accepted_at" TIMESTAMPTZ;
ALTER TABLE "user" ADD "is_use_terms_accepted" BOOL NOT NULL DEFAULT False;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "user" DROP COLUMN "use_terms_accepted_at";
ALTER TABLE "user" DROP COLUMN "is_use_terms_accepted";"""

0 comments on commit 2257f44

Please sign in to comment.