267 lines
7.9 KiB
TypeScript
267 lines
7.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
const { createAuditEvent } = vi.hoisted(() => ({
|
|
createAuditEvent: vi.fn(async () => ({ id: "audit-1" })),
|
|
}));
|
|
|
|
vi.mock("./audit-events.server.js", () => ({
|
|
createAuditEventInExistingTransaction: createAuditEvent,
|
|
}));
|
|
|
|
import {
|
|
createAdminSalesCoreService,
|
|
createPrismaAdminSalesCoreRepository,
|
|
writeAdminSalesAudit,
|
|
type AdminSalesCoreRepository,
|
|
type CreateAdminCompanyInput,
|
|
type CreateAdminContactInput,
|
|
type CreateAdminDealInput,
|
|
} from "./admin-sales-core.server.js";
|
|
|
|
const actor = {
|
|
adminUserId: "admin-1",
|
|
email: "sales@example.test",
|
|
reason: "Neuen Datensatz aus qualifiziertem Lead anlegen",
|
|
} as const;
|
|
|
|
function serviceFixture() {
|
|
const repository = {
|
|
createCompany: vi.fn(async (input: CreateAdminCompanyInput) => input as never),
|
|
createContact: vi.fn(async (input: CreateAdminContactInput) => input as never),
|
|
createDeal: vi.fn(async (input: CreateAdminDealInput) => input as never),
|
|
} satisfies AdminSalesCoreRepository;
|
|
const authorize = vi.fn(async () => undefined);
|
|
return { repository, authorize, service: createAdminSalesCoreService({ repository, authorize }) };
|
|
}
|
|
|
|
describe("admin sales core service", () => {
|
|
it("uses the smallest CRM write permission and normalizes a company", async () => {
|
|
const { service, repository, authorize } = serviceFixture();
|
|
|
|
await service.createCompany({
|
|
name: " Hive Grid GmbH ",
|
|
domain: "WWW.HIVEGRID.DE",
|
|
website: "https://hivegrid.de",
|
|
phone: "+49 (30) 123 456",
|
|
countryCode: "de",
|
|
actor,
|
|
});
|
|
|
|
expect(authorize).toHaveBeenCalledWith("admin_sales.crm.write");
|
|
expect(repository.createCompany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: "Hive Grid GmbH",
|
|
domain: "hivegrid.de",
|
|
website: "https://hivegrid.de/",
|
|
countryCode: "DE",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("normalizes contact identity before the repository sees PII", async () => {
|
|
const { service, repository } = serviceFixture();
|
|
|
|
await service.createContact({
|
|
firstName: " Ada ",
|
|
lastName: " Lovelace ",
|
|
email: " ADA@Example.TEST ",
|
|
phone: "+44 (0)20 1234 5678",
|
|
actor,
|
|
});
|
|
|
|
expect(repository.createContact).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
firstName: "Ada",
|
|
lastName: "Lovelace",
|
|
email: "ada@example.test",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("rejects invalid deal values before any database write", async () => {
|
|
const { service, repository } = serviceFixture();
|
|
|
|
await expect(
|
|
service.createDeal({
|
|
title: "Enterprise",
|
|
pipelineId: "pipeline-1",
|
|
stageId: "stage-1",
|
|
ownerAdminUserId: "admin-1",
|
|
probabilityBasisPoints: 10_001,
|
|
actor,
|
|
})
|
|
).rejects.toThrow("probability");
|
|
expect(repository.createDeal).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("admin sales Prisma repository", () => {
|
|
it("writes every sales audit into the tenantless admin chain", async () => {
|
|
const tx = { auditEvent: { create: vi.fn() } };
|
|
|
|
await writeAdminSalesAudit(tx as never, {
|
|
actor: {
|
|
...actor,
|
|
requestId: "request-1",
|
|
ip: "127.0.0.1",
|
|
userAgent: "contract-test",
|
|
},
|
|
action: "admin.sales.contract.checked",
|
|
targetType: "AdminDeal",
|
|
targetId: "deal-1",
|
|
after: { status: "won" },
|
|
});
|
|
|
|
expect(createAuditEvent).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
tenantId: null,
|
|
actorId: "admin-1",
|
|
actorType: "admin",
|
|
actorEmail: "sales@example.test",
|
|
action: "admin.sales.contract.checked",
|
|
targetType: "AdminDeal",
|
|
targetId: "deal-1",
|
|
sourceModule: "admin-sales",
|
|
metadata: { reason: actor.reason },
|
|
}),
|
|
tx
|
|
);
|
|
});
|
|
|
|
it("creates company, activity and audit inside one transaction", async () => {
|
|
const company = {
|
|
id: "company-1",
|
|
name: "Hive Grid GmbH",
|
|
sourceLeadId: "lead-1",
|
|
ownerAdminUserId: "admin-1",
|
|
};
|
|
const tx = {
|
|
adminLead: {
|
|
findUnique: vi.fn(async () => ({ id: "lead-1", archivedAt: null })),
|
|
},
|
|
adminCompany: {
|
|
create: vi.fn(async () => company),
|
|
},
|
|
adminActivity: {
|
|
create: vi.fn(async () => ({ id: "activity-1" })),
|
|
},
|
|
};
|
|
const client = {
|
|
$transaction: vi.fn(async (run: (transaction: typeof tx) => Promise<unknown>) => run(tx)),
|
|
};
|
|
const writeAudit = vi.fn(async () => undefined);
|
|
const repository = createPrismaAdminSalesCoreRepository(client as never, { writeAudit });
|
|
|
|
await repository.createCompany({
|
|
name: "Hive Grid GmbH",
|
|
sourceLeadId: "lead-1",
|
|
ownerAdminUserId: "admin-1",
|
|
actor,
|
|
});
|
|
|
|
expect(client.$transaction).toHaveBeenCalledOnce();
|
|
expect(tx.adminCompany.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
name: "Hive Grid GmbH",
|
|
normalizedName: "hive grid gmbh",
|
|
sourceLeadId: "lead-1",
|
|
}),
|
|
});
|
|
expect(tx.adminActivity.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
type: "company_created",
|
|
actorAdminUserId: "admin-1",
|
|
leadId: "lead-1",
|
|
companyId: "company-1",
|
|
}),
|
|
});
|
|
expect(writeAudit).toHaveBeenCalledWith(
|
|
tx,
|
|
expect.objectContaining({
|
|
action: "admin.sales.company.created",
|
|
targetType: "AdminCompany",
|
|
targetId: "company-1",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("creates a deal with stable buying-center membership and initial stage history", async () => {
|
|
const tx = {
|
|
adminPipelineStage: {
|
|
findFirst: vi.fn(async () => ({ id: "stage-1", probabilityBasisPoints: 3_000 })),
|
|
},
|
|
adminLead: {
|
|
findUnique: vi.fn(async () => ({ id: "lead-1", archivedAt: null })),
|
|
},
|
|
adminCompany: {
|
|
findUnique: vi.fn(async () => ({ id: "company-1", archivedAt: null })),
|
|
},
|
|
adminContact: {
|
|
count: vi.fn(async () => 2),
|
|
},
|
|
adminDeal: {
|
|
create: vi.fn(async () => ({
|
|
id: "deal-1",
|
|
title: "HiveGrid SaaS",
|
|
pipelineId: "pipeline-1",
|
|
stageId: "stage-1",
|
|
companyId: "company-1",
|
|
sourceLeadId: "lead-1",
|
|
ownerAdminUserId: "admin-1",
|
|
amountCents: 120_000,
|
|
currencyCode: "EUR",
|
|
})),
|
|
},
|
|
adminActivity: {
|
|
create: vi.fn(async () => ({ id: "activity-1" })),
|
|
},
|
|
};
|
|
const client = {
|
|
$transaction: vi.fn(async (run: (transaction: typeof tx) => Promise<unknown>) => run(tx)),
|
|
};
|
|
const writeAudit = vi.fn(async () => undefined);
|
|
const repository = createPrismaAdminSalesCoreRepository(client as never, { writeAudit });
|
|
|
|
await repository.createDeal({
|
|
title: "HiveGrid SaaS",
|
|
pipelineId: "pipeline-1",
|
|
stageId: "stage-1",
|
|
companyId: "company-1",
|
|
sourceLeadId: "lead-1",
|
|
ownerAdminUserId: "admin-1",
|
|
amountCents: 120_000,
|
|
contactIds: ["contact-2", "contact-1", "contact-2"],
|
|
actor,
|
|
});
|
|
|
|
expect(tx.adminContact.count).toHaveBeenCalledWith({
|
|
where: { id: { in: ["contact-2", "contact-1"] }, archivedAt: null },
|
|
});
|
|
expect(tx.adminDeal.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
probabilityBasisPoints: 3_000,
|
|
contacts: {
|
|
create: [
|
|
{ contactId: "contact-2", isPrimary: true },
|
|
{ contactId: "contact-1", isPrimary: false },
|
|
],
|
|
},
|
|
history: {
|
|
create: expect.objectContaining({
|
|
toStageId: "stage-1",
|
|
actorAdminUserId: "admin-1",
|
|
reason: "deal_created",
|
|
}),
|
|
},
|
|
}),
|
|
});
|
|
expect(tx.adminActivity.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
type: "deal_created",
|
|
dealId: "deal-1",
|
|
payload: expect.objectContaining({ contactCount: 2 }),
|
|
}),
|
|
});
|
|
});
|
|
});
|