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.

support chat upload when Plain customer externalId already exists

Pedro Ladeira602886869ff9
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 3561b8cd22..ef7c135b52 100644
--- a/apps/web/app/api/ai/support-chat/upload/route.ts
+++ b/apps/web/app/api/ai/support-chat/upload/route.ts
@@ -66,21 +66,38 @@ export const POST = withSession(async ({ req, session }) => {
}
// Ensure the Plain customer exists and get their Plain-scoped ID
- 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);
+ 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 ?? upsert.error;
+ }
+
+ if (customerError || !customer) {
+ console.error("Failed to resolve Plain customer:", customerError);
return new Response("Failed to resolve support customer", { status: 500 });
}
const { data, error } = await plain.createAttachmentUploadUrl({
attachmentType: AttachmentType.CustomTimelineEntry,
- customerId: customerData.customer.id,
+ customerId: customer.id,
fileName,
fileSizeBytes,
});