-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Support multiple handles per platform type for partners #4109
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
devkiran
wants to merge
8
commits into
main
Choose a base branch
from
multiple-social-handles
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 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0feb85b
Support multiple handles per platform type for partners
devkiran 560be53
Adapt admin platform routes to multiple handles per type
devkiran 01f2257
Enforce unique partner platform identifiers and harden platform updates
devkiran b0caf2b
Remove enforce-limits cron and clean up platform dedupe, sanitization…
devkiran fb7746e
code cleanup
devkiran 5fe4998
Update upsert-partner-platform.ts
devkiran a0b815c
Update route.ts
devkiran 7dd04a8
Merge branch 'main' into multiple-social-handles
devkiran 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
91 changes: 91 additions & 0 deletions
91
apps/web/app/(ee)/api/cron/partner-platforms/enforce-limits/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,91 @@ | ||
| import { withCron } from "@/lib/cron/with-cron"; | ||
| import { prisma } from "@/lib/prisma"; | ||
| import { MAX_PLATFORMS_PER_TYPE } from "@/lib/social-utils"; | ||
| import { logAndRespond } from "../../utils"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| /** | ||
| * Safety-net cron that enforces the per-type handle limit in case the | ||
| * application-level guard is ever bypassed (there is no DB-level unique key). | ||
| * For any (partnerId, type) group over the limit, it keeps the handles to | ||
| * retain — verified first, then oldest — and deletes the most-recently-added | ||
| * excess. | ||
| * | ||
| * Runs daily. GET /api/cron/partner-platforms/enforce-limits | ||
| */ | ||
| export const GET = withCron(async () => { | ||
| const overLimitGroups = await prisma.partnerPlatform.groupBy({ | ||
| by: ["partnerId", "type"], | ||
| _count: { | ||
| id: true, | ||
| }, | ||
| having: { | ||
| id: { | ||
| _count: { | ||
| gt: MAX_PLATFORMS_PER_TYPE, | ||
| }, | ||
| }, | ||
| }, | ||
| orderBy: { | ||
| partnerId: "asc", | ||
| }, | ||
| take: 100, | ||
| }); | ||
|
|
||
| if (overLimitGroups.length === 0) { | ||
| return logAndRespond("No partners over the per-type platform limit."); | ||
| } | ||
|
|
||
| let trimmedRows = 0; | ||
|
|
||
| for (const { partnerId, type } of overLimitGroups) { | ||
| const platforms = await prisma.partnerPlatform.findMany({ | ||
| where: { | ||
| partnerId, | ||
| type, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| verifiedAt: true, | ||
| createdAt: true, | ||
| }, | ||
| }); | ||
|
|
||
| // Retain verified handles first, then the oldest; delete the rest | ||
| const sortedPlatforms = platforms.sort((a, b) => { | ||
| const aVerified = a.verifiedAt ? 1 : 0; | ||
| const bVerified = b.verifiedAt ? 1 : 0; | ||
|
|
||
| if (aVerified !== bVerified) { | ||
| return bVerified - aVerified; | ||
| } | ||
|
|
||
| return a.createdAt.getTime() - b.createdAt.getTime(); | ||
| }); | ||
|
|
||
| const excessIds = sortedPlatforms | ||
| .slice(MAX_PLATFORMS_PER_TYPE) | ||
| .map((r) => r.id); | ||
|
|
||
| if (excessIds.length > 0) { | ||
| await prisma.partnerPlatform.deleteMany({ | ||
| where: { | ||
| id: { | ||
| in: excessIds, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| trimmedRows += excessIds.length; | ||
|
|
||
| console.log( | ||
| `Trimmed ${excessIds.length} excess ${type} handle(s) for partner ${partnerId}.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return logAndRespond( | ||
| `Enforced per-type platform limit: trimmed ${trimmedRows} row(s) across ${overLimitGroups.length} group(s).`, | ||
| ); | ||
| }); |
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
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.