mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-21 04:56:40 +02:00
add invalidate launcher cache on refresh
This commit is contained in:
@@ -500,6 +500,12 @@ authenticated.get(
|
|||||||
launcher.listLauncherViews
|
launcher.listLauncherViews
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.post(
|
||||||
|
"/org/:orgId/launcher/invalidate-cache",
|
||||||
|
verifyOrgAccess,
|
||||||
|
launcher.invalidateLauncherCache
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.post(
|
authenticated.post(
|
||||||
"/org/:orgId/launcher/views",
|
"/org/:orgId/launcher/views",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ export { updateLauncherView } from "./updateLauncherView";
|
|||||||
export { deleteLauncherView } from "./deleteLauncherView";
|
export { deleteLauncherView } from "./deleteLauncherView";
|
||||||
export { upsertLauncherDefaultView } from "./upsertLauncherDefaultView";
|
export { upsertLauncherDefaultView } from "./upsertLauncherDefaultView";
|
||||||
export { deleteLauncherDefaultView } from "./deleteLauncherDefaultView";
|
export { deleteLauncherDefaultView } from "./deleteLauncherDefaultView";
|
||||||
|
export { invalidateLauncherCache } from "./invalidateLauncherCache";
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { regionalCache as cache } from "#dynamic/lib/cache";
|
||||||
|
import { response } from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
|
||||||
|
async function invalidateLauncherCacheForUser(
|
||||||
|
orgId: string,
|
||||||
|
userId: string
|
||||||
|
): Promise<void> {
|
||||||
|
const prefixes = [
|
||||||
|
`launcherAccessibleIds:${orgId}:${userId}:`,
|
||||||
|
`launcher:groups:${orgId}:${userId}:`,
|
||||||
|
`launcher:results:${orgId}:${userId}:`,
|
||||||
|
`launcher:scale:counts:${orgId}:${userId}:`
|
||||||
|
];
|
||||||
|
|
||||||
|
const keys = (
|
||||||
|
await Promise.all(
|
||||||
|
prefixes.map((prefix) => cache.keysWithPrefix(prefix))
|
||||||
|
)
|
||||||
|
).flat();
|
||||||
|
|
||||||
|
if (keys.length > 0) {
|
||||||
|
await cache.del(keys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function invalidateLauncherCache(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const orgId = req.userOrgId;
|
||||||
|
const userId = req.user!.userId;
|
||||||
|
|
||||||
|
if (!orgId) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid organization ID")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await invalidateLauncherCacheForUser(orgId, userId);
|
||||||
|
|
||||||
|
return response(res, {
|
||||||
|
data: null,
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Launcher cache invalidated successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (createHttpError.isHttpError(error)) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
console.error("Error invalidating launcher cache:", error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Internal server error"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@ import {
|
|||||||
type LauncherViewConfig,
|
type LauncherViewConfig,
|
||||||
type LauncherViewRecord
|
type LauncherViewRecord
|
||||||
} from "@server/routers/launcher/types";
|
} from "@server/routers/launcher/types";
|
||||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { Search } from "lucide-react";
|
import { Search } from "lucide-react";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
@@ -105,6 +105,7 @@ export default function ResourceLauncher({
|
|||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { env } = useEnvContext();
|
const { env } = useEnvContext();
|
||||||
const api = createApiClient({ env });
|
const api = createApiClient({ env });
|
||||||
|
const queryClient = useQueryClient();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { navigate, isNavigating, searchParams } = useNavigationContext();
|
const { navigate, isNavigating, searchParams } = useNavigationContext();
|
||||||
const [isRefreshing, startRefreshTransition] = useTransition();
|
const [isRefreshing, startRefreshTransition] = useTransition();
|
||||||
@@ -489,6 +490,10 @@ export default function ResourceLauncher({
|
|||||||
const refreshData = () => {
|
const refreshData = () => {
|
||||||
startRefreshTransition(async () => {
|
startRefreshTransition(async () => {
|
||||||
try {
|
try {
|
||||||
|
await api.post(`/org/${orgId}/launcher/invalidate-cache`);
|
||||||
|
await queryClient.invalidateQueries({
|
||||||
|
queryKey: ["ORG", orgId, "LAUNCHER"]
|
||||||
|
});
|
||||||
router.refresh();
|
router.refresh();
|
||||||
} catch {
|
} catch {
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
Reference in New Issue
Block a user