refactor: simplify workflow page and improve load time (#16095)
* fix: simplify workflow page and improve load time * chore: use new endpoint * chore: save progress * refactor: code * refactor: remove not requried code * chore: remove schema * chore: fix typ * chore: improve * chore: change name * chore: remove unused * chore: remove page --------- Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
1 parent
b6ab1ef80a
commit
0e62800af6
8 files changed
+519
-156
No files matched your search
@@ -8,6 +8,7 @@ import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZDeleteInputSchema } from "./delete.schema";
|
||||
import { ZDuplicateInputSchema } from "./duplicate.schema";
|
||||
import { ZEventTypeInputSchema, ZGetEventTypesFromGroupSchema } from "./getByViewer.schema";
|
||||
import { ZGetTeamAndEventTypeOptionsSchema } from "./getTeamAndEventTypeOptions.schema";
|
||||
import { get } from "./procedures/get";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
import { eventOwnerProcedure } from "./util";
|
||||
@@ -16,6 +17,7 @@ type BookingsRouterHandlerCache = {
|
||||
getByViewer?: typeof import("./getByViewer.handler").getByViewerHandler;
|
||||
getUserEventGroups?: typeof import("./getUserEventGroups.handler").getUserEventGroups;
|
||||
getEventTypesFromGroup?: typeof import("./getEventTypesFromGroup.handler").getEventTypesFromGroup;
|
||||
getTeamAndEventTypeOptions?: typeof import("./getTeamAndEventTypeOptions.handler").getTeamAndEventTypeOptions;
|
||||
list?: typeof import("./list.handler").listHandler;
|
||||
listWithTeam?: typeof import("./listWithTeam.handler").listWithTeamHandler;
|
||||
create?: typeof import("./create.handler").createHandler;
|
||||
@@ -104,6 +106,32 @@ export const eventTypesRouter = router({
|
||||
return result;
|
||||
}),
|
||||
|
||||
getTeamAndEventTypeOptions: authedProcedure
|
||||
.input(ZGetTeamAndEventTypeOptionsSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getTeamAndEventTypeOptions) {
|
||||
UNSTABLE_HANDLER_CACHE.getTeamAndEventTypeOptions = await import(
|
||||
"./getTeamAndEventTypeOptions.handler"
|
||||
).then((mod) => mod.getTeamAndEventTypeOptions);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getTeamAndEventTypeOptions) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
const timer = logP(`getTeamAndEventTypeOptions(${ctx.user.id})`);
|
||||
|
||||
const result = await UNSTABLE_HANDLER_CACHE.getTeamAndEventTypeOptions({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
|
||||
timer();
|
||||
|
||||
return result;
|
||||
}),
|
||||
|
||||
list: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.list) {
|
||||
UNSTABLE_HANDLER_CACHE.list = await import("./list.handler").then((mod) => mod.listHandler);
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
||||
import { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
|
||||
import { MembershipRepository } from "@calcom/lib/server/repository/membership";
|
||||
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { MembershipRole, SchedulingType } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import { listOtherTeamHandler } from "../organizations/listOtherTeams.handler";
|
||||
import type { TGetTeamAndEventTypeOptionsSchema } from "./getTeamAndEventTypeOptions.schema";
|
||||
|
||||
type GetTeamAndEventTypeOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
prisma: PrismaClient;
|
||||
};
|
||||
input: TGetTeamAndEventTypeOptionsSchema;
|
||||
};
|
||||
|
||||
type Option = {
|
||||
value: string;
|
||||
label: string;
|
||||
};
|
||||
|
||||
type res = Awaited<
|
||||
ReturnType<typeof MembershipRepository.findAllByUpIdIncludeMinimalEventTypes>
|
||||
>[number]["team"]["eventTypes"][number];
|
||||
|
||||
type EventType = Omit<res, "forwardParamsSuccessRedirect"> & { children?: { id: number }[] };
|
||||
|
||||
export const getTeamAndEventTypeOptions = async ({ ctx, input }: GetTeamAndEventTypeOptions) => {
|
||||
await checkRateLimitAndThrowError({
|
||||
identifier: `eventTypes:getTeamAndEventTypeOptions.handler:${ctx.user.id}`,
|
||||
rateLimitingType: "common",
|
||||
});
|
||||
|
||||
const user = ctx.user;
|
||||
const teamId = input?.teamId;
|
||||
const isOrg = input?.isOrg;
|
||||
|
||||
const skipTeamOptions = !isOrg;
|
||||
const skipEventTypes = !!isOrg;
|
||||
|
||||
const userProfile = ctx.user.profile;
|
||||
const profile = await ProfileRepository.findByUpId(userProfile.upId);
|
||||
const parentOrgHasLockedEventTypes =
|
||||
profile?.organization?.organizationSettings?.lockEventTypeCreationForUsers;
|
||||
|
||||
const [profileMemberships, profileEventTypes] = await Promise.all([
|
||||
MembershipRepository.findAllByUpIdIncludeMinimalEventTypes(
|
||||
{
|
||||
upId: userProfile.upId,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
accepted: true,
|
||||
},
|
||||
skipEventTypes,
|
||||
}
|
||||
),
|
||||
teamId
|
||||
? []
|
||||
: EventTypeRepository.findAllByUpIdWithMinimalData(
|
||||
{
|
||||
upId: userProfile.upId,
|
||||
userId: user.id,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
teamId: null,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
position: "desc",
|
||||
},
|
||||
{
|
||||
id: "asc",
|
||||
},
|
||||
],
|
||||
}
|
||||
),
|
||||
]);
|
||||
|
||||
if (!profile) {
|
||||
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
|
||||
}
|
||||
|
||||
const memberships = profileMemberships.map((membership) => ({
|
||||
...membership,
|
||||
team: {
|
||||
...membership.team,
|
||||
metadata: teamMetadataSchema.parse(membership.team.metadata),
|
||||
},
|
||||
}));
|
||||
|
||||
type EventTypeGroup = {
|
||||
teamId?: number | null;
|
||||
parentId?: number | null;
|
||||
bookerUrl?: string;
|
||||
profile: {
|
||||
slug?: (typeof profile)["username"] | null;
|
||||
name: (typeof profile)["name"];
|
||||
image?: string;
|
||||
eventTypesLockedByOrg?: boolean;
|
||||
};
|
||||
eventTypes?: EventType[];
|
||||
};
|
||||
|
||||
let eventTypeGroups: EventTypeGroup[] = [];
|
||||
|
||||
eventTypeGroups.push({
|
||||
teamId: null,
|
||||
profile: {
|
||||
slug: profile.username,
|
||||
name: profile.name,
|
||||
eventTypesLockedByOrg: parentOrgHasLockedEventTypes,
|
||||
},
|
||||
eventTypes: profileEventTypes,
|
||||
});
|
||||
|
||||
eventTypeGroups = ([] as EventTypeGroup[]).concat(
|
||||
eventTypeGroups,
|
||||
await Promise.all(
|
||||
memberships
|
||||
.filter((mmship) => {
|
||||
if (mmship?.team?.isOrganization) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map(async (membership) => {
|
||||
const team = {
|
||||
...membership.team,
|
||||
metadata: teamMetadataSchema.parse(membership.team.metadata),
|
||||
};
|
||||
|
||||
const eventTypes = team.eventTypes;
|
||||
return {
|
||||
teamId: team.id,
|
||||
parentId: team.parentId,
|
||||
profile: {
|
||||
name: team.name,
|
||||
},
|
||||
eventTypes: eventTypes
|
||||
?.filter((evType) => {
|
||||
const res = evType.userId === null || evType.userId === user.id;
|
||||
return res;
|
||||
})
|
||||
?.filter((evType) =>
|
||||
membership.role === MembershipRole.MEMBER
|
||||
? evType.schedulingType !== SchedulingType.MANAGED
|
||||
: true
|
||||
),
|
||||
};
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
let teamOptions: Option[] = [];
|
||||
|
||||
if (!skipTeamOptions) {
|
||||
const profileTeamsOptions = eventTypeGroups
|
||||
.map((group) => ({
|
||||
...group.profile,
|
||||
teamId: group.teamId,
|
||||
}))
|
||||
.filter((profile) => !!profile.teamId)
|
||||
.map((profile) => {
|
||||
return {
|
||||
value: String(profile.teamId) || "",
|
||||
label: profile.name || profile.slug || "",
|
||||
};
|
||||
});
|
||||
|
||||
const otherTeams = await listOtherTeamHandler({ ctx });
|
||||
const otherTeamsOptions = otherTeams
|
||||
? otherTeams.map((team) => {
|
||||
return {
|
||||
value: String(team.id) || "",
|
||||
label: team.name || team.slug || "",
|
||||
};
|
||||
})
|
||||
: [];
|
||||
|
||||
teamOptions = profileTeamsOptions.concat(otherTeamsOptions);
|
||||
}
|
||||
|
||||
const eventTypeOptions =
|
||||
eventTypeGroups.reduce((options, group) => {
|
||||
// /** don't show team event types for user workflow */
|
||||
if (!teamId && group.teamId) return options;
|
||||
// /** only show correct team event types for team workflows */
|
||||
if (teamId && teamId !== group.teamId) return options;
|
||||
|
||||
return [
|
||||
...options,
|
||||
...(group?.eventTypes
|
||||
?.filter((evType) => {
|
||||
const metadata = EventTypeMetaDataSchema.parse(evType.metadata);
|
||||
return (
|
||||
!metadata?.managedEventConfig ||
|
||||
!!metadata?.managedEventConfig.unlockedFields?.workflows ||
|
||||
!!teamId
|
||||
);
|
||||
})
|
||||
?.map((eventType) => ({
|
||||
value: String(eventType.id),
|
||||
label: `${eventType.title} ${
|
||||
eventType?.children && eventType.children.length ? `(+${eventType.children.length})` : ``
|
||||
}`,
|
||||
})) ?? []),
|
||||
];
|
||||
}, [] as Option[]) || [];
|
||||
|
||||
return {
|
||||
eventTypeOptions,
|
||||
teamOptions,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZGetTeamAndEventTypeOptionsSchema = z
|
||||
.object({
|
||||
teamId: z.number().optional(),
|
||||
isOrg: z.boolean().default(false),
|
||||
})
|
||||
.nullish();
|
||||
|
||||
export type TGetTeamAndEventTypeOptionsSchema = z.infer<typeof ZGetTeamAndEventTypeOptionsSchema>;
|
||||
Reference in new issue
Block a user