fix: Managed Event Type Webhooks (#10300)

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
fix/reduce-team-list-payload
Pradumn Kumar 2023-07-25 22:35:02 +05:30 committed by GitHub
parent 2011a1bd38
commit 6b9d2331fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 26 deletions

View File

@ -9,6 +9,7 @@ import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
import { IS_SELF_HOSTED } from "@calcom/lib/constants"; import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { defaultHandler } from "@calcom/lib/server"; import { defaultHandler } from "@calcom/lib/server";
import { getTranslation } from "@calcom/lib/server/i18n"; import { getTranslation } from "@calcom/lib/server/i18n";
import prisma, { bookingMinimalSelect } from "@calcom/prisma"; import prisma, { bookingMinimalSelect } from "@calcom/prisma";
@ -92,6 +93,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
eventType: { eventType: {
select: { select: {
teamId: true, teamId: true,
parentId: true,
}, },
}, },
user: { user: {
@ -168,13 +170,20 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
uid: booking.uid, uid: booking.uid,
}; };
const teamId = await getTeamIdFromEventType({
eventType: {
team: { id: booking?.eventType?.teamId ?? null },
parentId: booking?.eventType?.parentId ?? null,
},
});
await triggerWebhook({ await triggerWebhook({
evt, evt,
downloadLink, downloadLink,
booking: { booking: {
userId: booking?.user?.id, userId: booking?.user?.id,
eventTypeId: booking.eventTypeId, eventTypeId: booking.eventTypeId,
teamId: booking.eventType?.teamId, teamId,
}, },
}); });

View File

@ -18,6 +18,7 @@ import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload"; import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib"; import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { HttpError } from "@calcom/lib/http-error"; import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger"; import logger from "@calcom/lib/logger";
import { handleRefundError } from "@calcom/lib/payment/handleRefundError"; import { handleRefundError } from "@calcom/lib/payment/handleRefundError";
@ -91,6 +92,7 @@ async function getBookingToDelete(id: number | undefined, uid: string | undefine
}, },
}, },
}, },
parentId: true,
}, },
}, },
uid: true, uid: true,
@ -131,11 +133,18 @@ async function handler(req: CustomRequest) {
// get webhooks // get webhooks
const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED"; const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED";
const teamId = await getTeamIdFromEventType({
eventType: {
team: { id: bookingToDelete.eventType?.teamId ?? null },
parentId: bookingToDelete?.eventType?.parentId ?? null,
},
});
const subscriberOptions = { const subscriberOptions = {
userId: bookingToDelete.userId, userId: bookingToDelete.userId,
eventTypeId: bookingToDelete.eventTypeId as number, eventTypeId: bookingToDelete.eventTypeId as number,
triggerEvent: eventTrigger, triggerEvent: eventTrigger,
teamId: bookingToDelete.eventType?.teamId, teamId,
}; };
const eventTypeInfo: EventTypeInfo = { const eventTypeInfo: EventTypeInfo = {
eventTitle: bookingToDelete?.eventType?.title || null, eventTitle: bookingToDelete?.eventType?.title || null,

View File

@ -8,6 +8,7 @@ import { scheduleWorkflowReminders } from "@calcom/features/ee/workflows/lib/rem
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload"; import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import logger from "@calcom/lib/logger"; import logger from "@calcom/lib/logger";
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums"; import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils"; import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
@ -31,6 +32,7 @@ export async function handleConfirmation(args: {
requiresConfirmation: boolean; requiresConfirmation: boolean;
title: string; title: string;
teamId?: number | null; teamId?: number | null;
parentId?: number | null;
} | null; } | null;
eventTypeId: number | null; eventTypeId: number | null;
smsReminderNumber: string | null; smsReminderNumber: string | null;
@ -236,11 +238,18 @@ export async function handleConfirmation(args: {
} }
try { try {
const teamId = await getTeamIdFromEventType({
eventType: {
team: { id: booking.eventType?.teamId ?? null },
parentId: booking?.eventType?.parentId ?? null,
},
});
const subscribersBookingCreated = await getWebhooks({ const subscribersBookingCreated = await getWebhooks({
userId: booking.userId, userId: booking.userId,
eventTypeId: booking.eventTypeId, eventTypeId: booking.eventTypeId,
triggerEvent: WebhookTriggerEvents.BOOKING_CREATED, triggerEvent: WebhookTriggerEvents.BOOKING_CREATED,
teamId: booking.eventType?.teamId, teamId,
}); });
const subscribersMeetingEnded = await getWebhooks({ const subscribersMeetingEnded = await getWebhooks({
userId: booking.userId, userId: booking.userId,

View File

@ -52,6 +52,7 @@ import { getDefaultEvent, getGroupName, getUsernameList } from "@calcom/lib/defa
import { getErrorFromUnknown } from "@calcom/lib/errors"; import { getErrorFromUnknown } from "@calcom/lib/errors";
import getIP from "@calcom/lib/getIP"; import getIP from "@calcom/lib/getIP";
import getPaymentAppData from "@calcom/lib/getPaymentAppData"; import getPaymentAppData from "@calcom/lib/getPaymentAppData";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { HttpError } from "@calcom/lib/http-error"; import { HttpError } from "@calcom/lib/http-error";
import isOutOfBounds, { BookingDateInPastError } from "@calcom/lib/isOutOfBounds"; import isOutOfBounds, { BookingDateInPastError } from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger"; import logger from "@calcom/lib/logger";
@ -651,26 +652,6 @@ function getCustomInputsResponses(
return customInputsResponses; return customInputsResponses;
} }
async function getTeamId({ eventType }: { eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> }) {
if (eventType?.team?.id) {
return eventType.team.id;
}
// If it's a managed event we need to find the teamId for it from the parent
if (eventType.parentId) {
const managedEvent = await prisma.eventType.findFirst({
where: {
id: eventType.parentId,
},
select: {
teamId: true,
},
});
return managedEvent?.teamId;
}
}
async function handler( async function handler(
req: NextApiRequest & { userId?: number | undefined }, req: NextApiRequest & { userId?: number | undefined },
{ {
@ -1151,7 +1132,7 @@ async function handler(
length: eventType.length, length: eventType.length,
}; };
const teamId = await getTeamId({ eventType }); const teamId = await getTeamIdFromEventType({ eventType });
const subscriberOptions: GetSubscriberOptions = { const subscriberOptions: GetSubscriberOptions = {
userId: organizerUser.id, userId: organizerUser.id,

View File

@ -0,0 +1,29 @@
import prisma from "@calcom/prisma";
export async function getTeamIdFromEventType({
eventType,
}: {
eventType: { team: { id: number | null } | null; parentId: number | null };
}) {
if (!eventType) {
return null;
}
if (eventType?.team?.id) {
return eventType.team.id;
}
// If it's a managed event we need to find the teamId for it from the parent
if (eventType?.parentId) {
const managedEvent = await prisma.eventType.findFirst({
where: {
id: eventType.parentId,
},
select: {
teamId: true,
},
});
return managedEvent?.teamId;
}
}

View File

@ -7,6 +7,7 @@ import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirma
import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger"; import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger";
import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload"; import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib"; import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { getTranslation } from "@calcom/lib/server"; import { getTranslation } from "@calcom/lib/server";
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat"; import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
import { prisma } from "@calcom/prisma"; import { prisma } from "@calcom/prisma";
@ -71,6 +72,7 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
}, },
}, },
customInputs: true, customInputs: true,
parentId: true,
}, },
}, },
location: true, location: true,
@ -310,12 +312,20 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
} }
await sendDeclinedEmails(evt); await sendDeclinedEmails(evt);
const teamId = await getTeamIdFromEventType({
eventType: {
team: { id: booking.eventType?.teamId ?? null },
parentId: booking?.eventType?.parentId ?? null,
},
});
// send BOOKING_REJECTED webhooks // send BOOKING_REJECTED webhooks
const subscriberOptions = { const subscriberOptions = {
userId: booking.userId, userId: booking.userId,
eventTypeId: booking.eventTypeId, eventTypeId: booking.eventTypeId,
triggerEvent: WebhookTriggerEvents.BOOKING_REJECTED, triggerEvent: WebhookTriggerEvents.BOOKING_REJECTED,
teamId: booking.eventType?.teamId, teamId,
}; };
const eventTrigger: WebhookTriggerEvents = WebhookTriggerEvents.BOOKING_REJECTED; const eventTrigger: WebhookTriggerEvents = WebhookTriggerEvents.BOOKING_REJECTED;
const eventTypeInfo: EventTypeInfo = { const eventTypeInfo: EventTypeInfo = {

View File

@ -15,6 +15,7 @@ import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventR
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import sendPayload from "@calcom/features/webhooks/lib/sendPayload"; import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
import { isPrismaObjOrUndefined } from "@calcom/lib"; import { isPrismaObjOrUndefined } from "@calcom/lib";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { getTranslation } from "@calcom/lib/server"; import { getTranslation } from "@calcom/lib/server";
import { prisma } from "@calcom/prisma"; import { prisma } from "@calcom/prisma";
import type { WebhookTriggerEvents } from "@calcom/prisma/enums"; import type { WebhookTriggerEvents } from "@calcom/prisma/enums";
@ -238,12 +239,19 @@ export const requestRescheduleHandler = async ({ ctx, input }: RequestReschedule
// Send webhook // Send webhook
const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED"; const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED";
const teamId = await getTeamIdFromEventType({
eventType: {
team: { id: bookingToReschedule.eventType?.teamId ?? null },
parentId: bookingToReschedule?.eventType?.parentId ?? null,
},
});
// Send Webhook call if hooked to BOOKING.CANCELLED // Send Webhook call if hooked to BOOKING.CANCELLED
const subscriberOptions = { const subscriberOptions = {
userId: bookingToReschedule.userId, userId: bookingToReschedule.userId,
eventTypeId: bookingToReschedule.eventTypeId as number, eventTypeId: bookingToReschedule.eventTypeId as number,
triggerEvent: eventTrigger, triggerEvent: eventTrigger,
teamId: bookingToReschedule.eventType?.teamId, teamId,
}; };
const webhooks = await getWebhooks(subscriberOptions); const webhooks = await getWebhooks(subscriberOptions);
const promises = webhooks.map((webhook) => const promises = webhooks.map((webhook) =>