-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into analytics-link-comments
- Loading branch information
Showing
5 changed files
with
174 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { DubApiError, ErrorCodes } from "@/lib/api/errors"; | ||
import { createLink, processLink } from "@/lib/api/links"; | ||
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw"; | ||
import { parseRequestBody } from "@/lib/api/utils"; | ||
import { withWorkspace } from "@/lib/auth"; | ||
import { sendWorkspaceWebhook } from "@/lib/webhook/publish"; | ||
import { linkEventSchema } from "@/lib/zod/schemas/links"; | ||
import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners"; | ||
import { prisma } from "@dub/prisma"; | ||
import { getApexDomain } from "@dub/utils"; | ||
import { waitUntil } from "@vercel/functions"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// POST /api/partners/links - create a link for a partner | ||
export const POST = withWorkspace( | ||
async ({ workspace, req, session }) => { | ||
const { programId, partnerId, tenantId, url, key, linkProps } = | ||
createPartnerLinkSchema.parse(await parseRequestBody(req)); | ||
|
||
const program = await getProgramOrThrow({ | ||
workspaceId: workspace.id, | ||
programId, | ||
}); | ||
|
||
if (!program.domain || !program.url) { | ||
throw new DubApiError({ | ||
code: "bad_request", | ||
message: | ||
"You need to set a domain and url for this program before creating a partner.", | ||
}); | ||
} | ||
|
||
if (getApexDomain(url) !== getApexDomain(program.url)) { | ||
throw new DubApiError({ | ||
code: "bad_request", | ||
message: `The provided URL domain (${getApexDomain(url)}) does not match the program's domain (${getApexDomain(program.url)}).`, | ||
}); | ||
} | ||
|
||
if (!partnerId && !tenantId) { | ||
throw new DubApiError({ | ||
code: "bad_request", | ||
message: "You must provide a partnerId or tenantId.", | ||
}); | ||
} | ||
|
||
const partner = await prisma.programEnrollment.findUnique({ | ||
where: partnerId | ||
? { partnerId_programId: { partnerId, programId } } | ||
: { tenantId_programId: { tenantId: tenantId!, programId } }, | ||
}); | ||
|
||
if (!partner) { | ||
throw new DubApiError({ | ||
code: "not_found", | ||
message: "Partner not found.", | ||
}); | ||
} | ||
|
||
const { link, error, code } = await processLink({ | ||
payload: { | ||
...linkProps, | ||
domain: program.domain, | ||
key: key || undefined, | ||
url, | ||
programId, | ||
tenantId, | ||
partnerId, | ||
trackConversion: true, | ||
}, | ||
workspace, | ||
userId: session.user.id, | ||
}); | ||
|
||
if (error != null) { | ||
throw new DubApiError({ | ||
code: code as ErrorCodes, | ||
message: error, | ||
}); | ||
} | ||
|
||
const partnerLink = await createLink(link); | ||
|
||
waitUntil( | ||
sendWorkspaceWebhook({ | ||
trigger: "link.created", | ||
workspace, | ||
data: linkEventSchema.parse(partnerLink), | ||
}), | ||
); | ||
|
||
return NextResponse.json(partnerLink, { status: 201 }); | ||
}, | ||
{ | ||
requiredPlan: [ | ||
"business", | ||
"business extra", | ||
"business max", | ||
"business plus", | ||
"enterprise", | ||
], | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { openApiErrorResponses } from "@/lib/openapi/responses"; | ||
import { LinkSchema } from "@/lib/zod/schemas/links"; | ||
import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners"; | ||
import { ZodOpenApiOperationObject } from "zod-openapi"; | ||
|
||
export const createPartnerLink: ZodOpenApiOperationObject = { | ||
operationId: "createPartnerLink", | ||
"x-speakeasy-name-override": "createLink", | ||
summary: "Create a link for a partner", | ||
description: | ||
"Create a new link for a partner that is enrolled in your program", | ||
requestBody: { | ||
content: { | ||
"application/json": { | ||
schema: createPartnerLinkSchema, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
"201": { | ||
description: "The created partner", | ||
content: { | ||
"application/json": { | ||
schema: LinkSchema, | ||
}, | ||
}, | ||
}, | ||
...openApiErrorResponses, | ||
}, | ||
tags: ["Partners"], | ||
security: [{ token: [] }], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { ZodOpenApiPathsObject } from "zod-openapi"; | ||
import { createPartner } from "./create-partner"; | ||
import { createPartnerLink } from "./create-partner-link"; | ||
|
||
export const partnersPaths: ZodOpenApiPathsObject = { | ||
"/partners": { | ||
post: createPartner, | ||
}, | ||
"/partners/links": { | ||
post: createPartnerLink, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters