Skip to content

Commit

Permalink
Start moving from edge to node
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Feb 25, 2025
1 parent f1f3ec6 commit 081e942
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions apps/web/app/api/track/sale/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { createId, parseRequestBody } from "@/lib/api/utils";
import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
import { withWorkspace } from "@/lib/auth";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhookOnEdge } from "@/lib/webhook/publish-edge";
Expand All @@ -13,17 +13,15 @@ import {
trackSaleRequestSchema,
trackSaleResponseSchema,
} from "@/lib/zod/schemas/sales";
import { prismaEdge } from "@dub/prisma/edge";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { differenceInMonths } from "date-fns";
import { NextResponse } from "next/server";
import { determinePartnerReward } from "../determine-partner-reward-edge";

export const runtime = "edge";

// POST /api/track/sale – Track a sale conversion event
export const POST = withWorkspaceEdge(
export const POST = withWorkspace(
async ({ req, workspace }) => {
const {
externalId,
Expand Down Expand Up @@ -63,7 +61,7 @@ export const POST = withWorkspaceEdge(
}

// Find customer
const customer = await prismaEdge.customer.findUnique({
const customer = await prisma.customer.findUnique({
where: {
projectId_externalId: {
projectId: workspace.id,
Expand Down Expand Up @@ -117,7 +115,7 @@ export const POST = withWorkspaceEdge(
recordSale(saleData),

// update link sales count
prismaEdge.link.update({
prisma.link.update({
where: {
id: clickData.link_id,
},
Expand All @@ -132,7 +130,7 @@ export const POST = withWorkspaceEdge(
include: includeTags,
}),
// update workspace sales usage
prismaEdge.project.update({
prisma.project.update({
where: {
id: workspace.id,
},
Expand Down Expand Up @@ -160,7 +158,7 @@ export const POST = withWorkspaceEdge(

if (typeof reward.maxDuration === "number") {
// Get the first commission (earliest sale) for this customer-partner pair
const firstCommission = await prismaEdge.commission.findFirst({
const firstCommission = await prisma.commission.findFirst({
where: {
partnerId: link.partnerId,
customerId: customer.id,
Expand Down Expand Up @@ -195,7 +193,7 @@ export const POST = withWorkspaceEdge(
},
});

await prismaEdge.commission.create({
await prisma.commission.create({
data: {
id: createId({ prefix: "cm_" }),
programId: link.programId,
Expand All @@ -211,7 +209,7 @@ export const POST = withWorkspaceEdge(
},
});

const program = await prismaEdge.program.findUniqueOrThrow({
const program = await prisma.program.findUniqueOrThrow({
where: {
id: link.programId!,
},
Expand Down

0 comments on commit 081e942

Please sign in to comment.