refactor: event types infinite scroll (#15949)

* chore

* chore

* feat: paginated event types

* fix: event types page

* fix: k bar

* fix: event type order

* fix: type error

* chore: remove commented code

* chore: type err

* chore: type err and feedback

* feat: add old component

* chore: missing import

* fix: add isInfiniteScrollEnabled prop

* chore: type err

* chore: type error

* feat: auto fetch next page when button is in view

* Update packages/lib/server/repository/eventType.ts

* Update packages/lib/server/repository/eventType.ts

* chore: feedback

* fix: managed event types

* fix: Review fixes for event-types-infinite-scroll (#16047)

* Review fixes

* chore: missing import

---------

Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>

* chore: type error

* chore

* fix: delete event type

* fix: create event dialog

* fix: create and duplicate dialog

* chore: simplify return type

* fix: type

* chore: invalidate query

* chore: remove query

* fix duplicated event not showing.

---------

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-08-05 16:01:03 +04:00
1 parent 583661a15d
commit 45db5c0d5e
18 files changed
+1377 -59

No files matched your search

+101 -1
View File
@@ -3,8 +3,11 @@ import { Prisma } from "@prisma/client";
import logger from "@calcom/lib/logger";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import type { Ensure } from "@calcom/types/utils";
import { TRPCError } from "@trpc/server";
import { safeStringify } from "../../safeStringify";
import { eventTypeSelect } from "../eventTypeSelect";
import { LookupTarget, ProfileRepository } from "./profile";
@@ -103,7 +106,14 @@ export class EventTypeRepository {
{
orderBy,
where = {},
}: { orderBy?: Prisma.EventTypeOrderByWithRelationInput[]; where?: Prisma.EventTypeWhereInput } = {}
cursor: cursorId,
limit,
}: {
orderBy?: Prisma.EventTypeOrderByWithRelationInput[];
where?: Prisma.EventTypeWhereInput;
cursor?: number | null;
limit?: number | null;
} = {}
) {
if (!upId) return [];
const lookupTarget = ProfileRepository.getLookupTarget(upId);
@@ -133,6 +143,9 @@ export class EventTypeRepository {
})
);
const cursor = cursorId ? { id: cursorId } : undefined;
const take = limit ? limit + 1 : undefined; // We take +1 as it'll be used for the next cursor
if (!profileId) {
// Lookup is by userId
return await prisma.eventType.findMany({
@@ -141,6 +154,8 @@ export class EventTypeRepository {
...where,
},
select,
cursor,
take,
orderBy,
});
}
@@ -172,6 +187,8 @@ export class EventTypeRepository {
...where,
},
select,
cursor,
take,
orderBy,
});
} else {
@@ -192,11 +209,94 @@ export class EventTypeRepository {
...where,
},
select,
cursor,
take,
orderBy,
});
}
}
static async findTeamEventTypes({
teamId,
parentId,
userId,
limit,
cursor,
orderBy,
where = {},
}: {
teamId: number;
parentId?: number | null;
userId: number;
limit?: number | null;
cursor?: number | null;
orderBy?: Prisma.EventTypeOrderByWithRelationInput[];
where?: Prisma.EventTypeWhereInput;
}) {
const userSelect = Prisma.validator<Prisma.UserSelect>()({
name: true,
avatarUrl: true,
username: true,
id: true,
});
const select = {
...eventTypeSelect,
hashedLink: true,
users: { select: userSelect, take: 5 },
children: {
include: {
users: { select: userSelect, take: 5 },
},
},
hosts: {
include: {
user: { select: userSelect },
},
take: 5,
},
};
const teamMembership = await prisma.membership.findFirst({
where: {
OR: [
{
teamId,
userId,
accepted: true,
},
{
team: {
parent: {
...(parentId ? { id: parentId } : {}),
members: {
some: {
userId,
accepted: true,
role: { in: [MembershipRole.ADMIN, MembershipRole.OWNER] },
},
},
},
},
},
],
},
});
if (!teamMembership) throw new TRPCError({ code: "UNAUTHORIZED" });
return await prisma.eventType.findMany({
where: {
teamId,
...where,
},
select,
cursor: cursor ? { id: cursor } : undefined,
take: limit ? limit + 1 : undefined, // We take +1 as itll be used for the next cursor
orderBy,
});
}
static async findAllByUserId({ userId }: { userId: number }) {
return await prisma.eventType.findMany({
where: {
@@ -131,4 +131,48 @@ export class MembershipRepository {
},
});
}
static async findAllByUpIdIncludeTeam(
{ upId }: { upId: string },
{ where }: { where?: Prisma.MembershipWhereInput } = {}
) {
const lookupTarget = ProfileRepository.getLookupTarget(upId);
let prismaWhere;
if (lookupTarget.type === LookupTarget.Profile) {
/**
* TODO: When we add profileId to membership, we lookup by profileId
* If the profile is movedFromUser, we lookup all memberships without profileId as well.
*/
const profile = await ProfileRepository.findById(lookupTarget.id);
if (!profile) {
return [];
}
prismaWhere = {
userId: profile.user.id,
...where,
};
} else {
prismaWhere = {
userId: lookupTarget.id,
...where,
};
}
return await prisma.membership.findMany({
where: prismaWhere,
include: {
team: {
include: {
// could slow down the query
// members: {
// select: membershipSelect,
// },
parent: {
select: teamParentSelect,
},
},
},
},
});
}
}