The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add /api/shopify/integration/callback
Kiran Kc3a1feeb2ab2
diff --git a/apps/web/app/api/oauth/userinfo/route.ts b/apps/web/app/api/oauth/userinfo/route.tsindex cb15d13369..7fd3bccab8 100644--- a/apps/web/app/api/oauth/userinfo/route.ts+++ b/apps/web/app/api/oauth/userinfo/route.ts@@ -30,6 +30,7 @@ export async function GET(req: NextRequest) {},project: {select: {+ id: true,name: true,slug: true,logo: true,@@ -52,6 +53,7 @@ export async function GET(req: NextRequest) {name: user.name,image: user.image,workspace: {+ id: `ws_${tokenRecord.project.id}`,slug: tokenRecord.project.slug,name: tokenRecord.project.name,logo: tokenRecord.project.logo,diff --git a/apps/web/app/api/shopify/integration/callback/route.ts b/apps/web/app/api/shopify/integration/callback/route.tsnew file mode 100644index 0000000000..564bd6da2d--- /dev/null+++ b/apps/web/app/api/shopify/integration/callback/route.ts@@ -0,0 +1,91 @@+import { DubApiError } from "@/lib/api/errors";+import { parseRequestBody } from "@/lib/api/utils";+import { withWorkspace } from "@/lib/auth";+import { installIntegration } from "@/lib/integrations/install";+import { prisma } from "@dub/prisma";+import { SHOPIFY_INTEGRATION_ID } from "@dub/utils";+import { waitUntil } from "@vercel/functions";+import { NextResponse } from "next/server";+import { z } from "zod";++const updateWorkspaceSchema = z.object({+ shopifyStoreId: z.string().nullable(),+});++// PATCH /api/shopify/integration/callback – update a shopify store id+export const PATCH = withWorkspace(+ async ({ req, workspace, session }) => {+ const body = await parseRequestBody(req);+ const { shopifyStoreId } = updateWorkspaceSchema.parse(body);++ try {+ const response = await prisma.project.update({+ where: {+ id: workspace.id,+ },+ data: {+ shopifyStoreId,+ },+ select: {+ shopifyStoreId: true,+ },+ });++ waitUntil(+ (async () => {+ const installation = await prisma.installedIntegration.findUnique({+ where: {+ userId_integrationId_projectId: {+ userId: session.user.id,+ projectId: workspace.id,+ integrationId: SHOPIFY_INTEGRATION_ID,+ },+ },+ select: {+ id: true,+ },+ });++ // Install the integration if it doesn't exist+ if (!installation) {+ await installIntegration({+ userId: session.user.id,+ workspaceId: workspace.id,+ integrationId: SHOPIFY_INTEGRATION_ID,+ credentials: {+ shopifyStoreId,+ },+ });+ }++ // Uninstall the integration if the shopify store id is null+ if (installation && shopifyStoreId === null) {+ await prisma.installedIntegration.delete({+ where: {+ id: installation.id,+ },+ });+ }+ })(),+ );++ return NextResponse.json(response);+ } catch (error) {+ if (error.code === "P2002") {+ throw new DubApiError({+ code: "conflict",+ message: `The shopify store "${shopifyStoreId}" is already in use.`,+ });+ }++ throw new DubApiError({+ code: "internal_server_error",+ message: error.message,+ });+ }+ },+ {+ requiredPermissions: ["workspaces.write"],+ requiredAddOn: "conversion",+ },+);diff --git a/apps/web/app/api/shopify/webhook/order-paid.ts b/apps/web/app/api/shopify/integration/webhook/order-paid.tssimilarity index 100%rename from apps/web/app/api/shopify/webhook/order-paid.tsrename to apps/web/app/api/shopify/integration/webhook/order-paid.tsdiff --git a/apps/web/app/api/shopify/webhook/route.ts b/apps/web/app/api/shopify/integration/webhook/route.tssimilarity index 93%rename from apps/web/app/api/shopify/webhook/route.tsrename to apps/web/app/api/shopify/integration/webhook/route.tsindex c6c9dc786e..1f8ce4c4b8 100644--- a/apps/web/app/api/shopify/webhook/route.ts+++ b/apps/web/app/api/shopify/integration/webhook/route.ts@@ -5,7 +5,7 @@ import { verifyShopifySignature } from "./utils";const relevantTopics = new Set(["orders/paid"]);-// POST /api/shopify/webhook – Listen to Shopify webhook events+// POST /api/shopify/integration/webhook – Listen to Shopify webhook eventsexport const POST = async (req: Request) => {const body = await req.json();diff --git a/apps/web/app/api/shopify/webhook/utils.ts b/apps/web/app/api/shopify/integration/webhook/utils.tssimilarity index 84%rename from apps/web/app/api/shopify/webhook/utils.tsrename to apps/web/app/api/shopify/integration/webhook/utils.tsindex e1902d6957..2212aad314 100644--- a/apps/web/app/api/shopify/webhook/utils.ts+++ b/apps/web/app/api/shopify/integration/webhook/utils.ts@@ -12,14 +12,7 @@ export const verifyShopifySignature = async ({.update(JSON.stringify(body)).digest("base64");- console.log({- generatedSignature,- signature,- });-if (generatedSignature !== signature) {throw new Error("Invalid webhook signature.");}-- return true;};diff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prismaindex 18cd572653..f8bf448e7e 100644--- a/packages/prisma/schema/workspace.prisma+++ b/packages/prisma/schema/workspace.prisma@@ -11,6 +11,7 @@ model Project {paymentFailedAt DateTime?stripeConnectId String? @unique // for Stripe IntegrationpayoutMethodId String? // for Stripe Connect payouts+ shopifyStoreId String? @unique // for Shopify Integrationusage Int @default(0)usageLimit Int @default(1000)diff --git a/packages/utils/src/constants/integrations.ts b/packages/utils/src/constants/integrations.tsindex b09dabc288..2b06e8225a 100644--- a/packages/utils/src/constants/integrations.ts+++ b/packages/utils/src/constants/integrations.ts@@ -1,2 +1,3 @@export const SLACK_INTEGRATION_ID = "clzu59rx9000110bm5fnlzwuj";export const STRIPE_INTEGRATION_ID = "clzra1ya60001wnj4a89zcg9h";+export const SHOPIFY_INTEGRATION_ID = "int_edz1HUu12L3kL7s9kLRLycuM";\ No newline at end of file