fix: delegation credential error webhooks + refactor repeated code (#25232)

* fix: delegation credential error webhooks

* refactor: extract repeated delegation credential error webhook logic into helper methods

- Added private triggerDelegationCredentialError method in Office365CalendarService class
- Added triggerDelegationCredentialError helper function in TeamsVideoApiAdapter
- Replaced all 4 instances in Office365CalendarService with helper method call
- Replaced all 4 instances in TeamsVideoApiAdapter with helper function call
- Keeps code DRY by eliminating repeated if statement and webhook trigger logic

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-11-18 00:00:42 +02:00
1 parent 7f1b94ad19
commit b8530e2d78
6 files changed
+99 -129

No files matched your search

@@ -498,10 +498,12 @@ export type DelegationCredentialErrorPayloadType = {
id: number;
type: string;
appId: string;
delegationCredentialId?: string;
};
user: {
id: number;
email: string;
organizationId?: number;
};
};
@@ -261,6 +261,36 @@ export class WebhookRepository implements IWebhookRepository {
});
}
async findByOrgIdAndTrigger({
orgId,
triggerEvent,
}: {
orgId: number;
triggerEvent: WebhookTriggerEvents;
}): Promise<WebhookSubscriber[]> {
return await this.prisma.webhook.findMany({
where: {
teamId: orgId,
platform: false,
eventTriggers: { has: triggerEvent },
},
select: {
id: true,
subscriberUrl: true,
payloadTemplate: true,
active: true,
eventTriggers: true,
secret: true,
teamId: true,
userId: true,
platform: true,
time: true,
timeUnit: true,
appId: true,
},
});
}
async getFilteredWebhooksForUser({ userId, userRole }: { userId: number; userRole?: UserPermissionRole }) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
@@ -1,3 +1,4 @@
import { DelegationCredentialRepository } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { DelegationCredentialErrorPayloadType } from "@calcom/features/webhooks/lib/dto/types";
import type { CalendarAppDelegationCredentialError } from "@calcom/lib/CalendarAppError";
import logger from "@calcom/lib/logger";
@@ -15,24 +16,30 @@ export async function triggerDelegationCredentialErrorWebhook(params: {
error: CalendarAppDelegationCredentialError;
credential: DelegationCredentialErrorPayloadType["credential"];
user: DelegationCredentialErrorPayloadType["user"];
orgId?: number | null;
delegationCredentialId: string;
}): Promise<void> {
try {
const { error, credential, user, orgId } = params;
const { error, credential, user, delegationCredentialId } = params;
if (!orgId) {
log.debug("Skipping webhook emission - no organization context");
const delegationCredential = await DelegationCredentialRepository.findById({
id: delegationCredentialId,
});
if (!delegationCredential) {
log.debug("No delegation credential found", { delegationCredentialId });
return;
}
const webhookRepository = WebhookRepository.getInstance();
const webhooks = await webhookRepository.getSubscribers({
teamId: orgId,
const webhooks = await webhookRepository.findByOrgIdAndTrigger({
orgId: delegationCredential.organizationId,
triggerEvent: WebhookTriggerEvents.DELEGATION_CREDENTIAL_ERROR,
});
if (webhooks.length === 0) {
log.debug("No webhooks subscribed to DELEGATION_CREDENTIAL_ERROR for organization", { orgId });
log.debug("No webhooks subscribed to DELEGATION_CREDENTIAL_ERROR for organization", {
orgId: delegationCredential.organizationId,
});
return;
}
@@ -45,10 +52,12 @@ export async function triggerDelegationCredentialErrorWebhook(params: {
id: credential.id,
type: credential.type,
appId: credential.appId || "unknown",
delegationCredentialId: delegationCredential.id,
},
user: {
id: user.id,
email: user.email,
organizationId: delegationCredential.organizationId,
},
} satisfies DelegationCredentialErrorPayloadType;