Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve partner invites / default link updates

Steven Tey9856ca6804ab
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts
index f994becc67..92e89ff3b3 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts
@@ -9,6 +9,7 @@ import { defaultLinkDeletedJob } from "@/lib/jobs/handlers/default-link-deleted-
import { prisma } from "@/lib/prisma";
import {
createOrUpdateDefaultLinkSchema,
+ DEFAULT_PARTNER_GROUP,
PartnerGroupDefaultLinkSchema,
} from "@/lib/zod/schemas/groups";
import { APP_DOMAIN_WITH_NGROK, constructURLFromUTMParams } from "@dub/utils";
@@ -58,7 +59,7 @@ export const PATCH = withWorkspace(
if (group.partnerGroupDefaultLinks.length === 0) {
throw new DubApiError({
code: "bad_request",
- message: `Default link ${params.defaultLinkId} not found for this group.`,
+ message: `Default link ${params.defaultLinkId} not found for this partner group.`,
});
}
@@ -68,7 +69,7 @@ export const PATCH = withWorkspace(
// - Update the program's domain
// - Update all default links across groups to use the new domain
// - Update all partner links to use the new domain (via cron job)
- if (domain !== group.program.domain) {
+ if (domain !== defaultLink.domain) {
await prisma.$transaction([
prisma.program.update({
where: {
@@ -99,23 +100,36 @@ export const PATCH = withWorkspace(
);
}
- try {
- const updatedDefaultLink = await prisma.partnerGroupDefaultLink.update({
- where: {
- id: defaultLink.id,
- },
- data: {
- domain,
- url: group.utmTemplate
- ? constructURLFromUTMParams(
- url,
- extractUtmParams(group.utmTemplate),
- )
- : url,
- },
- });
+ if (url !== defaultLink.url) {
+ try {
+ const [updatedDefaultLink] = await prisma.$transaction([
+ prisma.partnerGroupDefaultLink.update({
+ where: {
+ id: defaultLink.id,
+ },
+ data: {
+ url: group.utmTemplate
+ ? constructURLFromUTMParams(
+ url,
+ extractUtmParams(group.utmTemplate),
+ )
+ : url,
+ },
+ }),
+ // if the group being updated is the default partner group,
+ // also update the program's URL to the new default link destination URL
+ group.slug === DEFAULT_PARTNER_GROUP.slug
+ ? prisma.program.update({
+ where: {
+ id: programId,
+ },
+ data: {
+ url,
+ },
+ })
+ : null,
+ ]);
- if (updatedDefaultLink.url !== defaultLink.url) {
waitUntil(
qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/update-default-links`,
@@ -124,24 +138,28 @@ export const PATCH = withWorkspace(
},
}),
);
- }
- return NextResponse.json(
- PartnerGroupDefaultLinkSchema.parse(updatedDefaultLink),
- );
- } catch (error) {
- if (error.code === "P2002") {
+ return NextResponse.json(
+ PartnerGroupDefaultLinkSchema.parse(updatedDefaultLink),
+ );
+ } catch (error) {
+ if (error.code === "P2002") {
+ throw new DubApiError({
+ code: "conflict",
+ message:
+ "A default link with this destination URL already exists in this partner group.",
+ });
+ }
+
throw new DubApiError({
- code: "conflict",
- message: "A default link with this URL already exists.",
+ code: "unprocessable_entity",
+ message: error.message,
});
}
-
- throw new DubApiError({
- code: "unprocessable_entity",
- message: error.message,
- });
}
+
+ // if no url changes were made, just return defaultLink (no changes needed)
+ return NextResponse.json(PartnerGroupDefaultLinkSchema.parse(defaultLink));
},
{
requiredPermissions: ["groups.write"],
diff --git a/apps/web/app/(ee)/api/partner-profile/invites/route.ts b/apps/web/app/(ee)/api/partner-profile/invites/route.ts
index dc1e1214b6..c9e7eafac6 100644
--- a/apps/web/app/(ee)/api/partner-profile/invites/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/invites/route.ts
@@ -7,6 +7,8 @@ import {
MAX_PARTNER_USERS,
} from "@/lib/constants/partner-profile";
import { prisma } from "@/lib/prisma";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import {
getPartnerUsersQuerySchema,
invitePartnerUserSchema,
@@ -56,6 +58,11 @@ export const POST = withPartnerProfile(
});
}
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.partnerProfileInvite,
+ identifier: partner.id,
+ });
+
const emails = Array.from(new Set([...invites.map(({ email }) => email)]));
const [
diff --git a/apps/web/lib/api/partners/invite-partner-user.ts b/apps/web/lib/api/partners/invite-partner-user.ts
index 441f4674be..cd09045413 100644
--- a/apps/web/lib/api/partners/invite-partner-user.ts
+++ b/apps/web/lib/api/partners/invite-partner-user.ts
@@ -38,6 +38,7 @@ export async function invitePartnerUser({
message: "User has already been invited to this partner profile.",
});
}
+ throw error;
}
await prisma.verificationToken.create({
diff --git a/apps/web/lib/upstash/ratelimit-policies.ts b/apps/web/lib/upstash/ratelimit-policies.ts
index 464dacc9b2..e456d5f9a1 100644
--- a/apps/web/lib/upstash/ratelimit-policies.ts
+++ b/apps/web/lib/upstash/ratelimit-policies.ts
@@ -16,18 +16,6 @@ export type RatelimitPolicy = {
};
export const RATELIMIT_POLICIES = {
- programImageUpload: {
- attempts: 10,
- window: "24 h",
- keyPrefix: "rl:program:application:image:upload",
- },
-
- messageAttachmentUpload: {
- attempts: 20,
- window: "1 h",
- keyPrefix: "rl:message:attachment:upload",
- },
-
login: {
attempts: 5,
window: "1 m",
@@ -83,4 +71,22 @@ export const RATELIMIT_POLICIES = {
window: "1 m",
keyPrefix: "rl:auth:saml-verify",
},
+
+ programImageUpload: {
+ attempts: 10,
+ window: "24 h",
+ keyPrefix: "rl:program:application:image:upload",
+ },
+
+ messageAttachmentUpload: {
+ attempts: 20,
+ window: "1 h",
+ keyPrefix: "rl:message:attachment:upload",
+ },
+
+ partnerProfileInvite: {
+ attempts: 5,
+ window: "1 h",
+ keyPrefix: "rl:partner-profile:invite",
+ },
} as const satisfies Record<string, RatelimitPolicy>;