fix(storefront+TPV): storefront live search proxy, product expiry/weight display, TPV receipt popup timestamp

This commit is contained in:
chattie
2026-08-24 22:11:04 +02:00
parent 2ed2ac8cd1
commit 71165911ab
6 changed files with 79 additions and 11 deletions

View File

@@ -0,0 +1,34 @@
import { NextRequest, NextResponse } from 'next/server';
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
export async function GET(req: NextRequest) {
const path = req.nextUrl.pathname.replace('/api/catalog-proxy/', '');
const cookies = req.headers.get('cookie') ?? '';
try {
const res = await fetch(`${API}/${path}${req.nextUrl.search}`, {
headers: { accept: 'application/json', cookie: cookies },
});
const data = await res.json().catch(() => ({}));
return NextResponse.json(data, { status: res.status });
} catch (err) {
return NextResponse.json({ error: String(err) }, { status: 502 });
}
}
export async function POST(req: NextRequest) {
const path = req.nextUrl.pathname.replace('/api/catalog-proxy/', '');
const cookies = req.headers.get('cookie') ?? '';
const body = await req.text();
try {
const res = await fetch(`${API}/${path}${req.nextUrl.search}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', cookie: cookies },
body,
});
const data = await res.json().catch(() => ({}));
return NextResponse.json(data, { status: res.status });
} catch (err) {
return NextResponse.json({ error: String(err) }, { status: 502 });
}
}

View File

@@ -102,6 +102,18 @@ export default async function ProductPage({ params }: PageProps) {
<dt className="font-semibold text-emerald-950">Estado</dt>
<dd>{product.state}</dd>
</div>
{product.expirationDate && (
<div>
<dt className="font-semibold text-emerald-950">Caducidad</dt>
<dd>{new Date(product.expirationDate).toLocaleDateString('es-ES', { year: 'numeric', month: 'long', day: 'numeric' })}</dd>
</div>
)}
{product.unitWeightKg && (
<div>
<dt className="font-semibold text-emerald-950">Peso</dt>
<dd>{product.unitWeightKg >= 1 ? `${product.unitWeightKg} kg` : `${Math.round(product.unitWeightKg * 1000)} g`}</dd>
</div>
)}
</dl>
<AddToCart
productId={product.id}

View File

@@ -53,6 +53,8 @@ export interface ProductSummaryDto {
seoDescription: string | null;
categoryIds: string[];
brandId: string | null;
expirationDate?: string | null;
unitWeightKg?: number | null;
createdAt: string;
updatedAt: string;
}
@@ -79,7 +81,11 @@ export interface SearchProductsInput {
// Use env var if set, otherwise use relative URL (works for SSR and client-side via same-origin)
function apiBaseUrl(): string {
const base = process.env.API_BASE_URL ?? process.env.NEXT_PUBLIC_API_BASE_URL ?? '';
// Browser: use same-origin proxy to avoid network/IP issues
// SSR: use backend URL directly
const base = (typeof window !== 'undefined')
? '/api/catalog-proxy'
: (process.env.API_BASE_URL ?? process.env.NEXT_PUBLIC_API_BASE_URL ?? 'http://127.0.0.1:3000');
return base.replace(/\/$/, '');
}