Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api_certify/repositories/certificate_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ async def get_many_certificates(self, user_id: str) -> list[CertificateInDb]:

return [CertificateInDb(**doc) for doc in docs]

async def find_by_access_key(self, access_key: str) -> dict | None:
doc = await self.certificate_collection.find_one({
"access_key": access_key,
"status": {"$in": ["available", "emitted"]},
})
return doc

async def get_certificate(self, certificate_id: str) -> CertificateInDb:
existingCertificate = await self.certificate_collection.find_one(
{"user_id": certificate_id}
Expand Down
18 changes: 17 additions & 1 deletion api_certify/routes/v1/certificate_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import APIRouter, Depends, HTTPException

from api_certify.schemas.responses import SucessResponse
from api_certify.schemas.responses import SucessResponse, CertificateValidationResponse
from api_certify.models.certificate_model import (
CreateCertificate,
)
Expand All @@ -10,6 +10,22 @@
certificate_routes = APIRouter(prefix="/certificate", tags=["Certificates"])


@certificate_routes.get("/validate/{access_key}", response_model=SucessResponse, status_code=200)
async def validate_certificate(
access_key: str,
service: CertificateService = Depends(get_certificate_service),
):
try:
result = await service.validate_certificate(access_key)
return SucessResponse(
success=True,
message="Certificado válido.",
data=result.model_dump(),
)
except Exception as err:
raise HTTPException(status_code=404, detail=str(err))


@certificate_routes.post("/{user_id}", response_model=SucessResponse, status_code=201)
async def request_certificate(
user_id: str,
Expand Down
10 changes: 10 additions & 0 deletions api_certify/schemas/responses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydantic import BaseModel
from typing import Optional, Any
from datetime import datetime


class BaseResponse(BaseModel):
Expand All @@ -14,3 +15,12 @@ class SucessResponse(BaseResponse):

class ErrorResponse(BaseResponse):
error_code: Optional[str] = None


class CertificateValidationResponse(BaseModel):
participant_name: str
event_name: str
workload: str
issued_at: Optional[datetime] = None
event_start: Optional[datetime] = None
event_end: Optional[datetime] = None
16 changes: 15 additions & 1 deletion api_certify/service/certificate_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
CertificateInDb,
CreateCertificate,
)
from api_certify.schemas.responses import CertificateValidationResponse
from api_certify.repositories.auth_repository import AuthRepository

class CertificateService:
Expand Down Expand Up @@ -38,4 +39,17 @@ async def get_many_certificates(self, user_id: str) -> list[CertificateInDb]:
async def get_certificate_by_id(self, certificate_id: str) -> CertificateInDb:
response = await self.certificate_repository.get_certificate(certificate_id)
print(certificate_id)
return response
return response

async def validate_certificate(self, access_key: str) -> CertificateValidationResponse:
doc = await self.certificate_repository.find_by_access_key(access_key)
if not doc:
raise Exception("Certificado não encontrado ou código inválido.")
return CertificateValidationResponse(
participant_name=doc["participant_name"],
event_name=doc["event_name"],
workload=doc["workload"],
issued_at=doc.get("issued_at"),
event_start=doc.get("event_start"),
event_end=doc.get("event_end"),
)
67 changes: 13 additions & 54 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ motor-types = "^1.0.0b4"

pydantic = { extras = ["email"], version = "^2.12.2" }

python-jose = { extras = ["cryptography"], version = "^3.5.0" }
python-jose = {extras = ["cryptography"], version = "^3.5.0"}
passlib = { extras = ["bcrypt"], version = "^1.7.4" }
bcrypt = "4.0.1"

Expand Down