revert: "fix: view team bookings (#14079)" (#14561)

This reverts commit f86c448359.
This commit is contained in:
Keith Williams authored and GitHub committed 2024-04-13 00:53:05 +00:00
1 parent becad5ee67
commit fd193e651d
5 files changed
+82 -828

No files matched your search

@@ -40,12 +40,15 @@ export const getHandler = async ({ ctx, input }: GetOptions) => {
};
};
const getUniqueBookings = <T extends { id: number }>(arr: T[]) => {
const unique = new Set<number>();
arr.forEach((booking) => {
unique.add(booking.id);
const set = new Set();
const getUniqueBookings = <T extends { uid: string }>(arr: T[]) => {
const unique = arr.filter((booking) => {
const duplicate = set.has(booking.uid);
set.add(booking.uid);
return !duplicate;
});
return Array.from(unique.values());
set.clear();
return unique;
};
export async function getBookings({
@@ -125,6 +128,7 @@ export async function getBookings({
],
},
};
const filtersCombined: Prisma.BookingWhereInput[] = !filters
? []
: Object.keys(filters)
@@ -192,171 +196,15 @@ export async function getBookings({
},
};
const userBookingsFilter: Prisma.BookingWhereInput = {
userId: user.id,
};
const userAttendingBookingsFilter: Prisma.BookingWhereInput = {
attendees: {
some: {
email: user.email,
},
},
};
// all (collective + round-robin) team bookings should be visible to team-owner and team-admins
const teamBookingsFilter: Prisma.BookingWhereInput = {
eventType: {
team: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
};
// all (collective + round-robin) team bookings should be visible to org-owner and org-admins
const organizationBookingsFilter: Prisma.BookingWhereInput = {
eventType: {
team: {
parent: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
};
// all managed team bookings should be visible to team-owner and team-admins
const teamManagedBookingsFilter: Prisma.BookingWhereInput = {
eventType: {
parent: {
team: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
};
// all managed team bookings should be visible to org-owner and org-admins
const organizationManagedBookingsFilter: Prisma.BookingWhereInput = {
eventType: {
parent: {
team: {
parent: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
},
};
// show personal booking to org-owner and org-admins
const organizationPersonalBookingsFilter: Prisma.BookingWhereInput = {
AND: [
{
user: {
teams: {
some: {
team: {
parentId: { gt: 0 },
},
},
},
},
},
{
OR: [
{
user: {
teams: {
some: {
team: {
parent: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
},
},
},
{
user: {
teams: {
some: {
team: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
},
},
],
},
// filter out all (collective + round-robin) event bookings
{
eventType: {
teamId: null,
},
},
// filter out all (managed) event bookings
{
eventType: {
parentId: null,
},
},
],
};
const seatsReferenceFilter: Prisma.BookingWhereInput = {
seatsReferences: {
some: {
attendee: {
email: user.email,
},
},
},
};
const [
// Quering these in parallel to save time.
// Note that because we are applying `take` to individual queries, we will usually get more bookings then we need. It is okay to have more bookings faster than having what we need slower
bookingsQueryUserId,
bookingsQueryAttendees,
bookingsQueryTeamMember,
bookingsQuerySeatReference,
//////////////////////////
recurringInfoBasic,
recurringInfoExtended,
// We need all promises to be successful, so we are not using Promise.allSettled
@@ -364,19 +212,73 @@ export async function getBookings({
prisma.booking.findMany({
where: {
OR: [
userBookingsFilter,
userAttendingBookingsFilter,
teamBookingsFilter,
organizationBookingsFilter,
teamManagedBookingsFilter,
organizationManagedBookingsFilter,
organizationPersonalBookingsFilter,
seatsReferenceFilter,
{
userId: user.id,
},
],
AND: [passedBookingsStatusFilter, ...filtersCombined],
},
orderBy,
take: take + 1,
skip,
}),
prisma.booking.findMany({
where: {
OR: [
{
attendees: {
some: {
email: user.email,
},
},
},
],
AND: [passedBookingsStatusFilter, ...filtersCombined],
},
orderBy,
take: take + 1,
skip,
}),
prisma.booking.findMany({
where: {
OR: [
{
eventType: {
team: {
members: {
some: {
userId: user.id,
role: {
in: ["ADMIN", "OWNER"],
},
},
},
},
},
},
],
AND: [passedBookingsStatusFilter, ...filtersCombined],
},
orderBy,
take: take + 1,
skip,
}),
prisma.booking.findMany({
where: {
OR: [
{
seatsReferences: {
some: {
attendee: {
email: user.email,
},
},
},
},
],
AND: [passedBookingsStatusFilter, ...filtersCombined],
},
orderBy,
select: { id: true },
take: take + 1,
skip,
}),
@@ -443,6 +345,9 @@ export async function getBookings({
const plainBookings = getUniqueBookings(
// It's going to mess up the orderBy as we are concatenating independent queries results
bookingsQueryUserId
.concat(bookingsQueryAttendees)
.concat(bookingsQueryTeamMember)
.concat(bookingsQuerySeatReference)
);
// Now enrich bookings with relation data. We could have queried the relation data along with the bookings, but that would cause unnecessary queries to the database.
@@ -451,7 +356,7 @@ export async function getBookings({
await prisma.booking.findMany({
where: {
id: {
in: plainBookings,
in: plainBookings.map((booking) => booking.id),
},
},
select: bookingSelect,