-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac12930
commit 3573ec3
Showing
11 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
identity_socializer/db/migrations/versions/2023-12-03-00-49_18ff9184e762.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
33 changes: 33 additions & 0 deletions
33
identity_socializer/db/migrations/versions/2023-12-03-00-59_e4141c4c1350.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3573ec3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤨🤨🤨🤨
3573ec3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3573ec3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LuisCusihuaman ¿?
3573ec3
There was a problem hiding this comment.
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 👀