Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,154 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type LeadRecord = Record<string, unknown> & {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName?: string | null;
|
||||
lastName?: string | null;
|
||||
company?: string | null;
|
||||
status: string;
|
||||
source?: string | null;
|
||||
utmSource?: string | null;
|
||||
createdAt: Date;
|
||||
};
|
||||
|
||||
const dbMock = {
|
||||
lead: {
|
||||
findMany: vi.fn<() => Promise<LeadRecord[]>>(),
|
||||
},
|
||||
};
|
||||
const writeFileMock = vi.fn();
|
||||
|
||||
vi.mock("@toolctrl/db/client.server", () => ({
|
||||
getDbClient: () => dbMock,
|
||||
}));
|
||||
vi.mock("node:fs/promises", () => ({
|
||||
default: { writeFile: (...args: unknown[]) => writeFileMock(...args) },
|
||||
writeFile: (...args: unknown[]) => writeFileMock(...args),
|
||||
}));
|
||||
vi.mock("consola", () => ({
|
||||
default: {
|
||||
info: vi.fn(),
|
||||
log: vi.fn(),
|
||||
success: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import consola from "consola";
|
||||
import { leadsExportCommand } from "./export.js";
|
||||
import { leadsListCommand } from "./list.js";
|
||||
|
||||
const consoleMock = vi.mocked(consola);
|
||||
|
||||
describe("leads CLI commands", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
dbMock.lead.findMany.mockResolvedValue([]);
|
||||
writeFileMock.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
it("lists captured leads with bounded limit, status filter, and display fallbacks", async () => {
|
||||
dbMock.lead.findMany.mockResolvedValueOnce([
|
||||
{
|
||||
id: "lead_1",
|
||||
email: "ada@example.com",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
company: "Analytical",
|
||||
status: "new",
|
||||
source: null,
|
||||
utmSource: "newsletter",
|
||||
createdAt: new Date("2026-05-25T12:00:00.000Z"),
|
||||
},
|
||||
{
|
||||
id: "lead_2",
|
||||
email: "grace@example.com",
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
company: null,
|
||||
status: "contacted",
|
||||
source: "form",
|
||||
utmSource: null,
|
||||
createdAt: new Date("2026-05-24T12:00:00.000Z"),
|
||||
},
|
||||
]);
|
||||
|
||||
await leadsListCommand.run?.({ args: { status: "new", limit: "999" } } as never);
|
||||
|
||||
expect(dbMock.lead.findMany).toHaveBeenCalledWith({
|
||||
where: { status: "new" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 500,
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
company: true,
|
||||
status: true,
|
||||
source: true,
|
||||
utmSource: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
expect(consoleMock.log).toHaveBeenCalledWith(expect.stringContaining("utm=newsletter Ada Lovelace"));
|
||||
expect(consoleMock.log).toHaveBeenCalledWith(expect.stringContaining("src=form utm=- -"));
|
||||
expect(consoleMock.success).toHaveBeenCalledWith("2 leads listed.");
|
||||
});
|
||||
|
||||
it("reports empty lead lists with and without status filters", async () => {
|
||||
await leadsListCommand.run?.({ args: {} } as never);
|
||||
await leadsListCommand.run?.({ args: { status: "archived" } } as never);
|
||||
|
||||
expect(consoleMock.info).toHaveBeenCalledWith("No leads captured yet.");
|
||||
expect(consoleMock.info).toHaveBeenCalledWith("No leads with status=archived.");
|
||||
expect(consoleMock.success).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("exports escaped CSV to stdout", async () => {
|
||||
const stdoutSpy = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
||||
dbMock.lead.findMany.mockResolvedValueOnce([
|
||||
{
|
||||
id: "lead_1",
|
||||
email: "ada@example.com",
|
||||
firstName: "Ada",
|
||||
lastName: 'Love, "Countess"',
|
||||
phone: null,
|
||||
company: "Analytical\nEngines",
|
||||
source: "form",
|
||||
status: "new",
|
||||
utmSource: "newsletter",
|
||||
createdAt: new Date("2026-05-25T12:00:00.000Z"),
|
||||
lastTouchAt: null,
|
||||
},
|
||||
]);
|
||||
|
||||
await leadsExportCommand.run?.({ args: { status: "new" } } as never);
|
||||
|
||||
expect(dbMock.lead.findMany).toHaveBeenCalledWith({
|
||||
where: { status: "new" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
const csv = String(stdoutSpy.mock.calls[0]?.[0]);
|
||||
expect(csv).toContain("id,email,firstName,lastName");
|
||||
expect(csv).toContain('"Love, ""Countess"""');
|
||||
expect(csv).toContain('"Analytical\nEngines"');
|
||||
expect(writeFileMock).not.toHaveBeenCalled();
|
||||
expect(consoleMock.success).toHaveBeenCalledWith("Exported 1 leads.");
|
||||
|
||||
stdoutSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("writes CSV exports to a file when requested", async () => {
|
||||
dbMock.lead.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
await leadsExportCommand.run?.({ args: { out: "/tmp/leads.csv" } } as never);
|
||||
|
||||
expect(writeFileMock).toHaveBeenCalledWith(
|
||||
"/tmp/leads.csv",
|
||||
expect.stringMatching(/^id,email,firstName,/u),
|
||||
"utf8",
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith("Wrote 0 leads to /tmp/leads.csv.");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* `leads delete --id <leadId>` — hard-delete a single lead and its events.
|
||||
* Use for DSGVO right-to-be-forgotten requests.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
|
||||
export const leadsDeleteCommand = defineCommand({
|
||||
meta: { name: "delete", description: "Delete a lead (DSGVO right-to-be-forgotten)." },
|
||||
args: {
|
||||
id: { type: "string", required: true, description: "Lead id." },
|
||||
},
|
||||
async run({ args }) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const id = String(args.id);
|
||||
const existing = await prisma.lead.findUnique({ where: { id }, select: { email: true } });
|
||||
if (!existing) {
|
||||
consola.warn(`Lead ${id} not found.`);
|
||||
return;
|
||||
}
|
||||
await prisma.lead.delete({ where: { id } });
|
||||
consola.success(`Deleted lead ${id} (${existing.email}).`);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* `leads export [--out <path>]` — dump every lead as CSV to stdout or a file.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
|
||||
const COLUMNS = [
|
||||
"id",
|
||||
"email",
|
||||
"firstName",
|
||||
"lastName",
|
||||
"phone",
|
||||
"company",
|
||||
"source",
|
||||
"status",
|
||||
"utmSource",
|
||||
"utmMedium",
|
||||
"utmCampaign",
|
||||
"utmTerm",
|
||||
"utmContent",
|
||||
"gclid",
|
||||
"fbclid",
|
||||
"referrer",
|
||||
"landingPage",
|
||||
"score",
|
||||
"createdAt",
|
||||
"lastTouchAt",
|
||||
] as const;
|
||||
|
||||
function csvEscape(v: unknown): string {
|
||||
if (v === null || v === undefined) return "";
|
||||
const s = String(v);
|
||||
if (s.includes(",") || s.includes('"') || s.includes("\n")) {
|
||||
return `"${s.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
export const leadsExportCommand = defineCommand({
|
||||
meta: { name: "export", description: "Export all leads as CSV." },
|
||||
args: {
|
||||
out: { type: "string", required: false, description: "Output file path (default stdout)." },
|
||||
status: { type: "string", required: false, description: "Filter by status." },
|
||||
},
|
||||
async run({ args }) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const where = args.status ? { status: String(args.status) as never } : {};
|
||||
const leads = await prisma.lead.findMany({ where, orderBy: { createdAt: "desc" } });
|
||||
const header = COLUMNS.join(",");
|
||||
const lines = [header];
|
||||
for (const l of leads) {
|
||||
const row = COLUMNS.map((c) => csvEscape((l as Record<string, unknown>)[c])).join(",");
|
||||
lines.push(row);
|
||||
}
|
||||
const csv = `${lines.join("\n")}\n`;
|
||||
if (args.out) {
|
||||
await writeFile(String(args.out), csv, "utf8");
|
||||
consola.success(`Wrote ${leads.length} leads to ${args.out}.`);
|
||||
} else {
|
||||
process.stdout.write(csv);
|
||||
consola.success(`Exported ${leads.length} leads.`);
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { defineCommand } from "citty";
|
||||
import { leadsListCommand } from "./list.js";
|
||||
import { leadsExportCommand } from "./export.js";
|
||||
import { leadsDeleteCommand } from "./delete.js";
|
||||
|
||||
export const leadsCommand = defineCommand({
|
||||
meta: {
|
||||
name: "leads",
|
||||
description: "Marketing-funnel lead inbox commands (list, export, delete).",
|
||||
},
|
||||
subCommands: {
|
||||
list: leadsListCommand,
|
||||
export: leadsExportCommand,
|
||||
delete: leadsDeleteCommand,
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* `leads list [--status <s>] [--limit <n>]` — print the latest leads.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
|
||||
export const leadsListCommand = defineCommand({
|
||||
meta: { name: "list", description: "List captured marketing leads." },
|
||||
args: {
|
||||
status: { type: "string", required: false, description: "Filter by status (new|contacted|...)." },
|
||||
limit: { type: "string", required: false, description: "Max rows (default 50)." },
|
||||
},
|
||||
async run({ args }) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const limit = Math.min(500, Math.max(1, Number(args.limit ?? "50")));
|
||||
const where = args.status ? { status: String(args.status) as never } : {};
|
||||
const leads = await prisma.lead.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: limit,
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
company: true,
|
||||
status: true,
|
||||
source: true,
|
||||
utmSource: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
if (leads.length === 0) {
|
||||
consola.info(args.status ? `No leads with status=${args.status}.` : "No leads captured yet.");
|
||||
return;
|
||||
}
|
||||
for (const l of leads) {
|
||||
const name = [l.firstName, l.lastName].filter(Boolean).join(" ") || "-";
|
||||
consola.log(
|
||||
`${l.status.padEnd(13)} ${l.id.padEnd(28)} ${l.email.padEnd(40)} src=${l.source ?? "-"} utm=${l.utmSource ?? "-"} ${name}`,
|
||||
);
|
||||
}
|
||||
consola.success(`${leads.length} leads listed.`);
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user