173 lines
6.5 KiB
TypeScript
173 lines
6.5 KiB
TypeScript
import { buildQueueMonitorVM } from "@hivegrid/view-models";
|
|
import { LoaderFunctionArgs, MetaFunction, useActionData, useLoaderData, useNavigation, type ActionFunctionArgs } from "react-router";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { AdminJobsPage } from "~/components/screens/admin/AdminJobsPage";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import { getUser } from "~/utils/db/users.db.server";
|
|
import { userHasPermission } from "~/utils/db/permissions/userRoles.db.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import {
|
|
getQueueFailedJobsSnapshot,
|
|
getQueueJobDetail,
|
|
getQueueMonitorSnapshot,
|
|
pauseQueue,
|
|
QueueMonitorControlError,
|
|
resumeQueue,
|
|
retryQueueJob,
|
|
type QueueFailedJobsSnapshot,
|
|
type QueueJobDetail,
|
|
} from "~/services/admin/queue-monitor.server";
|
|
import { AdminActionInputError } from "~/services/admin/admin-action-audit.server";
|
|
import { ADMIN_AREA_ACCESS } from "~/services/admin/admin-access-contract";
|
|
import {
|
|
PORTAL_DOMAIN_KILL_SWITCH_PERMISSION,
|
|
PortalDomainOperatorError,
|
|
setOperatorPortalDomainKillSwitch,
|
|
} from "~/services/admin/portal-domain-operator.server";
|
|
import { PortalDomainError } from "~/services/customer-portal/portal-domain.server";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
vm: ReturnType<typeof buildQueueMonitorVM>;
|
|
failedJobs: QueueFailedJobsSnapshot[];
|
|
canManagePortalDomainKillSwitch: boolean;
|
|
jobDetail: { queueName: string; job: QueueJobDetail } | null;
|
|
};
|
|
|
|
type ActionData = {
|
|
success?: string;
|
|
error?: string;
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, ADMIN_AREA_ACCESS.jobs);
|
|
const { t } = await getTranslations(request);
|
|
const userInfo = await getUserInfo(request);
|
|
const canManagePortalDomainKillSwitch = await userHasPermission(
|
|
userInfo.userId,
|
|
null,
|
|
PORTAL_DOMAIN_KILL_SWITCH_PERMISSION,
|
|
);
|
|
|
|
const url = new URL(request.url);
|
|
const detailQueue = url.searchParams.get("detailQueue");
|
|
const detailJob = url.searchParams.get("detailJob");
|
|
|
|
try {
|
|
const [queues, failedJobs] = await Promise.all([getQueueMonitorSnapshot(), getQueueFailedJobsSnapshot(5)]);
|
|
let jobDetail: LoaderData["jobDetail"] = null;
|
|
if (detailQueue && detailJob) {
|
|
try {
|
|
const job = await getQueueJobDetail(detailQueue, detailJob);
|
|
jobDetail = job ? { queueName: detailQueue, job } : null;
|
|
} catch {
|
|
jobDetail = null;
|
|
}
|
|
}
|
|
return {
|
|
title: `${t("admin.jobs.title")} | ${process.env.APP_NAME}`,
|
|
vm: buildQueueMonitorVM({ queues }),
|
|
failedJobs,
|
|
canManagePortalDomainKillSwitch,
|
|
jobDetail,
|
|
} satisfies LoaderData;
|
|
} catch {
|
|
return {
|
|
title: `${t("admin.jobs.title")} | ${process.env.APP_NAME}`,
|
|
vm: { status: "error", error: { messageKey: "admin.jobs.errors.queueUnavailable", code: "QUEUE_UNAVAILABLE" } },
|
|
failedJobs: [],
|
|
canManagePortalDomainKillSwitch,
|
|
jobDetail: null,
|
|
} satisfies LoaderData;
|
|
}
|
|
};
|
|
|
|
function badRequest(data: ActionData) {
|
|
return Response.json(data, { status: 400 });
|
|
}
|
|
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
await verifyUserHasPermission(request, ADMIN_AREA_ACCESS.jobs);
|
|
const { t } = await getTranslations(request);
|
|
await validateCSRFToken(request);
|
|
|
|
const form = await request.formData();
|
|
const action = form.get("action")?.toString() ?? "";
|
|
const queueName = form.get("queueName")?.toString() ?? "";
|
|
if (action === "portal-domain-kill-enable" || action === "portal-domain-kill-disable") {
|
|
await verifyUserHasPermission(request, PORTAL_DOMAIN_KILL_SWITCH_PERMISSION);
|
|
}
|
|
const userInfo = await getUserInfo(request);
|
|
const actor = await getUser(userInfo.userId);
|
|
const baseInput = { queueName, actorUserId: userInfo.userId, actorEmail: actor?.email ?? null, request };
|
|
|
|
try {
|
|
if (action === "pause") {
|
|
await pauseQueue(baseInput);
|
|
return Response.json({ success: t("admin.jobs.actions.paused") } satisfies ActionData);
|
|
}
|
|
if (action === "resume") {
|
|
await resumeQueue(baseInput);
|
|
return Response.json({ success: t("admin.jobs.actions.resumed") } satisfies ActionData);
|
|
}
|
|
if (action === "retry-job") {
|
|
await retryQueueJob({ ...baseInput, jobId: form.get("jobId")?.toString() ?? "" });
|
|
return Response.json({ success: t("admin.jobs.actions.retried") } satisfies ActionData);
|
|
}
|
|
if (action === "portal-domain-kill-enable" || action === "portal-domain-kill-disable") {
|
|
const enabled = action === "portal-domain-kill-enable";
|
|
await setOperatorPortalDomainKillSwitch({
|
|
tenantId: form.get("tenantId")?.toString() ?? "",
|
|
actorUserId: userInfo.userId,
|
|
enabled,
|
|
reason: form.get("reason")?.toString() ?? "",
|
|
request,
|
|
});
|
|
return Response.json({
|
|
success: t(enabled ? "admin.jobs.portalDomainKillSwitch.enabled" : "admin.jobs.portalDomainKillSwitch.disabled"),
|
|
} satisfies ActionData);
|
|
}
|
|
return badRequest({ error: t("shared.invalidForm") });
|
|
} catch (error) {
|
|
if (error instanceof AdminActionInputError) {
|
|
return badRequest({ error: t(error.i18nKey) });
|
|
}
|
|
if (error instanceof PortalDomainOperatorError) {
|
|
return badRequest({ error: t("admin.jobs.errors.portalDomainKillSwitchNotAllowed") });
|
|
}
|
|
if (error instanceof PortalDomainError) {
|
|
return badRequest({ error: t("admin.jobs.errors.portalDomainKillSwitchFailed") });
|
|
}
|
|
if (error instanceof QueueMonitorControlError && error.code === "DIRECT_OUTBOX_RETRY_FORBIDDEN") {
|
|
return badRequest({ error: t("admin.jobs.errors.directOutboxRetryForbidden") });
|
|
}
|
|
return badRequest({ error: t("admin.jobs.errors.actionFailed") });
|
|
}
|
|
};
|
|
|
|
export default function AdminJobsRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const navigation = useNavigation();
|
|
const rootData = useRootData();
|
|
return (
|
|
<AdminJobsPage
|
|
vm={data.vm}
|
|
failedJobs={data.failedJobs}
|
|
csrf={rootData.csrf ?? ""}
|
|
submitting={navigation.state === "submitting"}
|
|
feedback={actionData}
|
|
canManagePortalDomainKillSwitch={data.canManagePortalDomainKillSwitch}
|
|
jobDetail={data.jobDetail}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|