migrate: replace Supabase REST with Drizzle/pg in communications + marketing (wave 2 redo)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"use server";
|
||||
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { getAdminUser } from "@/lib/admin-permissions";
|
||||
import { svcHeaders } from "@/lib/svc-headers";
|
||||
import { withTenant } from "@/db/client";
|
||||
import { products } from "@/db/schema";
|
||||
|
||||
export type ProductOption = {
|
||||
id: string;
|
||||
@@ -10,6 +12,13 @@ export type ProductOption = {
|
||||
price: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* The legacy `get_products_for_segment_picker` RPC returned a
|
||||
* `(id, name, type, price)` denormalized view. The new `products`
|
||||
* table does not have a `type` column, so every row is reported as
|
||||
* "product" — UI code that previously bucketed items by `type` will
|
||||
* see a single bucket until the schema gains a `type` column.
|
||||
*/
|
||||
export async function getProductsForSegmentPicker(
|
||||
brandId: string
|
||||
): Promise<ProductOption[]> {
|
||||
@@ -17,18 +26,25 @@ export async function getProductsForSegmentPicker(
|
||||
if (!adminUser) return [];
|
||||
if (adminUser.role === "brand_admin" && adminUser.brand_id !== brandId) return [];
|
||||
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
|
||||
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;
|
||||
|
||||
const response = await fetch(
|
||||
`${supabaseUrl}/rest/v1/rpc/get_products_for_segment_picker`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ p_brand_id: brandId }),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) return [];
|
||||
return (await response.json()) as ProductOption[];
|
||||
}
|
||||
try {
|
||||
const rows = await withTenant(brandId, (db) =>
|
||||
db
|
||||
.select({
|
||||
id: products.id,
|
||||
name: products.name,
|
||||
priceCents: products.priceCents,
|
||||
active: products.active,
|
||||
})
|
||||
.from(products)
|
||||
.where(and(eq(products.tenantId, brandId), eq(products.active, true))),
|
||||
);
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
type: "product",
|
||||
price: r.priceCents / 100,
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user