Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: split up routers to separate lambdas #8041

Merged
merged 37 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
25fafc0
split up routers to separate lambdas
juliusmarminge Mar 31, 2023
6b719a2
fix responsemeta
juliusmarminge Mar 31, 2023
4ec8c9a
move
juliusmarminge Mar 31, 2023
70a34f7
add typeguards to make sure all endpoints are covered in the approuter
juliusmarminge Mar 31, 2023
0a993e5
prettier
juliusmarminge Mar 31, 2023
ed78cb2
move slotsrouter
juliusmarminge Mar 31, 2023
0dddf3c
split ssg/ssr
juliusmarminge Mar 31, 2023
6a4df55
make sure correct headers are sent on viewer.public
juliusmarminge Mar 31, 2023
13ed9f4
make sure correct headers are sent + use ctx.prisma
juliusmarminge Mar 31, 2023
b02b169
Merge branch 'main' into split-router
roae Apr 1, 2023
ae25067
Merge branch 'main' into split-router
emrysal Apr 4, 2023
fee7d26
Merged with main
emrysal Apr 5, 2023
176f5c8
Merge branch 'main' into split-router
emrysal Apr 5, 2023
775973c
Fixed new prefetch broken by merge
emrysal Apr 5, 2023
042729c
Merge branch 'main' into split-router
PeerRich Apr 7, 2023
c9b6a6d
Merge branch 'main' into split-router
keithwillcode Apr 11, 2023
201f978
Merge branch 'main' into pr/8041
zomars Apr 12, 2023
af5c7c5
Merge branch 'main' into pr/8041
zomars Apr 12, 2023
b12329d
Merge remote-tracking branch 'calcom/main' into split-router
keithwillcode May 1, 2023
3177ca4
Fixes after merge
keithwillcode May 1, 2023
7e2c348
Created separate API route for all tRPC routers
keithwillcode Apr 26, 2023
5188527
More fixes from refactor
keithwillcode May 1, 2023
970f6f1
Fixed tRPC query for slots
keithwillcode May 1, 2023
21db6cc
Put back extra line
keithwillcode May 1, 2023
2232d26
Fixed type checks
keithwillcode May 1, 2023
112f5fb
Removed Endpoint type check since it loads from client
keithwillcode May 1, 2023
d298846
Reverted change in getSchedule test
keithwillcode May 1, 2023
8befee5
Merge branch 'main' into split-router
keithwillcode May 2, 2023
eb3858e
Merge branch 'main' into split-router
keithwillcode May 2, 2023
656ce94
Merge branch 'main' into split-router
keithwillcode May 2, 2023
2d39cd9
Merge branch 'main' into split-router
keithwillcode May 2, 2023
d7fa176
Merge branch 'main' into split-router
keithwillcode May 2, 2023
e7b6109
Fix trpc routes in expectations
hariombalhara May 3, 2023
f8fcc12
Fix one more route test
hariombalhara May 3, 2023
4d1e31d
Merge branch 'main' into split-router
keithwillcode May 3, 2023
0fbba6f
Merge branch 'main' into split-router
keithwillcode May 3, 2023
86a3a7e
Merge branch 'main' into split-router
keithwillcode May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make sure correct headers are sent + use ctx.prisma
  • Loading branch information
