Skip to content

Commit

Permalink
Add certified request functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
noxethiems committed Dec 3, 2023
1 parent ac12930 commit 3573ec3
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 0 deletions.
90 changes: 90 additions & 0 deletions identity_socializer/db/dao/certified_request_dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import uuid
from enum import Enum
from typing import List, Optional

from fastapi import Depends
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from identity_socializer.db.dependencies import get_db_session
from identity_socializer.db.models.certified_request_model import CertifiedRequestModel


class StatusRequest(Enum):
"""Enum for status requests."""

APPROVED = "Aprobado"
REJECTED = "Rechazado"
PENDING = "Pendiente"


class CertifiedRequestDAO:
"""Class for accessing certified requests table."""

def __init__(self, session: AsyncSession = Depends(get_db_session)):
self.session = session

async def create_certified_request(
self,
user_id: str,
dni: Optional[str],
img1_url: Optional[str],
img2_url: Optional[str],
) -> None:
"""Add single certified request to session."""
query = CertifiedRequestModel(
user_id=user_id,
dni=dni,
img1_url=img1_url,
img2_url=img2_url,
status=StatusRequest.PENDING.value,
)
self.session.add(query)

async def delete_certified_request(
self,
certified_request_id: uuid.UUID,
) -> None:
"""Delete single certified request from session."""
query = delete(CertifiedRequestModel).where(
CertifiedRequestModel.id == certified_request_id,
)
await self.session.execute(query)

async def get_all_certified_requests(
self,
limit: int,
offset: int,
) -> List[CertifiedRequestModel]:
"""Get all certified request."""
query = select(CertifiedRequestModel).limit(limit).offset(offset)
result = await self.session.execute(query)
return list(result.scalars().fetchall())

async def update_status(
self,
certified_request_id: str,
status: str,
) -> None:
"""Update status certified request."""
query = (
update(CertifiedRequestModel)
.where(
CertifiedRequestModel.id == certified_request_id,
)
.values(
status=status,
)
)
await self.session.execute(query)

