-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Duplicate partners links #4153
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
marcusljf
wants to merge
6
commits into
main
Choose a base branch
from
duplicate-links
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
Duplicate partners links #4153
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a40686
Duplicate partners links
marcusljf 52b7c67
Merge branch 'main' into duplicate-links
pepeladeira 62671bc
code improvements
pepeladeira be0bb56
code improvements
pepeladeira 55f54f4
Merge branch 'main' into duplicate-links
pepeladeira 234121c
code improvements
pepeladeira 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,4 @@ packages/stripe-app/.build/* | |
| playwright-report/ | ||
| **/playwright/.auth/ | ||
| test-results/ | ||
| blob-report/ | ||
| blob-report/ | ||
107 changes: 3 additions & 104 deletions
107
apps/web/app/(ee)/api/admin/partners/[partnerId]/shared-platforms/route.ts
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
41 changes: 41 additions & 0 deletions
41
apps/web/app/(ee)/api/partners/[partnerId]/shared-platforms/route.ts
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,41 @@ | ||
| import { DubApiError } from "@/lib/api/errors"; | ||
| import { getSharedPartnerPlatforms } from "@/lib/api/partners/get-shared-partner-platforms"; | ||
| import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw"; | ||
| import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw"; | ||
| import { withWorkspace } from "@/lib/auth"; | ||
| import { partnerProgramSharedPlatformSchema } from "@/lib/zod/schemas/partners"; | ||
| import { NextResponse } from "next/server"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| // GET /api/partners/:partnerId/shared-platforms – other partners in this program with the same verified platform identifiers | ||
| export const GET = withWorkspace( | ||
| async ({ workspace, params }) => { | ||
| const { partnerId } = params; | ||
| const programId = getDefaultProgramIdOrThrow(workspace); | ||
|
|
||
| await getProgramEnrollmentOrThrow({ | ||
| partnerId, | ||
| programId, | ||
| include: {}, | ||
| }); | ||
|
|
||
| const sharedPlatforms = await getSharedPartnerPlatforms({ | ||
| partnerId, | ||
| programId, | ||
| }); | ||
|
|
||
| if (sharedPlatforms === null) { | ||
| throw new DubApiError({ | ||
| code: "not_found", | ||
| message: "Partner not found.", | ||
| }); | ||
| } | ||
|
|
||
| return NextResponse.json( | ||
| z.array(partnerProgramSharedPlatformSchema).parse(sharedPlatforms), | ||
| ); | ||
| }, | ||
| { | ||
| requiredPlan: ["business", "advanced", "enterprise"], | ||
| }, | ||
| ); |
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
170 changes: 170 additions & 0 deletions
170
apps/web/lib/api/partners/get-shared-partner-platforms.ts
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,170 @@ | ||
| import { prisma } from "@/lib/prisma"; | ||
| import { getDomainWithoutWWW } from "@dub/utils"; | ||
| import { | ||
| Partner, | ||
| PartnerPlatform, | ||
| Prisma, | ||
| ProgramEnrollmentStatus, | ||
| } from "@prisma/client"; | ||
|
|
||
| type SharedPlatformMatch = PartnerPlatform & { | ||
| partner: Pick<Partner, "id" | "name" | "email" | "image"> & { | ||
| // only selected when programId is provided | ||
| programs?: { status: ProgramEnrollmentStatus }[]; | ||
| }; | ||
| }; | ||
|
|
||
| // Finds other partners with the same verified platform identifiers. | ||
| // When programId is provided, only matches partners enrolled in that program | ||
| // and includes each matched partner's enrollment status. | ||
| export async function getSharedPartnerPlatforms({ | ||
| partnerId, | ||
| programId, | ||
| }: { | ||
| partnerId: string; | ||
| programId?: string; | ||
| }) { | ||
| const partner = await prisma.partner.findUnique({ | ||
| where: { id: partnerId }, | ||
| include: { | ||
| platforms: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!partner) { | ||
| return null; | ||
| } | ||
|
|
||
| const verifiedPlatforms = partner.platforms.filter( | ||
| (platform) => platform.verifiedAt !== null, | ||
| ); | ||
|
|
||
| // a partner has at most one platform per type, so there is at most one website | ||
| const websitePlatform = verifiedPlatforms.find( | ||
| (platform) => platform.type === "website", | ||
| ); | ||
|
|
||
| const websiteDomain = websitePlatform | ||
| ? getDomainWithoutWWW(websitePlatform.identifier) ?? null | ||
| : null; | ||
|
|
||
| const matchConditions: Prisma.PartnerPlatformWhereInput[] = []; | ||
|
|
||
| for (const platform of verifiedPlatforms) { | ||
| if (platform.type === "website") { | ||
| // website identifiers are full URLs with inconsistent normalization, | ||
| // so match on the domain instead of the exact identifier | ||
| if (websiteDomain) { | ||
| matchConditions.push({ | ||
| identifier: { | ||
| contains: websiteDomain, | ||
| }, | ||
| type: platform.type, | ||
| }); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| matchConditions.push({ | ||
| identifier: platform.identifier, | ||
| type: platform.type, | ||
| }); | ||
| } | ||
|
|
||
| if (matchConditions.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const sharedPlatformMatches = (await prisma.partnerPlatform.findMany({ | ||
| where: { | ||
| OR: matchConditions, | ||
| partnerId: { | ||
| not: partner.id, | ||
| }, | ||
| verifiedAt: { | ||
| not: null, | ||
| }, | ||
| ...(programId && { | ||
| partner: { | ||
| programs: { | ||
| some: { | ||
| programId, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| }, | ||
| include: { | ||
| partner: { | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| email: true, | ||
| image: true, | ||
| ...(programId && { | ||
| programs: { | ||
| where: { | ||
| programId, | ||
| }, | ||
| select: { | ||
| status: true, | ||
| }, | ||
| }, | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| orderBy: { | ||
| createdAt: "asc", | ||
| }, | ||
| take: 100, | ||
| })) as SharedPlatformMatch[]; | ||
|
|
||
| const sharedPlatforms = verifiedPlatforms | ||
| .map((platform) => { | ||
| let platformMatches = sharedPlatformMatches.filter( | ||
| (match) => match.type === platform.type, | ||
| ); | ||
|
|
||
| // "contains" matches on the domain need exact verification | ||
| // (e.g. contains "example.com" would also match "notexample.com") | ||
| if (platform.type === "website") { | ||
| platformMatches = platformMatches.filter( | ||
| (match) => | ||
| websiteDomain !== null && | ||
| getDomainWithoutWWW(match.identifier) === websiteDomain, | ||
| ); | ||
| } | ||
|
|
||
| const partners = platformMatches | ||
| .flatMap((match) => { | ||
| const status = programId | ||
| ? match.partner.programs?.[0]?.status | ||
| : undefined; | ||
|
|
||
| if (programId && !status) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| id: match.partner.id, | ||
| name: match.partner.name, | ||
| email: match.partner.email, | ||
| image: match.partner.image, | ||
| ...(status && { status }), | ||
| }, | ||
| ]; | ||
| }) | ||
| .slice(0, 10); | ||
|
|
||
| return { | ||
| type: platform.type, | ||
| identifier: platform.identifier, | ||
| partners, | ||
| }; | ||
| }) | ||
| .filter((platform) => platform.partners.length > 0); | ||
|
|
||
| return sharedPlatforms; | ||
| } | ||
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
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.