Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions apps/web/lib/api/partners/bulk-delete-partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function bulkDeletePartners({
});

const linksToDelete = programEnrollments.flatMap((pe) => pe.links);
const programEnrollmentIds = programEnrollments.map((pe) => pe.id);

if (linksToDelete.length > 0) {
while (true) {
Expand Down Expand Up @@ -69,7 +70,7 @@ export async function bulkDeletePartners({
where: {
programEnrollment: {
id: {
in: programEnrollments.map((pe) => pe.id),
in: programEnrollmentIds,
},
},
},
Expand All @@ -94,24 +95,37 @@ export async function bulkDeletePartners({
where: {
programEnrollment: {
id: {
in: programEnrollments.map((pe) => pe.id),
in: programEnrollmentIds,
},
},
},
});
console.log(`Deleted ${deletedPayouts.count} payouts`);

// Delete the messages
const deletedMessages = await prisma.message.deleteMany({
where: {
programEnrollment: {
id: {
in: programEnrollments.map((pe) => pe.id),
in: programEnrollmentIds,
},
},
},
});
console.log(`Deleted ${deletedMessages.count} messages`);

// Delete the bounty submissions
const deletedBountySubmissions = await prisma.bountySubmission.deleteMany({
where: {
programEnrollment: {
id: {
in: programEnrollmentIds,
},
},
},
});
console.log(`Deleted ${deletedBountySubmissions.count} bounty submissions`);
Comment on lines +105 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

cat -n apps/web/lib/api/partners/bulk-delete-partners.ts | head -150

Repository: dubinc/dub

Length of output: 4791


🏁 Script executed:

fd -e prisma

Repository: dubinc/dub

Length of output: 1223


🏁 Script executed:

rg -n 'programEnrollment' --type ts -B 2 -A 2 | head -100

Repository: dubinc/dub

Length of output: 6732


🏁 Script executed:

rg -n 'programEnrollment' packages/prisma/schema/ -A 3 -B 1

Repository: dubinc/dub

Length of output: 4613


🏁 Script executed:

# Also search for models that might reference programEnrollment indirectly or through relations
rg -n 'ProgramEnrollment' packages/prisma/schema/ --type prisma

Repository: dubinc/dub

Length of output: 84


🏁 Script executed:

# Check if DiscountCode is being deleted elsewhere or has cascade rules
rg -n 'discountCode\|DiscountCode' apps/web/lib/api/partners/bulk-delete-partners.ts

Repository: dubinc/dub

Length of output: 36


🏁 Script executed:

# Check full DiscountCode schema definition
cat -n packages/prisma/schema/discount.prisma

Repository: dubinc/dub

Length of output: 1909


🏁 Script executed:

# Check FraudCheck schema to confirm cascade behavior
cat -n packages/prisma/schema/fraud.prisma

Repository: dubinc/dub

Length of output: 3311


DiscountCode records must be deleted to prevent orphaned foreign keys.

The deletions are incomplete. The code deletes links (line 56-65), but DiscountCode has a required foreign key to Link without cascading deletion. This will leave orphaned DiscountCode records after links are deleted. Additionally, DiscountCode references programEnrollment optionally, which is also being deleted. Add explicit deletion of DiscountCode records that reference the programs being cleaned up.

🤖 Prompt for AI Agents
In apps/web/lib/api/partners/bulk-delete-partners.ts around lines 105 to 127,
you must also remove DiscountCode records to avoid FK constraint/orphan issues:
add explicit deleteMany calls for DiscountCode that target records referencing
the programEnrollmentIds (and any linkIds you remove) before deleting
Links/ProgramEnrollments; in practice run prisma.discountCode.deleteMany({
where: { OR: [{ programEnrollmentId: { in: programEnrollmentIds } }, { linkId: {
in: linkIds } }] } }) (or separate deletes) prior to deleting links/enrollments
so required foreign keys are not left orphaned.


if (deletePartners) {
const deletedPartners = await prisma.partner.deleteMany({
where: {
Expand All @@ -126,7 +140,7 @@ export async function bulkDeletePartners({
{
where: {
id: {
in: programEnrollments.map((pe) => pe.id),
in: programEnrollmentIds,
},
},
},
Expand Down