juliusmarminge committed Mar 31, 2023
commit 13ed9f49752d8a100b0a13255018cca53293fc42
6 changes: 3 additions & 3 deletions apps/web/pages/api/trpc/viewer/[trpc].ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export default trpcNext.createNextApiHandler({
// Our cache can change depending on our current paths value. Since paths is an array,
// we want to create a map that can match potential paths with their desired cache value
const cacheRules = {
"viewer.public.session": `no-cache`,
"viewer.public.i18n": `no-cache`,
"viewer.public.cityTimezones": `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate`,
"public.session": `no-cache`,
"public.i18n": `no-cache`,
"public.cityTimezones": `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate`,
} as const;

// Find which element above is an exact match for this group of paths
Expand Down
2 changes: 1 addition & 1 deletion packages/trpc/react/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { AppRouter } from "../server/routers/_app";
* We deploy our tRPC router on multiple lambdas to keep bundle size as small as possible
*/
const ENDPOINTS = ["slots", "viewer"] as const;
export type Endpoint = typeof ENDPOINTS[number];
export type Endpoint = (typeof ENDPOINTS)[number];

/**
* A set of strongly-typed React hooks from your `AppRouter` type signature with `createTRPCReact`.
Expand Down
64 changes: 32 additions & 32 deletions packages/trpc/server/routers/viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
deleteWebUser as syncServicesDeleteWebUser,
updateWebUser as syncServicesUpdateWebUser,
} from "@calcom/lib/sync/SyncServiceManager";
import prisma, { bookingMinimalSelect } from "@calcom/prisma";
import { bookingMinimalSelect } from "@calcom/prisma";
import { EventTypeMetaDataSchema, userMetadata } from "@calcom/prisma/zod-utils";

import { TRPCError } from "@trpc/server";
Expand Down Expand Up @@ -208,7 +208,7 @@ const loggedInViewerRouter = router({
)
.mutation(async ({ ctx, input }) => {
// Check if input.password is correct
const user = await prisma.user.findUnique({
const user = await ctx.prisma.user.findUnique({
where: {
email: ctx.user.email.toLowerCase(),
},
Expand Down Expand Up @@ -274,7 +274,7 @@ const loggedInViewerRouter = router({
return;
}),
deleteMeWithoutPassword: authedProcedure.mutation(async ({ ctx }) => {
const user = await prisma.user.findUnique({
const user = await ctx.prisma.user.findUnique({
where: {
email: ctx.user.email.toLowerCase(),
},
Expand Down Expand Up @@ -324,7 +324,7 @@ const loggedInViewerRouter = router({
connectedCalendars: authedProcedure.query(async ({ ctx }) => {
const { user, prisma } = ctx;

const userCredentials = await prisma.credential.findMany({
const userCredentials = await ctx.prisma.credential.findMany({
where: {
userId: ctx.user.id,
app: {
Expand Down Expand Up @@ -426,7 +426,7 @@ const loggedInViewerRouter = router({

if (eventTypeId) {
if (
!(await prisma.eventType.findFirst({
!(await ctx.prisma.eventType.findFirst({
where: {
id: eventTypeId,
userId: user.id,
Expand Down Expand Up @@ -562,7 +562,7 @@ const loggedInViewerRouter = router({
prisma,
} = ctx;

const user = await prisma.user.findUnique({
const user = await ctx.prisma.user.findUnique({
where: {
id: userId,
},
Expand Down Expand Up @@ -617,7 +617,7 @@ const loggedInViewerRouter = router({
})
)
.mutation(async ({ ctx, input }) => {
const { user, prisma } = ctx;
const { user } = ctx;
const data: Prisma.UserUpdateInput = {
...input,
metadata: input.metadata as Prisma.InputJsonValue,
Expand All @@ -638,7 +638,7 @@ const loggedInViewerRouter = router({
if (input.avatar) {
data.avatar = await resizeBase64Image(input.avatar);
}
const userToUpdate = await prisma.user.findUnique({
const userToUpdate = await ctx.prisma.user.findUnique({
where: {
id: user.id,
},
Expand Down Expand Up @@ -681,7 +681,7 @@ const loggedInViewerRouter = router({
}
}

const updatedUser = await prisma.user.update({
const updatedUser = await ctx.prisma.user.update({
where: {
id: user.id,
},
Expand Down Expand Up @@ -713,7 +713,7 @@ const loggedInViewerRouter = router({
// Revalidate booking pages
const res = ctx.res as NextApiResponse;
if (typeof res?.revalidate !== "undefined") {
const eventTypes = await prisma.eventType.findMany({
const eventTypes = await ctx.prisma.eventType.findMany({
where: {
userId: user.id,
team: null,
Expand Down Expand Up @@ -777,7 +777,7 @@ const loggedInViewerRouter = router({
}
await Promise.all(
reverse(input.ids).map((id, position) => {
return prisma.eventType.update({
return ctx.prisma.eventType.update({
where: {
id,
},
Expand Down Expand Up @@ -818,7 +818,7 @@ const loggedInViewerRouter = router({
if (process.env.SEND_FEEDBACK_EMAIL && comment) sendFeedbackEmail(feedback);
}),
locationOptions: authedProcedure.query(async ({ ctx }) => {
const credentials = await prisma.credential.findMany({
const credentials = await ctx.prisma.credential.findMany({
where: {
userId: ctx.user.id,
},
Expand Down Expand Up @@ -850,7 +850,7 @@ const loggedInViewerRouter = router({
.mutation(async ({ ctx, input }) => {
const { id, externalId } = input;

const credential = await prisma.credential.findFirst({
const credential = await ctx.prisma.credential.findFirst({
where: {
id: id,
userId: ctx.user.id,
Expand All @@ -872,7 +872,7 @@ const loggedInViewerRouter = router({
throw new TRPCError({ code: "NOT_FOUND" });
}

const eventTypes = await prisma.eventType.findMany({
const eventTypes = await ctx.prisma.eventType.findMany({
where: {
userId: ctx.user.id,
},
Expand Down Expand Up @@ -914,7 +914,7 @@ const loggedInViewerRouter = router({
return location;
});

await prisma.eventType.update({
await ctx.prisma.eventType.update({
where: {
id: eventType.id,
},
Expand All @@ -928,13 +928,13 @@ const loggedInViewerRouter = router({
// If it's a calendar, remove the destination calendar from the event type
if (credential.app?.categories.includes(AppCategories.calendar)) {
if (eventType.destinationCalendar?.credential?.appId === credential.appId) {
const destinationCalendar = await prisma.destinationCalendar.findFirst({
const destinationCalendar = await ctx.prisma.destinationCalendar.findFirst({
where: {
id: eventType.destinationCalendar?.id,
},
});
if (destinationCalendar) {
await prisma.destinationCalendar.delete({
await ctx.prisma.destinationCalendar.delete({
where: {
id: destinationCalendar.id,
},
Expand All @@ -943,14 +943,14 @@ const loggedInViewerRouter = router({
}

if (externalId) {
const existingSelectedCalendar = await prisma.selectedCalendar.findFirst({
const existingSelectedCalendar = await ctx.prisma.selectedCalendar.findFirst({
where: {
externalId: externalId,
},
});
// @TODO: SelectedCalendar doesn't have unique ID so we should only delete one item
if (existingSelectedCalendar) {
await prisma.selectedCalendar.delete({
await ctx.prisma.selectedCalendar.delete({
where: {
userId_integration_externalId: {
userId: existingSelectedCalendar.userId,
Expand All @@ -970,8 +970,8 @@ const loggedInViewerRouter = router({
// If it's a payment, hide the event type and set the price to 0. Also cancel all pending bookings
if (credential.app?.categories.includes(AppCategories.payment)) {
if (stripeAppData.price) {
await prisma.$transaction(async () => {
await prisma.eventType.update({
await ctx.prisma.$transaction(async () => {
await ctx.prisma.eventType.update({
where: {
id: eventType.id,
},
Expand All @@ -991,7 +991,7 @@ const loggedInViewerRouter = router({
});

// Assuming that all bookings under this eventType need to be paid
const unpaidBookings = await prisma.booking.findMany({
const unpaidBookings = await ctx.prisma.booking.findMany({
where: {
userId: ctx.user.id,
eventTypeId: eventType.id,
Expand Down Expand Up @@ -1043,7 +1043,7 @@ const loggedInViewerRouter = router({
});

for (const booking of unpaidBookings) {
await prisma.booking.update({
await ctx.prisma.booking.update({
where: {
id: booking.id,
},
Expand All @@ -1059,20 +1059,20 @@ const loggedInViewerRouter = router({
} catch (e) {
console.error(e);
}
await prisma.payment.delete({
await ctx.prisma.payment.delete({
where: {
id: payment.id,
},
});
}

await prisma.attendee.deleteMany({
await ctx.prisma.attendee.deleteMany({
where: {
bookingId: booking.id,
},
});

await prisma.bookingReference.deleteMany({
await ctx.prisma.bookingReference.deleteMany({
where: {
bookingId: booking.id,
},
Expand Down Expand Up @@ -1123,19 +1123,19 @@ const loggedInViewerRouter = router({

// if zapier get disconnected, delete zapier apiKey, delete zapier webhooks and cancel all scheduled jobs from zapier
if (credential.app?.slug === "zapier") {
await prisma.apiKey.deleteMany({
await ctx.prisma.apiKey.deleteMany({
where: {
userId: ctx.user.id,
appId: "zapier",
},
});
await prisma.webhook.deleteMany({
await ctx.prisma.webhook.deleteMany({
where: {
userId: ctx.user.id,
appId: "zapier",
},
});
const bookingsWithScheduledJobs = await prisma.booking.findMany({
const bookingsWithScheduledJobs = await ctx.prisma.booking.findMany({
where: {
userId: ctx.user.id,
scheduledJobs: {
Expand All @@ -1149,7 +1149,7 @@ const loggedInViewerRouter = router({
}

// Validated that credential is user's above
await prisma.credential.delete({
await ctx.prisma.credential.delete({
where: {
id: id,
},
Expand All @@ -1161,14 +1161,14 @@ const loggedInViewerRouter = router({
}),
bookingUnconfirmedCount: authedProcedure.query(async ({ ctx }) => {
const { prisma, user } = ctx;
const count = await prisma.booking.count({
const count = await ctx.prisma.booking.count({
where: {
status: BookingStatus.PENDING,
userId: user.id,
endTime: { gt: new Date() },
},
});
const recurringGrouping = await prisma.booking.groupBy({
const recurringGrouping = await ctx.prisma.booking.groupBy({
by: ["recurringEventId"],
_count: {
recurringEventId: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/trpc/server/routers/viewer/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { z } from "zod";
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
import { validPassword } from "@calcom/features/auth/lib/validPassword";
import { verifyPassword } from "@calcom/features/auth/lib/verifyPassword";
import prisma from "@calcom/prisma";

import { TRPCError } from "@trpc/server";

Expand All @@ -21,7 +20,7 @@ export const authRouter = router({
.mutation(async ({ input, ctx }) => {
const { oldPassword, newPassword } = input;

const { user } = ctx;
const { user, prisma } = ctx;

if (user.identityProvider !== IdentityProvider.CAL) {
throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" });
Expand Down Expand Up @@ -72,7 +71,7 @@ export const authRouter = router({
})
)
.mutation(async ({ input, ctx }) => {
const user = await prisma.user.findUnique({
const user = await ctx.prisma.user.findUnique({
where: {
id: ctx.user.id,
},
Expand Down
6 changes: 2 additions & 4 deletions packages/trpc/server/routers/viewer/deploymentSetup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { z } from "zod";

import prisma from "@calcom/prisma";

import { router, authedAdminProcedure } from "../../trpc";

export const deploymentSetupRouter = router({
Expand All @@ -11,13 +9,13 @@ export const deploymentSetupRouter = router({
licenseKey: z.string().optional(),
})
)
.mutation(async ({ input }) => {
.mutation(async ({ ctx, input }) => {
const data = {
agreedLicenseAt: new Date(),
licenseKey: input.licenseKey,
};

await prisma.deployment.upsert({ where: { id: 1 }, create: data, update: data });
await ctx.prisma.deployment.upsert({ where: { id: 1 }, create: data, update: data });

return;
}),
Expand Down