fix: Get dsyncData based on database (#14067)

* Set unique tenant

* Rely on current DB for dsync data

* Fix rendering dsync config

* Remove console.log

* Address feedback

* Add relationship between dsync data and team group mapping

* Fix `dsyncData` and `organizationSetting` relationship
This commit is contained in:
Joe Au-Yeung authored and GitHub committed 2024-03-14 01:25:50 +00:00
1 parent a7405db0d4
commit 3aff79e7d1
5 files changed
+49 -13

No files matched your search

@@ -39,7 +39,7 @@ const ConfigureDirectorySync = ({ organizationId }: { organizationId: number | n
return <SkeletonLoader />;
}
const directory = data ? data[0] : null;
const directory = data ?? null;
const onDeleteConfirmation = (e: Event | React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault();
@@ -0,0 +1,17 @@
/*
Warnings:
- A unique constraint covering the columns `[directoryId]` on the table `DSyncData` will be added. If there are existing duplicate values, this will fail.
*/
-- DropForeignKey
ALTER TABLE "DSyncData" DROP CONSTRAINT "DSyncData_organizationId_fkey";
-- CreateIndex
CREATE UNIQUE INDEX "DSyncData_directoryId_key" ON "DSyncData"("directoryId");
-- AddForeignKey
ALTER TABLE "DSyncData" ADD CONSTRAINT "DSyncData_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "OrganizationSettings"("organizationId") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DSyncTeamGroupMapping" ADD CONSTRAINT "DSyncTeamGroupMapping_directoryId_fkey" FOREIGN KEY ("directoryId") REFERENCES "DSyncData"("directoryId") ON DELETE CASCADE ON UPDATE CASCADE;
+9 -7
View File
@@ -1151,19 +1151,21 @@ model OutOfOfficeEntry {
}
model DSyncData {
id Int @id @default(autoincrement())
directoryId String
tenant String
organizationId Int? @unique
org OrganizationSettings? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
id Int @id @default(autoincrement())
directoryId String @unique
tenant String
organizationId Int? @unique
org OrganizationSettings? @relation(fields: [organizationId], references: [organizationId], onDelete: Cascade)
teamGroupMapping DSyncTeamGroupMapping[]
}
model DSyncTeamGroupMapping {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
organizationId Int
teamId Int
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
directoryId String
directory DSyncData @relation(fields: [directoryId], references: [directoryId], onDelete: Cascade)
groupName String
@@unique([teamId, groupName])
@@ -1,7 +1,7 @@
import type { DirectoryType } from "@boxyhq/saml-jackson";
import jackson from "@calcom/features/ee/sso/lib/jackson";
import { canAccess, samlProductID, samlTenantID, tenantPrefix } from "@calcom/features/ee/sso/lib/saml";
import { canAccess, samlProductID, samlTenantID } from "@calcom/features/ee/sso/lib/saml";
import prisma from "@calcom/prisma";
import { TRPCError } from "@trpc/server";
@@ -30,7 +30,9 @@ export const createHandler = async ({ ctx, input }: Options) => {
});
}
const tenant = organizationId ? `${tenantPrefix}${organizationId}` : (samlTenantID as string);
const { organization } = ctx.user;
const tenant = input.organizationId ? `${organization.slug}-${organization.id}` : (samlTenantID as string);
const { data, error } = await dsyncController.directories.create({
tenant,
@@ -1,5 +1,6 @@
import jackson from "@calcom/features/ee/sso/lib/jackson";
import { canAccess, samlProductID, samlTenantID, tenantPrefix } from "@calcom/features/ee/sso/lib/saml";
import { canAccess } from "@calcom/features/ee/sso/lib/saml";
import prisma from "@calcom/prisma";
import { TRPCError } from "@trpc/server";
@@ -26,9 +27,23 @@ export const getHandler = async ({ ctx, input }: Options) => {
});
}
const tenant = input.organizationId ? `${tenantPrefix}${input.organizationId}` : (samlTenantID as string);
// Right now dsync is only available for organizations
if (!input.organizationId) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Organization ID is required",
});
}
const { data, error } = await dsyncController.directories.getByTenantAndProduct(tenant, samlProductID);
const dsyncData = await prisma.dSyncData.findUnique({
where: {
organizationId: input.organizationId,
},
});
if (!dsyncData) return null;
const { data, error } = await dsyncController.directories.get(dsyncData.directoryId);
if (error) {
console.error("Error fetching directory sync connection", error);