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.

sync Plain customer email when users change their Dub email

Pedro Ladeira1f0eb5a095ac
diff --git a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
index 63353fe27a..5e4512ac7c 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
@@ -1,5 +1,6 @@
import { getSession, hashToken } from "@/lib/auth";
import { hasPermission } from "@/lib/auth/partner-users/partner-user-permissions";
+import { syncPlainCustomerEmail } from "@/lib/plain/upsert-plain-customer";
import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import EmptyState from "@/ui/shared/empty-state";
@@ -211,6 +212,9 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
});
}
+ const shouldSyncPlainCustomerEmail =
+ !!data.syncIdentity || !data.isPartnerProfile;
+
waitUntil(
Promise.allSettled([
deleteRequest(tokenFound),
@@ -225,6 +229,16 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
syncIdentity: !!data.syncIdentity,
}),
}),
+
+ ...(shouldSyncPlainCustomerEmail
+ ? [
+ syncPlainCustomerEmail({
+ id: userId,
+ name: session.user.name ?? null,
+ email: data.newEmail,
+ }),
+ ]
+ : []),
]),
);
diff --git a/apps/web/lib/plain/upsert-plain-customer.ts b/apps/web/lib/plain/upsert-plain-customer.ts
index 4f85917175..a5e44660b5 100644
--- a/apps/web/lib/plain/upsert-plain-customer.ts
+++ b/apps/web/lib/plain/upsert-plain-customer.ts
@@ -32,3 +32,38 @@ export const upsertPlainCustomer = async (
},
});
};
+
+// Sync Plain customer email by Dub user id (externalId).
+export const syncPlainCustomerEmail = async (
+ user: PlainUser & { email: string },
+) => {
+ const fullName = user.name || user.email;
+ const shortName = user.name || user.email.split("@")[0];
+
+ const result = await plain.upsertCustomer({
+ identifier: {
+ externalId: user.id,
+ },
+ onCreate: {
+ fullName,
+ shortName,
+ email: {
+ email: user.email,
+ isVerified: true,
+ },
+ externalId: user.id,
+ },
+ onUpdate: {
+ email: {
+ email: user.email,
+ isVerified: true,
+ },
+ },
+ });
+
+ if (result.error) {
+ console.error("Failed to sync Plain customer email:", result.error);
+ }
+
+ return result;
+};