fix(torob): normalize suitable-for pet targets and deduplicate package size in product titles
This commit is contained in:
1 parent
075f00a4ed
commit
44bfb5c593
2 files changed
+100
-43
No files matched your search
@@ -5,6 +5,22 @@ import { getMediaUrl } from '../../../../lib/media';
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const revalidate = 3600;
|
||||
|
||||
function normalizeSuitableFor(val?: string | null): string {
|
||||
if (!val) return '';
|
||||
const s = String(val).trim();
|
||||
const upper = s.toUpperCase();
|
||||
if (['ALL', 'BOTH', 'DOG_AND_CAT', 'CAT_AND_DOG', 'DOGS_AND_CATS', 'CATS_AND_DOGS', 'هر دو', 'سگ و گربه', 'سگ، گربه', 'سگ وگربه'].includes(upper)) {
|
||||
return 'سگ و گربه';
|
||||
}
|
||||
if (['DOG', 'DOGS', 'سگ'].includes(upper)) {
|
||||
return 'سگ';
|
||||
}
|
||||
if (['CAT', 'CATS', 'گربه'].includes(upper)) {
|
||||
return 'گربه';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
function buildTorobProductTitle(product: {
|
||||
nameFa?: string | null;
|
||||
name?: string | null;
|
||||
@@ -21,23 +37,34 @@ function buildTorobProductTitle(product: {
|
||||
nameEn = nameEn.replace(/^Canina\s*[-–:]*\s*/i, '').trim();
|
||||
}
|
||||
|
||||
// Package size & unit
|
||||
// Package size & unit deduplication
|
||||
let sizePart = '';
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || '').trim();
|
||||
|
||||
let sizeAlreadyInNames = false;
|
||||
if (numSize && numSize > 0) {
|
||||
sizePart = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
const escapedUnit = unit.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const regexList = [
|
||||
new RegExp(`\\b${numSize}\\s*${escapedUnit}\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}${escapedUnit}\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}\\s*میلی?لیتر\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}\\s*گرم\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}\\s*عددی?\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}\\s*Tab\\b`, 'i'),
|
||||
new RegExp(`\\b${numSize}\\s*tablet\\b`, 'i'),
|
||||
];
|
||||
sizeAlreadyInNames = regexList.some((rgx) => rgx.test(nameFa) || rgx.test(nameEn));
|
||||
if (!sizeAlreadyInNames) {
|
||||
sizePart = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Suitable for pet
|
||||
let petPart = '';
|
||||
if (product.suitableFor) {
|
||||
const pet = product.suitableFor.trim();
|
||||
if (pet === 'هر دو' || pet === 'سگ و گربه' || pet === 'سگ، گربه') {
|
||||
petPart = 'مناسب سگ و گربه';
|
||||
} else if (pet) {
|
||||
petPart = `مناسب ${pet}`;
|
||||
}
|
||||
const pet = normalizeSuitableFor(product.suitableFor);
|
||||
if (pet) {
|
||||
petPart = `مناسب ${pet}`;
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
@@ -78,8 +105,9 @@ function buildTorobProductSpec(product: {
|
||||
'ضمانت': 'اصالت ۱۰۰٪ کالای آلمانی و سلامت فیزیکی',
|
||||
};
|
||||
|
||||
if (product.suitableFor) {
|
||||
spec['گونه هدف'] = product.suitableFor === 'هر دو' ? 'سگ و گربه' : product.suitableFor;
|
||||
const pet = normalizeSuitableFor(product.suitableFor);
|
||||
if (pet) {
|
||||
spec['گونه هدف'] = pet;
|
||||
}
|
||||
|
||||
const numSize = Number(product.packageSize);
|
||||
|
||||
Reference in new issue
Block a user