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
+268
-63
@@ -1,5 +1,10 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getRlsExecutionContext, runWithRlsContext, runWithRlsTransaction } from "@hivegrid/db/rls-context.server";
|
||||
import { buildLeadConversionEventType, LeadAlreadyConvertedError, LeadNotFoundError } from "./convertLead.server";
|
||||
import { TENANT_PENDING_OWNER_REASON } from "~/domain/tenant-bootstrap/tenantBootstrap.constants";
|
||||
|
||||
const capabilityMocks = vi.hoisted(() => ({ convertSelfServiceMarketingLeadCapability: vi.fn() }));
|
||||
const ownerInvitationMocks = vi.hoisted(() => ({ issueReservedOwnerInvitationWithTx: vi.fn() }));
|
||||
|
||||
vi.mock("~/utils/db.server", () => {
|
||||
return { db: { $transaction: vi.fn() } };
|
||||
@@ -11,6 +16,28 @@ vi.mock("@hivegrid/db/outbox", () => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@hivegrid/db/pretenant-security.server", () => ({
|
||||
convertSelfServiceMarketingLeadCapability: (...args: unknown[]) => capabilityMocks.convertSelfServiceMarketingLeadCapability(...args),
|
||||
}));
|
||||
|
||||
vi.mock("~/domain/tenant-bootstrap/tenantBootstrap.constants", () => ({
|
||||
TENANT_PENDING_OWNER_REASON: "pending_owner",
|
||||
}));
|
||||
vi.mock("~/domain/tenant-bootstrap/ownerInvitationIssuance.server", () => ({
|
||||
issueReservedOwnerInvitationWithTx: ownerInvitationMocks.issueReservedOwnerInvitationWithTx,
|
||||
}));
|
||||
|
||||
const OWNER_INVITATION_TOKEN = "o".repeat(43);
|
||||
const OWNER_INVITATION = {
|
||||
issuanceKey: "lead:lead_1",
|
||||
email: "test@example.com",
|
||||
firstName: "Test",
|
||||
lastName: "User",
|
||||
fromUserId: null,
|
||||
token: OWNER_INVITATION_TOKEN,
|
||||
mail: { subject: "Invitation", html: `https://app.example.test/invitation/${OWNER_INVITATION_TOKEN}` },
|
||||
};
|
||||
|
||||
describe("buildLeadConversionEventType", () => {
|
||||
it("returns convertedtotrial for trial status", () => {
|
||||
expect(buildLeadConversionEventType("trial")).toBe("marketing.lead.convertedtotrial");
|
||||
@@ -30,6 +57,7 @@ describe("convertLeadToTenant", () => {
|
||||
dbMock = (await import("~/utils/db.server")).db as any;
|
||||
publishMock = (await import("@hivegrid/db/outbox")).publishOutboxEvent as any;
|
||||
convertLeadToTenant = (await import("./convertLead.server")).convertLeadToTenant;
|
||||
ownerInvitationMocks.issueReservedOwnerInvitationWithTx.mockResolvedValue({ invitationId: "invite_owner", replayed: false });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -38,6 +66,7 @@ describe("convertLeadToTenant", () => {
|
||||
|
||||
function buildTx(leadOverrides: Record<string, unknown> = {}) {
|
||||
const tx: any = {
|
||||
$executeRaw: vi.fn().mockResolvedValue(1),
|
||||
lead: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "lead_1",
|
||||
@@ -83,6 +112,7 @@ describe("convertLeadToTenant", () => {
|
||||
|
||||
const result = await convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
});
|
||||
|
||||
@@ -90,16 +120,20 @@ describe("convertLeadToTenant", () => {
|
||||
expect(result.status).toBe("trial");
|
||||
expect(result.alreadyConverted).toBe(false);
|
||||
expect(result.outboxEventId).toBe("outbox_1");
|
||||
expect(tx.$executeRaw).toHaveBeenCalledOnce();
|
||||
expect(String(tx.$executeRaw.mock.calls[0]?.[0])).toContain("pg_advisory_xact_lock");
|
||||
expect(tx.tenant.create).toHaveBeenCalledOnce();
|
||||
expect(ownerInvitationMocks.issueReservedOwnerInvitationWithTx).toHaveBeenCalledWith(tx, {
|
||||
tenantId: "tenant_1",
|
||||
...OWNER_INVITATION,
|
||||
});
|
||||
expect(tx.lead.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "lead_1" },
|
||||
data: expect.objectContaining({ status: "trial", convertedToTenantId: "tenant_1" }),
|
||||
})
|
||||
);
|
||||
expect(tx.leadEvent.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ type: "converted" }) })
|
||||
);
|
||||
expect(tx.leadEvent.create).toHaveBeenCalledWith(expect.objectContaining({ data: expect.objectContaining({ type: "converted" }) }));
|
||||
expect(tx.tenantAcquisition.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
@@ -118,6 +152,7 @@ describe("convertLeadToTenant", () => {
|
||||
dispatchKey: "lead-convert:lead_1",
|
||||
})
|
||||
);
|
||||
expect(JSON.stringify(publishMock.mock.calls)).not.toContain(OWNER_INVITATION_TOKEN);
|
||||
});
|
||||
|
||||
it("publishes customer event when targetStatus=customer", async () => {
|
||||
@@ -127,12 +162,29 @@ describe("convertLeadToTenant", () => {
|
||||
await convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
targetStatus: "customer",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
});
|
||||
expect(publishMock).toHaveBeenCalledWith(
|
||||
tx,
|
||||
expect.objectContaining({ type: "marketing.lead.convertedtocustomer" })
|
||||
);
|
||||
expect(publishMock).toHaveBeenCalledWith(tx, expect.objectContaining({ type: "marketing.lead.convertedtocustomer" }));
|
||||
});
|
||||
|
||||
it("propagates owner-mail issuance failure before mutating the lead or publishing the business event", async () => {
|
||||
const tx = buildTx();
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
ownerInvitationMocks.issueReservedOwnerInvitationWithTx.mockRejectedValueOnce(new Error("mail outbox unavailable"));
|
||||
|
||||
await expect(
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
).rejects.toThrow("mail outbox unavailable");
|
||||
|
||||
expect(tx.lead.update).not.toHaveBeenCalled();
|
||||
expect(tx.leadEvent.create).not.toHaveBeenCalled();
|
||||
expect(tx.tenantAcquisition.create).not.toHaveBeenCalled();
|
||||
expect(publishMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns alreadyConverted=true without re-creating tenant when lead already converted", async () => {
|
||||
@@ -140,6 +192,7 @@ describe("convertLeadToTenant", () => {
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
const result = await convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
});
|
||||
expect(result.alreadyConverted).toBe(true);
|
||||
@@ -148,6 +201,111 @@ describe("convertLeadToTenant", () => {
|
||||
expect(publishMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects changed owner identity before an already-converted replay can disclose the tenant id", async () => {
|
||||
const tx = buildTx({ convertedToTenantId: "tenant_existing" });
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
|
||||
await expect(
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: { ...OWNER_INVITATION, email: "attacker@example.com" },
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
).rejects.toMatchObject({ name: "LeadOwnerInvitationMismatchError" });
|
||||
|
||||
expect(tx.tenant.create).not.toHaveBeenCalled();
|
||||
expect(ownerInvitationMocks.issueReservedOwnerInvitationWithTx).not.toHaveBeenCalled();
|
||||
expect(publishMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ firstName: null, lastName: "User" },
|
||||
{ firstName: "Test", lastName: " " },
|
||||
])("rejects an incomplete lead owner identity before creating the tenant", async (leadNames) => {
|
||||
const tx = buildTx(leadNames);
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
|
||||
await expect(
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: {
|
||||
...OWNER_INVITATION,
|
||||
firstName: leadNames.firstName ?? "",
|
||||
lastName: leadNames.lastName,
|
||||
},
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
).rejects.toMatchObject({ name: "LeadOwnerInvitationMismatchError" });
|
||||
|
||||
expect(tx.tenant.create).not.toHaveBeenCalled();
|
||||
expect(ownerInvitationMocks.issueReservedOwnerInvitationWithTx).not.toHaveBeenCalled();
|
||||
expect(tx.lead.update).not.toHaveBeenCalled();
|
||||
expect(publishMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("serializes concurrent conversions so only one ownerless tenant is created", async () => {
|
||||
let convertedToTenantId: string | null = null;
|
||||
let lockTail = Promise.resolve();
|
||||
const transactions: any[] = [];
|
||||
|
||||
dbMock.$transaction.mockImplementation(async (fn: (tx: any) => Promise<unknown>) => {
|
||||
const tx = buildTx();
|
||||
let releaseLock: (() => void) | undefined;
|
||||
tx.$executeRaw.mockImplementationOnce(async () => {
|
||||
const previous = lockTail;
|
||||
lockTail = new Promise<void>((resolve) => {
|
||||
releaseLock = resolve;
|
||||
});
|
||||
await previous;
|
||||
return 1;
|
||||
});
|
||||
tx.lead.findUnique.mockImplementation(async () => ({
|
||||
id: "lead_1",
|
||||
email: "test@example.com",
|
||||
firstName: "Test",
|
||||
lastName: "User",
|
||||
company: "Acme",
|
||||
source: "landing-pro",
|
||||
utmSource: null,
|
||||
utmMedium: null,
|
||||
utmCampaign: null,
|
||||
utmTerm: null,
|
||||
utmContent: null,
|
||||
gclid: null,
|
||||
fbclid: null,
|
||||
referrer: null,
|
||||
landingPage: null,
|
||||
firstSeenAt: new Date("2026-05-10T00:00:00Z"),
|
||||
status: "new",
|
||||
convertedToTenantId,
|
||||
}));
|
||||
tx.tenant.create.mockImplementation(async ({ data }: { data: { id: string } }) => ({ id: data.id }));
|
||||
tx.lead.update.mockImplementation(async ({ data }: { data: { convertedToTenantId: string } }) => {
|
||||
convertedToTenantId = data.convertedToTenantId;
|
||||
return {};
|
||||
});
|
||||
transactions.push(tx);
|
||||
try {
|
||||
return await fn(tx);
|
||||
} finally {
|
||||
releaseLock?.();
|
||||
}
|
||||
});
|
||||
publishMock.mockResolvedValue({ id: "outbox_concurrent" });
|
||||
|
||||
const input = {
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
} as const;
|
||||
const [first, second] = await Promise.all([convertLeadToTenant(input), convertLeadToTenant(input)]);
|
||||
|
||||
expect([first.alreadyConverted, second.alreadyConverted].sort()).toEqual([false, true]);
|
||||
expect(first.tenantId).toBe(second.tenantId);
|
||||
expect(transactions.reduce((count, tx) => count + tx.tenant.create.mock.calls.length, 0)).toBe(1);
|
||||
expect(transactions.every((tx) => tx.$executeRaw.mock.calls.length === 1)).toBe(true);
|
||||
});
|
||||
|
||||
it("throws LeadNotFoundError when lead missing", async () => {
|
||||
const tx = buildTx();
|
||||
tx.lead.findUnique.mockResolvedValue(null);
|
||||
@@ -155,6 +313,7 @@ describe("convertLeadToTenant", () => {
|
||||
await expect(
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_unknown",
|
||||
ownerInvitation: { ...OWNER_INVITATION, issuanceKey: "lead:lead_unknown" },
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
).rejects.toMatchObject({ name: "LeadNotFoundError" });
|
||||
@@ -163,12 +322,17 @@ describe("convertLeadToTenant", () => {
|
||||
|
||||
it("admin-override path stamps conversionPath=admin-override + actorId on LeadEvent", async () => {
|
||||
const tx = buildTx();
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
let transactionContext: ReturnType<typeof getRlsExecutionContext>;
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => {
|
||||
transactionContext = getRlsExecutionContext();
|
||||
return fn(tx);
|
||||
});
|
||||
publishMock.mockResolvedValue({ id: "outbox_3" });
|
||||
await convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
conversionPath: "admin-override",
|
||||
actorId: "user_admin",
|
||||
ownerInvitation: { ...OWNER_INVITATION, fromUserId: "user_admin" },
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
});
|
||||
expect(tx.leadEvent.create).toHaveBeenCalledWith(
|
||||
@@ -181,6 +345,45 @@ describe("convertLeadToTenant", () => {
|
||||
data: expect.objectContaining({ conversionPath: "admin-override" }),
|
||||
})
|
||||
);
|
||||
expect(transactionContext!).toMatchObject({ tenantId: expect.any(String), userId: "user_admin" });
|
||||
expect(tx.tenant.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
id: transactionContext!.tenantId,
|
||||
active: false,
|
||||
deactivatedReason: TENANT_PENDING_OWNER_REASON,
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(getRlsExecutionContext()).toBeNull();
|
||||
});
|
||||
|
||||
it("isolates conversion from an inherited tenant transaction for the same operator", async () => {
|
||||
const tx = buildTx();
|
||||
const inheritedTransaction = { id: "old-transaction" };
|
||||
let transactionContext: ReturnType<typeof getRlsExecutionContext>;
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => {
|
||||
transactionContext = getRlsExecutionContext();
|
||||
return fn(tx);
|
||||
});
|
||||
publishMock.mockResolvedValue({ id: "outbox_isolated" });
|
||||
|
||||
await runWithRlsContext({ tenantId: "tenant-old", userId: "user_admin" }, () =>
|
||||
runWithRlsTransaction(inheritedTransaction, () =>
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
conversionPath: "admin-override",
|
||||
actorId: "user_admin",
|
||||
ownerInvitation: { ...OWNER_INVITATION, fromUserId: "user_admin" },
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
expect(transactionContext!).toMatchObject({ tenantId: expect.any(String), userId: "user_admin" });
|
||||
expect(transactionContext!.tenantId).not.toBe("tenant-old");
|
||||
expect(transactionContext!.transaction).toBeUndefined();
|
||||
expect(getRlsExecutionContext()).toBeNull();
|
||||
});
|
||||
|
||||
it("respects LEAD_CONVERSION_ENABLED=false rollback flag", async () => {
|
||||
@@ -190,6 +393,7 @@ describe("convertLeadToTenant", () => {
|
||||
await expect(
|
||||
convertLeadToTenant({
|
||||
leadId: "lead_1",
|
||||
ownerInvitation: OWNER_INVITATION,
|
||||
tenant: { name: "Acme", slug: "acme", inboundAddress: "acme@inbox.local" },
|
||||
})
|
||||
).rejects.toThrow(/disabled/i);
|
||||
@@ -210,72 +414,63 @@ describe("recordExistingTenantLeadConversion", () => {
|
||||
dbMock = (await import("~/utils/db.server")).db as any;
|
||||
publishMock = (await import("@hivegrid/db/outbox")).publishOutboxEvent as any;
|
||||
recordExistingTenantLeadConversion = (await import("./convertLead.server")).recordExistingTenantLeadConversion;
|
||||
capabilityMocks.convertSelfServiceMarketingLeadCapability.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function buildExistingTenantTx(leadOverrides: Record<string, unknown> = {}) {
|
||||
const tx: any = {
|
||||
lead: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "lead_1",
|
||||
email: "test@example.com",
|
||||
firstName: "Test",
|
||||
lastName: "User",
|
||||
company: "Acme",
|
||||
source: "landing-pro",
|
||||
utmSource: "google",
|
||||
utmMedium: "cpc",
|
||||
utmCampaign: "q2-launch",
|
||||
utmTerm: null,
|
||||
utmContent: null,
|
||||
gclid: null,
|
||||
fbclid: null,
|
||||
referrer: null,
|
||||
landingPage: "/pricing",
|
||||
firstSeenAt: new Date("2026-05-10T00:00:00Z"),
|
||||
convertedToTenantId: null,
|
||||
...leadOverrides,
|
||||
}),
|
||||
update: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
leadEvent: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
tenantAcquisition: {
|
||||
upsert: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
function conversionSummary(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
leadId: "lead_1",
|
||||
convertedToTenantId: "tenant_registered",
|
||||
alreadyConverted: false,
|
||||
email: "test@example.com",
|
||||
firstName: "Test",
|
||||
lastName: "User",
|
||||
company: "Acme",
|
||||
source: "landing-pro",
|
||||
utmSource: "google",
|
||||
utmMedium: "cpc",
|
||||
utmCampaign: "q2-launch",
|
||||
utmTerm: null,
|
||||
utmContent: null,
|
||||
gclid: null,
|
||||
fbclid: null,
|
||||
referrer: null,
|
||||
landingPage: "/pricing",
|
||||
firstSeenAt: "2026-05-10T00:00:00.000Z",
|
||||
convertedAt: "2026-05-11T00:00:00.000Z",
|
||||
...overrides,
|
||||
};
|
||||
return tx;
|
||||
}
|
||||
|
||||
it("links an already-created registration tenant to the lead attribution and outbox event", async () => {
|
||||
const tx = buildExistingTenantTx();
|
||||
const tx = {};
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
capabilityMocks.convertSelfServiceMarketingLeadCapability.mockResolvedValue(conversionSummary());
|
||||
publishMock.mockResolvedValue({ id: "outbox_4" });
|
||||
|
||||
const result = await recordExistingTenantLeadConversion({
|
||||
leadId: "lead_1",
|
||||
tenantId: "tenant_registered",
|
||||
userId: "user_1",
|
||||
expectedEmail: "test@example.com",
|
||||
planId: "plan_pro",
|
||||
conversionPath: "self-service",
|
||||
});
|
||||
|
||||
expect(result).toMatchObject({ tenantId: "tenant_registered", leadId: "lead_1", alreadyConverted: false, outboxEventId: "outbox_4" });
|
||||
expect(tx.lead.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "lead_1" },
|
||||
data: expect.objectContaining({ status: "trial", convertedToTenantId: "tenant_registered" }),
|
||||
})
|
||||
);
|
||||
expect(tx.tenantAcquisition.upsert).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { tenantId: "tenant_registered" },
|
||||
create: expect.objectContaining({ tenantId: "tenant_registered", leadId: "lead_1", conversionPath: "self-service" }),
|
||||
update: expect.objectContaining({ leadId: "lead_1", conversionPath: "self-service" }),
|
||||
})
|
||||
expect(capabilityMocks.convertSelfServiceMarketingLeadCapability).toHaveBeenCalledWith(
|
||||
{
|
||||
leadId: "lead_1",
|
||||
tenantId: "tenant_registered",
|
||||
userId: "user_1",
|
||||
expectedEmail: "test@example.com",
|
||||
planId: "plan_pro",
|
||||
},
|
||||
tx
|
||||
);
|
||||
expect(publishMock).toHaveBeenCalledWith(
|
||||
tx,
|
||||
@@ -289,26 +484,36 @@ describe("recordExistingTenantLeadConversion", () => {
|
||||
});
|
||||
|
||||
it("is idempotent when the same tenant already owns the lead conversion", async () => {
|
||||
const tx = buildExistingTenantTx({ convertedToTenantId: "tenant_registered" });
|
||||
const tx = {};
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
capabilityMocks.convertSelfServiceMarketingLeadCapability.mockResolvedValue(conversionSummary({ alreadyConverted: true }));
|
||||
|
||||
const result = await recordExistingTenantLeadConversion({ leadId: "lead_1", tenantId: "tenant_registered" });
|
||||
const result = await recordExistingTenantLeadConversion({
|
||||
leadId: "lead_1",
|
||||
tenantId: "tenant_registered",
|
||||
userId: "user_1",
|
||||
expectedEmail: "test@example.com",
|
||||
});
|
||||
|
||||
expect(result.alreadyConverted).toBe(true);
|
||||
expect(tx.lead.update).not.toHaveBeenCalled();
|
||||
expect(tx.tenantAcquisition.upsert).not.toHaveBeenCalled();
|
||||
expect(publishMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects a lead already converted to a different tenant", async () => {
|
||||
const tx = buildExistingTenantTx({ convertedToTenantId: "tenant_other" });
|
||||
const tx = {};
|
||||
dbMock.$transaction.mockImplementation(async (fn: any) => fn(tx));
|
||||
capabilityMocks.convertSelfServiceMarketingLeadCapability.mockResolvedValue(
|
||||
conversionSummary({ convertedToTenantId: "tenant_other", alreadyConverted: true })
|
||||
);
|
||||
|
||||
await expect(recordExistingTenantLeadConversion({ leadId: "lead_1", tenantId: "tenant_registered" })).rejects.toMatchObject({
|
||||
name: "LeadAlreadyConvertedError",
|
||||
convertedToTenantId: "tenant_other",
|
||||
});
|
||||
await expect(
|
||||
recordExistingTenantLeadConversion({
|
||||
leadId: "lead_1",
|
||||
tenantId: "tenant_registered",
|
||||
userId: "user_1",
|
||||
expectedEmail: "test@example.com",
|
||||
})
|
||||
).rejects.toMatchObject({ name: "LeadAlreadyConvertedError", convertedToTenantId: "tenant_other" });
|
||||
expect(LeadAlreadyConvertedError.name).toBe("LeadAlreadyConvertedError");
|
||||
expect(tx.lead.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user