feat: org-wide webhooks (#15144)

* feat: include org in teams list

* feat: org-wide webhooks

* Move getOrgIdFromMemberOrTeamId to packages/lib
This commit is contained in:
Kartik Saini authored and GitHub committed 2024-06-04 20:16:46 +00:00
1 parent d72f7f8a42
commit 4017c0e28f
20 files changed
+264 -63

No files matched your search

@@ -7,12 +7,15 @@ export type GetSubscriberOptions = {
eventTypeId?: number | null;
triggerEvent: WebhookTriggerEvents;
teamId?: number | null;
orgId?: number | null;
};
const getWebhooks = async (options: GetSubscriberOptions, prisma: PrismaClient = defaultPrisma) => {
const userId = options.userId ?? 0;
const eventTypeId = options.eventTypeId ?? 0;
const teamId = options.teamId ?? 0;
const orgId = options.orgId ?? 0;
// if we have userId and teamId it is a managed event type and should trigger for team and user
const allWebhooks = await prisma.webhook.findMany({
where: {
@@ -27,7 +30,9 @@ const getWebhooks = async (options: GetSubscriberOptions, prisma: PrismaClient =
eventTypeId,
},
{
teamId,
teamId: {
in: [teamId, orgId],
},
},
],
AND: {
@@ -355,18 +355,72 @@ export async function updateTriggerForExistingBookings(
if (Array.isArray(where.AND)) {
if (webhook.teamId) {
const teamEvents = await prisma.eventType.findMany({
const org = await prisma.team.findFirst({
where: {
teamId: webhook.teamId,
id: webhook.teamId,
isOrganization: true,
},
select: {
bookings: {
where,
id: true,
children: {
select: {
id: true,
},
},
members: {
select: {
userId: true,
},
},
},
});
// checking if teamId is an org id
if (org) {
const teamEvents = await prisma.eventType.findMany({
where: {
teamId: {
in: org.children.map((team) => team.id),
},
},
select: {
bookings: {
where,
},
},
});
const teamEventBookings = teamEvents.flatMap((event) => event.bookings);
const teamBookingsId = teamEventBookings.map((booking) => booking.id);
const orgMemberIds = org.members.map((member) => member.userId);
where.AND.push({
userId: {
in: orgMemberIds,
},
});
// don't want to get the team bookings again
where.AND.push({
id: {
notIn: teamBookingsId,
},
});
const userBookings = await prisma.booking.findMany({
where,
});
// add teams bookings and users bookings to get total org bookings
bookings = teamEventBookings.concat(userBookings);
} else {
const teamEvents = await prisma.eventType.findMany({
where: {
teamId: webhook.teamId,
},
select: {
bookings: {
where,
},
},
});
bookings = teamEvents.flatMap((event) => event.bookings);
bookings = teamEvents.flatMap((event) => event.bookings);
}
} else {
if (webhook.eventTypeId) {
where.AND.push({ eventTypeId: webhook.eventTypeId });