refactor: Remove circular deps between @calcom/lib and @calcom/features [1] (#24399)

* add eslint config

* migrate autoLock to features

* migrate teamService to features

* migrate userCreationService

* migrate insights services to features

* migrate ProfileRepository

* update imports

* migrate filter segmen tests

* migrate filter segment repository

* migrate getBusyTimes

* migrate getLocaleFromRequest

* refactor csvUtils

* make filename clearer

* migrate getLuckyUser integration test

* migrate autoLock test to features

* wip

* refactors

* migrate useBookerUrl

* migrate more

* wip

* Migrate eventTypeRepository

* membership repository

* update imports

* update imports

* migrate

* move organization repository

* update imports

* update imports

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix tests

* fix type checks

* fix

* fix

* migrate

* update imports

* fix tests

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
This commit is contained in:
Benny Joo authored and GitHub committed 2025-10-13 12:01:02 -03:00
1 parent 1dc20e3aec
commit ff38d6c7db
339 files changed
+876 -833

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
import type { TGetTranscriptAccessLink } from "@calcom/app-store/dailyvideo/zod";
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/trpc/utils";
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
import type { TimeUnit, WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { CalendarEvent, Person } from "@calcom/types/Calendar";
@@ -1,4 +1,4 @@
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/trpc/utils";
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { FormSubmittedDTO, FormSubmittedNoEventDTO } from "../dto/types";
@@ -1,4 +1,5 @@
import type { WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { WebhookTriggerEvents, UserPermissionRole } from "@calcom/prisma/enums";
import type { Webhook } from "@calcom/prisma/client";
import type { WebhookSubscriber } from "../dto/types";
@@ -11,6 +12,48 @@ export interface GetSubscribersOptions {
oAuthClientId?: string | null;
}
type WebhookGroup = {
teamId?: number | null;
profile: {
slug: string | null;
name: string | null;
image?: string;
};
metadata?: {
canModify: boolean;
canDelete: boolean;
};
webhooks: Webhook[];
};
export interface IWebhookRepository {
getSubscribers(options: GetSubscribersOptions): Promise<WebhookSubscriber[]>;
getWebhookById(id: string): Promise<WebhookSubscriber | null>;
findByWebhookId(webhookId?: string): Promise<{
id: string;
subscriberUrl: string;
payloadTemplate: string | null;
active: boolean;
eventTriggers: WebhookTriggerEvents[];
secret: string | null;
teamId: number | null;
userId: number | null;
platform: boolean;
time: number | null;
timeUnit: string | null;
}>;
getFilteredWebhooksForUser(options: {
userId: number;
userRole?: UserPermissionRole;
}): Promise<{
webhookGroups: WebhookGroup[];
profiles: {
teamId: number | null | undefined;
slug: string | null;
name: string | null;
image?: string | undefined;
canModify?: boolean;
canDelete?: boolean;
}[];
}>;
}
@@ -1,7 +1,12 @@
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
import { withReporting } from "@calcom/lib/sentryWrapper";
import { prisma as defaultPrisma } from "@calcom/prisma";
import type { PrismaClient } from "@calcom/prisma";
import type { Webhook } from "@calcom/prisma/client";
import type { TimeUnit, WebhookTriggerEvents } from "@calcom/prisma/enums";
import { UserPermissionRole, MembershipRole } from "@calcom/prisma/enums";
import type { WebhookSubscriber } from "../dto/types";
import type { IWebhookRepository } from "../interface/repository";
@@ -20,9 +25,42 @@ interface WebhookQueryResult {
priority: number; // This field is added by the query and removed before returning
}
type WebhookGroup = {
teamId?: number | null;
profile: {
slug: string | null;
name: string | null;
image?: string;
};
metadata?: {
canModify: boolean;
canDelete: boolean;
};
webhooks: Webhook[];
};
const filterWebhooks = (webhook: Webhook) => {
const appIds = [
"zapier",
"make",
// Add more if needed
];
return !appIds.some((appId: string) => webhook.appId == appId);
};
export class WebhookRepository implements IWebhookRepository {
constructor(private prisma: PrismaClient = defaultPrisma) {}
private static _instance: WebhookRepository;
static getInstance(): WebhookRepository {
if (!WebhookRepository._instance) {
WebhookRepository._instance = new WebhookRepository();
}
return WebhookRepository._instance;
}
async getSubscribers(options: GetSubscribersOptions): Promise<WebhookSubscriber[]> {
const teamId = options.teamId;
const userId = options.userId;
@@ -201,9 +239,164 @@ export class WebhookRepository implements IWebhookRepository {
eventTriggers: webhook.eventTriggers,
};
}
async findByWebhookId(webhookId?: string) {
return await this.prisma.webhook.findUniqueOrThrow({
where: {
id: webhookId,
},
select: {
id: true,
subscriberUrl: true,
payloadTemplate: true,
active: true,
eventTriggers: true,
secret: true,
teamId: true,
userId: true,
platform: true,
time: true,
timeUnit: true,
},
});
}
async getFilteredWebhooksForUser({ userId, userRole }: { userId: number; userRole?: UserPermissionRole }) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
username: true,
name: true,
avatarUrl: true,
webhooks: true,
teams: {
where: {
accepted: true,
},
select: {
role: true,
team: {
select: {
id: true,
name: true,
slug: true,
logoUrl: true,
webhooks: true,
},
},
},
},
},
});
if (!user) {
throw new Error("User not found");
}
// Use permission service which handles both PBAC and role-based fallbacks
const permissionService = new PermissionCheckService();
// Build webhook groups with proper permissions
const webhookGroups: WebhookGroup[] = [];
// Add user's personal webhooks
webhookGroups.push({
teamId: null,
profile: {
slug: user.username,
name: user.name,
image: getUserAvatarUrl({ avatarUrl: user.avatarUrl }),
},
webhooks: user.webhooks.filter(filterWebhooks),
metadata: {
canModify: true,
canDelete: true,
},
});
// Check permissions for each team
// The permission service handles PBAC when enabled and falls back to role-based permissions
for (const membership of user.teams) {
const teamId = membership.team.id;
// Check read permission (fallback: MEMBER, ADMIN, OWNER can read)
const canRead = await permissionService.checkPermission({
userId,
teamId,
permission: "webhook.read",
fallbackRoles: [MembershipRole.MEMBER, MembershipRole.ADMIN, MembershipRole.OWNER],
});
if (!canRead) {
// User doesn't have permission to view this team's webhooks
continue;
}
// Check update/delete permissions in parallel (fallback: only ADMIN, OWNER can modify)
const [canUpdate, canDelete] = await Promise.all([
permissionService.checkPermission({
userId,
teamId,
permission: "webhook.update",
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
}),
permissionService.checkPermission({
userId,
teamId,
permission: "webhook.delete",
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
}),
]);
webhookGroups.push({
teamId: membership.team.id,
profile: {
name: membership.team.name,
slug: membership.team.slug || null,
image: getPlaceholderAvatar(membership.team.logoUrl, membership.team.name),
},
webhooks: membership.team.webhooks.filter(filterWebhooks),
metadata: {
canModify: canUpdate,
canDelete,
},
});
}
// Add platform webhooks for admins
if (userRole === UserPermissionRole.ADMIN) {
const platformWebhooks = await this.prisma.webhook.findMany({
where: { platform: true },
});
webhookGroups.push({
teamId: null,
profile: {
slug: "Platform",
name: "Platform",
image: getPlaceholderAvatar(null, "Platform"),
},
webhooks: platformWebhooks,
metadata: {
canDelete: true,
canModify: true,
},
});
}
return {
webhookGroups: webhookGroups.filter((group) => group.webhooks.length > 0),
profiles: webhookGroups.map((group) => ({
teamId: group.teamId,
...group.profile,
...group.metadata,
})),
};
}
}
export const webhookRepository = withReporting(
(options: GetSubscribersOptions) => new WebhookRepository().getSubscribers(options),
(options: GetSubscribersOptions) => WebhookRepository.getInstance().getSubscribers(options),
"WebhookRepository.getSubscribers"
);
@@ -1,4 +1,4 @@
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/trpc/utils";
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
import dayjs from "@calcom/dayjs";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
@@ -2,9 +2,9 @@
import { useRouter } from "next/navigation";
import { useBookerUrl } from "@calcom/features/bookings/hooks/useBookerUrl";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { APP_NAME, WEBAPP_URL } from "@calcom/lib/constants";
import { useBookerUrl } from "@calcom/lib/hooks/useBookerUrl";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import type { RouterOutputs } from "@calcom/trpc/react";
import classNames from "@calcom/ui/classNames";