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.

Add Axiom tracking to critical webhooks and payout crons (#4325)

Pedro Ladeirad24567479925
diff --git a/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts b/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
index 1c2320ec4d..746876cbe6 100644
--- a/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
+++ b/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { createPaymentIntent } from "@/lib/stripe/create-payment-intent";
import { ACME_WORKSPACE_ID, DUB_WORKSPACE_ID } from "@dub/utils";
@@ -12,93 +11,82 @@ const schema = z.object({
});
// POST /api/cron/invoices/retry-failed
-export async function POST(req: Request) {
- try {
- const rawBody = await req.text();
-
- await verifyQstashSignature({
- req,
- rawBody,
- });
-
- const { invoiceId } = schema.parse(JSON.parse(rawBody));
-
- const invoice = await prisma.invoice.findUnique({
- where: {
- id: invoiceId,
- },
- select: {
- id: true,
- type: true,
- status: true,
- total: true,
- failedAttempts: true,
- workspace: {
- select: {
- id: true,
- stripeId: true,
- },
+export const POST = withCron(async ({ rawBody }) => {
+ const { invoiceId } = schema.parse(JSON.parse(rawBody));
+
+ const invoice = await prisma.invoice.findUnique({
+ where: {
+ id: invoiceId,
+ },
+ select: {
+ id: true,
+ type: true,
+ status: true,
+ total: true,
+ failedAttempts: true,
+ workspace: {
+ select: {
+ id: true,
+ stripeId: true,
},
},
- });
+ },
+ });
- if (!invoice) {
- console.log(`Invoice ${invoiceId} not found.`);
- return new Response(`Invoice ${invoiceId} not found.`);
- }
+ if (!invoice) {
+ console.log(`Invoice ${invoiceId} not found.`);
+ return new Response(`Invoice ${invoiceId} not found.`);
+ }
- if (invoice.status !== "failed") {
- console.log(`Invoice ${invoiceId} is not failed.`);
- return new Response(`Invoice ${invoiceId} is not failed.`);
- }
+ if (invoice.status !== "failed") {
+ console.log(`Invoice ${invoiceId} is not failed.`);
+ return new Response(`Invoice ${invoiceId} is not failed.`);
+ }
- if (invoice.failedAttempts >= 3) {
- console.log(`Invoice ${invoiceId} has reached max failed attempts of 3.`);
- return new Response(
- `Invoice ${invoiceId} has reached max failed attempts of 3.`,
- );
- }
+ if (invoice.failedAttempts >= 3) {
+ console.log(`Invoice ${invoiceId} has reached max failed attempts of 3.`);
+ return new Response(
+ `Invoice ${invoiceId} has reached max failed attempts of 3.`,
+ );
+ }
- if (invoice.type !== "domainRenewal") {
- console.log(`Only domain renewals can be retried at this time.`);
- return new Response(`Only domain renewals can be retried at this time.`);
- }
+ if (invoice.type !== "domainRenewal") {
+ console.log(`Only domain renewals can be retried at this time.`);
+ return new Response(`Only domain renewals can be retried at this time.`);
+ }
- let { workspace } = invoice;
+ let { workspace } = invoice;
- // If Acme workspace, use Dub workspace stripeId
- if (workspace.id === ACME_WORKSPACE_ID) {
- const dubWorkspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: DUB_WORKSPACE_ID,
- },
- select: {
- stripeId: true,
- },
- });
+ // If Acme workspace, use Dub workspace stripeId
+ if (workspace.id === ACME_WORKSPACE_ID) {
+ const dubWorkspace = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: DUB_WORKSPACE_ID,
+ },
+ select: {
+ stripeId: true,
+ },
+ });
- workspace = {
- ...workspace,
- stripeId: dubWorkspace.stripeId,
- };
- }
+ workspace = {
+ ...workspace,
+ stripeId: dubWorkspace.stripeId,
+ };
+ }
- if (!workspace.stripeId) {
- console.log(`Workspace ${workspace.id} has no stripeId.`);
- return new Response(`Workspace ${workspace.id} has no stripeId.`);
- }
+ if (!workspace.stripeId) {
+ console.log(`Workspace ${workspace.id} has no stripeId.`);
+ return new Response(`Workspace ${workspace.id} has no stripeId.`);
+ }
- await createPaymentIntent({
- stripeId: workspace.stripeId,
- amount: invoice.total,
- invoiceId: invoice.id,
- statementDescriptor: "DUB.CO DOMAIN RENEWAL",
- description: `Domain renewal invoice (${invoice.id})`,
- idempotencyKey: `${invoice.id}-${invoice.failedAttempts}`,
- });
+ await createPaymentIntent({
+ stripeId: workspace.stripeId,
+ amount: invoice.total,
+ invoiceId: invoice.id,
+ statementDescriptor: "DUB.CO DOMAIN RENEWAL",
+ description: `Domain renewal invoice (${invoice.id})`,
+ idempotencyKey: `${invoice.id}-${invoice.failedAttempts}`,
+ });
- return new Response(`Retrying invoice charge ${invoice.id}...`);
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
-}
+ return new Response(`Retrying invoice charge ${invoice.id}...`);
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/balance-available/route.ts b/apps/web/app/(ee)/api/cron/payouts/balance-available/route.ts
index 4b5cff4bde..274af4aad0 100644
--- a/apps/web/app/(ee)/api/cron/payouts/balance-available/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/balance-available/route.ts
@@ -1,7 +1,6 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { BANK_ACCOUNT_STATUS_DESCRIPTIONS } from "@/lib/constants/payouts";
import { qstash } from "@/lib/cron";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { getPartnerBankAccount } from "@/lib/partners/get-partner-bank-account";
import { prisma } from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
@@ -24,11 +23,8 @@ const payloadSchema = z.object({
});
// POST /api/cron/payouts/balance-available
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
const { stripeAccount } = payloadSchema.parse(JSON.parse(rawBody));
const partner = await prisma.partner.findUnique({
@@ -222,6 +218,6 @@ export async function POST(req: Request) {
type: "errors",
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
index 3498b1b253..37b5e0af77 100644
--- a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { log } from "@dub/utils";
import { PartnerPayoutMethod } from "@prisma/client";
@@ -21,11 +20,8 @@ const payloadSchema = z.object({
// POST /api/cron/payouts/charge-succeeded
// This route is used to process the charge-succeeded event from Stripe.
// We're intentionally offloading this to a cron job so we can return a 200 to Stripe immediately.
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
const { invoiceId } = payloadSchema.parse(JSON.parse(rawBody));
const invoice = await prisma.invoice.findUnique({
@@ -131,6 +127,6 @@ export async function POST(req: Request) {
type: "cron",
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/payout-failed/route.ts b/apps/web/app/(ee)/api/cron/payouts/payout-failed/route.ts
index f7a0a144f5..5c2c2957c8 100644
--- a/apps/web/app/(ee)/api/cron/payouts/payout-failed/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/payout-failed/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { getPartnerBankAccount } from "@/lib/partners/get-partner-bank-account";
import { prisma } from "@/lib/prisma";
import { sendEmail } from "@dub/email";
@@ -19,11 +18,8 @@ const payloadSchema = z.object({
});
// POST /api/cron/payouts/payout-failed
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
const { stripeAccount, stripePayout } = payloadSchema.parse(
JSON.parse(rawBody),
);
@@ -86,6 +82,6 @@ export async function POST(req: Request) {
type: "errors",
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/payout-paid/route.ts b/apps/web/app/(ee)/api/cron/payouts/payout-paid/route.ts
index e9ed1d10a4..9c97d263ee 100644
--- a/apps/web/app/(ee)/api/cron/payouts/payout-paid/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/payout-paid/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { sendEmail } from "@dub/email";
import PartnerPayoutWithdrawalCompleted from "@dub/email/templates/partner-payout-withdrawal-completed";
@@ -19,11 +18,8 @@ const payloadSchema = z.object({
});
// POST /api/cron/payouts/payout-paid
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
const { stripeAccount, stripePayout } = payloadSchema.parse(
JSON.parse(rawBody),
);
@@ -83,6 +79,6 @@ export async function POST(req: Request) {
type: "errors",
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/route.ts b/apps/web/app/(ee)/api/cron/payouts/process/route.ts
index ba3385a311..4a877aea41 100644
--- a/apps/web/app/(ee)/api/cron/payouts/process/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/process/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { CUTOFF_PERIOD_ENUM } from "@/lib/partners/cutoff-period";
import { prisma } from "@/lib/prisma";
import { log } from "@dub/utils";
@@ -24,12 +23,8 @@ const processPayoutsCronSchema = z.object({
// POST /api/cron/payouts/process
// This route is used to process payouts for a given invoice
// we're intentionally offloading this to a cron job to avoid blocking the main thread
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
-
- await verifyQstashSignature({ req, rawBody });
-
const {
workspaceId,
userId,
@@ -107,6 +102,6 @@ export async function POST(req: Request) {
mention: true,
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/updates/route.ts b/apps/web/app/(ee)/api/cron/payouts/process/updates/route.ts
index a7628e0162..493ee08354 100644
--- a/apps/web/app/(ee)/api/cron/payouts/process/updates/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/process/updates/route.ts
@@ -1,7 +1,6 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";
@@ -20,15 +19,8 @@ const BATCH_SIZE = 100;
// POST /api/cron/payouts/process/updates
// Recursive cron job to handle side effects of the `cron/payouts/process` job (recordAuditLog, sendBatchEmails)
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
-
- await verifyQstashSignature({
- req,
- rawBody,
- });
-
const { invoiceId, startingAfter } = payloadSchema.parse(
JSON.parse(rawBody),
);
@@ -149,6 +141,6 @@ export async function POST(req: Request) {
mention: true,
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/cron/payouts/send-stripe-payout/route.ts b/apps/web/app/(ee)/api/cron/payouts/send-stripe-payout/route.ts
index d84321d36f..5520cab723 100644
--- a/apps/web/app/(ee)/api/cron/payouts/send-stripe-payout/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/send-stripe-payout/route.ts
@@ -1,5 +1,4 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { withCron } from "@/lib/cron/with-cron";
import { createStablecoinPayout } from "@/lib/partners/create-stablecoin-payout";
import { createStripeTransfer } from "@/lib/partners/create-stripe-transfer";
import { prisma } from "@/lib/prisma";
@@ -16,15 +15,8 @@ const payloadSchema = z.object({
});
// POST /api/cron/payouts/send-stripe-payout
-export async function POST(req: Request) {
+export const POST = withCron(async ({ rawBody }) => {
try {
- const rawBody = await req.text();
-
- await verifyQstashSignature({
- req,
- rawBody,
- });
-
const { partnerId, invoiceId, chargeId } = payloadSchema.parse(
JSON.parse(rawBody),
);
@@ -76,6 +68,6 @@ export async function POST(req: Request) {
mention: true,
});
- return handleAndReturnErrorResponse(error);
+ throw error;
}
-}
+});
diff --git a/apps/web/app/(ee)/api/paypal/webhook/route.ts b/apps/web/app/(ee)/api/paypal/webhook/route.ts
index 3d7a0a25d7..12ab8d0ddc 100644
--- a/apps/web/app/(ee)/api/paypal/webhook/route.ts
+++ b/apps/web/app/(ee)/api/paypal/webhook/route.ts
@@ -1,3 +1,4 @@
+import { withAxiom } from "@/lib/axiom/server";
import { log } from "@dub/utils";
import { payoutsItemFailed } from "./payouts-item-failed";
import { payoutsItemSucceeded } from "./payouts-item-succeeded";
@@ -17,7 +18,7 @@ const relevantEvents = new Set([
]);
// POST /api/paypal/webhook – Listen to Paypal webhook events
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const rawBody = await req.text();
const headers = req.headers;
@@ -69,4 +70,4 @@ export const POST = async (req: Request) => {
}
return new Response("OK");
-};
+});
diff --git a/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts b/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
index f51475c319..41813984ed 100644
--- a/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
@@ -1,5 +1,6 @@
import { captureWebhookLog } from "@/lib/api-logs/capture-webhook-log";
import { isLocalDev } from "@/lib/api/environment";
+import { withAxiom } from "@/lib/axiom/server";
import { prisma } from "@/lib/prisma";
import { waitUntil } from "@vercel/functions";
import { logAndRespond } from "app/(ee)/api/cron/utils";
@@ -21,7 +22,7 @@ const relevantTopics = new Set([
]);
// POST /api/shopify/integration/webhook – Listen to Shopify webhook events
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const startTime = Date.now();
const data = await req.text();
const headers = req.headers;
@@ -150,4 +151,4 @@ export const POST = async (req: Request) => {
}
return new Response(response);
-};
+});
diff --git a/apps/web/app/(ee)/api/stripe/webhook/route.ts b/apps/web/app/(ee)/api/stripe/webhook/route.ts
index 6ca0c1561b..153ceca9ad 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/route.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/route.ts
@@ -1,3 +1,4 @@
+import { withAxiom } from "@/lib/axiom/server";
import { stripe } from "@/lib/stripe";
import { log } from "@dub/utils";
import Stripe from "stripe";
@@ -29,13 +30,15 @@ const relevantEvents = new Set([
]);
// POST /api/stripe/webhook – listen to Stripe webhooks
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const buf = await req.text();
const sig = req.headers.get("Stripe-Signature") as string;
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
let event: Stripe.Event;
try {
- if (!sig || !webhookSecret) return;
+ if (!sig || !webhookSecret) {
+ return logAndRespond("Invalid request", { status: 400 });
+ }
event = stripe.webhooks.constructEvent(buf, sig, webhookSecret);
} catch (err: any) {
console.log(`❌ Error message: ${err.message}`);
@@ -99,4 +102,4 @@ export const POST = async (req: Request) => {
}
return logAndRespond(`[${event.type}]: ${response}`);
-};
+});
diff --git a/apps/web/app/api/dub/webhook/route.ts b/apps/web/app/api/dub/webhook/route.ts
index 630365f3fc..9b2cfc974c 100644
--- a/apps/web/app/api/dub/webhook/route.ts
+++ b/apps/web/app/api/dub/webhook/route.ts
@@ -1,10 +1,11 @@
+import { withAxiom } from "@/lib/axiom/server";
import { webhookPayloadSchema } from "@/lib/webhook/schemas";
import crypto from "crypto";
import { leadCreated } from "./lead-created";
import { saleCreated } from "./sale-created";
// POST /api/dub/webhook - receive webhooks for Dub
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const body = await req.json();
const { event, data } = webhookPayloadSchema.parse(body);
@@ -35,4 +36,4 @@ export const POST = async (req: Request) => {
}
return new Response(response);
-};
+});
diff --git a/apps/web/app/api/resend/webhook/route.ts b/apps/web/app/api/resend/webhook/route.ts
index ef479c5137..db8ffb5b86 100644
--- a/apps/web/app/api/resend/webhook/route.ts
+++ b/apps/web/app/api/resend/webhook/route.ts
@@ -1,3 +1,4 @@
+import { withAxiom } from "@/lib/axiom/server";
import { NextResponse } from "next/server";
import { Webhook } from "svix";
import { emailBounced } from "./email-bounced";
@@ -7,7 +8,7 @@ import { emailOpened } from "./email-opened";
const webhookSecret = process.env.RESEND_WEBHOOK_SECRET!;
// POST /api/resend/webhook – listen to Resend webhooks
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const rawBody = await req.text();
const webhook = new Webhook(webhookSecret);
@@ -33,4 +34,4 @@ export const POST = async (req: Request) => {
}
return NextResponse.json({ message: "Webhook processed." });
-};
+});
diff --git a/apps/web/app/api/veriff/webhook/route.ts b/apps/web/app/api/veriff/webhook/route.ts
index 0dd3c21526..b560aeb05d 100644
--- a/apps/web/app/api/veriff/webhook/route.ts
+++ b/apps/web/app/api/veriff/webhook/route.ts
@@ -1,10 +1,11 @@
+import { withAxiom } from "@/lib/axiom/server";
import { logAndRespond } from "app/(ee)/api/cron/utils";
import crypto from "crypto";
import { handleDecisionEvent } from "./handle-decision-event";
import { handleSessionEvent } from "./handle-session-event";
// POST /api/veriff/webhook
-export const POST = async (req: Request) => {
+export const POST = withAxiom(async (req: Request) => {
const rawBody = await req.text();
const signature = req.headers.get("x-hmac-signature");
@@ -62,4 +63,4 @@ export const POST = async (req: Request) => {
} else {
return await handleSessionEvent(body);
}
-};
+});