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.

update createPlainThread

Steven Tey49cab7ab593f
diff --git a/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts b/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts
index ad36d5ecde..5fba0eb521 100644
--- a/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts
+++ b/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts
@@ -24,14 +24,20 @@ export async function customersDataRequest({
orders_requested: ordersRequested,
} = schema.parse(event);
- const [user, customer] = await Promise.all([
+ const [{ user }, customer] = await Promise.all([
prisma.projectUsers.findFirstOrThrow({
where: {
projectId: workspaceId,
role: "owner",
},
select: {
- userId: true,
+ user: {
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ },
+ },
},
}),
prisma.customer.findUnique({
@@ -68,7 +74,11 @@ export async function customersDataRequest({
waitUntil(
createPlainThread({
- userId: user.userId,
+ user: {
+ id: user.id,
+ name: user.name ?? "",
+ email: user.email ?? "",
+ },
title: `Shopify - Customer data request received for ${shopDomain}`,
components: rows.map((row) => ({
componentRow: {
diff --git a/apps/web/app/api/shopify/integration/webhook/customers-redact.ts b/apps/web/app/api/shopify/integration/webhook/customers-redact.ts
index 2db29b9f41..3ee966986f 100644
--- a/apps/web/app/api/shopify/integration/webhook/customers-redact.ts
+++ b/apps/web/app/api/shopify/integration/webhook/customers-redact.ts
@@ -45,13 +45,19 @@ export async function customersRedact({
return `[Shopify] Failed to redact customer data. Reason: ${error.message}`;
}
- const { userId } = await prisma.projectUsers.findFirstOrThrow({
+ const { user } = await prisma.projectUsers.findFirstOrThrow({
where: {
projectId: workspaceId,
role: "owner",
},
select: {
- userId: true,
+ user: {
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ },
+ },
},
});
@@ -65,7 +71,11 @@ export async function customersRedact({
waitUntil(
createPlainThread({
- userId,
+ user: {
+ id: user.id,
+ name: user.name ?? "",
+ email: user.email ?? "",
+ },
title: `Shopify - Customer Redacted request received for ${shopDomain}`,
components: rows.map((row) => ({
componentRow: {
diff --git a/apps/web/app/api/shopify/integration/webhook/shop-redact.ts b/apps/web/app/api/shopify/integration/webhook/shop-redact.ts
index ae4ef5cedf..cf8dd94f21 100644
--- a/apps/web/app/api/shopify/integration/webhook/shop-redact.ts
+++ b/apps/web/app/api/shopify/integration/webhook/shop-redact.ts
@@ -16,13 +16,19 @@ export async function shopRedact({
}) {
const { shop_domain: shopDomain } = schema.parse(event);
- const { userId } = await prisma.projectUsers.findFirstOrThrow({
+ const { user } = await prisma.projectUsers.findFirstOrThrow({
where: {
projectId: workspaceId,
role: "owner",
},
select: {
- userId: true,
+ user: {
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ },
+ },
},
});
@@ -30,7 +36,11 @@ export async function shopRedact({
waitUntil(
createPlainThread({
- userId,
+ user: {
+ id: user.id,
+ name: user.name ?? "",
+ email: user.email ?? "",
+ },
title: `Shopify - Shop Redacted request received for ${shopDomain}`,
components: rows.map((row) => ({
componentRow: {
diff --git a/apps/web/app/api/support/route.ts b/apps/web/app/api/support/route.ts
index 1d80dc27d2..8a070b201f 100644
--- a/apps/web/app/api/support/route.ts
+++ b/apps/web/app/api/support/route.ts
@@ -23,7 +23,7 @@ export const POST = withSession(async ({ req, session }) => {
if (plainCustomer.data) {
plainCustomerId = plainCustomer.data.id;
} else {
- const { data } = await upsertPlainCustomer(session);
+ const { data } = await upsertPlainCustomer(session.user);
if (data) {
plainCustomerId = data.customer.id;
}
diff --git a/apps/web/app/api/support/upload/route.ts b/apps/web/app/api/support/upload/route.ts
index f1fd4f8d5e..8cbd51e925 100644
--- a/apps/web/app/api/support/upload/route.ts
+++ b/apps/web/app/api/support/upload/route.ts
@@ -14,7 +14,7 @@ export const GET = withSession(async ({ searchParams, session }) => {
if (plainCustomer.data) {
plainCustomerId = plainCustomer.data.id;
} else {
- const { data } = await upsertPlainCustomer(session);
+ const { data } = await upsertPlainCustomer(session.user);
if (data) {
plainCustomerId = data.customer.id;
}
diff --git a/apps/web/lib/actions/submit-oauth-app-for-review.ts b/apps/web/lib/actions/submit-oauth-app-for-review.ts
index 734380f63c..9aada8a4ac 100644
--- a/apps/web/lib/actions/submit-oauth-app-for-review.ts
+++ b/apps/web/lib/actions/submit-oauth-app-for-review.ts
@@ -46,7 +46,7 @@ export const submitOAuthAppForReview = authActionClient
if (plainCustomer.data) {
plainCustomerId = plainCustomer.data.id;
} else {
- const { data } = await upsertPlainCustomer({ user });
+ const { data } = await upsertPlainCustomer(user);
if (data) {
plainCustomerId = data.customer.id;
}
diff --git a/apps/web/lib/plain.ts b/apps/web/lib/plain.ts
index cc9b2d8e84..e446cf8ae5 100644
--- a/apps/web/lib/plain.ts
+++ b/apps/web/lib/plain.ts
@@ -5,53 +5,59 @@ export const plain = new PlainClient({
apiKey: process.env.PLAIN_API_KEY as string,
});
-export const upsertPlainCustomer = async (session: Session) => {
+type PlainUser = Pick<Session["user"], "id" | "name" | "email">;
+
+export const upsertPlainCustomer = async (user: PlainUser) => {
return await plain.upsertCustomer({
identifier: {
- externalId: session.user.id,
+ externalId: user.id,
},
onCreate: {
- fullName: session.user.name,
- shortName: session.user.name,
+ fullName: user.name,
+ shortName: user.name,
email: {
- email: session.user.email,
+ email: user.email,
isVerified: true,
},
- externalId: session.user.id,
+ externalId: user.id,
},
onUpdate: {},
});
};
export const createPlainThread = async ({
- userId,
+ user,
title,
components,
- labelTypeIds,
}: {
- userId: string;
+ user: PlainUser;
title: string;
components: CreateThreadInput["components"];
- labelTypeIds?: string[];
}) => {
- if (!process.env.PLAIN_API_KEY) {
- console.warn("No PLAIN_API_KEY found. Skipping thread creation.");
- console.log("createPlainThread", {
- userId,
- title,
- components,
- labelTypeIds,
+ let plainCustomerId: string | undefined;
+ const plainCustomer = await plain.getCustomerByEmail({
+ email: user.email ?? "",
+ });
+
+ if (plainCustomer.data) {
+ plainCustomerId = plainCustomer.data.id;
+ } else {
+ const { data } = await upsertPlainCustomer({
+ id: user.id,
+ name: user.name ?? "",
+ email: user.email ?? "",
});
- return;
+ if (data) {
+ plainCustomerId = data.customer.id;
+ }
}
const { data, error } = await plain.createThread({
- title,
- components,
- labelTypeIds,
customerIdentifier: {
- externalId: userId,
+ customerId: plainCustomerId,
},
+ title,
+ components,
});
if (error) {