The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Shopify compliance webhook updates
Kiran Kfadf2196518a
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.tsindex c78fee458b..d29b219c22 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@@ -1,5 +1,6 @@+import { createPlainThread } from "@/lib/plain";+import { prisma } from "@dub/prisma";import { waitUntil } from "@vercel/functions";-import { sendEmail } from "emails";import { z } from "zod";const schema = z.object({@@ -7,28 +8,58 @@ const schema = z.object({orders_requested: z.array(z.number()),customer: z.object({id: z.number(),- email: z.string(),- phone: z.string(),}),});-export async function customersDataRequest({ event }: { event: any }) {+export async function customersDataRequest({+ event,+ workspaceId,+}: {+ event: any;+ workspaceId: string;+}) {const {customer,shop_domain: shopDomain,orders_requested: ordersRequested,} = schema.parse(event);+ const customerExternalId = customer.id.toString();++ const { userId } = await prisma.projectUsers.findFirstOrThrow({+ where: {+ projectId: workspaceId,+ role: "owner",+ },+ select: {+ userId: true,+ },+ });++ const rows = [+ {+ text: "Shop domain",+ value: shopDomain,+ },+ {+ text: "Customer ID",+ value: customerExternalId,+ },+ ...(ordersRequested.length > 0+ ? [{ text: "Orders requested", value: ordersRequested.join(", ") }]+ : []),+ ];+waitUntil(- sendEmail({- email: "[email protected]",- from: "Steven Tey <[email protected]>",- subject: "[Shopify] - Customer Data Request received",- text: `Customer Data Request received for shop: ${shopDomain}.- Customer ID: ${customer.id},- Email: ${customer.email},- Phone: ${customer.phone},- Orders Requested: ${ordersRequested.join(", ")}`,+ createPlainThread({+ userId,+ title: `Shopify - Customer data request received for ${shopDomain}`,+ components: rows.map((row) => ({+ componentRow: {+ rowMainContent: [{ componentText: { text: row.text } }],+ rowAsideContent: [{ componentText: { text: row.value } }],+ },+ })),}),);diff --git a/apps/web/app/api/shopify/integration/webhook/customers-redact.ts b/apps/web/app/api/shopify/integration/webhook/customers-redact.tsindex d67e27ae0f..6fd18ba8e9 100644--- a/apps/web/app/api/shopify/integration/webhook/customers-redact.ts+++ b/apps/web/app/api/shopify/integration/webhook/customers-redact.ts@@ -1,5 +1,6 @@+import { createPlainThread } from "@/lib/plain";+import { prisma } from "@dub/prisma";import { waitUntil } from "@vercel/functions";-import { sendEmail } from "emails";import { z } from "zod";const schema = z.object({@@ -7,28 +8,70 @@ const schema = z.object({orders_to_redact: z.array(z.number()),customer: z.object({id: z.number(),- email: z.string(),- phone: z.string(),}),});-export async function customersRedact({ event }: { event: any }) {+export async function customersRedact({+ event,+ workspaceId,+}: {+ event: any;+ workspaceId: string;+}) {const {customer,shop_domain: shopDomain,orders_to_redact: ordersToRedact,} = schema.parse(event);+ const customerExternalId = customer.id.toString();++ // Redact the customer's data+ try {+ await prisma.customer.update({+ where: {+ projectId_externalId: {+ projectId: workspaceId,+ externalId: customerExternalId,+ },+ },+ data: {+ name: "Redacted",+ email: "Redacted",+ },+ });+ } catch (error) {+ return `[Shopify] Failed to redact customer data. Reason: ${error.message}`;+ }++ const { userId } = await prisma.projectUsers.findFirstOrThrow({+ where: {+ projectId: workspaceId,+ role: "owner",+ },+ select: {+ userId: true,+ },+ });++ const rows = [+ { text: "Shop domain", value: shopDomain },+ { text: "Customer ID", value: customerExternalId },+ ...(ordersToRedact.length > 0+ ? [{ text: "Orders to redact", value: ordersToRedact.join(", ") }]+ : []),+ ];+waitUntil(- sendEmail({- email: "[email protected]",- from: "Steven Tey <[email protected]>",- subject: "[Shopify] - Customer Redacted request received",- text: `Customer Redacted request received for shop: ${shopDomain}.- Customer ID: ${customer.id},- Email: ${customer.email},- Phone: ${customer.phone},- Orders to Redact: ${ordersToRedact.join(", ")}`,+ createPlainThread({+ userId,+ title: `Shopify - Customer Redacted request received for ${shopDomain}`,+ components: rows.map((row) => ({+ componentRow: {+ rowMainContent: [{ componentText: { text: row.text } }],+ rowAsideContent: [{ componentText: { text: row.value } }],+ },+ })),}),);diff --git a/apps/web/app/api/shopify/integration/webhook/route.ts b/apps/web/app/api/shopify/integration/webhook/route.tsindex 653884ccb6..6ae36761c3 100644--- a/apps/web/app/api/shopify/integration/webhook/route.ts+++ b/apps/web/app/api/shopify/integration/webhook/route.ts@@ -73,16 +73,19 @@ export const POST = async (req: Request) => {case "customers/data_request":response = await customersDataRequest({event,+ workspaceId: workspace.id,});break;case "customers/redact":response = await customersRedact({event,+ workspaceId: workspace.id,});break;case "shop/redact":response = await shopRedact({event,+ workspaceId: workspace.id,});break;case "app/uninstalled":diff --git a/apps/web/app/api/shopify/integration/webhook/shop-redact.ts b/apps/web/app/api/shopify/integration/webhook/shop-redact.tsindex ca4220b7c9..ae4ef5cedf 100644--- a/apps/web/app/api/shopify/integration/webhook/shop-redact.ts+++ b/apps/web/app/api/shopify/integration/webhook/shop-redact.ts@@ -1,20 +1,43 @@+import { createPlainThread } from "@/lib/plain";+import { prisma } from "@dub/prisma";import { waitUntil } from "@vercel/functions";-import { sendEmail } from "emails";import { z } from "zod";const schema = z.object({shop_domain: z.string(),});-export async function shopRedact({ event }: { event: any }) {+export async function shopRedact({+ event,+ workspaceId,+}: {+ event: any;+ workspaceId: string;+}) {const { shop_domain: shopDomain } = schema.parse(event);+ const { userId } = await prisma.projectUsers.findFirstOrThrow({+ where: {+ projectId: workspaceId,+ role: "owner",+ },+ select: {+ userId: true,+ },+ });++ const rows = [{ text: "Shop domain", value: shopDomain }];+waitUntil(- sendEmail({- email: "[email protected]",- from: "Steven Tey <[email protected]>",- subject: "[Shopify] - Shop Redacted request received",- text: `Shop Redacted request received for shop: ${shopDomain}`,+ createPlainThread({+ userId,+ title: `Shopify - Shop Redacted request received for ${shopDomain}`,+ components: rows.map((row) => ({+ componentRow: {+ rowMainContent: [{ componentText: { text: row.text } }],+ rowAsideContent: [{ componentText: { text: row.value } }],+ },+ })),}),);diff --git a/apps/web/lib/plain.ts b/apps/web/lib/plain.tsindex 33a0c78513..cc9b2d8e84 100644--- a/apps/web/lib/plain.ts+++ b/apps/web/lib/plain.ts@@ -1,4 +1,4 @@-import { PlainClient } from "@team-plain/typescript-sdk";+import { CreateThreadInput, PlainClient } from "@team-plain/typescript-sdk";import { Session } from "./auth";export const plain = new PlainClient({@@ -22,3 +22,41 @@ export const upsertPlainCustomer = async (session: Session) => {onUpdate: {},});};++export const createPlainThread = async ({+ userId,+ title,+ components,+ labelTypeIds,+}: {+ userId: string;+ 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,+ });+ return;+ }++ const { data, error } = await plain.createThread({+ title,+ components,+ labelTypeIds,+ customerIdentifier: {+ externalId: userId,+ },+ });++ if (error) {+ throw new Error(`Failed to create thread: ${error.message}`);+ }++ return data;+};