Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+102
@@ -0,0 +1,102 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { service, actor } = vi.hoisted(() => ({
|
||||
service: {
|
||||
listToday: vi.fn(),
|
||||
completeTask: vi.fn(),
|
||||
},
|
||||
actor: vi.fn((reason: string) => ({ adminUserId: "admin_1", reason })),
|
||||
}));
|
||||
|
||||
vi.mock(
|
||||
"~/components/screens/admin/sales/tasks/AdminSalesTodayPage",
|
||||
() => ({ AdminSalesTodayPage: () => null })
|
||||
);
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
getTranslations: vi.fn(async () => ({
|
||||
locale: "de-DE",
|
||||
t: (key: string) => key,
|
||||
})),
|
||||
}));
|
||||
vi.mock("~/services/admin/admin-sales-tasks.server", () => ({
|
||||
getAdminSalesTaskDayRange: () => ({
|
||||
start: new Date("2026-07-23T22:00:00.000Z"),
|
||||
end: new Date("2026-07-24T22:00:00.000Z"),
|
||||
}),
|
||||
runAsAdminSalesTaskOperator: vi.fn(
|
||||
async (_request, _permission, operation) =>
|
||||
operation({
|
||||
service,
|
||||
actor,
|
||||
adminUserId: "admin_1",
|
||||
timeZone: "Europe/Berlin",
|
||||
canWrite: true,
|
||||
})
|
||||
),
|
||||
}));
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
validateCSRFToken: vi.fn(),
|
||||
}));
|
||||
|
||||
import { runAsAdminSalesTaskOperator } from "~/services/admin/admin-sales-tasks.server";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
import { action, loader } from "./today";
|
||||
|
||||
describe("admin sales today route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
service.listToday.mockResolvedValue([]);
|
||||
service.completeTask.mockResolvedValue({ task: {}, nextTask: null });
|
||||
});
|
||||
|
||||
it("loads only the signed-in admin's task queue", async () => {
|
||||
const result = await loader({
|
||||
request: new Request("https://admin.hivegrid.de/sales/tasks/today"),
|
||||
params: {},
|
||||
context: {},
|
||||
} as never);
|
||||
|
||||
expect(runAsAdminSalesTaskOperator).toHaveBeenCalledWith(
|
||||
expect.any(Request),
|
||||
"admin_sales.crm.read",
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(service.listToday).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
assignedToAdminUserId: "admin_1",
|
||||
timeZone: "Europe/Berlin",
|
||||
})
|
||||
);
|
||||
expect(result.vm).toMatchObject({
|
||||
status: "empty",
|
||||
permissions: { canRead: true, canWrite: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("validates CSRF and completes through the write-gated task service", async () => {
|
||||
const response = await action({
|
||||
request: new Request("https://admin.hivegrid.de/sales/tasks/today", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
taskId: "task_1",
|
||||
reason: "Aufgabe heute erledigt",
|
||||
}),
|
||||
}),
|
||||
params: {},
|
||||
context: {},
|
||||
} as never);
|
||||
|
||||
expect(validateCSRFToken).toHaveBeenCalledOnce();
|
||||
expect(runAsAdminSalesTaskOperator).toHaveBeenCalledWith(
|
||||
expect.any(Request),
|
||||
"admin_sales.crm.write",
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(service.completeTask).toHaveBeenCalledWith({
|
||||
taskId: "task_1",
|
||||
actor: { adminUserId: "admin_1", reason: "Aufgabe heute erledigt" },
|
||||
});
|
||||
expect(response.status).toBe(303);
|
||||
});
|
||||
});
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
import { adminSalesTaskCompleteActionSchema, buildAdminSalesTodayVM } from "@hivegrid/view-models";
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useActionData, useLoaderData, useNavigation, useSubmit } from "react-router";
|
||||
|
||||
import { AdminSalesTodayPage } from "~/components/screens/admin/sales/tasks/AdminSalesTodayPage";
|
||||
import { AdminSalesLoadingPage } from "~/components/screens/admin/sales/AdminSalesLoadingPage";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAdminSalesTaskDayRange, runAsAdminSalesTaskOperator } from "~/services/admin/admin-sales-tasks.server";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { appendCSRFToken } from "~/utils/csrf";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
const { locale } = await getTranslations(request);
|
||||
return runAsAdminSalesTaskOperator(request, "admin_sales.crm.read", async ({ service, adminUserId, timeZone, canWrite }) => {
|
||||
const now = new Date();
|
||||
const tasks = await service.listToday({
|
||||
assignedToAdminUserId: adminUserId,
|
||||
now,
|
||||
timeZone,
|
||||
});
|
||||
const { start } = getAdminSalesTaskDayRange(now, timeZone);
|
||||
return {
|
||||
vm: buildAdminSalesTodayVM({
|
||||
now,
|
||||
todayStart: start,
|
||||
locale,
|
||||
timeZone,
|
||||
tasks,
|
||||
buildContactHref: (contactId) =>
|
||||
adminPath(`sales/contacts/${encodeURIComponent(contactId)}`, {
|
||||
mode: "host-aware",
|
||||
}),
|
||||
buildDealHref: (dealId) =>
|
||||
adminPath(`sales/deals/${encodeURIComponent(dealId)}`, {
|
||||
mode: "host-aware",
|
||||
}),
|
||||
permissions: { canRead: true, canWrite },
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
const form = await request.formData();
|
||||
try {
|
||||
const command = adminSalesTaskCompleteActionSchema.parse({
|
||||
taskId: form.get("taskId"),
|
||||
reason: form.get("reason"),
|
||||
});
|
||||
await runAsAdminSalesTaskOperator(request, "admin_sales.crm.write", ({ service, actor }) =>
|
||||
service.completeTask({
|
||||
taskId: command.taskId,
|
||||
actor: actor(command.reason),
|
||||
})
|
||||
);
|
||||
const target = new URL(adminPath("sales/tasks/today", { mode: "host-aware" }), request.url);
|
||||
target.searchParams.set("toast", "admin.sales.tasks.toast.completed");
|
||||
return Response.redirect(target, 303);
|
||||
} catch {
|
||||
return Response.json({ error: t("admin.sales.tasks.errors.actionFailed") }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
export function HydrateFallback(): React.JSX.Element {
|
||||
return <AdminSalesLoadingPage />;
|
||||
}
|
||||
|
||||
export default function AdminSalesTodayRoute(): React.JSX.Element {
|
||||
const { t } = useTranslation();
|
||||
const { vm } = useLoaderData<typeof loader>();
|
||||
const actionData = useActionData() as { readonly error?: string } | undefined;
|
||||
const navigation = useNavigation();
|
||||
const submit = useSubmit();
|
||||
return (
|
||||
<AdminSalesTodayPage
|
||||
vm={vm}
|
||||
submitting={navigation.state === "submitting"}
|
||||
errorMessage={actionData?.error ?? null}
|
||||
actions={{
|
||||
onComplete: (taskId) => {
|
||||
const form = appendCSRFToken(new FormData());
|
||||
form.set("taskId", taskId);
|
||||
form.set("reason", t("admin.sales.tasks.audit.completed"));
|
||||
submit(form, { method: "post" });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user