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.

fix plain customer upsert after email changes

Pedro Ladeirac21a2722eb90
diff --git a/apps/web/app/api/ai/support-chat/upload/route.ts b/apps/web/app/api/ai/support-chat/upload/route.ts
index b3659a4b18..3561b8cd22 100644
--- a/apps/web/app/api/ai/support-chat/upload/route.ts
+++ b/apps/web/app/api/ai/support-chat/upload/route.ts
@@ -66,38 +66,21 @@ export const POST = withSession(async ({ req, session }) => {
}
// Ensure the Plain customer exists and get their Plain-scoped ID
- const user = {
- id: session.user.id,
- name: session.user.name ?? "",
- email: session.user.email,
- };
-
- const upsert = await upsertPlainCustomer(user);
-
- let customer = upsert.data?.customer ?? null;
- let customerError = upsert.error ?? null;
-
- if (
- !customer &&
- upsert.error?.type === "mutation_error" &&
- upsert.error.errorDetails.code ===
- "customer_already_exists_with_external_id"
- ) {
- const { data: existingCustomer, error: lookupError } =
- await plain.getCustomerByExternalId({ externalId: user.id });
-
- customer = existingCustomer ?? null;
- customerError = lookupError ?? null;
- }
-
- if (customerError || !customer) {
- console.error("Failed to resolve Plain customer:", customerError);
+ const { data: customerData, error: customerError } =
+ await upsertPlainCustomer({
+ id: session.user.id,
+ name: session.user.name ?? "",
+ email: session.user.email,
+ });
+
+ if (customerError || !customerData) {
+ console.error("Failed to upsert Plain customer:", customerError);
return new Response("Failed to resolve support customer", { status: 500 });
}
const { data, error } = await plain.createAttachmentUploadUrl({
attachmentType: AttachmentType.CustomTimelineEntry,
- customerId: customer.id,
+ customerId: customerData.customer.id,
fileName,
fileSizeBytes,
});
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 5e4512ac7c..8df0b38920 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
@@ -236,6 +236,7 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
id: userId,
name: session.user.name ?? null,
email: data.newEmail,
+ oldEmail: data.email,
}),
]
: []),
diff --git a/apps/web/lib/plain/upsert-plain-customer.ts b/apps/web/lib/plain/upsert-plain-customer.ts
index a5e44660b5..e823f15df6 100644
--- a/apps/web/lib/plain/upsert-plain-customer.ts
+++ b/apps/web/lib/plain/upsert-plain-customer.ts
@@ -6,7 +6,7 @@ export const upsertPlainCustomer = async (
const fullName = user.name || user.email;
const shortName = user.name || user.email.split("@")[0];
- return await plain.upsertCustomer({
+ const result = await plain.upsertCustomer({
identifier: {
emailAddress: user.email,
},
@@ -31,33 +31,105 @@ export const upsertPlainCustomer = async (
},
},
});
+
+ if (
+ result.error?.type === "mutation_error" &&
+ result.error.errorDetails.code ===
+ "customer_already_exists_with_external_id"
+ ) {
+ return await plain.upsertCustomer({
+ identifier: {
+ externalId: user.id,
+ },
+ onCreate: {
+ fullName,
+ shortName,
+ email: {
+ email: user.email,
+ isVerified: true,
+ },
+ externalId: user.id,
+ },
+ onUpdate: {
+ fullName: {
+ value: fullName,
+ },
+ shortName: {
+ value: shortName,
+ },
+ email: {
+ email: user.email,
+ isVerified: true,
+ },
+ },
+ });
+ }
+
+ return result;
};
-// 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];
+export const syncPlainCustomerEmail = async ({
+ id,
+ name,
+ email,
+ oldEmail,
+}: PlainUser & { email: string; oldEmail?: string }) => {
+ const fullName = name || email;
+ const shortName = name || email.split("@")[0];
+
+ const { data: byExternalId, error: externalIdError } =
+ await plain.getCustomerByExternalId({ externalId: id });
+
+ if (externalIdError) {
+ console.error(
+ "Failed to lookup Plain customer by externalId:",
+ externalIdError,
+ );
+ return;
+ }
+
+ let customer = byExternalId ?? null;
+
+ if (!customer && oldEmail) {
+ const { data: byEmail, error: emailError } = await plain.getCustomerByEmail(
+ {
+ email: oldEmail,
+ },
+ );
+
+ if (emailError) {
+ console.error("Failed to lookup Plain customer by email:", emailError);
+ return;
+ }
+
+ customer = byEmail ?? null;
+ }
+
+ if (!customer) {
+ return;
+ }
const result = await plain.upsertCustomer({
identifier: {
- externalId: user.id,
+ customerId: customer.id,
},
onCreate: {
fullName,
shortName,
email: {
- email: user.email,
+ email,
isVerified: true,
},
- externalId: user.id,
+ externalId: id,
},
onUpdate: {
email: {
- email: user.email,
+ email,
isVerified: true,
},
+ externalId: {
+ value: id,
+ },
},
});