159 lines
6.8 KiB
TypeScript
159 lines
6.8 KiB
TypeScript
// AK-P16a · calendar connection detail route (M25 Block B/F). Owner-only (or
|
|
// calendar.admin) view of one connection: selected calendars, status, and the last
|
|
// 50 conflicts in Klartext (DoD 4). Disconnect action revokes + soft-deletes. A
|
|
// non-owner / missing-permission / unknown-id all render 404 (Bauprinzip A.5.3).
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useLoaderData, Form } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
buildCalendarConnectionDetailVM,
|
|
calendarConflictResolutionKey,
|
|
calendarConnectionStatusKey,
|
|
type CalendarConnectionDetailVM,
|
|
type ConflictLogRow,
|
|
type SelectedCalendarRow,
|
|
} from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { loadCalendarContext, requireCalendarPermission, requireConnectionOwner, calendarHasPermission, CALENDAR_PERMISSIONS } from "~/modules/calendar/routes/calendarAccess.server";
|
|
import { CALENDAR_MODULE_KEY, calendarConnectionDetailPath, calendarConnectionsPath, notFoundResponse } from "~/modules/calendar/routes/calendarRoutePaths";
|
|
import { getConnectionForTenant, listConflictsForConnection } from "~/modules/calendar/db/calendar.db.server";
|
|
import { disconnectConnection } from "~/modules/calendar/services/CalendarConnectionService.server";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: CalendarConnectionDetailVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadCalendarContext({ request, params, requireModule: true });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const connectionId = params.id ?? "";
|
|
|
|
try {
|
|
requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const connection = await getConnectionForTenant(ctx.tenantId, connectionId);
|
|
if (!connection) {
|
|
throw notFoundResponse();
|
|
}
|
|
try {
|
|
requireConnectionOwner(ctx, connection.userId);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const conflictRows = await listConflictsForConnection(ctx.tenantId, connectionId);
|
|
const conflicts: ConflictLogRow[] = conflictRows.map((c) => ({
|
|
id: c.id,
|
|
resolution: c.resolution,
|
|
resolutionKey: calendarConflictResolutionKey(c.resolution),
|
|
descriptionText: t(c.descriptionText),
|
|
occurredAt: c.createdAt.toISOString(),
|
|
}));
|
|
|
|
const rawCalendars = Array.isArray(connection.selectedCalendars) ? (connection.selectedCalendars as Array<Record<string, unknown>>) : [];
|
|
const selectedCalendars: SelectedCalendarRow[] = rawCalendars.map((c) => ({
|
|
externalCalendarId: String(c.externalCalendarId ?? c.id ?? ""),
|
|
name: String(c.name ?? c.externalCalendarId ?? ""),
|
|
isPrimary: Boolean(c.isPrimary),
|
|
}));
|
|
|
|
const vm = buildCalendarConnectionDetailVM({
|
|
id: connection.id,
|
|
externalAccountEmail: connection.externalAccountEmail,
|
|
status: connection.status,
|
|
statusKey: calendarConnectionStatusKey(connection.status),
|
|
syncDirection: connection.syncDirection,
|
|
lastSyncAt: connection.lastSyncAt ? connection.lastSyncAt.toISOString() : null,
|
|
lastErrorText: connection.lastErrorText,
|
|
selectedCalendars,
|
|
conflicts,
|
|
canManage: calendarHasPermission(ctx, CALENDAR_PERMISSIONS.connectOwn),
|
|
disconnectHref: calendarConnectionDetailPath(tenantSlug, connection.id),
|
|
});
|
|
return Response.json({ title: `${t("calendar.detail.title")} | ${process.env.APP_NAME}`, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadCalendarContext({ request, params, requireModule: true });
|
|
requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn);
|
|
const connectionId = params.id ?? "";
|
|
const connection = await getConnectionForTenant(ctx.tenantId, connectionId);
|
|
if (!connection) {
|
|
throw notFoundResponse();
|
|
}
|
|
requireConnectionOwner(ctx, connection.userId);
|
|
await validateCSRFToken(request);
|
|
|
|
const form = await request.formData();
|
|
if (form.get("intent") !== "disconnect") {
|
|
throw notFoundResponse();
|
|
}
|
|
await disconnectConnection({ tenantId: ctx.tenantId, connectionId });
|
|
return redirect(calendarConnectionsPath(params.tenant ?? ""));
|
|
};
|
|
|
|
export default function CalendarConnectionDetailRoute() {
|
|
const { t } = useTranslation();
|
|
const rootData = useRootData();
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <main className="mx-auto max-w-3xl px-4 py-10" data-ui-id="ui.calendar.detail" />;
|
|
}
|
|
const { externalAccountEmail, statusKey, selectedCalendars, conflicts, conflictsEmptyKey, canManage } = vm.data;
|
|
|
|
return (
|
|
<main className="mx-auto max-w-3xl px-4 py-10" data-ui-id="ui.calendar.detail">
|
|
<h1 className="text-foreground text-xl font-semibold">{externalAccountEmail}</h1>
|
|
<p className="text-muted-foreground mt-1 text-sm">{t(statusKey)}</p>
|
|
|
|
<section className="mt-6" data-ui-id="ui.calendar.detail.calendars">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("calendar.detail.calendarsHeading")}</h2>
|
|
<ul className="mt-2 text-sm">
|
|
{selectedCalendars.map((cal) => (
|
|
<li key={cal.externalCalendarId} data-row-id={cal.externalCalendarId}>
|
|
{cal.name}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</section>
|
|
|
|
<section className="mt-6" data-ui-id="ui.calendar.detail.conflicts">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("calendar.detail.conflictsHeading")}</h2>
|
|
{conflicts.length === 0 ? (
|
|
<p className="text-muted-foreground mt-2 text-sm">{t(conflictsEmptyKey)}</p>
|
|
) : (
|
|
<ul className="mt-2 divide-border divide-y text-sm">
|
|
{conflicts.map((conflict) => (
|
|
<li key={conflict.id} className="py-2" data-row-id={conflict.id}>
|
|
<span className="text-foreground">{conflict.descriptionText}</span>
|
|
<span className="text-muted-foreground ml-2 text-xs">{t(conflict.resolutionKey)}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</section>
|
|
|
|
{canManage && (
|
|
<Form method="post" className="mt-8">
|
|
{rootData.csrf && <input type="hidden" name="csrf" value={rootData.csrf} />}
|
|
<input type="hidden" name="intent" value="disconnect" />
|
|
<button type="submit" className="border-border text-foreground rounded-md border px-4 py-2 text-sm" data-ui-id="ui.calendar.detail.disconnect">
|
|
{t("calendar.detail.disconnect")}
|
|
</button>
|
|
</Form>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: CALENDAR_MODULE_KEY };
|