-
Notifications
You must be signed in to change notification settings - Fork 90
add referral handler #698
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
Merged
Merged
add referral handler #698
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect, useState } from 'react'; | ||
| import { useSearchParams } from 'next/navigation'; | ||
| import { api } from '@/trpc/client'; | ||
|
|
||
| interface Props { | ||
| appId: string; | ||
| } | ||
|
|
||
| export const ReferralHandler: React.FC<Props> = ({ appId }) => { | ||
| const searchParams = useSearchParams(); | ||
| const referralCode = searchParams.get('referral_code'); | ||
| const [processed, setProcessed] = useState(false); | ||
|
|
||
| const utils = api.useUtils(); | ||
| const { mutateAsync: createMembership } = | ||
| api.apps.app.memberships.create.useMutation(); | ||
| const { mutateAsync: updateReferrer } = | ||
| api.apps.app.memberships.update.referrer.useMutation(); | ||
|
|
||
| useEffect(() => { | ||
| if (!referralCode || processed) return; | ||
|
|
||
| const processReferralCode = async () => { | ||
| const referralCodeData = await utils.apps.app.referralCode.get.byCode | ||
| .fetch(referralCode) | ||
| .catch(() => null); | ||
|
|
||
| if (!referralCodeData) { | ||
| setProcessed(true); | ||
| return; | ||
| } | ||
|
|
||
| const membership = await utils.apps.app.memberships.get | ||
| .fetch({ appId }) | ||
| .catch(() => null); | ||
|
|
||
| // Only update if user has no referrer yet | ||
| if (membership?.referrerId === null) { | ||
| await updateReferrer({ | ||
| appId, | ||
| referrerId: referralCodeData.id, | ||
| }).catch(() => { | ||
| // Silently fail - user might already have a referrer from a race condition | ||
| }); | ||
| } | ||
|
|
||
| // Create membership if it doesn't exist | ||
| if (!membership) { | ||
| await createMembership({ | ||
| appId, | ||
| referrerId: referralCodeData.id, | ||
| }).catch(() => { | ||
| // Silently fail - membership might have been created in the meantime | ||
| }); | ||
| } | ||
|
|
||
| setProcessed(true); | ||
| }; | ||
|
|
||
| void processReferralCode(); | ||
| }, [referralCode, appId, createMembership, updateReferrer, processed, utils]); | ||
|
|
||
| return null; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component conflates API fetch errors with "membership doesn't exist", potentially causing duplicate membership creation attempts if the membership fetch fails.
View Details
📝 Patch Details
Analysis
ReferralHandler conflates API fetch errors with membership non-existence, risking duplicate membership creation
What fails: In
ReferralHandler.processReferralCode(), whenmemberships.get.fetch()fails with a network or server error (lines 35-37), it's silently caught and converted tonullvia.catch(() => null). This creates ambiguity: the code cannot distinguish between "membership doesn't exist" (legitimate case where create is safe) and "membership fetch failed" (error case where state is unknown). Result: if the fetch fails when a membership already exists, the code proceeds to attempt creating a duplicate membership at line 50.How to reproduce:
memberships.get.fetch({ appId })call.catch(() => null)suppresses the error and setsmembershiptonullif (!membership)evaluates to truecreateMembership()even though the membership likely existsResult: Multiple membership creation attempts triggered by transient API errors, leading to:
Expected behavior: Errors during membership fetch should be handled distinctly from the legitimate "no membership exists" case. If we cannot determine membership state due to a fetch error, we should not attempt creation.
Fix implemented: Replaced
.catch(() => null)pattern with explicit try-catch blocks that:This ensures duplicate creation attempts only occur due to documented race conditions (lines 54, 65 comments), not API failures.