perf: remove trpc basecamp3 router (#23785)
* remove trpc basecamp3 router * update * define business logic in appstore package * cleaner * cleaner code * refactor * use zod * cleaner * update
This commit is contained in:
1 parent
a63802dea7
commit
68f2fc9174
7 files changed
+106
-111
No files matched your search
@@ -1,26 +0,0 @@
|
||||
import authedProcedure from "@calcom/trpc/server/procedures/authedProcedure";
|
||||
import { router } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { ZProjectMutationInputSchema } from "./projectMutation.schema";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const UNSTABLE_HANDLER_CACHE: any = {};
|
||||
|
||||
const appBasecamp3 = router({
|
||||
projects: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.projects) {
|
||||
UNSTABLE_HANDLER_CACHE.projects = await import("./projects.handler").then((mod) => mod.projectHandler);
|
||||
}
|
||||
return UNSTABLE_HANDLER_CACHE.projects({ ctx });
|
||||
}),
|
||||
projectMutation: authedProcedure.input(ZProjectMutationInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.projectMutation) {
|
||||
UNSTABLE_HANDLER_CACHE.projectMutation = await import("./projectMutation.handler").then(
|
||||
(mod) => mod.projectMutationHandler
|
||||
);
|
||||
}
|
||||
return UNSTABLE_HANDLER_CACHE.projectMutation({ ctx, input });
|
||||
}),
|
||||
});
|
||||
|
||||
export default appBasecamp3;
|
||||
@@ -1,63 +0,0 @@
|
||||
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 { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TProjectMutationInputSchema } from "./projectMutation.schema";
|
||||
|
||||
interface ProjectMutationHandlerOptions {
|
||||
ctx: {
|
||||
prisma: PrismaClient;
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TProjectMutationInputSchema;
|
||||
}
|
||||
|
||||
interface IDock {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
export const projectMutationHandler = async ({ ctx, input }: ProjectMutationHandlerOptions) => {
|
||||
const { user_agent } = await getAppKeysFromSlug("basecamp3");
|
||||
const { user, prisma } = ctx;
|
||||
|
||||
const { projectId } = input;
|
||||
const credential = await prisma.credential.findFirst({
|
||||
where: {
|
||||
userId: user?.id,
|
||||
},
|
||||
select: credentialForCalendarServiceSelect,
|
||||
});
|
||||
|
||||
if (!credential) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "No credential found for user" });
|
||||
}
|
||||
let credentialKey = credential.key as BasecampToken;
|
||||
|
||||
if (credentialKey.expires_at < Date.now()) {
|
||||
credentialKey = (await refreshAccessToken(credential)) as BasecampToken;
|
||||
}
|
||||
// get schedule id
|
||||
const basecampUserId = credentialKey.account.id;
|
||||
const scheduleResponse = await fetch(
|
||||
`https://3.basecampapi.com/${basecampUserId}/projects/${projectId}.json`,
|
||||
{
|
||||
headers: {
|
||||
"User-Agent": user_agent as string,
|
||||
Authorization: `Bearer ${credentialKey.access_token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
const scheduleJson = await scheduleResponse.json();
|
||||
const scheduleId = scheduleJson.dock.find((dock: IDock) => dock.name === "schedule").id;
|
||||
await prisma.credential.update({
|
||||
where: { id: credential.id },
|
||||
data: { key: { ...credentialKey, projectId: Number(projectId), scheduleId } },
|
||||
});
|
||||
|
||||
return { message: "Updated project successfully" };
|
||||
};
|
||||
@@ -1,7 +0,0 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZProjectMutationInputSchema = z.object({
|
||||
projectId: z.string(),
|
||||
});
|
||||
|
||||
export type TProjectMutationInputSchema = z.infer<typeof ZProjectMutationInputSchema>;
|
||||
@@ -1,45 +0,0 @@
|
||||
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 { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
interface ProjectsHandlerOptions {
|
||||
ctx: {
|
||||
prisma: PrismaClient;
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
select: credentialForCalendarServiceSelect,
|
||||
});
|
||||
if (!credential) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "No credential found for user" });
|
||||
}
|
||||
let credentialKey = credential.key as BasecampToken;
|
||||
if (!credentialKey.account) {
|
||||
return;
|
||||
}
|
||||
|
||||
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}` },
|
||||
});
|
||||
const projects = await resp.json();
|
||||
return { currentProject: credentialKey.projectId, projects };
|
||||
};
|
||||
@@ -3,7 +3,6 @@ import { featureFlagRouter } from "@calcom/features/flags/server/router";
|
||||
import { insightsRouter } from "@calcom/features/insights/server/trpc-router";
|
||||
|
||||
import { router, mergeRouters } from "../../trpc";
|
||||
import app_Basecamp3 from "../apps/basecamp3/_router";
|
||||
import app_RoutingForms from "../apps/routing-forms/_router";
|
||||
import { loggedInViewerRouter } from "../loggedInViewer/_router";
|
||||
import { publicViewerRouter } from "../publicViewer/_router";
|
||||
@@ -79,7 +78,6 @@ export const viewerRouter = router({
|
||||
// NOTE: Add all app related routes in the bottom till the problem described in @calcom/app-store/trpc-routers.ts is solved.
|
||||
// After that there would just one merge call here for all the apps.
|
||||
appRoutingForms: app_RoutingForms,
|
||||
appBasecamp3: app_Basecamp3,
|
||||
features: featureFlagRouter,
|
||||
users: userAdminRouter,
|
||||
oAuth: oAuthRouter,
|
||||
|
||||
Reference in new issue
Block a user