Skip to content

Commit

Permalink
Merge pull request #197 from DocShow-AI/dev
Browse files Browse the repository at this point in the history
potential fixes
  • Loading branch information
liberty-rising authored Jan 14, 2024
2 parents 65e5421 + 1792d93 commit 77b9629
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 11 additions & 7 deletions backend/routes/user_routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException

import asyncio

from models.user import (
ChangePasswordRequest,
ForgotPasswordRequest,
Expand Down Expand Up @@ -31,6 +33,12 @@
user_router = APIRouter()


def run_in_new_loop(coro):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro)


@user_router.get("/users/")
async def get_users(current_admin_user: User = Depends(get_current_admin_user)):
# Open a database session and fetch all users
Expand Down Expand Up @@ -162,26 +170,22 @@ async def reset_password(request: ResetPasswordRequest):
async def send_verification_email(
request: SendVerificationEmailRequest, background_tasks: BackgroundTasks
):
print("Getting user by email")
with DatabaseManager() as session:
user_manager = UserManager(session)
user = user_manager.get_user_by_email(request.email)
if not user:
raise HTTPException(status_code=404, detail="User not found")

print("Generating verification token")
# Generate a verification token and save it in the database
token = generate_email_verification_token(user.email)
user_manager.update_user_verification_token(
username=user.username, verification_token=token
)

print("Sending verification email")
# Add a background task to send the email
# background_tasks.add_task(
# send_verification_email_with_sendgrid, user.email, token
# )
await send_verification_email_with_sendgrid([user.email], token)
background_tasks.add_task(
run_in_new_loop, send_verification_email_with_sendgrid([user.email], token)
)

return {"message": f"Verification email sent for {user.email}"}

Expand Down
2 changes: 0 additions & 2 deletions backend/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ async def send_verification_email_with_sendgrid(email: List[str], token: str):
message.tracking_settings = TrackingSettings()
message.tracking_settings.click_tracking = ClickTracking(False, False)
try:
print("Sending email")
sg = SendGridAPIClient(SENDGRID_API_KEY)
print("Sending email")
response = await sg.send(message)
print(response.status_code)
print(response.body)
Expand Down

0 comments on commit 77b9629

Please sign in to comment.