async def get_certified_request(
self,
certified_request_id: str,
) -> Optional[CertifiedRequestModel]:
"""Get single certified request."""
query = select(CertifiedRequestModel).where(
CertifiedRequestModel.id == certified_request_id,
)
result = await self.session.execute(query)
return result.scalars().first()
16 changes: 16 additions & 0 deletions identity_socializer/db/dao/user_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,19 @@ async def count_new_users_between_dates(
rows = await self.session.execute(query)

return rows.scalar() or 0

async def update_certified(
self,
user_id: str,
certified: bool,
) -> None:
"""Update certified."""
stmt = (
update(UserModel)
.where(UserModel.id == user_id)
.values(
certified=certified,
)
)

await self.session.execute(stmt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Added certified in users table
Revision ID: 18ff9184e762
Revises: ed0c0284f370
Create Date: 2023-12-03 00:49:10.111619
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "18ff9184e762"
down_revision = "ed0c0284f370"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column("users", sa.Column("certified", sa.Boolean(), default=False))
op.execute("UPDATE users SET certified = FALSE")


def downgrade() -> None:
op.drop_column("users", "certified")
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Added certified_requests table
Revision ID: e4141c4c1350
Revises: 18ff9184e762
Create Date: 2023-12-03 00:59:23.183303
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "e4141c4c1350"
down_revision = "18ff9184e762"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.create_table(
"certified_requests",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("user_id", sa.String(), nullable=False),
sa.Column("dni", sa.String(), nullable=True),
sa.Column("img1_url", sa.String(), nullable=True),
sa.Column("img2_url", sa.String(), nullable=True),
sa.Column("status", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)


def downgrade() -> None:
op.drop_table("certified_requests")
29 changes: 29 additions & 0 deletions identity_socializer/db/models/certified_request_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import datetime
import uuid

from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.sql.sqltypes import DateTime, String, Uuid

from identity_socializer.db.base import Base


class CertifiedRequestModel(Base):
"""Model for CertifiedRequest."""

__tablename__ = "certified_requests"

id: Mapped[uuid.UUID] = mapped_column(
Uuid,
default=uuid.uuid4,
primary_key=True,
)
user_id: Mapped[str] = mapped_column(String())
dni: Mapped[str] = mapped_column(String())
img1_url: Mapped[str] = mapped_column(String())
img2_url: Mapped[str] = mapped_column(String())
status: Mapped[str] = mapped_column(String())

created_at: Mapped[DateTime] = mapped_column(
DateTime,
default=datetime.datetime.utcnow,
)
1 change: 1 addition & 0 deletions identity_socializer/db/models/user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class UserModel(Base):
ubication: Mapped[str] = mapped_column(String(length), nullable=True)
profile_photo_id: Mapped[str] = mapped_column(String(length), nullable=True)
blocked: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
certified: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
created_at: Mapped[DateTime] = mapped_column(
DateTime,
default=datetime.datetime.utcnow,
Expand Down
1 change: 1 addition & 0 deletions identity_socializer/web/api/auth/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ class AppUserModel(BaseModel):
ubication: Optional[str]
is_followed: Optional[bool]
blocked: bool = False
certified: bool = False
model_config = ConfigDict(from_attributes=True)
4 changes: 4 additions & 0 deletions identity_socializer/web/api/certified_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Logger API."""
from identity_socializer.web.api.certified_request.views import router

__all__ = ["router"]
14 changes: 14 additions & 0 deletions identity_socializer/web/api/certified_request/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Optional

from pydantic import BaseModel, ConfigDict


class CertifiedRequestDTO(BaseModel):
"""DTO for certified request entry."""

user_id: str
dni: Optional[str]
img1_url: Optional[str]
img2_url: Optional[str]

model_config = ConfigDict(from_attributes=True)
100 changes: 100 additions & 0 deletions identity_socializer/web/api/certified_request/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from typing import List

from fastapi import APIRouter, Depends

from identity_socializer.db.dao.certified_request_dao import (
CertifiedRequestDAO,
StatusRequest,
)
from identity_socializer.db.dao.user_dao import UserDAO
from identity_socializer.db.models.certified_request_model import CertifiedRequestModel
from identity_socializer.web.api.certified_request.schema import CertifiedRequestDTO

router = APIRouter()


@router.post("/register", response_model=None)
async def create_certified_request(
body: CertifiedRequestDTO,
dao: CertifiedRequestDAO = Depends(),
dao_user: UserDAO = Depends(),
) -> None:
# Create certified request
"""Register certified request from user."""
await dao.create_certified_request(
user_id=body.user_id,
dni=body.dni,
img1_url=body.img1_url,
img2_url=body.img2_url,
)

# Update user certified identity
await dao_user.update_certified(user_id=body.user_id, certified=False)


@router.get("/get_all_certified_requests", response_model=None)
async def get_all_certified_requests(
limit: int = 10,
offset: int = 0,
dao: CertifiedRequestDAO = Depends(),
) -> List[CertifiedRequestModel]:
"""Get all certified requests."""
return await dao.get_all_certified_requests(limit=limit, offset=offset)


@router.delete("/delete/{certified_request_id}", response_model=None)
async def delete_certified_request(
certified_request_id: str,
dao: CertifiedRequestDAO = Depends(),
dao_user: UserDAO = Depends(),
) -> None:
"""Delete certified request."""
# Check if certified request exists
certified_request = await dao.get_certified_request(certified_request_id)

if not certified_request:
return

# Delete certified request
await dao.delete_certified_request(certified_request.id)

# Update user certified identity
await dao_user.update_certified(user_id=certified_request.user_id, certified=False)


@router.put("/approve/{certified_request_id}", response_model=None)
async def approve_certified_request(
certified_request_id: str,
dao: CertifiedRequestDAO = Depends(),
dao_user: UserDAO = Depends(),
) -> None:
"""Approve certified request."""
# Check if certified request exists
certified_request = await dao.get_certified_request(certified_request_id)

if not certified_request:
return

# Update certified request status
new_status = StatusRequest.APPROVED.value
await dao.update_status(certified_request_id, new_status)

# Update user certified identity
await dao_user.update_certified(user_id=certified_request.user_id, certified=True)


@router.put("/reject/{certified_request_id}", response_model=None)
async def reject_certified_request(
certified_request_id: str,
dao: CertifiedRequestDAO = Depends(),
) -> None:
"""Reject certified request."""
# Check if certified request exists
certified_request = await dao.get_certified_request(certified_request_id)

if not certified_request:
return

# Update certified request status
new_status = StatusRequest.REJECTED.value
await dao.update_status(certified_request_id, new_status)
6 changes: 6 additions & 0 deletions identity_socializer/web/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from identity_socializer.web.api import (
auth,
certified_request,
chat,
docs,
filter,
Expand Down Expand Up @@ -32,3 +33,8 @@
tags=["notification"],
)
api_router.include_router(pushtoken.router, prefix="/pushtoken", tags=["pushtoken"])
api_router.include_router(
certified_request.router,
prefix="/certified_request",
tags=["certified_request"],
)

4 comments on commit 3573ec3

@LuisCusihuaman
Copy link
Contributor

Choose a reason for hiding this comment

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

🤨🤨🤨🤨

@LuisCusihuaman
Copy link
Contributor

Choose a reason for hiding this comment

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

image

@noxethiems
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@noxethiems
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mucha carita y poco movimiento en snap 👀

Please sign in to comment.