80 lines
3.3 KiB
TypeScript
80 lines
3.3 KiB
TypeScript
import { 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; // Revalidate every hour
|
|
|
|
export async function GET() {
|
|
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://canina.ir';
|
|
|
|
let productsList: any[] = [];
|
|
try {
|
|
const res = await productService.getProducts({ limit: 999 });
|
|
productsList = res.data && res.data.length > 0 ? res.data : PRODUCTS;
|
|
} catch {
|
|
productsList = PRODUCTS;
|
|
}
|
|
|
|
// Only active, indexable products
|
|
const indexableProducts = productsList.filter(p => !p.noIndex && p.stockStatus !== 'DISCONTINUED');
|
|
|
|
const itemsXml = indexableProducts.map(product => {
|
|
const slug = product.slug || product.artNo || product.id;
|
|
const link = `${baseUrl}/shop/${slug}`;
|
|
const id = product.artNo || product.id;
|
|
const title = product.nameEn ? `${product.nameFa || product.name} (${product.nameEn})` : (product.nameFa || product.name);
|
|
const description = product.metaDescription || product.shortDescription || product.description || 'مکمل درمانی اصل کنینا آلمان';
|
|
|
|
const primaryImg = product.ogImage || product.image;
|
|
const imageLink = primaryImg ? (primaryImg.startsWith('http') ? primaryImg : `${baseUrl}${primaryImg}`) : '';
|
|
|
|
let availability = 'in_stock';
|
|
if (product.stockStatus === 'OUT_OF_STOCK') availability = 'out_of_stock';
|
|
if (product.stockStatus === 'PRE_ORDER') availability = 'preorder';
|
|
|
|
const price = product.priceValue ? `${product.priceValue * 10} IRR` : '0 IRR';
|
|
const gtinTag = product.barcode ? `<g:gtin>${product.barcode}</g:gtin>` : '';
|
|
const additionalImages = (product.images || [])
|
|
.filter((img: string) => img && img !== primaryImg)
|
|
.slice(0, 5)
|
|
.map((img: string) => `<g:additional_image_link>${img.startsWith('http') ? img : `${baseUrl}${img}`}</g:additional_image_link>`)
|
|
.join('\n ');
|
|
|
|
return ` <item>
|
|
<g:id>${id}</g:id>
|
|
<title><![CDATA[${title}]]></title>
|
|
<description><![CDATA[${description}]]></description>
|
|
<link>${link}</link>
|
|
<g:image_link>${imageLink}</g:image_link>
|
|
${additionalImages}
|
|
<g:availability>${availability}</g:availability>
|
|
<g:price>${price}</g:price>
|
|
<g:brand>Canina pharma GmbH</g:brand>
|
|
<g:mpn>${product.artNo}</g:mpn>
|
|
${gtinTag}
|
|
<g:condition>new</g:condition>
|
|
<g:product_type><![CDATA[${product.category || 'مکمل حیوانات'}]]></g:product_type>
|
|
</item>`;
|
|
}).join('\n');
|
|
|
|
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
|
|
<channel>
|
|
<title>فید محصولات و مکملهای تخصصی کنینا ایران</title>
|
|
<link>${baseUrl}/shop</link>
|
|
<description>فید رسمی محصولات و مکملهای دارویی سگ و گربه برند Canina آلمان</description>
|
|
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
|
|
${itemsXml}
|
|
</channel>
|
|
</rss>`;
|
|
|
|
return new NextResponse(xmlContent, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/xml; charset=utf-8',
|
|
'Cache-Control': 'public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400',
|
|
},
|
|
});
|
|
}
|