Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: profile upsert #652

Merged
merged 1 commit into from
Jan 6, 2025
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
8 changes: 4 additions & 4 deletions server/auth/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from abc import abstractmethod
from fastapi import Request
from core.dao.profilesDAO import ProfilesDAO
from utils.random_str import random_str
from petercat_utils import get_client


class BaseAuthClient:
Expand All @@ -29,8 +29,8 @@
async def anonymouseLogin(self, request: Request) -> dict:
clientId = request.query_params.get("clientId") or random_str()
token, data = self.generateAnonymousUser(clientId = clientId)
supabase = get_client()
supabase.table("profiles").upsert(data).execute()
profiles_dao = ProfilesDAO()
profiles_dao.upsert_user(data)

Check warning on line 33 in server/auth/clients/base.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/base.py#L32-L33

Added lines #L32 - L33 were not covered by tests
xingwanying marked this conversation as resolved.
Show resolved Hide resolved
request.session["user"] = data
return data

Expand All @@ -49,7 +49,7 @@
@abstractmethod
async def get_user_info(self, request: Request) -> dict:
pass

@abstractmethod
async def get_access_token(self, user_id: str, provider="github") -> str:
pass
13 changes: 7 additions & 6 deletions server/auth/clients/local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import secrets
from fastapi import Request
from fastapi.responses import RedirectResponse
from petercat_utils import get_client, get_env_variable
from core.dao.profilesDAO import ProfilesDAO
from petercat_utils import get_env_variable
from auth.clients.base import BaseAuthClient

PETERCAT_LOCAL_UID = get_env_variable("PETERCAT_LOCAL_UID")
Expand All @@ -13,20 +14,20 @@
class LocalClient(BaseAuthClient):
def __init__(self):
pass

async def login(self, request: Request):
data = await self.get_user_info()
supabase = get_client()
supabase.table("profiles").upsert(data).execute()
profiles_dao = ProfilesDAO()
profiles_dao.upsert_user(data)

Check warning on line 21 in server/auth/clients/local.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/local.py#L20-L21

Added lines #L20 - L21 were not covered by tests

Choose a reason for hiding this comment

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

Ensure that the ProfilesDAO instance is properly initialized and that the upsert_user method handles all necessary exceptions that might occur during the database operation. This is crucial to prevent any runtime errors that could disrupt the user login process.

request.session["user"] = data

Check warning on line 23 in server/auth/clients/local.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/local.py#L23

Added line #L23 was not covered by tests
return RedirectResponse(url=f"{WEB_LOGIN_SUCCESS_URL}", status_code=302)

async def logout(self, request: Request, redirect: str):
if redirect:
return RedirectResponse(url=f"{redirect}", status_code=302)
return {"success": True}

async def get_user_info(self, user_id):
token = PETERCAT_LOCAL_UID
username = PETERCAT_LOCAL_UNAME
Expand Down
12 changes: 10 additions & 2 deletions server/core/dao/profilesDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
.execute()
)
return resp.data[0] if resp.data else None

def get_agreement_status(self, user_id: str):

def upsert_user(self, user_data: dict):

resp = (

Check warning on line 25 in server/core/dao/profilesDAO.py

View check run for this annotation

Codecov / codecov/patch

server/core/dao/profilesDAO.py#L25

Added line #L25 was not covered by tests
self.client.table("profiles")
.upsert(user_data)
.execute()
)
return resp.data

def get_agreement_status(self, user_id: str):

Check warning on line 32 in server/core/dao/profilesDAO.py

View check run for this annotation

Codecov / codecov/patch

server/core/dao/profilesDAO.py#L31-L32

Added lines #L31 - L32 were not covered by tests
resp = (
self.client.table("profiles")
.select("agreement_accepted")
Expand Down
Loading