-
Notifications
You must be signed in to change notification settings - Fork 112
feat: implement A/B testing for link placements (#687) #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
Open
Dev1822
wants to merge
7
commits into
vishnukothakapu:main
Choose a base branch
from
Dev1822:feature-ab-testing-687
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ec0ffd
feat: implement A/B testing for link placements (#687)
Dev1822 c6f8dec
Merge branch 'main' into feature-ab-testing-687
Dev1822 d5774af
fix: remove duplicate WorkspaceRole enum definition
Dev1822 1e813a1
fix: remove duplicate link variable declaration in delete route
Dev1822 191958e
fix: address PR review comments for A/B testing and workspace cache
Dev1822 831cc4e
fix: add lastTotpStep to User model in schema.prisma
Dev1822 a59c150
Merge branch 'main' into feature-ab-testing-687
Dev1822 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
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
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
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,138 @@ | ||
| import { NextResponse, NextRequest } from "next/server"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { authOptions } from "@/lib/auth"; | ||
| import prisma from "@/lib/prisma"; | ||
|
|
||
| class ApiError extends Error { | ||
| status: number; | ||
| constructor(message: string, status: number) { | ||
| super(message); | ||
| this.status = status; | ||
| } | ||
| } | ||
|
|
||
| export async function POST(req: NextRequest) { | ||
| const session = await getServerSession(authOptions); | ||
| if (!session?.user?.email) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| const { linkId } = await req.json(); | ||
|
|
||
| if (!linkId) { | ||
| return NextResponse.json({ error: "Missing linkId" }, { status: 400 }); | ||
| } | ||
|
|
||
| const user = await prisma.user.findUnique({ | ||
| where: { email: session.user.email }, | ||
| }); | ||
|
|
||
| if (!user) { | ||
| return NextResponse.json({ error: "User not found" }, { status: 404 }); | ||
| } | ||
|
vishnukothakapu marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| const result = await prisma.$transaction(async (tx) => { | ||
| // Atomically claim the link by setting abTestVariant = "A" and abTestParentId = id | ||
| // only if abTestParentId is currently null. | ||
| const updateCount = await tx.link.updateMany({ | ||
| where: { | ||
| id: linkId, | ||
| userId: user.id, | ||
| abTestParentId: null, | ||
| }, | ||
| data: { | ||
| abTestVariant: "A", | ||
| abTestParentId: linkId, | ||
| }, | ||
| }); | ||
|
|
||
| if (updateCount.count !== 1) { | ||
| const existing = await tx.link.findUnique({ | ||
| where: { id: linkId }, | ||
| }); | ||
| if (!existing) { | ||
| throw new ApiError("Link not found", 404); | ||
| } | ||
| if (existing.userId !== user.id) { | ||
| throw new ApiError("Unauthorized", 403); | ||
| } | ||
| if (existing.abTestParentId) { | ||
| throw new ApiError("Link is already part of an A/B test", 409); | ||
| } | ||
| throw new ApiError("Failed to initialize A/B test", 400); | ||
| } | ||
|
|
||
| const originalLink = await tx.link.findUnique({ | ||
| where: { id: linkId }, | ||
| }); | ||
|
|
||
| if (!originalLink) { | ||
| throw new ApiError("Link not found", 404); | ||
| } | ||
|
|
||
| // Shift subsequent links' positions to avoid position duplication | ||
| await tx.link.updateMany({ | ||
| where: { | ||
| userId: user.id, | ||
| position: { gte: originalLink.position + 1 }, | ||
| }, | ||
| data: { | ||
| position: { increment: 1 }, | ||
| }, | ||
| }); | ||
|
|
||
| // Create variant B directly after original link | ||
| const newLink = await tx.link.create({ | ||
| data: { | ||
| workspaceId: originalLink.workspaceId, | ||
| userId: user.id, | ||
| platform: `${originalLink.platform}__ab_b`, | ||
| alias: originalLink.alias ? `${originalLink.alias}-b` : null, | ||
| label: `${originalLink.label} (Variant B)`, | ||
| url: originalLink.url, | ||
| position: originalLink.position + 1, | ||
| isPublic: originalLink.isPublic, | ||
| isGroup: originalLink.isGroup, | ||
| parentId: originalLink.parentId, | ||
| pinCode: originalLink.pinCode, | ||
| isSocialIcon: originalLink.isSocialIcon, | ||
| startDate: originalLink.startDate, | ||
| endDate: originalLink.endDate, | ||
| abTestVariant: "B", | ||
| abTestParentId: originalLink.id, | ||
| }, | ||
| }); | ||
|
|
||
| return { variantA: originalLink, variantB: newLink }; | ||
| }); | ||
|
|
||
| return NextResponse.json(result); | ||
| } catch (err: unknown) { | ||
| if (err instanceof ApiError) { | ||
| return NextResponse.json( | ||
| { error: err.message }, | ||
| { status: err.status } | ||
| ); | ||
| } | ||
|
|
||
| if (err instanceof Error) { | ||
| if (err.message === "Link not found") { | ||
| return NextResponse.json({ error: err.message }, { status: 404 }); | ||
| } | ||
| if (err.message === "Unauthorized") { | ||
| return NextResponse.json({ error: err.message }, { status: 403 }); | ||
| } | ||
| if (err.message.includes("already part of")) { | ||
| return NextResponse.json({ error: err.message }, { status: 409 }); | ||
| } | ||
| } | ||
|
|
||
| console.error("A/B test creation error:", err); | ||
| return NextResponse.json( | ||
| { error: "Failed to create A/B test" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
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.