Skip to content

Commit

Permalink
refactore: get the agreement statues from db instead of session (#552)
Browse files Browse the repository at this point in the history
* style: fix the loading

* chore: update the signatureIcon

* chore: fix type

* chore: release  @petercatai/assistant@1.0.18

* refactore: get the agreement statues from db instead of session

* chore: remove log

* chore: fix ci

* chore: fix test
  • Loading branch information
xingwanying authored Dec 3, 2024
1 parent 5f33065 commit 11e80d1
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 33 deletions.
25 changes: 18 additions & 7 deletions client/app/factory/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
useBotCreate,
useBotEdit,
} from '@/app/hooks/useBot';
import { useAgreement } from '@/app/hooks/useAgreement';
import { useAgreement, useAgreementStatus } from '@/app/hooks/useAgreement';
import FullPageSkeleton from '@/components/FullPageSkeleton';
import { isEmpty } from 'lodash';
import { Chat } from '@petercatai/assistant';
Expand Down Expand Up @@ -64,6 +64,11 @@ export default function Edit() {
const { language } = useGlobal();
const { botProfile, setBotProfile } = useBot();
const { user, status } = useUser();
const {
data: agreementStatus,
isLoading: getAgreementStatusLoading,
error: getAgreementStatusError,
} = useAgreementStatus();
const router = useRouter();
const searchParams = useSearchParams();
const id = searchParams.get('id');
Expand Down Expand Up @@ -103,15 +108,21 @@ export default function Edit() {
}
if (!user || user.id.startsWith('client|')) {
router.push(`${apiDomain}/api/auth/login`);
} else {
if (!user?.agreement_accepted) {
setAgreementModalIsOpen(true);
} else {
setAgreementModalIsOpen(false);
}
}
}, [user, status]);

useEffect(() => {
if (getAgreementStatusLoading) {
return;
}
if (getAgreementStatusError) {
setAgreementModalIsOpen(true);
} else {
const agreementAccepted = agreementStatus?.data?.agreement_accepted;
setAgreementModalIsOpen(!agreementAccepted);
}
}, [agreementStatus, getAgreementStatusError, getAgreementStatusLoading]);

const {
updateBot: onUpdateBot,
isLoading: updateBotLoading,
Expand Down
15 changes: 13 additions & 2 deletions client/app/hooks/useAgreement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useMutation } from '@tanstack/react-query';
import { acceptAgreement } from '@/app/services/UserController';
import { useMutation, useQuery } from '@tanstack/react-query';
import {
acceptAgreement,
getAgreementStatus,
} from '@/app/services/UserController';
export function useAgreement() {
const mutation = useMutation({
mutationFn: acceptAgreement,
Expand All @@ -13,3 +16,11 @@ export function useAgreement() {
isSuccess: mutation.isSuccess,
};
}

export const useAgreementStatus = () => {
return useQuery({
queryKey: [`agreement`],
queryFn: async () => getAgreementStatus(),
retry: false,
});
};
7 changes: 7 additions & 0 deletions client/app/services/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export async function acceptAgreement() {
return response.data;
}

export async function getAgreementStatus() {
const response = await axios.get(`${apiDomain}/api/auth/agreement/status`, {
withCredentials: true,
});
return response.data;
}

export async function getAvailableLLMs() {
const response = await axios.get(`${apiDomain}/api/user/llms`, {
withCredentials: true,
Expand Down
47 changes: 24 additions & 23 deletions server/auth/router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from core.dao.profilesDAO import ProfilesDAO
from fastapi import APIRouter, Request, HTTPException, status, Depends
from fastapi.responses import RedirectResponse, JSONResponse
import secrets
from petercat_utils import get_client, get_env_variable
from starlette.config import Config
from authlib.integrations.starlette_client import OAuth
from typing import Annotated
from typing import Annotated, Optional

from auth.get_user_info import generateAnonymousUser, getUserInfoByToken, get_user_id

Expand Down Expand Up @@ -98,33 +99,33 @@ async def userinfo(request: Request):
return { "data": data, "status": 200}
return { "data": user, "status": 200}

@router.get("/agreement/status")
async def get_agreement_status(user_id: Optional[str] = Depends(get_user_id)):
if not user_id:
raise HTTPException(status_code=401, detail="User not found")
try:
profiles_dao = ProfilesDAO()
response = profiles_dao.get_agreement_status(user_id=user_id)
if not response:
raise HTTPException(status_code=404, detail="User does not exist, accept failed.")
return {"success": True, "data": response}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")

@router.post("/accept/agreement", status_code=200)
async def bot_generator(
request: Request,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
if not user_id:
return JSONResponse(
content={
"success": False,
"errorMessage": "User not found",
},
status_code=401,
)
raise HTTPException(status_code=401, detail="User not found")
try:
supabase = get_client()
response = supabase.table("profiles").update({"agreement_accepted": True}).match({"id": user_id}).execute()

if not response.data:
return JSONResponse(
content={
"success": False,
"errorMessage": "User does not exist, accept failed.",
}
)
request.session['user'] = response.data[0]
return JSONResponse(content={"success": True})
profiles_dao = ProfilesDAO()
response = profiles_dao.accept_agreement(user_id=user_id)
if response:
request.session['user'] = response
return JSONResponse(content={"success": True})
else:
raise HTTPException(status_code=400, detail="User update failed")
except Exception as e:
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
33 changes: 33 additions & 0 deletions server/core/dao/profilesDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from core.dao.BaseDAO import BaseDAO
from supabase.client import Client

from core.models.profiles import Profiles
from petercat_utils.db.client.supabase import get_client

class ProfilesDAO(BaseDAO):
client: Client

def __init__(self):
super().__init__()
self.client = get_client()

def get_agreement_status(self, user_id: str):

resp = self.client.table("profiles")\
.select("agreement_accepted") \
.eq("id", user_id) \
.execute()
agreement_accepted = resp.data[0]
return agreement_accepted

def accept_agreement(self, user_id: str):
try:
response = self.client.table("profiles").update({"agreement_accepted": True}).match({"id": user_id}).execute()

if not response.data:
return False, {"message": "User does not exist, accept failed."}
return response.data[0]
except Exception as e:
print("Error: ", e)
return False, {"message": "Profile Update failed"}

13 changes: 13 additions & 0 deletions server/core/models/profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pydantic import BaseModel
from datetime import datetime
from typing import Optional

class Profiles(BaseModel):
id: str
created_at: datetime = datetime.now()
nickname: Optional[str] = ""
name: Optional[str] = ""
picture: Optional[str] = ""
sid: Optional[str] = ""
sub: Optional[str] = ""
agreement_accepted: Optional[bool] = False
1 change: 0 additions & 1 deletion server/github_app/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
get_installation_repositories,
get_jwt,
get_private_key,
get_user_orgs,
)

from petercat_utils import get_env_variable
Expand Down

0 comments on commit 11e80d1

Please sign in to comment.