109 lines
4.5 KiB
TypeScript
109 lines
4.5 KiB
TypeScript
// R-P5 (P5) §P3 — sync history for one connection. Loader builds SyncHistoryVM
|
|
// from the AppSyncRun list. Requires integrations.view_logs. Manual re-sync posts
|
|
// back to the app route's action. Renders the interim SyncHistoryPage from ~/ui
|
|
// (AD-28); final optic = D-P5 delta.
|
|
|
|
import { redirect, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildSyncHistoryVM, syncKindKey, syncStatusKey, type SyncRunRow, type SyncHistoryVM } from "@hivegrid/view-models";
|
|
import { getCatalogEntry } from "@hivegrid/integrations";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { integrationsHasPermission, loadIntegrationsContext, notFoundResponse, requireIntegrationsPermission } from "~/utils/integrations/integrationsAccess.server";
|
|
import { getConnectionOrThrow, listSyncRuns } from "~/utils/integrations/appsEngineService.server";
|
|
import { integrationsAppPath, integrationsCatalogPath, integrationsSyncsPath, legacyIntegrationsRedirectPath } from "~/utils/integrations/integrationsRoutePaths";
|
|
import { requireKwpIntegrationModule } from "~/modules/kwp/routes/kwpAccess.server";
|
|
import { SyncHistoryPage, type SyncHistoryActions } from "~/components/screens/integrations/SyncHistoryPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { useSaveToast, type SaveActionResult } from "~/hooks/use-save-toast";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
breadcrumbAncestors: { label: string; href: string }[];
|
|
actionHref: string;
|
|
runHrefBase: string;
|
|
vm: SyncHistoryVM;
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const legacyRedirect = legacyIntegrationsRedirectPath(request.url, params.tenant ?? "");
|
|
if (legacyRedirect) {
|
|
throw redirect(legacyRedirect);
|
|
}
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadIntegrationsContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const appKey = params.app ?? "";
|
|
const connectionId = params.connectionId ?? "";
|
|
|
|
if (!ctx.hasModule) {
|
|
throw notFoundResponse();
|
|
}
|
|
try {
|
|
requireIntegrationsPermission(ctx, "integrations.view_logs");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const app = getCatalogEntry(appKey);
|
|
if (!app) {
|
|
throw notFoundResponse();
|
|
}
|
|
if (appKey === "kwp") {
|
|
await requireKwpIntegrationModule(ctx);
|
|
throw notFoundResponse();
|
|
}
|
|
// Tenant-scoped; 404 if the connection is not in this tenant.
|
|
await getConnectionOrThrow(ctx.tenantId, connectionId, appKey);
|
|
|
|
const runs = await listSyncRuns(ctx.tenantId, connectionId, appKey);
|
|
const rows: SyncRunRow[] = runs.map((run) => ({
|
|
id: run.id,
|
|
kind: run.kind,
|
|
kindKey: syncKindKey(run.kind),
|
|
status: run.status,
|
|
statusKey: syncStatusKey(run.status),
|
|
itemsTotal: run.itemsTotal,
|
|
itemsSucceeded: run.itemsSucceeded,
|
|
itemsFailed: run.itemsFailed,
|
|
summaryKey: run.summaryText,
|
|
startedIso: run.startedAt ? run.startedAt.toISOString() : null,
|
|
completedIso: run.completedAt ? run.completedAt.toISOString() : null,
|
|
}));
|
|
|
|
const vm = buildSyncHistoryVM({ connectionId, rows, canManualSync: integrationsHasPermission(ctx, "integrations.manual_sync") });
|
|
return Response.json({
|
|
// Breadcrumb: … > Erweiterungen > Integrationen > <app title> > Sync-Historie.
|
|
title: `${t("integrations.syncs.title")} | ${process.env.APP_NAME}`,
|
|
breadcrumbAncestors: [
|
|
{ label: t("integrations.nav.title"), href: integrationsCatalogPath(tenantSlug) },
|
|
{ label: t(app.titleKey), href: integrationsAppPath(tenantSlug, appKey) },
|
|
],
|
|
actionHref: integrationsAppPath(tenantSlug, appKey),
|
|
runHrefBase: integrationsSyncsPath(tenantSlug, appKey, connectionId),
|
|
vm,
|
|
} satisfies LoaderData);
|
|
};
|
|
|
|
type MutationResponse = { ok?: boolean; error?: string };
|
|
|
|
export default function IntegrationsSyncHistoryRoute() {
|
|
const { actionHref, runHrefBase, vm } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher<MutationResponse>();
|
|
const submitting = fetcher.state !== "idle";
|
|
|
|
useSaveToast(fetcher.data as SaveActionResult);
|
|
|
|
const actions: SyncHistoryActions = {
|
|
onManualSync: (connectionId: string) => {
|
|
const formData = new FormData();
|
|
formData.set("intent", "manual-sync");
|
|
formData.set("connectionId", connectionId);
|
|
fetcher.submit(formData, { method: "post", action: actionHref });
|
|
},
|
|
};
|
|
|
|
return <SyncHistoryPage vm={vm} runHrefBase={runHrefBase} actions={actions} submitting={submitting} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|