-
Notifications
You must be signed in to change notification settings - Fork 11.7k
perf: remove trpc basecamp3 router
#23785
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
Changes from all commits
9974c81
275918d
f584f1d
e438660
ffba1a3
5907065
6b975cb
e4ed175
60046c7
f9e0d93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,45 +1,54 @@ | ||||||||||||||||||||||||
| import type { NextApiRequest } from "next"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug"; | ||||||||||||||||||||||||
| import { refreshAccessToken } from "@calcom/app-store/basecamp3/lib/helpers"; | ||||||||||||||||||||||||
| import type { BasecampToken } from "@calcom/app-store/basecamp3/lib/types"; | ||||||||||||||||||||||||
| import type { PrismaClient } from "@calcom/prisma/client"; | ||||||||||||||||||||||||
| import { defaultHandler } from "@calcom/lib/server/defaultHandler"; | ||||||||||||||||||||||||
| import { defaultResponder } from "@calcom/lib/server/defaultResponder"; | ||||||||||||||||||||||||
| import prisma from "@calcom/prisma"; | ||||||||||||||||||||||||
| import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; | ||||||||||||||||||||||||
| import type { TrpcSessionUser } from "@calcom/trpc/server/types"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import { TRPCError } from "@trpc/server"; | ||||||||||||||||||||||||
| import { HttpError } from "@calcom/lib/http-error"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| interface ProjectsHandlerOptions { | ||||||||||||||||||||||||
| ctx: { | ||||||||||||||||||||||||
| prisma: PrismaClient; | ||||||||||||||||||||||||
| user: NonNullable<TrpcSessionUser>; | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| async function handler(req: NextApiRequest) { | ||||||||||||||||||||||||
| const userId = req.session?.user?.id; | ||||||||||||||||||||||||
| if (!userId) { | ||||||||||||||||||||||||
| throw new HttpError({ statusCode: 401, message: "Unauthorized" }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const projectHandler = async ({ ctx }: ProjectsHandlerOptions) => { | ||||||||||||||||||||||||
| const { user_agent } = await getAppKeysFromSlug("basecamp3"); | ||||||||||||||||||||||||
| const { user, prisma } = ctx; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const credential = await prisma.credential.findFirst({ | ||||||||||||||||||||||||
| where: { | ||||||||||||||||||||||||
| userId: user?.id, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| where: { userId }, | ||||||||||||||||||||||||
| select: credentialForCalendarServiceSelect, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
20
to
23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Filter credential by Basecamp3 app to avoid cross-app leakage. Without - const credential = await prisma.credential.findFirst({
- where: { userId },
- select: credentialForCalendarServiceSelect,
- });
+ const credential = await prisma.credential.findFirst({
+ where: { userId, appId: "basecamp3" },
+ select: credentialForCalendarServiceSelect,
+ });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!credential) { | ||||||||||||||||||||||||
| throw new TRPCError({ code: "FORBIDDEN", message: "No credential found for user" }); | ||||||||||||||||||||||||
| throw new HttpError({ statusCode: 403, message: "No credential found for user" }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let credentialKey = credential.key as BasecampToken; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!credentialKey.account) { | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| return { currentProject: null, projects: [] }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (credentialKey.expires_at < Date.now()) { | ||||||||||||||||||||||||
| credentialKey = (await refreshAccessToken(credential)) as BasecampToken; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const url = `${credentialKey.account.href}/projects.json`; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const resp = await fetch(url, { | ||||||||||||||||||||||||
| headers: { "User-Agent": user_agent as string, Authorization: `Bearer ${credentialKey.access_token}` }, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!resp.ok) { | ||||||||||||||||||||||||
| throw new HttpError({ statusCode: 400, message: "Failed to fetch Basecamp projects" }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const projects = await resp.json(); | ||||||||||||||||||||||||
| return { currentProject: credentialKey.projectId, projects }; | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export default defaultHandler({ | ||||||||||||||||||||||||
| GET: Promise.resolve({ default: defaultResponder(handler) }), | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.