169 lines
5.1 KiB
TypeScript
169 lines
5.1 KiB
TypeScript
// @vitest-environment node
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
findDeal: vi.fn(),
|
|
findTenant: vi.fn(),
|
|
prepareInvitation: vi.fn(),
|
|
issueInvitation: vi.fn(),
|
|
createTenant: vi.fn(),
|
|
linkDeal: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/utils/db.server", () => ({
|
|
db: {
|
|
adminDeal: { findFirst: mocks.findDeal },
|
|
tenant: { findUnique: mocks.findTenant },
|
|
},
|
|
}));
|
|
|
|
vi.mock("~/domain/tenant-bootstrap/ownerInvitationIssuance.server", () => ({
|
|
prepareReservedOwnerInvitation: mocks.prepareInvitation,
|
|
issueReservedOwnerInvitationWithTx: mocks.issueInvitation,
|
|
}));
|
|
|
|
vi.mock("~/utils/db/tenants.db.server", () => ({
|
|
createTenant: mocks.createTenant,
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/admin-sales-pipeline.server", () => ({
|
|
linkAdminDealToTenantWithTx: mocks.linkDeal,
|
|
}));
|
|
|
|
import {
|
|
createAndLinkAdminSalesTenant,
|
|
deriveAdminSalesTenantReservationKey,
|
|
} from "./admin-sales-tenant.server";
|
|
|
|
const input = {
|
|
request: new Request("https://admin.hivegrid.de/sales/deals/deal_1"),
|
|
dealId: "deal_1",
|
|
tenantName: "Muster GmbH",
|
|
tenantSlug: "muster-gmbh",
|
|
ownerEmail: "ada@example.test",
|
|
ownerFirstName: "Ada",
|
|
ownerLastName: "Lovelace",
|
|
admin: { id: "admin_1", firstName: "Raffael", locale: "de" },
|
|
actor: {
|
|
adminUserId: "admin_1",
|
|
email: "raffael@example.test",
|
|
reason: "Gewonnenen Deal ausdrücklich als Tenant anlegen",
|
|
},
|
|
} as const;
|
|
|
|
describe("admin sales tenant promotion", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.findDeal.mockResolvedValue({
|
|
status: "won",
|
|
promotedTenantId: null,
|
|
});
|
|
mocks.prepareInvitation.mockResolvedValue({
|
|
issuanceKey: deriveAdminSalesTenantReservationKey("deal_1"),
|
|
email: "ada@example.test",
|
|
firstName: "Ada",
|
|
lastName: "Lovelace",
|
|
fromUserId: null,
|
|
token: "token_value_that_is_long_enough_123456",
|
|
mail: { subject: "Einladung", html: "<p>token</p>" },
|
|
});
|
|
mocks.createTenant.mockImplementation(async (options) => {
|
|
const tenant = {
|
|
id: options.tenantId,
|
|
name: options.name,
|
|
slug: options.slug,
|
|
active: false,
|
|
deactivatedReason: "pending_owner",
|
|
};
|
|
await options.setup(tenant, { adminDeal: {} });
|
|
return tenant;
|
|
});
|
|
mocks.issueInvitation.mockResolvedValue({
|
|
invitationId: "invitation_1",
|
|
replayed: false,
|
|
});
|
|
mocks.linkDeal.mockResolvedValue({
|
|
id: "deal_1",
|
|
promotedTenantId: deriveAdminSalesTenantReservationKey("deal_1"),
|
|
});
|
|
});
|
|
|
|
it("derives one stable UUID-shaped reservation identity per deal", () => {
|
|
const first = deriveAdminSalesTenantReservationKey("deal_1");
|
|
expect(first).toBe(deriveAdminSalesTenantReservationKey("deal_1"));
|
|
expect(first).toMatch(
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-8[0-9a-f]{3}-[0-9a-f]{12}$/u
|
|
);
|
|
expect(first).not.toBe(deriveAdminSalesTenantReservationKey("deal_2"));
|
|
});
|
|
|
|
it("creates the pending owner reservation and deal link in one setup transaction", async () => {
|
|
await expect(createAndLinkAdminSalesTenant(input)).resolves.toMatchObject({
|
|
name: "Muster GmbH",
|
|
active: false,
|
|
deactivatedReason: "pending_owner",
|
|
});
|
|
|
|
expect(mocks.prepareInvitation).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
issuanceKey: deriveAdminSalesTenantReservationKey("deal_1"),
|
|
issuer: { id: null, firstName: "Raffael", locale: "de" },
|
|
})
|
|
);
|
|
expect(mocks.createTenant).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
active: false,
|
|
emitCreatedHook: false,
|
|
rejectSlugCollision: true,
|
|
rlsUserId: "admin_1",
|
|
setup: expect.any(Function),
|
|
validateExistingReplay: expect.any(Function),
|
|
})
|
|
);
|
|
expect(mocks.issueInvitation).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({ tenantId: expect.any(String), fromUserId: null })
|
|
);
|
|
expect(mocks.linkDeal).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
dealId: "deal_1",
|
|
actor: input.actor,
|
|
allowPendingOwner: true,
|
|
})
|
|
);
|
|
});
|
|
|
|
it("replays an existing link without creating or sending again", async () => {
|
|
mocks.findDeal.mockResolvedValue({
|
|
status: "won",
|
|
promotedTenantId: "tenant_existing",
|
|
});
|
|
mocks.findTenant.mockResolvedValue({
|
|
id: "tenant_existing",
|
|
name: "Muster GmbH",
|
|
slug: "muster",
|
|
});
|
|
|
|
await expect(createAndLinkAdminSalesTenant(input)).resolves.toMatchObject({
|
|
id: "tenant_existing",
|
|
});
|
|
expect(mocks.prepareInvitation).not.toHaveBeenCalled();
|
|
expect(mocks.createTenant).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("aborts before every side effect when the deal is not won", async () => {
|
|
mocks.findDeal.mockResolvedValue({
|
|
status: "open",
|
|
promotedTenantId: null,
|
|
});
|
|
|
|
await expect(createAndLinkAdminSalesTenant(input)).rejects.toThrow(
|
|
"requires a won deal"
|
|
);
|
|
expect(mocks.prepareInvitation).not.toHaveBeenCalled();
|
|
expect(mocks.createTenant).not.toHaveBeenCalled();
|
|
expect(mocks.linkDeal).not.toHaveBeenCalled();
|
|
});
|
|
});
|