From 795814332379ffb23db8526395331bab2386546c Mon Sep 17 00:00:00 2001 From: Krzysztof Czerwinski Date: Fri, 3 Jan 2025 16:26:03 +0100 Subject: [PATCH] Cleanup --- .../backend/backend/data/credit.py | 28 ++++++++++++------- .../backend/backend/server/routers/v1.py | 4 ++- .../src/app/store/(user)/credits/page.tsx | 2 +- .../src/app/store/(user)/dashboard/page.tsx | 6 ++-- .../frontend/src/app/store/search/page.tsx | 2 +- .../frontend/src/hooks/useAgentGraph.ts | 2 +- .../frontend/src/hooks/useCredits.ts | 2 +- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py index 2bffd714d..7fecfa8ec 100644 --- a/autogpt_platform/backend/backend/data/credit.py +++ b/autogpt_platform/backend/backend/data/credit.py @@ -80,7 +80,9 @@ class UserCreditBase(ABC): pass @abstractmethod - async def fulfill_checkout(self, *, session_id: str | None = None, user_id: str | None = None): + async def fulfill_checkout( + self, *, session_id: str | None = None, user_id: str | None = None + ): """ Fulfill the Stripe checkout session. @@ -260,7 +262,7 @@ class UserCredit(UserCreditBase): # Create checkout session # https://docs.stripe.com/checkout/quickstart?client=react # unit_amount param is always in the smallest currency unit (so cents for usd) - # which equals to amount of credits + # which is equal to amount of credits checkout_session = stripe.checkout.Session.create( customer=user.stripeCustomerId, line_items=[ @@ -297,21 +299,25 @@ class UserCredit(UserCreditBase): return checkout_session.url or "" # https://docs.stripe.com/checkout/fulfillment - async def fulfill_checkout(self, *, session_id: str | None = None, user_id: str | None = None): + async def fulfill_checkout( + self, *, session_id: str | None = None, user_id: str | None = None + ): if (not session_id and not user_id) or (session_id and user_id): raise ValueError("Either session_id or user_id must be provided") - + # Retrieve CreditTransaction credit_transaction = await CreditTransaction.prisma().find_first_or_raise( where={ "OR": [ - {"transactionKey": session_id} if session_id is not None else {"transactionKey": ""}, - {"userId": user_id} if user_id is not None else {"userId": ""} + ( + {"transactionKey": session_id} + if session_id is not None + else {"transactionKey": ""} + ), + {"userId": user_id} if user_id is not None else {"userId": ""}, ] }, - order={ - "createdAt": "desc" - } + order={"createdAt": "desc"}, ) # This can be called multiple times for one id, so ignore if already fulfilled @@ -319,7 +325,9 @@ class UserCredit(UserCreditBase): return # Retrieve the Checkout Session from the API - checkout_session = stripe.checkout.Session.retrieve(credit_transaction.transactionKey) + checkout_session = stripe.checkout.Session.retrieve( + credit_transaction.transactionKey + ) # Check the Checkout Session's payment_status property # to determine if fulfillment should be peformed diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.py index c35dd70f0..ca7281ddd 100644 --- a/autogpt_platform/backend/backend/server/routers/v1.py +++ b/autogpt_platform/backend/backend/server/routers/v1.py @@ -179,7 +179,9 @@ async def stripe_webhook(request: Request): event["type"] == "checkout.session.completed" or event["type"] == "checkout.session.async_payment_succeeded" ): - await _user_credit_model.fulfill_checkout(session_id=event["data"]["object"]["id"]) + await _user_credit_model.fulfill_checkout( + session_id=event["data"]["object"]["id"] + ) return Response(status_code=200) diff --git a/autogpt_platform/frontend/src/app/store/(user)/credits/page.tsx b/autogpt_platform/frontend/src/app/store/(user)/credits/page.tsx index 510c9f51a..f4a8e34a0 100644 --- a/autogpt_platform/frontend/src/app/store/(user)/credits/page.tsx +++ b/autogpt_platform/frontend/src/app/store/(user)/credits/page.tsx @@ -18,7 +18,7 @@ export default function CreditsPage() { api.fulfillCheckout(); setPatched(true); } - }, [topupStatus]); + }, [api, patched, topupStatus]); return (
diff --git a/autogpt_platform/frontend/src/app/store/(user)/dashboard/page.tsx b/autogpt_platform/frontend/src/app/store/(user)/dashboard/page.tsx index 375375233..16abc15be 100644 --- a/autogpt_platform/frontend/src/app/store/(user)/dashboard/page.tsx +++ b/autogpt_platform/frontend/src/app/store/(user)/dashboard/page.tsx @@ -33,14 +33,14 @@ export default function Page({}: {}) { } catch (error) { console.error("Error fetching submissions:", error); } - }, [api, supabase]); + }, [api]); useEffect(() => { if (!supabase) { return; } fetchData(); - }, [supabase]); + }, [supabase, fetchData]); const onEditSubmission = useCallback((submission: StoreSubmissionRequest) => { setSubmissionData(submission); @@ -56,7 +56,7 @@ export default function Page({}: {}) { api.deleteStoreSubmission(submission_id); fetchData(); }, - [supabase], + [api, supabase, fetchData], ); const onOpenPopout = useCallback(() => { diff --git a/autogpt_platform/frontend/src/app/store/search/page.tsx b/autogpt_platform/frontend/src/app/store/search/page.tsx index 2bb57cd47..7e3806fcf 100644 --- a/autogpt_platform/frontend/src/app/store/search/page.tsx +++ b/autogpt_platform/frontend/src/app/store/search/page.tsx @@ -61,7 +61,7 @@ function SearchResults({ }; fetchData(); - }, [searchTerm, sort]); + }, [api, searchTerm, sort]); const agentsCount = agents.length; const creatorsCount = creators.length; diff --git a/autogpt_platform/frontend/src/hooks/useAgentGraph.ts b/autogpt_platform/frontend/src/hooks/useAgentGraph.ts index cd1fe4747..2aaa76e18 100644 --- a/autogpt_platform/frontend/src/hooks/useAgentGraph.ts +++ b/autogpt_platform/frontend/src/hooks/useAgentGraph.ts @@ -873,7 +873,7 @@ export default function useAgentGraph( request: "save", state: "saving", }); - }, [saveAgent]); + }, [saveAgent, saveRunRequest.state]); const requestSaveAndRun = useCallback(() => { saveAgent(); diff --git a/autogpt_platform/frontend/src/hooks/useCredits.ts b/autogpt_platform/frontend/src/hooks/useCredits.ts index a6bfbf880..de4ece0a2 100644 --- a/autogpt_platform/frontend/src/hooks/useCredits.ts +++ b/autogpt_platform/frontend/src/hooks/useCredits.ts @@ -19,7 +19,7 @@ export default function useCredits(): { const fetchCredits = useCallback(async () => { const response = await api.getUserCredit(); setCredits(response.credits); - }, []); + }, [api]); useEffect(() => { fetchCredits();