chore: get platform me from trpc instead of apiv2 (#16515)

This commit is contained in:
Morgan
2024-09-06 12:36:47 -07:00
committed by GitHub
parent f07b73dc79
commit 21ef1900e9
4 changed files with 60 additions and 17 deletions
@@ -1,21 +1,15 @@
import { useQuery } from "@tanstack/react-query";
import { trpc } from "@calcom/trpc/react";
import type { UserResponse } from "@calcom/platform-types";
export const usePlatformMe = () => {
const QUERY_KEY = "get-platform-me";
const platformMeQuery = useQuery<UserResponse>({
queryKey: [QUERY_KEY],
queryFn: async (): Promise<UserResponse> => {
const response = await fetch(`/api/v2/me`, {
method: "get",
headers: { "Content-type": "application/json" },
});
const data = await response.json();
return data.data as UserResponse;
export function usePlatformMe() {
const meQuery = trpc.viewer.platformMe.useQuery(undefined, {
retry(failureCount) {
return failureCount > 3;
},
// 5 minutes
staleTime: 300000,
});
return platformMeQuery;
};
return meQuery;
}
export default usePlatformMe;
@@ -16,6 +16,7 @@ import { ZLocationOptionsInputSchema } from "./locationOptions.schema";
import { ZNoShowInputSchema } from "./markNoShow.schema";
import { ZOutOfOfficeInputSchema, ZOutOfOfficeDelete } from "./outOfOffice.schema";
import { me } from "./procedures/me";
import { platformMe } from "./procedures/platformMe";
import { teamsAndUserProfilesQuery } from "./procedures/teamsAndUserProfilesQuery";
import { ZRemoveNotificationsSubscriptionInputSchema } from "./removeNotificationsSubscription.schema";
import { ZRoutingFormOrderInputSchema } from "./routingFormOrder.schema";
@@ -31,6 +32,7 @@ const namespaced = (s: string) => `${NAMESPACE}.${s}`;
type AppsRouterHandlerCache = {
me?: typeof import("./me.handler").meHandler;
platformMe?: typeof import("./platformMe.handler").platformMeHandler;
shouldVerifyEmail?: typeof import("./shouldVerifyEmail.handler").shouldVerifyEmailHandler;
deleteMe?: typeof import("./deleteMe.handler").deleteMeHandler;
deleteMeWithoutPassword?: typeof import("./deleteMeWithoutPassword.handler").deleteMeWithoutPasswordHandler;
@@ -70,6 +72,7 @@ const UNSTABLE_HANDLER_CACHE: AppsRouterHandlerCache = {};
export const loggedInViewerRouter = router({
me,
platformMe,
deleteMe: authedProcedure.input(ZDeleteMeInputSchema).mutation(async ({ ctx, input }) => {
if (!UNSTABLE_HANDLER_CACHE.deleteMe) {
@@ -0,0 +1,39 @@
import type { Session } from "next-auth";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
type MeOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
session: Session;
};
};
export const platformMeHandler = async ({ ctx }: MeOptions) => {
const { user: sessionUser } = ctx;
const allUserEnrichedProfiles = await ProfileRepository.findAllProfilesForUserIncludingMovedUser(
sessionUser
);
const mainProfile =
allUserEnrichedProfiles.find((profile) => sessionUser.movedToProfileId === profile.id) ||
allUserEnrichedProfiles.find((profile) => sessionUser.organizationId === profile.organizationId) ||
allUserEnrichedProfiles[0];
return {
id: sessionUser.id,
username: sessionUser.username,
email: sessionUser.email,
timeFormat: sessionUser.timeFormat,
timeZone: sessionUser.timeZone,
defaultScheduleId: sessionUser.defaultScheduleId,
weekStart: sessionUser.weekStart,
organizationId: mainProfile.organizationId,
organization: {
isPlatform: mainProfile.organization?.isPlatform ?? false,
id: mainProfile.organizationId ?? null,
},
};
};
@@ -0,0 +1,7 @@
import authedProcedure from "../../../procedures/authedProcedure";
export const platformMe = authedProcedure.query(async ({ ctx }) => {
const handler = (await import("../platformMe.handler")).platformMeHandler;
return handler({ ctx });
});