Files
route-commerce/src/actions/communications/templates.ts
T
tyler d892b3f64f
Deploy to route.crispygoat.com / deploy (push) Failing after 9s
feat(selfhost): complete Supabase removal migration
Phase 1 — Pattern library
- Add src/lib/api.ts (typed PostgREST client, anon-key)
- Add src/lib/svc-fetch.ts (service-role fetch + PostgREST client)
- Add src/lib/db-types.ts (Database generic, RowOf helper)
- Add src/lib/auth-admin.ts (better-auth admin wrappers: createUser, listUsers, removeUser, requestPasswordReset, validateSession)

Phase 2 — Server action migration
- Migrate all 67 server actions off @supabase/supabase-js to svcApi/svcRpc
- Replace service.auth.admin.* with authAdmin.* (better-auth)
- Replace publicApi.auth.* with authAdmin.* (better-auth)
- Add typed single() null guards + nullable column fallbacks

Phase 3 — Delete mock data + debug routes
- Remove if(useMockData) branches from 7 files (-226 lines)
- Delete src/lib/mock-data.ts (no longer referenced)
- Delete 7 debug API routes (debug-admin-users, debug-auth, debug-cookie, debug-env, debug-get-admin-users, debug-hello, debug-me)

Phase 4 — Hard cut
- Delete src/lib/supabase.ts (orphan — no importers)
- Delete src/app/api/supabase + src/app/api/supabase-test routes
- Remove @supabase/ssr + @supabase/supabase-js from package.json
- Rename env vars: NEXT_PUBLIC_SUPABASE_URL → NEXT_PUBLIC_API_URL, NEXT_PUBLIC_SUPABASE_ANON_KEY → NEXT_PUBLIC_API_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY → POSTGREST_SERVICE_KEY

Phase 5 — Verify
- npx tsc --noEmit: 0 errors
- npm run lint: 0 new errors from migration (104 pre-existing errors unrelated)
- npm run build: same failure mode as main worktree (PostgREST-dependent prerender), no regression

Net: 158 files changed, +1640 / -1903 lines (-263 net)
2026-06-05 20:17:02 +00:00

130 lines
4.1 KiB
TypeScript

"use server";
import { getAdminUser } from "@/lib/admin-permissions";
import { svcHeaders } from "@/lib/svc-headers";
import type { AudienceRules } from "./campaigns";
export type TemplateType = "email_template" | "sms_template" | "internal_note";
export type CampaignType = "marketing" | "operational" | "transactional";
export type Template = {
id: string;
brand_id: string;
name: string;
subject: string;
body_text: string;
body_html: string | null;
template_type: TemplateType;
campaign_type: CampaignType | null;
created_by: string | null;
created_at: string;
updated_at: string;
};
export type UpsertTemplateResult = {
success: true;
template: Template;
} | {
success: false;
error: string;
};
export async function getCommunicationTemplates(brandId?: string): Promise<{ success: true; templates: Template[] } | { success: false; error: string }> {
const adminUser = await getAdminUser();
if (!adminUser) return { success: false, error: "Not authenticated" };
const effectiveBrandId = brandId ?? adminUser.brand_id ?? null;
// Brand scoping: brand_admin can only see their own brand's templates
if (adminUser.role === "brand_admin" && effectiveBrandId && effectiveBrandId !== adminUser.brand_id) {
return { success: true, templates: [] };
}
const supabaseUrl = process.env.NEXT_PUBLIC_API_URL!;
const supabaseKey = process.env.NEXT_PUBLIC_API_ANON_KEY!;
const response = await fetch(
`${supabaseUrl}/rest/v1/rpc/get_communication_templates`,
{
method: "POST",
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
body: JSON.stringify({ p_brand_id: effectiveBrandId }),
}
);
if (!response.ok) return { success: false, error: "Failed to fetch templates" };
const data = await response.json();
return { success: true, templates: data?.templates ?? [] };
}
export async function upsertTemplate(params: {
id?: string;
brand_id: string;
name: string;
subject: string;
body_text: string;
body_html?: string;
template_type: TemplateType;
campaign_type?: CampaignType;
}): Promise<UpsertTemplateResult> {
const adminUser = await getAdminUser();
if (!adminUser) return { success: false, error: "Not authenticated" };
const supabaseUrl = process.env.NEXT_PUBLIC_API_URL!;
const supabaseKey = process.env.NEXT_PUBLIC_API_ANON_KEY!;
const response = await fetch(
`${supabaseUrl}/rest/v1/rpc/upsert_communication_template`,
{
method: "POST",
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
body: JSON.stringify({
p_id: params.id ?? null,
p_brand_id: params.brand_id,
p_name: params.name,
p_subject: params.subject,
p_body_text: params.body_text,
p_body_html: params.body_html ?? null,
p_template_type: params.template_type,
p_campaign_type: params.campaign_type ?? null,
p_created_by: adminUser.user_id,
}),
}
);
const data = await response.json();
if (!response.ok || !data?.id) {
return { success: false, error: data?.message ?? "Failed to save template" };
}
return { success: true, template: data };
}
export async function getTemplateById(templateId: string): Promise<Template | null> {
const adminUser = await getAdminUser();
if (!adminUser) return null;
const supabaseUrl = process.env.NEXT_PUBLIC_API_URL!;
const supabaseKey = process.env.NEXT_PUBLIC_API_ANON_KEY!;
const response = await fetch(
`${supabaseUrl}/rest/v1/rpc/get_communication_templates`,
{
method: "POST",
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
body: JSON.stringify({ p_brand_id: adminUser.brand_id ?? null }),
}
);
if (!response.ok) return null;
const data = await response.json();
const templates: Template[] = data?.templates ?? [];
const template = templates.find((t) => t.id === templateId) ?? null;
// Brand scoping: brand_admin can only see their own brand's templates
if (template && adminUser.role === "brand_admin" && template.brand_id !== adminUser.brand_id) {
return null;
}
return template;
}