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.

Fix `tenantId` conflict in `createAndEnrollPartner`

Steven Teyce5f84a1a9df
diff --git a/apps/web/lib/api/partners/create-and-enroll-partner.ts b/apps/web/lib/api/partners/create-and-enroll-partner.ts
index ab2a69766b..17a1cbb156 100644
--- a/apps/web/lib/api/partners/create-and-enroll-partner.ts
+++ b/apps/web/lib/api/partners/create-and-enroll-partner.ts
@@ -24,6 +24,32 @@ interface CreateAndEnrollPartnerInput {
userId: string;
}
+// check if the tenantId already exists for a different enrolled partner
+// if so, throw an error
+const throwIfExistingTenantEnrollmentExists = async ({
+ tenantId,
+ programId,
+}: {
+ tenantId: string;
+ programId: string;
+}) => {
+ const existingTenantEnrollment = await prisma.programEnrollment.findUnique({
+ where: {
+ tenantId_programId: {
+ tenantId,
+ programId,
+ },
+ },
+ });
+
+ if (existingTenantEnrollment) {
+ throw new DubApiError({
+ message: `Partner with tenantId '${tenantId}' already enrolled in this program.`,
+ code: "conflict",
+ });
+ }
+};
+
export const createAndEnrollPartner = async ({
workspace,
program,
@@ -65,24 +91,10 @@ export const createAndEnrollPartner = async ({
});
// else, if the passed tenantId is different from the existing enrollment...
} else if (partner.tenantId) {
- const existingTenantEnrollment =
- await prisma.programEnrollment.findUnique({
- where: {
- tenantId_programId: {
- tenantId: partner.tenantId,
- programId: program.id,
- },
- },
- });
-
- // check if the tenantId already exists for a different enrolled partner
- // if so, throw an error
- if (existingTenantEnrollment) {
- throw new DubApiError({
- message: `Partner with tenantId '${partner.tenantId}' already enrolled in this program.`,
- code: "conflict",
- });
- }
+ await throwIfExistingTenantEnrollmentExists({
+ tenantId: partner.tenantId,
+ programId: program.id,
+ });
// else, update the existing enrollment with the new tenantId
const updatedProgramEnrollment = await prisma.programEnrollment.update({
@@ -106,6 +118,11 @@ export const createAndEnrollPartner = async ({
});
}
}
+ } else if (partner.tenantId) {
+ await throwIfExistingTenantEnrollmentExists({
+ tenantId: partner.tenantId,
+ programId: program.id,
+ });
}
const finalGroupId = partner.groupId || program.defaultGroupId;