From 9995a4cb87b5c50d0741a0e7a3cc22ce46170d02 Mon Sep 17 00:00:00 2001 From: seeleng Date: Tue, 24 Sep 2024 21:46:18 +0800 Subject: [PATCH 1/2] fix: terminate early if adding duplicate event --- backend/src/events/process.py | 14 ++++++++++++++ backend/src/scripts/seed.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/src/events/process.py b/backend/src/events/process.py index 3c957b03..227b4743 100644 --- a/backend/src/events/process.py +++ b/backend/src/events/process.py @@ -37,6 +37,20 @@ def add_event_to_db(event: EventLLM) -> bool: categories = get_categories() with Session(engine) as session: + # Check for duplicates + eventORM = session.scalars( + select(Event).where( + Event.title == event.title, + Event.description == Event.description, + Event.duplicate == event.duplicate, + Event.is_singapore == event.is_singapore, + Event.original_article_id == event.original_article_id, + ) + ).first() + if eventORM: + print("duplicate detected:", event) + return False + try: article = session.get(Article, event.original_article_id) if not article: diff --git a/backend/src/scripts/seed.py b/backend/src/scripts/seed.py index ee76df83..387b6da2 100644 --- a/backend/src/scripts/seed.py +++ b/backend/src/scripts/seed.py @@ -97,4 +97,4 @@ def test_associations(): # session.delete(original_article) -# test_associations() +test_associations() From bb87d69dcb8844173a2f37dea0a02e3c115d22fd Mon Sep 17 00:00:00 2001 From: seeleng Date: Tue, 24 Sep 2024 22:03:19 +0800 Subject: [PATCH 2/2] fix: login right after register --- backend/src/auth/router.py | 7 +++++-- backend/src/events/process.py | 3 +-- frontend/app/(unauthenticated)/register/page.tsx | 3 +++ frontend/client/types.gen.ts | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/src/auth/router.py b/backend/src/auth/router.py index 457607fa..3a553fec 100644 --- a/backend/src/auth/router.py +++ b/backend/src/auth/router.py @@ -38,7 +38,9 @@ @router.post("/signup") -def sign_up(data: SignUpData, session=Depends(get_session)): +def sign_up( + data: SignUpData, response: Response, session=Depends(get_session) +) -> Token: existing_user = session.scalars( select(User).where(User.email == data.email) ).first() @@ -52,8 +54,9 @@ def sign_up(data: SignUpData, session=Depends(get_session)): ) session.add(new_user) session.commit() + session.refresh(new_user) - return + return create_token(new_user, response) @router.post("/login") diff --git a/backend/src/events/process.py b/backend/src/events/process.py index 227b4743..4962d426 100644 --- a/backend/src/events/process.py +++ b/backend/src/events/process.py @@ -41,8 +41,7 @@ def add_event_to_db(event: EventLLM) -> bool: eventORM = session.scalars( select(Event).where( Event.title == event.title, - Event.description == Event.description, - Event.duplicate == event.duplicate, + Event.description == event.description, Event.is_singapore == event.is_singapore, Event.original_article_id == event.original_article_id, ) diff --git a/frontend/app/(unauthenticated)/register/page.tsx b/frontend/app/(unauthenticated)/register/page.tsx index 4b026381..f596061f 100644 --- a/frontend/app/(unauthenticated)/register/page.tsx +++ b/frontend/app/(unauthenticated)/register/page.tsx @@ -16,6 +16,7 @@ import { Alert, AlertDescription } from "@/components/ui/alert"; import { Box } from "@/components/ui/box"; import { Button } from "@/components/ui/button"; import { Form } from "@/components/ui/form"; +import { useUserStore } from "@/store/user/user-store-provider"; const registerFormSchema = z.object({ email: z.string().email("Invalid email address"), @@ -31,6 +32,7 @@ type RegisterForm = z.infer; function RegisterPage() { const router = useRouter(); + const setLoggedIn = useUserStore((state) => state.setLoggedIn); const [isError, setIsError] = useState(false); const form = useForm({ resolver: zodResolver(registerFormSchema), @@ -47,6 +49,7 @@ function RegisterPage() { setIsError(true); } else { setIsError(false); + setLoggedIn(response.data.user); router.push("/login"); } }; diff --git a/frontend/client/types.gen.ts b/frontend/client/types.gen.ts index 32ef05b3..040e3131 100644 --- a/frontend/client/types.gen.ts +++ b/frontend/client/types.gen.ts @@ -166,7 +166,7 @@ export type SignUpAuthSignupPostData = { body: SignUpData; }; -export type SignUpAuthSignupPostResponse = (unknown); +export type SignUpAuthSignupPostResponse = (Token); export type SignUpAuthSignupPostError = (HTTPValidationError);