// api/restrictions/[productId]/[brandId]/[categoryId]/route.ts
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/db";
import { restrictionGroupTable } from "@/db/schema";
import { eq, sql, or, inArray } from "drizzle-orm";
export const revalidate = 3600;
export async function GET(
request: NextRequest,
{
params,
}: { params: { productId: string; brandId: string; categoryId: string } },
) {
const { productId, brandId, categoryId } = params;
try {
console.log("Fetching restrictions for:", {
productId,
brandId,
categoryId,
});
const restrictions = await db
.select({
name: restrictionGroupTable.name,
brands: restrictionGroupTable.brands,
categories: restrictionGroupTable.categories,
products: restrictionGroupTable.products,
zipcodes: restrictionGroupTable.zipcodes,
states: restrictionGroupTable.states,
})
.from(restrictionGroupTable)
.where(
or(
inArray(restrictionGroupTable.products, [sql${productId}::jsonb]),
inArray(restrictionGroupTable.brands, [sql${brandId}::jsonb]),
inArray(restrictionGroupTable.categories, [
sql${categoryId}::jsonb,
]),
),
)
.execute();
console.log("Restrictions fetched:", restrictions);
if (restrictions.length === 0) {
console.log(
"No restrictions found for this product, brand, or category.",
);
}
return NextResponse.json(restrictions);
} catch (error) {
console.error("Error fetching restrictions:", error);
return NextResponse.json(
{ error: "Failed to fetch restrictions" },
{ status: 500 },
);
}
}
// api/restrictions/[productId]/[brandId]/[categoryId]/route.ts
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/db";
import { restrictionGroupTable } from "@/db/schema";
import { eq, sql, or, inArray } from "drizzle-orm";
export const revalidate = 3600;
export async function GET(
request: NextRequest,
{
params,
}: { params: { productId: string; brandId: string; categoryId: string } },
) {
const { productId, brandId, categoryId } = params;
try {
console.log("Fetching restrictions for:", {
productId,
brandId,
categoryId,
});
const restrictions = await db
.select({
name: restrictionGroupTable.name,
brands: restrictionGroupTable.brands,
categories: restrictionGroupTable.categories,
products: restrictionGroupTable.products,
zipcodes: restrictionGroupTable.zipcodes,
states: restrictionGroupTable.states,
})
.from(restrictionGroupTable)
.where(
or(
inArray(restrictionGroupTable.products, [sql${productId}::jsonb]),
inArray(restrictionGroupTable.brands, [sql${brandId}::jsonb]),
inArray(restrictionGroupTable.categories, [
sql${categoryId}::jsonb,
]),
),
)
.execute();
console.log("Restrictions fetched:", restrictions);
if (restrictions.length === 0) {
console.log(
"No restrictions found for this product, brand, or category.",
);
}
return NextResponse.json(restrictions);
} catch (error) {
console.error("Error fetching restrictions:", error);
return NextResponse.json(
{ error: "Failed to fetch restrictions" },
{ status: 500 },
);
}
}