Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/integrations/hrPayslipService.atomic.test.ts
T

122 lines
3.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
employeeFindFirst: vi.fn(),
fileFindFirst: vi.fn(),
payslipFindUnique: vi.fn(),
payslipUpsert: vi.fn(),
linkDeleteMany: vi.fn(),
linkUpsert: vi.fn(),
activity: vi.fn(),
notify: vi.fn(),
audit: vi.fn(),
}));
const tx = {
hrEmployeeProfile: { findFirst: mocks.employeeFindFirst },
file: { findFirst: mocks.fileFindFirst },
employeePayslip: {
findUnique: mocks.payslipFindUnique,
upsert: mocks.payslipUpsert,
},
fileEntityLink: {
deleteMany: mocks.linkDeleteMany,
upsert: mocks.linkUpsert,
},
};
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
withTenantRLS: vi.fn(async (_tenantId: string, callback: (client: typeof tx) => Promise<unknown>) => callback(tx)),
}));
vi.mock("@hivegrid/activity", () => ({ logActivity: mocks.activity }));
vi.mock("@hivegrid/db/audit-events.server", () => ({
createAuditEvent: mocks.audit,
createAuditEventInExistingTransaction: mocks.audit,
}));
vi.mock("@hivegrid/foundation", async (importOriginal) => {
const actual = await importOriginal<typeof import("@hivegrid/foundation")>();
return { ...actual, notify: mocks.notify };
});
import { createPayslip } from "./hrPayslipService.server";
describe("createPayslip atomic file ownership", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.employeeFindFirst.mockResolvedValue({ userId: "employee_1" });
mocks.fileFindFirst.mockResolvedValue({ id: "file_new" });
mocks.payslipFindUnique.mockResolvedValue({ fileId: "file_old" });
mocks.payslipUpsert.mockResolvedValue({
id: "payslip_1",
tenantId: "tenant_1",
employeeUserId: "employee_1",
uploadedByUserId: "hr_1",
periodYear: 2026,
periodMonth: 7,
fileId: "file_new",
isRead: false,
readAt: null,
createdAt: new Date("2026-07-25T10:00:00.000Z"),
updatedAt: new Date("2026-07-25T10:00:00.000Z"),
archivedAt: null,
});
mocks.linkDeleteMany.mockResolvedValue({ count: 1 });
mocks.linkUpsert.mockResolvedValue({ id: "link_1" });
mocks.activity.mockResolvedValue(undefined);
mocks.notify.mockResolvedValue(undefined);
mocks.audit.mockResolvedValue(undefined);
});
it("validates tenant-owned targets and links the uploaded file in the payslip transaction", async () => {
await createPayslip({
tenantId: "tenant_1",
employeeUserId: "employee_1",
uploadedByUserId: "hr_1",
periodYear: 2026,
periodMonth: 7,
fileId: "file_new",
});
expect(mocks.employeeFindFirst).toHaveBeenCalledWith({
where: { tenantId: "tenant_1", userId: "employee_1" },
select: { userId: true },
});
expect(mocks.fileFindFirst).toHaveBeenCalledWith({
where: { tenantId: "tenant_1", id: "file_new", archivedAt: null },
select: { id: true },
});
expect(mocks.linkUpsert).toHaveBeenCalledWith(
expect.objectContaining({
create: expect.objectContaining({
tenantId: "tenant_1",
fileId: "file_new",
entityType: "user",
entityId: "employee_1",
linkRole: "hr_payslip",
}),
}),
);
expect(mocks.activity).toHaveBeenCalledOnce();
expect(mocks.notify).toHaveBeenCalledOnce();
expect(mocks.audit).toHaveBeenCalledOnce();
});
it("fails closed before persistence for a cross-tenant employee or file", async () => {
mocks.employeeFindFirst.mockResolvedValueOnce(null);
await expect(
createPayslip({
tenantId: "tenant_1",
employeeUserId: "employee_foreign",
uploadedByUserId: "hr_1",
periodYear: 2026,
periodMonth: 7,
fileId: "file_new",
}),
).rejects.toMatchObject({ name: "CatalogError" });
expect(mocks.payslipUpsert).not.toHaveBeenCalled();
expect(mocks.linkUpsert).not.toHaveBeenCalled();
});
});