feat(seo): add Torob meta tags and Torob automated products API endpoint
This commit is contained in:
1 parent
812bc2920c
commit
f2535b4aee
13 files changed
+10405
-10094
No files matched your search
@@ -0,0 +1,90 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { productService } from '../../../../lib/services/productService';
|
||||
import { PRODUCTS } from '../../../../lib/data/products';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const revalidate = 3600;
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10));
|
||||
const pageSize = Math.min(100, Math.max(10, parseInt(searchParams.get('page_size') || '100', 10)));
|
||||
|
||||
const baseUrl = (process.env.NEXT_PUBLIC_SITE_URL || 'https://canina.ir').replace(/\/$/, '');
|
||||
|
||||
let allProducts: any[] = [];
|
||||
try {
|
||||
const res = await productService.getProducts({ limit: 999 });
|
||||
allProducts = res.data && res.data.length > 0 ? res.data : PRODUCTS;
|
||||
} catch {
|
||||
allProducts = PRODUCTS;
|
||||
}
|
||||
|
||||
// Filter indexable active products
|
||||
const indexableProducts = allProducts.filter(
|
||||
(p) => !p.noIndex && p.stockStatus !== 'DISCONTINUED'
|
||||
);
|
||||
|
||||
const totalCount = indexableProducts.length;
|
||||
const maxPages = Math.ceil(totalCount / pageSize) || 1;
|
||||
const startIndex = (page - 1) * pageSize;
|
||||
const pagedProducts = indexableProducts.slice(startIndex, startIndex + pageSize);
|
||||
|
||||
const results = pagedProducts.map((product) => {
|
||||
const slug = product.slug || product.artNo || product.id;
|
||||
const pageUrl = `${baseUrl}/shop/${slug}`;
|
||||
const productId = String(product.artNo || product.id || slug);
|
||||
const nameFa = product.nameFa || product.name || '';
|
||||
const nameEn = product.nameEn || '';
|
||||
const title = nameEn ? `${nameFa} (${nameEn})` : nameFa;
|
||||
const primaryImg = product.ogImage || product.image || product.imageUrl || '';
|
||||
const imageLink = primaryImg
|
||||
? primaryImg.startsWith('http')
|
||||
? primaryImg
|
||||
: `${baseUrl}${primaryImg.startsWith('/') ? primaryImg : `/${primaryImg}`}`
|
||||
: '';
|
||||
|
||||
const isInStock =
|
||||
product.stockStatus !== 'OUT_OF_STOCK' &&
|
||||
product.stockStatus !== 'out_of_stock' &&
|
||||
product.inStock !== false;
|
||||
|
||||
const price = Number(product.priceValue || product.price || 0);
|
||||
const oldPrice = Number(product.oldPrice || product.priceValue || product.price || 0);
|
||||
|
||||
return {
|
||||
product_id: productId,
|
||||
page_unique_code: productId,
|
||||
title,
|
||||
subtitle: nameEn || product.scientificTagline || '',
|
||||
page_url: pageUrl,
|
||||
price,
|
||||
old_price: oldPrice,
|
||||
availability: isInStock ? 'instock' : 'outofstock',
|
||||
image_link: imageLink,
|
||||
guarantee: 'ضمانت اصالت ۱۰۰٪ کمپانی Canina آلمان',
|
||||
category_name: product.category || 'مکمل حیوانات خانگی',
|
||||
};
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
count: totalCount,
|
||||
max_pages: maxPages,
|
||||
results,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=7200',
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('[Torob API Error]', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal Server Error fetching products for Torob' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user