Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
This commit is contained in:
commit
8580c996c3
1285 files changed
+325780
No files matched your search
@@ -0,0 +1,187 @@
|
||||
import { api, ApiError } from "$lib/api";
|
||||
import { trackEvent } from "$lib/analytics";
|
||||
import { unwrapList } from "$lib/list";
|
||||
import { DEFAULT_SEASON_TEMPLATES } from "./templates";
|
||||
import type {
|
||||
Campaign,
|
||||
CreateCampaignInput,
|
||||
GenerateCampaignInput,
|
||||
ScheduleCampaignInput,
|
||||
SeasonTemplate,
|
||||
SendTestInput
|
||||
} from "./types";
|
||||
|
||||
export function isCampaignsUnavailable(err: unknown): boolean {
|
||||
return (
|
||||
err instanceof ApiError &&
|
||||
(err.status === 404 || err.status === 501 || err.status === 502 || err.status === 503)
|
||||
);
|
||||
}
|
||||
|
||||
export function isUpgradeRequired(err: unknown): boolean {
|
||||
if (!(err instanceof ApiError)) return false;
|
||||
if (err.status === 402) return true;
|
||||
if (err.status !== 403) return false;
|
||||
const msg = err.message.toLowerCase();
|
||||
return (
|
||||
msg.includes("upgrade") ||
|
||||
msg.includes("free") ||
|
||||
msg.includes("plan") ||
|
||||
msg.includes("ai") ||
|
||||
msg.includes("credit")
|
||||
);
|
||||
}
|
||||
|
||||
function asCampaign(raw: unknown): Campaign | null {
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const record = raw as Record<string, unknown>;
|
||||
const nested = record.campaign;
|
||||
if (nested && typeof nested === "object") return nested as Campaign;
|
||||
if (typeof record.id === "string" || typeof record.id === "number") {
|
||||
return { ...(record as Campaign), id: String(record.id) };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function listCampaigns(opts?: {
|
||||
signal?: AbortSignal;
|
||||
}): Promise<{ campaigns: Campaign[]; unavailable: boolean }> {
|
||||
try {
|
||||
const payload = await api<Record<string, unknown>>("/api/campaigns?limit=200", {
|
||||
signal: opts?.signal
|
||||
});
|
||||
const list = unwrapList<Campaign>(payload).map((c) => ({
|
||||
...c,
|
||||
id: String(c.id)
|
||||
}));
|
||||
return { campaigns: list, unavailable: false };
|
||||
} catch (err) {
|
||||
if (opts?.signal?.aborted) throw err;
|
||||
if (isCampaignsUnavailable(err) || isUpgradeRequired(err)) {
|
||||
return { campaigns: [], unavailable: true };
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCampaign(id: string): Promise<Campaign> {
|
||||
const payload = await api<unknown>(`/api/campaigns/${encodeURIComponent(id)}`);
|
||||
const campaign = asCampaign(payload);
|
||||
if (!campaign) throw new Error("Campaign not found");
|
||||
return { ...campaign, id: String(campaign.id) };
|
||||
}
|
||||
|
||||
export async function createCampaign(input: CreateCampaignInput): Promise<Campaign> {
|
||||
const payload = await api<unknown>("/api/campaigns", { method: "POST", body: input });
|
||||
const campaign = asCampaign(payload);
|
||||
if (!campaign) throw new Error("Invalid create response");
|
||||
trackEvent("campaign_created");
|
||||
return { ...campaign, id: String(campaign.id) };
|
||||
}
|
||||
|
||||
export async function updateCampaign(
|
||||
id: string,
|
||||
input: Partial<CreateCampaignInput> & { name?: string; status?: string }
|
||||
): Promise<Campaign> {
|
||||
const payload = await api<unknown>(`/api/campaigns/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
body: input
|
||||
});
|
||||
const campaign = asCampaign(payload);
|
||||
if (!campaign) throw new Error("Invalid update response");
|
||||
return { ...campaign, id: String(campaign.id) };
|
||||
}
|
||||
|
||||
export async function deleteCampaign(id: string): Promise<void> {
|
||||
await api(`/api/campaigns/${encodeURIComponent(id)}`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
export async function listTemplates(): Promise<SeasonTemplate[]> {
|
||||
try {
|
||||
const payload = await api<Record<string, unknown>>("/api/campaigns/templates");
|
||||
const list = unwrapList<SeasonTemplate>(payload);
|
||||
if (list.length) return list;
|
||||
const named = payload.templates;
|
||||
if (Array.isArray(named) && named.length) return named as SeasonTemplate[];
|
||||
} catch (err) {
|
||||
if (!isCampaignsUnavailable(err)) throw err;
|
||||
}
|
||||
return DEFAULT_SEASON_TEMPLATES;
|
||||
}
|
||||
|
||||
export async function generateCampaign(
|
||||
id: string,
|
||||
input: GenerateCampaignInput = { use_ai: true }
|
||||
): Promise<Campaign> {
|
||||
const payload = await api<unknown>(`/api/campaigns/${encodeURIComponent(id)}/generate`, {
|
||||
method: "POST",
|
||||
body: input
|
||||
});
|
||||
const campaign = asCampaign(payload);
|
||||
if (!campaign) throw new Error("Invalid generate response");
|
||||
trackEvent("campaign_generated", { use_ai: input.use_ai !== false });
|
||||
return { ...campaign, id: String(campaign.id) };
|
||||
}
|
||||
|
||||
export async function sendTestCampaign(id: string, input: SendTestInput): Promise<void> {
|
||||
await api(`/api/campaigns/${encodeURIComponent(id)}/send-test`, {
|
||||
method: "POST",
|
||||
body: input
|
||||
});
|
||||
}
|
||||
|
||||
export async function scheduleCampaign(id: string, input: ScheduleCampaignInput): Promise<Campaign> {
|
||||
const payload = await api<unknown>(`/api/campaigns/${encodeURIComponent(id)}/schedule`, {
|
||||
method: "POST",
|
||||
body: input
|
||||
});
|
||||
const campaign = asCampaign(payload);
|
||||
if (!campaign) throw new Error("Invalid schedule response");
|
||||
return { ...campaign, id: String(campaign.id) };
|
||||
}
|
||||
|
||||
export function previewSubject(campaign: Campaign): string {
|
||||
return (
|
||||
campaign.latest_version?.subject ||
|
||||
campaign.subject ||
|
||||
campaign.versions?.[0]?.subject ||
|
||||
"(No subject yet)"
|
||||
);
|
||||
}
|
||||
|
||||
export function previewHtml(campaign: Campaign): string {
|
||||
return (
|
||||
campaign.latest_version?.html_body ||
|
||||
campaign.html_body ||
|
||||
campaign.versions?.[0]?.html_body ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
export function previewPlain(campaign: Campaign): string {
|
||||
return (
|
||||
campaign.latest_version?.plain_body ||
|
||||
campaign.plain_body ||
|
||||
campaign.versions?.[0]?.plain_body ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
/** Best-effort: orders exist for “purchased” audience option. */
|
||||
export async function hasOrderAudience(): Promise<boolean> {
|
||||
const paths = ["/api/woocommerce/orders?limit=1"];
|
||||
for (const path of paths) {
|
||||
try {
|
||||
const payload = await api<Record<string, unknown>>(path);
|
||||
const list = unwrapList(payload);
|
||||
const total = typeof payload.total === "number" ? payload.total : null;
|
||||
if (list.length > 0 || (total !== null && total > 0)) return true;
|
||||
// Endpoint exists but empty — still allow the option.
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && (err.status === 401 || err.status === 403)) throw err;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { SeasonTemplate } from "./types";
|
||||
|
||||
/** Client-side defaults when GET /api/campaigns/templates is unavailable. */
|
||||
export const DEFAULT_SEASON_TEMPLATES: SeasonTemplate[] = [
|
||||
{
|
||||
key: "black_friday",
|
||||
name: "Black Friday",
|
||||
emoji: "🛍️",
|
||||
description: "Urgent deals, limited-time offers, and product highlights.",
|
||||
default_subject: "Black Friday picks from {{brand}}",
|
||||
default_prompt:
|
||||
"Write a Black Friday email highlighting the selected products. Emphasize limited-time savings, keep the tone energetic but trustworthy, include a clear CTA to shop, and mention 2–4 hero products with short benefit-led blurbs."
|
||||
},
|
||||
{
|
||||
key: "christmas",
|
||||
name: "Christmas",
|
||||
emoji: "🎄",
|
||||
description: "Gift guides and warm seasonal offers.",
|
||||
default_subject: "Gift ideas for the holidays",
|
||||
default_prompt:
|
||||
"Write a Christmas / holiday gift-guide email for the selected products. Warm and festive tone, suggest who each product is for, keep copy scannable, and end with a clear shop CTA."
|
||||
},
|
||||
{
|
||||
key: "spring",
|
||||
name: "Spring",
|
||||
emoji: "🌸",
|
||||
description: "Fresh arrivals and seasonal refresh.",
|
||||
default_subject: "New for spring: {{brand}} favorites",
|
||||
default_prompt:
|
||||
"Write a spring refresh email featuring the selected products. Light, optimistic tone; focus on what’s new or renewed; short product blurbs and one primary CTA."
|
||||
},
|
||||
{
|
||||
key: "summer",
|
||||
name: "Summer",
|
||||
emoji: "☀️",
|
||||
description: "Warm-weather picks and outdoor-ready products.",
|
||||
default_subject: "Summer essentials from {{brand}}",
|
||||
default_prompt:
|
||||
"Write a summer email featuring the selected products. Bright and inviting tone; highlight seasonal use-cases; keep paragraphs short with a clear shop CTA."
|
||||
},
|
||||
{
|
||||
key: "custom",
|
||||
name: "Custom",
|
||||
emoji: "✉️",
|
||||
description: "Start from a blank prompt and shape your own campaign.",
|
||||
default_subject: "News from {{brand}}",
|
||||
default_prompt:
|
||||
"Write a promotional email for the selected products. Clear subject line energy, benefit-focused product blurbs, on-brand voice, and a single primary call to action."
|
||||
}
|
||||
];
|
||||
|
||||
export function templateByKey(
|
||||
key: string,
|
||||
templates: SeasonTemplate[] = DEFAULT_SEASON_TEMPLATES
|
||||
): SeasonTemplate | undefined {
|
||||
return templates.find((t) => t.key === key);
|
||||
}
|
||||
|
||||
export function defaultCampaignName(template: SeasonTemplate): string {
|
||||
const year = new Date().getFullYear();
|
||||
if (template.key === "custom") return `Campaign ${year}`;
|
||||
return `${template.name} ${year}`;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
export type CampaignStatus = "draft" | "ready" | "scheduled" | "sending" | "sent" | "failed";
|
||||
|
||||
export type AudienceType = "all" | "by_category" | "purchased" | "not_purchased";
|
||||
|
||||
export type AudienceFilter = {
|
||||
type: AudienceType;
|
||||
category_ids?: string[];
|
||||
product_ids?: string[];
|
||||
};
|
||||
|
||||
export type SeasonTemplate = {
|
||||
key: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
default_prompt: string;
|
||||
default_subject?: string;
|
||||
emoji?: string;
|
||||
};
|
||||
|
||||
export type CampaignVersion = {
|
||||
id?: string;
|
||||
subject?: string | null;
|
||||
html_body?: string | null;
|
||||
plain_body?: string | null;
|
||||
generated_at?: string | null;
|
||||
};
|
||||
|
||||
export type Campaign = {
|
||||
id: string;
|
||||
name: string;
|
||||
season?: string | null;
|
||||
template_key?: string | null;
|
||||
status?: CampaignStatus | string | null;
|
||||
category_ids?: string[] | null;
|
||||
product_ids?: string[] | null;
|
||||
prompt?: string | null;
|
||||
use_default_prompt?: boolean | null;
|
||||
audience_filter?: AudienceFilter | null;
|
||||
scheduled_at?: string | null;
|
||||
subject?: string | null;
|
||||
html_body?: string | null;
|
||||
plain_body?: string | null;
|
||||
latest_version?: CampaignVersion | null;
|
||||
versions?: CampaignVersion[] | null;
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
export type CreateCampaignInput = {
|
||||
name: string;
|
||||
template_key: string;
|
||||
season?: string;
|
||||
category_ids?: string[];
|
||||
product_ids?: string[];
|
||||
prompt?: string;
|
||||
use_default_prompt?: boolean;
|
||||
audience_filter?: AudienceFilter;
|
||||
};
|
||||
|
||||
export type GenerateCampaignInput = {
|
||||
use_ai?: boolean;
|
||||
prompt?: string;
|
||||
};
|
||||
|
||||
export type ScheduleCampaignInput = {
|
||||
scheduled_at: string;
|
||||
};
|
||||
|
||||
export type SendTestInput = {
|
||||
email: string;
|
||||
};
|
||||
Reference in new issue
Block a user