fix: hide cal branding for orgs/teams (#27643)

* fix: hide branding for teams

* fix: remove unused organizationId and username fields from profiles select

Addresses Cubic AI review feedback (confidence 9/10) to select only
the profile fields that are actually used. The organizationId and
username fields were fetched but never referenced in this function.

Co-Authored-By: unknown <>

* fix: unit tests

* fix: add prisma named export to test mock

Co-Authored-By: [email protected] <[email protected]>

* Add tests: packages/features/profile/lib/hideBranding.test.ts

Generated by Paragon from proposal for PR #27643

* chore: implement cubic feedback

* fix: merge conflicts

* fix: unit tests

* fixup

* refactor: implement DI pattern for event type service

* fix: atoms build

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-02-25 20:53:01 +09:00
1 parent 4081d11fbe
commit 5d65a0f091
39 files changed
+1129 -154

No files matched your search

@@ -0,0 +1,18 @@
import { createContainer } from "@calcom/features/di/di";
import { prismaModule } from "@calcom/features/di/modules/Prisma";
import { DI_TOKENS } from "@calcom/features/di/tokens";
import {
type EventTypeBrandingData,
type EventTypeService,
moduleLoader as eventTypeServiceModule,
} from "./EventTypeService.module";
const eventTypeServiceContainer = createContainer();
eventTypeServiceContainer.load(DI_TOKENS.PRISMA_MODULE, prismaModule);
export type { EventTypeBrandingData };
export function getEventTypeService(): EventTypeService {
eventTypeServiceModule.loadModule(eventTypeServiceContainer);
return eventTypeServiceContainer.get<EventTypeService>(eventTypeServiceModule.token);
}
@@ -0,0 +1,24 @@
import { bindModuleToClassOnToken, createModule, type ModuleLoader } from "@calcom/features/di/di";
import { moduleLoader as eventTypeRepositoryModuleLoader } from "@calcom/features/di/modules/EventType";
import { DI_TOKENS } from "@calcom/features/di/tokens";
import { EventTypeService } from "../service/EventTypeService";
const thisModule = createModule();
const token = DI_TOKENS.EVENT_TYPE_SERVICE;
const moduleToken = DI_TOKENS.EVENT_TYPE_SERVICE_MODULE;
const loadModule = bindModuleToClassOnToken({
module: thisModule,
moduleToken,
token,
classs: EventTypeService,
dep: eventTypeRepositoryModuleLoader,
});
export const moduleLoader = {
token,
loadModule,
} satisfies ModuleLoader;
export type { EventTypeService };
export type { EventTypeBrandingData } from "../service/EventTypeService";
@@ -0,0 +1,4 @@
export const EVENT_TYPE_DI_TOKENS = {
EVENT_TYPE_SERVICE: Symbol("EventTypeService"),
EVENT_TYPE_SERVICE_MODULE: Symbol("EventTypeServiceModule"),
};
@@ -1771,6 +1771,32 @@ export class EventTypeRepository implements IEventTypesRepository {
});
}
async findByIdIncludeBrandingInfo({ id }: { id: number }) {
return await this.prismaClient.eventType.findUnique({
where: { id },
select: {
id: true,
team: {
select: {
hideBranding: true,
parent: { select: { hideBranding: true } },
},
},
owner: {
select: {
id: true,
hideBranding: true,
profiles: {
select: {
organization: { select: { hideBranding: true } },
},
},
},
},
},
});
}
async getEventTypeList({
teamId,
userId,
@@ -0,0 +1,141 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { EventTypeRepository } from "../repositories/eventTypeRepository";
import type { EventTypeBrandingData } from "./EventTypeService";
import { EventTypeService } from "./EventTypeService";
function createMockRepository(overrides: Partial<EventTypeRepository> = {}): EventTypeRepository {
return {
findByIdIncludeBrandingInfo: vi.fn().mockResolvedValue(null),
...overrides,
} as unknown as EventTypeRepository;
}
describe("EventTypeService", () => {
let service: EventTypeService;
let mockRepo: EventTypeRepository;
beforeEach(() => {
mockRepo = createMockRepository();
service = new EventTypeService(mockRepo);
});
describe("shouldHideBrandingForEventType", () => {
describe("hot path (prefetchedData provided)", () => {
it("returns false when prefetchedData has no team and no owner", async () => {
const prefetchedData: EventTypeBrandingData = { team: null, owner: null };
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(false);
expect(mockRepo.findByIdIncludeBrandingInfo).not.toHaveBeenCalled();
});
it("returns true when team has hideBranding enabled", async () => {
const prefetchedData: EventTypeBrandingData = {
team: { hideBranding: true, parent: null },
owner: null,
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(true);
expect(mockRepo.findByIdIncludeBrandingInfo).not.toHaveBeenCalled();
});
it("returns true when team parent (organization) has hideBranding enabled", async () => {
const prefetchedData: EventTypeBrandingData = {
team: { hideBranding: false, parent: { hideBranding: true } },
owner: null,
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(true);
});
it("returns false when team hideBranding is false and no parent", async () => {
const prefetchedData: EventTypeBrandingData = {
team: { hideBranding: false, parent: null },
owner: null,
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(false);
});
it("returns true when owner has hideBranding enabled", async () => {
const prefetchedData: EventTypeBrandingData = {
team: null,
owner: { id: 42, hideBranding: true, profiles: [] },
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(true);
});
it("returns true when owner's organization has hideBranding enabled", async () => {
const prefetchedData: EventTypeBrandingData = {
team: null,
owner: {
id: 42,
hideBranding: false,
profiles: [{ organization: { hideBranding: true } }],
},
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(true);
});
it("returns false when owner hideBranding is false and no org branding", async () => {
const prefetchedData: EventTypeBrandingData = {
team: null,
owner: {
id: 42,
hideBranding: false,
profiles: [{ organization: { hideBranding: false } }],
},
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(false);
});
it("handles owner with empty profiles array", async () => {
const prefetchedData: EventTypeBrandingData = {
team: null,
owner: { id: 42, hideBranding: false, profiles: [] },
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(false);
});
it("prioritises team over owner when both provided", async () => {
const prefetchedData: EventTypeBrandingData = {
team: { hideBranding: true, parent: null },
owner: { id: 42, hideBranding: false, profiles: [] },
};
const result = await service.shouldHideBrandingForEventType(1, prefetchedData);
expect(result).toBe(true);
});
});
describe("cold path (no prefetchedData)", () => {
it("fetches from repository when no prefetchedData is provided", async () => {
const repoData = {
team: { hideBranding: true, parent: null },
owner: null,
};
mockRepo = createMockRepository({
findByIdIncludeBrandingInfo: vi.fn().mockResolvedValue(repoData),
});
service = new EventTypeService(mockRepo);
const result = await service.shouldHideBrandingForEventType(99);
expect(result).toBe(true);
expect(mockRepo.findByIdIncludeBrandingInfo).toHaveBeenCalledWith({ id: 99 });
});
it("returns false when repository returns null", async () => {
mockRepo = createMockRepository({
findByIdIncludeBrandingInfo: vi.fn().mockResolvedValue(null),
});
service = new EventTypeService(mockRepo);
const result = await service.shouldHideBrandingForEventType(99);
expect(result).toBe(false);
expect(mockRepo.findByIdIncludeBrandingInfo).toHaveBeenCalledWith({ id: 99 });
});
});
});
});
@@ -0,0 +1,55 @@
import { shouldHideBrandingForEventUsingProfile } from "@calcom/features/profile/lib/hideBranding";
import type { EventTypeRepository } from "../repositories/eventTypeRepository";
/**
* Shape of pre-fetched branding data that callers can pass to avoid a DB query.
* Matches the raw DB shape returned by EventTypeRepository.findByIdIncludeBrandingInfo.
*/
export type EventTypeBrandingData = {
team: {
hideBranding: boolean | null;
parent: { hideBranding: boolean | null } | null;
} | null;
owner: {
id: number;
hideBranding: boolean | null;
profiles: Array<{ organization: { hideBranding: boolean | null } | null }>;
} | null;
};
export class EventTypeService {
constructor(private eventTypeRepository: EventTypeRepository) {}
/**
* Determines whether branding should be hidden for the given event type.
*
* - **Hot path** (prefetchedData provided): uses the supplied data directly, no DB query.
* - **Cold path** (no prefetchedData): fetches from DB via the repository.
*/
async shouldHideBrandingForEventType(
eventTypeId: number,
prefetchedData?: EventTypeBrandingData
): Promise<boolean> {
const data =
prefetchedData ?? (await this.eventTypeRepository.findByIdIncludeBrandingInfo({ id: eventTypeId }));
if (!data) return false;
return shouldHideBrandingForEventUsingProfile({
eventTypeId,
team: data.team
? {
hideBranding: data.team.hideBranding,
parent: data.team.parent,
}
: null,
owner: data.owner
? {
id: data.owner.id,
hideBranding: data.owner.hideBranding,
profile: data.owner.profiles?.[0] ? { organization: data.owner.profiles[0].organization } : null,
}
: null,
});
}
}