refactor: Enhance event type retrieval logic (#24985)
- Deleted the getEventTypeById.test.ts file as it was no longer needed. - Updated getEventTypeById.ts to include OrganizationRepository for improved organization admin checks. - Refactored event type retrieval logic to ensure proper access control for organization admins.
This commit is contained in:
1 parent
9a52938f64
commit
83488f0a35
3 files changed
+469
-360
No files matched your search
@@ -0,0 +1,430 @@
|
||||
import { prisma } from "@calcom/prisma"
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import i18nMock from "../../../../tests/libs/__mocks__/libServerI18n";
|
||||
|
||||
// import { mockNoTranslations } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
|
||||
|
||||
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
|
||||
|
||||
import { getRawEventType } from "./getEventTypeById";
|
||||
|
||||
export function mockNoTranslations() {
|
||||
console.log("Mocking i18n.getTranslation to return identity function");
|
||||
i18nMock.getTranslation.mockImplementation(() => {
|
||||
return new Promise((resolve) => {
|
||||
const identityFn = (key: string) => key;
|
||||
resolve(identityFn);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
vi.mock("@calcom/lib/server/i18n", () => ({
|
||||
getTranslation: (key: string) => () => key,
|
||||
}));
|
||||
|
||||
describe("getRawEventType", () => {
|
||||
const createdResources: {
|
||||
eventTypes: number[];
|
||||
users: number[];
|
||||
teams: number[];
|
||||
memberships: number[];
|
||||
profiles: number[];
|
||||
} = {
|
||||
eventTypes: [],
|
||||
users: [],
|
||||
teams: [],
|
||||
memberships: [],
|
||||
profiles: [],
|
||||
};
|
||||
|
||||
// Helper functions to create test data
|
||||
const createTestUser = async (overrides?: {
|
||||
organizationId?: number;
|
||||
username?: string;
|
||||
withProfile?: boolean;
|
||||
}) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const username = overrides?.username ?? `testuser-${timestamp}`;
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
email: `testuser-${timestamp}@example.com`,
|
||||
organizationId: overrides?.organizationId,
|
||||
...(overrides?.withProfile &&
|
||||
overrides.organizationId && {
|
||||
profiles: {
|
||||
create: {
|
||||
organizationId: overrides.organizationId,
|
||||
uid: username,
|
||||
username,
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
});
|
||||
createdResources.users.push(user.id);
|
||||
return user;
|
||||
};
|
||||
|
||||
const createTestOrganization = async (overrides?: { isPlatform?: boolean }) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: `Test Organization ${timestamp}`,
|
||||
slug: `test-org-${timestamp}`,
|
||||
isOrganization: true,
|
||||
isPlatform: overrides?.isPlatform ?? false,
|
||||
},
|
||||
});
|
||||
createdResources.teams.push(team.id);
|
||||
return team;
|
||||
};
|
||||
|
||||
const createTestTeam = async (parentId?: number) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: `Test Team ${timestamp}`,
|
||||
slug: `test-team-${timestamp}`,
|
||||
parentId: parentId ?? null,
|
||||
},
|
||||
});
|
||||
createdResources.teams.push(team.id);
|
||||
return team;
|
||||
};
|
||||
|
||||
const createTestOrgAdmin = async (organizationId: number) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `orgadmin-${timestamp}`,
|
||||
email: `orgadmin-${timestamp}@example.com`,
|
||||
organizationId,
|
||||
},
|
||||
});
|
||||
createdResources.users.push(user.id);
|
||||
return user;
|
||||
};
|
||||
|
||||
const createTestTeamMember = async (teamId: number, userId: number) => {
|
||||
const membership = await prisma.membership.create({
|
||||
data: {
|
||||
teamId,
|
||||
userId,
|
||||
role: "MEMBER",
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
createdResources.memberships.push(membership.id);
|
||||
return membership;
|
||||
};
|
||||
|
||||
const createTestEventType = async (
|
||||
userId: number,
|
||||
overrides?: { slug?: string; title?: string }
|
||||
) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const eventType = await prisma.eventType.create({
|
||||
data: {
|
||||
title: overrides?.title ?? "Test Event Type",
|
||||
slug: overrides?.slug ?? `test-event-${timestamp}`,
|
||||
length: 30,
|
||||
userId,
|
||||
users: {
|
||||
connect: [{ id: userId }],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
createdResources.eventTypes.push(eventType.id);
|
||||
return eventType;
|
||||
};
|
||||
|
||||
const createTestTeamEventType = async (teamId: number) => {
|
||||
const timestamp = Date.now() + Math.random();
|
||||
const eventType = await prisma.eventType.create({
|
||||
data: {
|
||||
title: `Team Event ${timestamp}`,
|
||||
slug: `team-event-${timestamp}`,
|
||||
length: 30,
|
||||
teamId,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
createdResources.eventTypes.push(eventType.id);
|
||||
return eventType;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockNoTranslations();
|
||||
// Reset tracking arrays
|
||||
createdResources.eventTypes = [];
|
||||
createdResources.users = [];
|
||||
createdResources.teams = [];
|
||||
createdResources.memberships = [];
|
||||
createdResources.profiles = [];
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up in reverse order to avoid foreign key violations
|
||||
if (createdResources.eventTypes.length > 0) {
|
||||
await prisma.eventType.deleteMany({
|
||||
where: { id: { in: createdResources.eventTypes } },
|
||||
});
|
||||
}
|
||||
if (createdResources.memberships.length > 0) {
|
||||
await prisma.membership.deleteMany({
|
||||
where: { id: { in: createdResources.memberships } },
|
||||
});
|
||||
}
|
||||
if (createdResources.profiles.length > 0) {
|
||||
await prisma.profile.deleteMany({
|
||||
where: { id: { in: createdResources.profiles } },
|
||||
});
|
||||
}
|
||||
if (createdResources.teams.length > 0) {
|
||||
await prisma.team.deleteMany({
|
||||
where: { id: { in: createdResources.teams } },
|
||||
});
|
||||
}
|
||||
if (createdResources.users.length > 0) {
|
||||
await prisma.user.deleteMany({
|
||||
where: { id: { in: createdResources.users } },
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("Regular user access", () => {
|
||||
test("should fetch event type when user owns it", async () => {
|
||||
const user = await createTestUser();
|
||||
const eventType = await createTestEventType(user.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: user.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: false,
|
||||
currentOrganizationId: null,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.title).toBe("Test Event Type");
|
||||
expect(result?.userId).toBe(user.id);
|
||||
});
|
||||
|
||||
test.skip("should return null when user doesn't have access to event type", async () => {
|
||||
// note(Lauris): test skipped because somehow when creating event type eventType.users includes otherUser
|
||||
const owner = await prisma.user.create({
|
||||
data: {
|
||||
username: "owner",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const otherUser = await prisma.user.create({
|
||||
data: {
|
||||
username: "otheruser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prisma.eventType.create({
|
||||
data: {
|
||||
title: "Owner's Event Type",
|
||||
slug: "owner-event",
|
||||
length: 30,
|
||||
userId: owner.id,
|
||||
users: {
|
||||
connect: [{ id: owner.id }],
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
id: otherUser.id,
|
||||
},
|
||||
data: {
|
||||
eventTypes: {
|
||||
disconnect: [{ id: eventType.id }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: otherUser.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: false,
|
||||
currentOrganizationId: null,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Organization admin access", () => {
|
||||
test("should fetch team event type when user is org admin and is a team member", async () => {
|
||||
const organization = await createTestOrganization();
|
||||
const team = await createTestTeam(organization.id);
|
||||
const orgAdmin = await createTestOrgAdmin(organization.id);
|
||||
await createTestTeamMember(team.id, orgAdmin.id);
|
||||
const eventType = await createTestTeamEventType(team.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: organization.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.title).toContain("Team Event");
|
||||
expect(result?.teamId).toBe(team.id);
|
||||
});
|
||||
|
||||
test("should return null when org admin tries to access event type from different org", async () => {
|
||||
const org1 = await createTestOrganization();
|
||||
const org2 = await createTestOrganization();
|
||||
const team1 = await createTestTeam(org1.id);
|
||||
const org2Admin = await createTestOrgAdmin(org2.id);
|
||||
const eventType = await createTestTeamEventType(team1.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: org2Admin.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: org2.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test("should fallback to regular user access when org admin flag is true but no organizationId", async () => {
|
||||
const user = await createTestUser();
|
||||
const eventType = await createTestEventType(user.id, { title: "Regular User Event" });
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: user.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: null,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.userId).toBe(user.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when user is platform organization admin", () => {
|
||||
test("should access any team event type within the platform organization", async () => {
|
||||
const platformOrg = await createTestOrganization({ isPlatform: true });
|
||||
const orgSubTeam = await createTestTeam(platformOrg.id);
|
||||
const platformAdmin = await createTestOrgAdmin(platformOrg.id);
|
||||
const teamEvent = await createTestTeamEventType(orgSubTeam.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: platformAdmin.id,
|
||||
eventTypeId: teamEvent.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: platformOrg.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(teamEvent.id);
|
||||
expect(result?.teamId).toBe(orgSubTeam.id);
|
||||
});
|
||||
|
||||
test("should access user event types within the platform organization", async () => {
|
||||
const platformOrg = await createTestOrganization({ isPlatform: true });
|
||||
const platformAdmin = await createTestOrgAdmin(platformOrg.id);
|
||||
const orgUser = await createTestUser({ organizationId: platformOrg.id, withProfile: true });
|
||||
const userEvent = await createTestEventType(orgUser.id, { title: "Platform User Event" });
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: platformAdmin.id,
|
||||
eventTypeId: userEvent.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: platformOrg.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(userEvent.id);
|
||||
expect(result?.userId).toBe(orgUser.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when user is non-platform organization admin", () => {
|
||||
test("should access regular team event when admin is a team member", async () => {
|
||||
const regularOrg = await createTestOrganization({ isPlatform: false });
|
||||
const standaloneTeam = await createTestTeam();
|
||||
const orgAdmin = await createTestOrgAdmin(regularOrg.id);
|
||||
await createTestTeamMember(standaloneTeam.id, orgAdmin.id);
|
||||
const teamEvent = await createTestTeamEventType(standaloneTeam.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: teamEvent.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: regularOrg.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(teamEvent.id);
|
||||
expect(result?.teamId).toBe(standaloneTeam.id);
|
||||
});
|
||||
|
||||
test("should not access regular team event when admin is not a team member", async () => {
|
||||
const regularOrg = await createTestOrganization({ isPlatform: false });
|
||||
const standaloneTeam = await createTestTeam();
|
||||
const orgAdmin = await createTestOrgAdmin(regularOrg.id);
|
||||
const teamEvent = await createTestTeamEventType(standaloneTeam.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: teamEvent.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: regularOrg.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test("should not access organization sub-team event when admin is not a team member", async () => {
|
||||
const regularOrg = await createTestOrganization({ isPlatform: false });
|
||||
const orgSubTeam = await createTestTeam(regularOrg.id);
|
||||
const orgAdmin = await createTestOrgAdmin(regularOrg.id);
|
||||
const subTeamEvent = await createTestTeamEventType(orgSubTeam.id);
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: subTeamEvent.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: regularOrg.id,
|
||||
prisma: prisma as unknown as PrismaClient,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,327 +0,0 @@
|
||||
import prismock from "../../../../tests/libs/__mocks__/prisma";
|
||||
|
||||
import { mockNoTranslations } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
|
||||
|
||||
import { describe, test, expect, beforeEach, vi } from "vitest";
|
||||
|
||||
import { getRawEventType } from "./getEventTypeById";
|
||||
|
||||
vi.mock("@calcom/lib/server/i18n", () => ({
|
||||
getTranslation: (key: string) => () => key,
|
||||
}));
|
||||
|
||||
describe("getRawEventType", () => {
|
||||
beforeEach(() => {
|
||||
mockNoTranslations();
|
||||
});
|
||||
|
||||
describe("Regular user access", () => {
|
||||
test("should fetch event type when user owns it", async () => {
|
||||
const user = await prismock.user.create({
|
||||
data: {
|
||||
username: "testuser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Test Event Type",
|
||||
slug: "test-event",
|
||||
length: 30,
|
||||
userId: user.id,
|
||||
users: {
|
||||
connect: [{ id: user.id }],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: user.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: false,
|
||||
currentOrganizationId: null,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.title).toBe("Test Event Type");
|
||||
expect(result?.userId).toBe(user.id);
|
||||
});
|
||||
|
||||
test.skip("should return null when user doesn't have access to event type", async () => {
|
||||
// note(Lauris): test skipped because somehow when creating event type eventType.users includes otherUser
|
||||
const owner = await prismock.user.create({
|
||||
data: {
|
||||
username: "owner",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const otherUser = await prismock.user.create({
|
||||
data: {
|
||||
username: "otheruser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Owner's Event Type",
|
||||
slug: "owner-event",
|
||||
length: 30,
|
||||
userId: owner.id,
|
||||
users: {
|
||||
connect: [{ id: owner.id }],
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prismock.user.update({
|
||||
where: {
|
||||
id: otherUser.id,
|
||||
},
|
||||
data: {
|
||||
eventTypes: {
|
||||
disconnect: [{ id: eventType.id }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: otherUser.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: false,
|
||||
currentOrganizationId: null,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Organization admin access", () => {
|
||||
test("should fetch team event type when user is org admin", async () => {
|
||||
const organization = await prismock.team.create({
|
||||
data: {
|
||||
id: 100,
|
||||
name: "Test Organization",
|
||||
slug: "test-org",
|
||||
isOrganization: true,
|
||||
},
|
||||
});
|
||||
|
||||
const team = await prismock.team.create({
|
||||
data: {
|
||||
id: 200,
|
||||
name: "Test Team",
|
||||
slug: "test-team",
|
||||
parentId: organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
const orgAdmin = await prismock.user.create({
|
||||
data: {
|
||||
username: "orgadmin",
|
||||
email: "[email protected]",
|
||||
organizationId: organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Team Event Type",
|
||||
slug: "team-event",
|
||||
length: 30,
|
||||
teamId: team.id,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: organization.id,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.title).toBe("Team Event Type");
|
||||
expect(result?.teamId).toBe(team.id);
|
||||
});
|
||||
|
||||
test("should fetch user event type when user is org admin and user belongs to org", async () => {
|
||||
const organization = await prismock.team.create({
|
||||
data: {
|
||||
id: 101,
|
||||
name: "Test Organization 2",
|
||||
slug: "test-org-2",
|
||||
isOrganization: true,
|
||||
},
|
||||
});
|
||||
|
||||
const orgAdmin = await prismock.user.create({
|
||||
data: {
|
||||
username: "orgadmin2",
|
||||
email: "[email protected]",
|
||||
organizationId: organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
const orgUser = await prismock.user.create({
|
||||
data: {
|
||||
username: "orguser",
|
||||
email: "[email protected]",
|
||||
organizationId: organization.id,
|
||||
profiles: {
|
||||
create: {
|
||||
organizationId: organization.id,
|
||||
uid: "orguser",
|
||||
username: "orguser",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Org User Event Type",
|
||||
slug: "org-user-event",
|
||||
length: 30,
|
||||
userId: orgUser.id,
|
||||
users: {
|
||||
connect: [{ id: orgUser.id }],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
users: true,
|
||||
owner: true,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: orgAdmin.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: organization.id,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.title).toBe("Org User Event Type");
|
||||
expect(result?.userId).toBe(orgUser.id);
|
||||
});
|
||||
|
||||
test("should return null when org admin tries to access event type from different org", async () => {
|
||||
const org1 = await prismock.team.create({
|
||||
data: {
|
||||
id: 102,
|
||||
name: "Organization 1",
|
||||
slug: "org-1",
|
||||
isOrganization: true,
|
||||
},
|
||||
});
|
||||
|
||||
const org2 = await prismock.team.create({
|
||||
data: {
|
||||
id: 103,
|
||||
name: "Organization 2",
|
||||
slug: "org-2",
|
||||
isOrganization: true,
|
||||
},
|
||||
});
|
||||
|
||||
const team1 = await prismock.team.create({
|
||||
data: {
|
||||
id: 201,
|
||||
name: "Team in Org 1",
|
||||
slug: "team-org-1",
|
||||
parentId: org1.id,
|
||||
},
|
||||
});
|
||||
|
||||
const org2Admin = await prismock.user.create({
|
||||
data: {
|
||||
username: "org2admin",
|
||||
email: "[email protected]",
|
||||
organizationId: org2.id,
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Org 1 Team Event",
|
||||
slug: "org1-team-event",
|
||||
length: 30,
|
||||
teamId: team1.id,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
users: true,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: org2Admin.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: org2.id,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test("should fallback to regular user access when org admin flag is true but no organizationId", async () => {
|
||||
const user = await prismock.user.create({
|
||||
data: {
|
||||
username: "regularuser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const eventType = await prismock.eventType.create({
|
||||
data: {
|
||||
title: "Regular User Event",
|
||||
slug: "regular-user-event",
|
||||
length: 30,
|
||||
userId: user.id,
|
||||
users: {
|
||||
connect: [{ id: user.id }],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
users: true,
|
||||
owner: true,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await getRawEventType({
|
||||
userId: user.id,
|
||||
eventTypeId: eventType.id,
|
||||
isUserOrganizationAdmin: true,
|
||||
currentOrganizationId: null,
|
||||
prisma: prismock as any,
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.id).toBe(eventType.id);
|
||||
expect(result?.userId).toBe(user.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -18,8 +18,9 @@ import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { SchedulingType, MembershipRole } from "@calcom/prisma/enums";
|
||||
import { customInputSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
|
||||
|
||||
interface getEventTypeByIdProps {
|
||||
eventTypeId: number;
|
||||
@@ -57,7 +58,7 @@ export const getEventTypeById = async ({
|
||||
eventTypeId,
|
||||
isUserOrganizationAdmin,
|
||||
currentOrganizationId,
|
||||
prisma,
|
||||
prisma
|
||||
});
|
||||
|
||||
if (!rawEventType) {
|
||||
@@ -89,8 +90,8 @@ export const getEventTypeById = async ({
|
||||
...child,
|
||||
owner: child.owner
|
||||
? await userRepo.enrichUserWithItsProfile({
|
||||
user: child.owner,
|
||||
})
|
||||
user: child.owner,
|
||||
})
|
||||
: null,
|
||||
});
|
||||
}
|
||||
@@ -135,24 +136,24 @@ export const getEventTypeById = async ({
|
||||
bookerUrl: restEventType.team
|
||||
? await getBookerBaseUrl(restEventType.team.parentId)
|
||||
: restEventType.owner
|
||||
? await getBookerBaseUrl(currentOrganizationId)
|
||||
: WEBSITE_URL,
|
||||
? await getBookerBaseUrl(currentOrganizationId)
|
||||
: WEBSITE_URL,
|
||||
children: childrenWithUserProfile.flatMap((ch) =>
|
||||
ch.owner !== null
|
||||
? {
|
||||
...ch,
|
||||
owner: {
|
||||
...ch.owner,
|
||||
avatar: getUserAvatarUrl(ch.owner),
|
||||
email: ch.owner.email,
|
||||
name: ch.owner.name ?? "",
|
||||
username: ch.owner.username ?? "",
|
||||
membership:
|
||||
restEventType.team?.members.find((tm) => tm.user.id === ch.owner?.id)?.role ||
|
||||
MembershipRole.MEMBER,
|
||||
},
|
||||
created: true,
|
||||
}
|
||||
...ch,
|
||||
owner: {
|
||||
...ch.owner,
|
||||
avatar: getUserAvatarUrl(ch.owner),
|
||||
email: ch.owner.email,
|
||||
name: ch.owner.name ?? "",
|
||||
username: ch.owner.username ?? "",
|
||||
membership:
|
||||
restEventType.team?.members.find((tm) => tm.user.id === ch.owner?.id)?.role ||
|
||||
MembershipRole.MEMBER,
|
||||
},
|
||||
created: true,
|
||||
}
|
||||
: []
|
||||
),
|
||||
};
|
||||
@@ -223,19 +224,19 @@ export const getEventTypeById = async ({
|
||||
const isOrgEventType = !!eventTypeObject.team?.parentId;
|
||||
const teamMembers = eventTypeObject.team
|
||||
? eventTeamMembershipsWithUserProfile
|
||||
.filter((member) => member.accepted || isOrgEventType)
|
||||
.map((member) => {
|
||||
const user: typeof member.user & { avatar: string } = {
|
||||
...member.user,
|
||||
avatar: getUserAvatarUrl(member.user),
|
||||
};
|
||||
return {
|
||||
...user,
|
||||
profileId: user.profile.id,
|
||||
eventTypes: user.eventTypes.map((evTy) => evTy.slug),
|
||||
membership: member.role,
|
||||
};
|
||||
})
|
||||
.filter((member) => member.accepted || isOrgEventType)
|
||||
.map((member) => {
|
||||
const user: typeof member.user & { avatar: string } = {
|
||||
...member.user,
|
||||
avatar: getUserAvatarUrl(member.user),
|
||||
};
|
||||
return {
|
||||
...user,
|
||||
profileId: user.profile.id,
|
||||
eventTypes: user.eventTypes.map((evTy) => evTy.slug),
|
||||
membership: member.role,
|
||||
};
|
||||
})
|
||||
: [];
|
||||
|
||||
// Find the current users membership so we can check role to enable/disable deletion.
|
||||
@@ -272,14 +273,19 @@ export async function getRawEventType({
|
||||
prisma,
|
||||
}: Omit<getEventTypeByIdProps, "isTrpcCall">) {
|
||||
const eventTypeRepo = new EventTypeRepository(prisma);
|
||||
const organizationRepo = getOrganizationRepository();
|
||||
const isUserInPlatformOrganization = currentOrganizationId ? !!(await organizationRepo.findById({ id: currentOrganizationId }))?.isPlatform : false;
|
||||
|
||||
if (isUserOrganizationAdmin && currentOrganizationId) {
|
||||
if (isUserOrganizationAdmin && currentOrganizationId && isUserInPlatformOrganization) {
|
||||
// Platform Organization Admin can access any event of the organization even without being a member of the sub-teams
|
||||
return await eventTypeRepo.findByIdForOrgAdmin({
|
||||
id: eventTypeId,
|
||||
organizationId: currentOrganizationId,
|
||||
});
|
||||
}
|
||||
|
||||
// Regular(Non Platform) Organization member(admin/non-admin) can access any event-type they are are a member of including sub-team events and Regular Team(non-subteam) events.
|
||||
// Remember an organization member can stay a part of Regular Team still if that team hasn't been moved to the organization yet.
|
||||
return await eventTypeRepo.findById({
|
||||
id: eventTypeId,
|
||||
userId,
|
||||
|
||||
Reference in new issue
Block a user