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 OpenAPI spec

Kiran Kf756797e3ad8
diff --git a/apps/web/app/api/partners/sale/route.ts b/apps/web/app/api/partners/sale/route.ts
index 7ef732a2c2..c49336e426 100644
--- a/apps/web/app/api/partners/sale/route.ts
+++ b/apps/web/app/api/partners/sale/route.ts
@@ -1,50 +1,78 @@
import { DubApiError } from "@/lib/api/errors";
+import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
+import { calculateSaleEarnings } from "@/lib/api/sales/calculate-earnings";
+import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth/workspace";
+import { SaleSchema, updateSaleSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
-export const PATCH = withWorkspace(async ({ req, workspace, searchParams }) => {
- const { programId, invoiceId } = searchParams;
+export const PATCH = withWorkspace(
+ async ({ req, workspace }) => {
+ const { programId, invoiceId, amount } = updateSaleSchema.parse(
+ await parseRequestBody(req),
+ );
- const { amount } = await req.json();
-
- const program = await getProgramOrThrow({
- workspaceId: workspace.id,
- programId,
- });
+ const program = await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
- const sale = await prisma.commission.findUnique({
- where: {
- programId_invoiceId: {
- programId: program.id,
- invoiceId,
+ const sale = await prisma.commission.findUnique({
+ where: {
+ programId_invoiceId: {
+ programId: program.id,
+ invoiceId,
+ },
},
- },
- });
+ });
+
+ if (!sale) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `Sale with invoice ID ${invoiceId} not found for program ${programId}.`,
+ });
+ }
+
+ if (sale.status === "paid") {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Cannot update amount: Sale with invoice ID ${invoiceId} has already been paid.`,
+ });
+ }
- if (!sale) {
- throw new DubApiError({
- code: "not_found",
- message: "Sale not found",
+ const partner = await getProgramEnrollmentOrThrow({
+ programId,
+ partnerId: sale.partnerId,
});
- }
- if (sale.status === "paid") {
- throw new DubApiError({
- code: "bad_request",
- message: "Sale already paid",
+ const earnings = calculateSaleEarnings({
+ program,
+ partner,
+ sales: sale.quantity,
+ saleAmount: amount,
});
- }
-
- const updatedSale = await prisma.commission.update({
- where: {
- id: sale.id,
- },
- data: {
- amount,
- },
- });
-
- return NextResponse.json(updatedSale);
-});
+
+ const updatedSale = await prisma.commission.update({
+ where: {
+ id: sale.id,
+ },
+ data: {
+ amount,
+ earnings,
+ },
+ });
+
+ return NextResponse.json(SaleSchema.parse(updatedSale));
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business extra",
+ "business max",
+ "business plus",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/lib/openapi/partners/index.ts b/apps/web/lib/openapi/partners/index.ts
index 1a31df1a3a..193a12d235 100644
--- a/apps/web/lib/openapi/partners/index.ts
+++ b/apps/web/lib/openapi/partners/index.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiPathsObject } from "zod-openapi";
import { createPartner } from "./create-partner";
import { createPartnerLink } from "./create-partner-link";
import { retrievePartnerAnalytics } from "./retrieve-analytics";
+import { updateSale } from "./update-sale";
import { upsertPartnerLink } from "./upsert-partner-link";
export const partnersPaths: ZodOpenApiPathsObject = {
@@ -17,4 +18,7 @@ export const partnersPaths: ZodOpenApiPathsObject = {
"/partners/analytics": {
get: retrievePartnerAnalytics,
},
+ "/partners/sale": {
+ patch: updateSale,
+ },
};
diff --git a/apps/web/lib/openapi/partners/update-sale.ts b/apps/web/lib/openapi/partners/update-sale.ts
new file mode 100644
index 0000000000..cf62bb9160
--- /dev/null
+++ b/apps/web/lib/openapi/partners/update-sale.ts
@@ -0,0 +1,32 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import { updateSaleSchema } from "@/lib/zod/schemas/partners";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+import { SaleSchema } from "../../zod/schemas/partners";
+
+export const updateSale: ZodOpenApiOperationObject = {
+ operationId: "updateSale",
+ "x-speakeasy-name-override": "updateSale",
+ summary: "Update an existing sale.",
+ description:
+ "This is useful if you need to change the amount of a sale after it has been created.",
+ requestBody: {
+ content: {
+ "application/json": {
+ schema: updateSaleSchema,
+ },
+ },
+ },
+ responses: {
+ "200": {
+ description: "The updated sale.",
+ content: {
+ "application/json": {
+ schema: SaleSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Partners"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 135688b3a2..b2f7251030 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -350,3 +350,12 @@ export const partnerAnalyticsResponseSchema = {
title: "PartnerAnalyticsTopLinks",
}),
} as const;
+
+export const updateSaleSchema = z.object({
+ amount: z
+ .number({ required_error: "Amount is required." })
+ .min(0)
+ .describe("The new amount for the sale."),
+ programId: z.string(),
+ invoiceId: z.string(),
+});