chore: Add initial webhook wiring (TRPC handlers) to the new architecture (#27170)

* --init

* clean up

* unnecessary comment

* remove comment

* fix factory resolver

* Add active filter to webhook query
This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2026-01-23 16:50:34 +04:00
1 parent 646d18e286
commit 4115eaacbd
14 files changed
+142 -77

No files matched your search

@@ -1,6 +1,5 @@
import type { WebhookTriggerEvents, UserPermissionRole } from "@calcom/prisma/enums";
import type { Webhook, WebhookSubscriber, WebhookGroup } from "../dto/types";
import type { UserPermissionRole, WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { Webhook, WebhookGroup, WebhookSubscriber } from "../dto/types";
/**
* Webhook Version enum - defines the payload format versions.
@@ -8,20 +7,20 @@ import type { Webhook, WebhookSubscriber, WebhookGroup } from "../dto/types";
* This is a TypeScript-only enum (not Prisma).
* DB operations go through the repository which enforces these values.
*/
export const WebhookVersion = {
const WebhookVersion = {
V_2021_10_20: "2021-10-20",
} as const;
export type WebhookVersion = (typeof WebhookVersion)[keyof typeof WebhookVersion];
type WebhookVersion = (typeof WebhookVersion)[keyof typeof WebhookVersion];
/**
* Default webhook version - used for new webhooks and as fallback
*/
export const DEFAULT_WEBHOOK_VERSION = WebhookVersion.V_2021_10_20;
const VALID_WEBHOOK_VERSIONS = new Set<string>(Object.values(WebhookVersion));
const DEFAULT_WEBHOOK_VERSION: WebhookVersion = WebhookVersion.V_2021_10_20;
const VALID_WEBHOOK_VERSIONS: Set<string> = new Set<string>(Object.values(WebhookVersion));
export { WebhookVersion, DEFAULT_WEBHOOK_VERSION, VALID_WEBHOOK_VERSIONS };
export function isValidWebhookVersion(value: string): value is WebhookVersion {
return VALID_WEBHOOK_VERSIONS.has(value);
}
@@ -71,6 +70,10 @@ export interface IWebhookRepository {
timeUnit: string | null;
version: WebhookVersion;
}>;
findByOrgIdAndTrigger(options: {
orgId: number;
triggerEvent: WebhookTriggerEvents;
}): Promise<WebhookSubscriber[]>;
getFilteredWebhooksForUser(options: { userId: number; userRole?: UserPermissionRole }): Promise<{
webhookGroups: WebhookGroup[];
profiles: {
@@ -84,4 +87,3 @@ export interface IWebhookRepository {
}>;
listWebhooks(options: ListWebhooksOptions): Promise<Webhook[]>;
}
@@ -1,4 +1,8 @@
import type { IEventTypesRepository } from "@calcom/features/eventtypes/eventtypes.repository.interface";
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { UsersRepository } from "@calcom/features/users/users.repository";
import type { IUsersRepository } from "@calcom/features/users/users.repository.interface";
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
import type { PrismaClient } from "@calcom/prisma";
@@ -30,7 +34,7 @@ interface WebhookQueryResult {
priority: number; // This field is added by the query and removed before returning
}
const filterWebhooks = (webhook: { appId: string | null }) => {
const filterWebhooks = (webhook: { appId: string | null }): boolean => {
const appIds = [
"zapier",
"make",
@@ -41,17 +45,30 @@ const filterWebhooks = (webhook: { appId: string | null }) => {
};
export class WebhookRepository implements IWebhookRepository {
private static _instance: WebhookRepository;
private static _instance: WebhookRepository | undefined;
constructor(private readonly prisma: typeof defaultPrisma = defaultPrisma) {}
constructor(
private readonly prisma: PrismaClient,
private readonly eventTypeRepository: IEventTypesRepository,
private readonly userRepository: IUsersRepository
) {}
/**
* Singleton accessor for backward compatibility.
* @deprecated Use DI container (getWebhookFeature().repository) instead
*
* @deprecated Use DI container instead:
* ```typescript
* import { getWebhookFeature } from "@calcom/features/di/webhooks/containers/webhook";
* const { repository } = getWebhookFeature();
* ```
*
*/
static getInstance(): WebhookRepository {
if (!WebhookRepository._instance) {
WebhookRepository._instance = new WebhookRepository(defaultPrisma);
WebhookRepository._instance = new WebhookRepository(
defaultPrisma,
new EventTypeRepository(defaultPrisma),
new UsersRepository()
);
}
return WebhookRepository._instance;
}
@@ -66,18 +83,8 @@ export class WebhookRepository implements IWebhookRepository {
let managedParentEventTypeId: number | undefined;
if (eventTypeId) {
const managedChildEventType = await this.prisma.eventType.findFirst({
where: {
id: eventTypeId,
parentId: {
not: null,
},
},
select: {
parentId: true,
},
});
managedParentEventTypeId = managedChildEventType?.parentId ?? undefined;
managedParentEventTypeId =
(await this.eventTypeRepository.findParentEventTypeId(eventTypeId)) ?? undefined;
}
const webhooks = await this.getSubscribersRaw({
@@ -277,6 +284,7 @@ export class WebhookRepository implements IWebhookRepository {
teamId: orgId,
platform: false,
eventTriggers: { has: triggerEvent },
active: true,
},
select: {
id: true,
@@ -508,26 +516,15 @@ export class WebhookRepository implements IWebhookRepository {
{ appId: appId ?? null },
];
// Get user's teams
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: { teams: true },
});
const user = await this.userRepository.findUserTeams(userId);
if (eventTypeId) {
// Check for managed event type parent
const managedParentEvt = await this.prisma.eventType.findFirst({
where: {
id: eventTypeId,
parentId: { not: null },
},
select: { parentId: true },
});
const managedParentId = await this.eventTypeRepository.findParentEventTypeId(eventTypeId);
if (managedParentEvt?.parentId) {
if (managedParentId) {
// Include webhooks from both the event type and its parent (if active)
whereConditions.push({
OR: [{ eventTypeId }, { eventTypeId: managedParentEvt.parentId, active: true }],
OR: [{ eventTypeId }, { eventTypeId: managedParentId, active: true }],
});
} else {
whereConditions.push({ eventTypeId });
@@ -1,10 +1,9 @@
import { DelegationCredentialRepository } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { DelegationCredentialErrorPayloadType } from "@calcom/features/webhooks/lib/dto/types";
import { getWebhookFeature } from "@calcom/features/di/webhooks/containers/webhook";
import type { DelegationCredentialErrorPayloadType } from "@calcom/features/webhooks/lib/dto/types";
import type { CalendarAppDelegationCredentialError } from "@calcom/lib/CalendarAppError";
import logger from "@calcom/lib/logger";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
import { WebhookRepository } from "./repository/WebhookRepository";
import sendPayload from "./sendPayload";
const log = logger.getSubLogger({ prefix: ["triggerDelegationCredentialErrorWebhook"] });
@@ -30,7 +29,7 @@ export async function triggerDelegationCredentialErrorWebhook(params: {
return;
}
const webhookRepository = WebhookRepository.getInstance();
const { repository: webhookRepository } = getWebhookFeature();
const webhooks = await webhookRepository.findByOrgIdAndTrigger({
orgId: delegationCredential.organizationId,
triggerEvent: WebhookTriggerEvents.DELEGATION_CREDENTIAL_ERROR,