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
@@ -2,48 +2,76 @@ import { Controller, Get, Query } from "@nestjs/common";
|
||||
import { ApiTags, ApiOperation } from "@nestjs/swagger";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
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;
|
||||
nameEn?: string | null;
|
||||
packageSize?: unknown;
|
||||
unit?: string | null;
|
||||
suitableFor?: string | null;
|
||||
}): string {
|
||||
const nameFa = (product.nameFa || "").trim();
|
||||
let nameEn = (product.nameEn || "").trim();
|
||||
const nameFa = (product.nameFa || product.name || '').trim();
|
||||
let nameEn = (product.nameEn || '').trim();
|
||||
|
||||
// Clean brand duplicate if nameEn already starts with Canina
|
||||
if (/^Canina\b/i.test(nameEn)) {
|
||||
nameEn = nameEn.replace(/^Canina\s*[-–:]*\s*/i, "").trim();
|
||||
nameEn = nameEn.replace(/^Canina\s*[-–:]*\s*/i, '').trim();
|
||||
}
|
||||
|
||||
// Package size & unit
|
||||
let sizePart = "";
|
||||
// Package size & unit deduplication
|
||||
let sizePart = '';
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || "").trim();
|
||||
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}`;
|
||||
}
|
||||
let petPart = '';
|
||||
const pet = normalizeSuitableFor(product.suitableFor);
|
||||
if (pet) {
|
||||
petPart = `مناسب ${pet}`;
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
if (nameFa) parts.push(nameFa);
|
||||
|
||||
// Brand part
|
||||
if (!nameFa.includes("کنینا") && !nameFa.includes("Canina")) {
|
||||
parts.push("برند کنینا Canina");
|
||||
} else if (!nameFa.includes("Canina")) {
|
||||
parts.push("Canina");
|
||||
if (!nameFa.includes('کنینا') && !nameFa.includes('Canina')) {
|
||||
parts.push('برند کنینا Canina');
|
||||
} else if (!nameFa.includes('Canina')) {
|
||||
parts.push('Canina');
|
||||
}
|
||||
|
||||
if (nameEn && nameEn.toLowerCase() !== nameFa.toLowerCase()) {
|
||||
@@ -58,7 +86,7 @@ function buildTorobProductTitle(product: {
|
||||
parts.push(petPart);
|
||||
}
|
||||
|
||||
return parts.join(" ").replace(/\s+/g, " ").trim();
|
||||
return parts.join(' ').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function buildTorobProductSpec(product: {
|
||||
@@ -66,31 +94,32 @@ function buildTorobProductSpec(product: {
|
||||
unit?: string | null;
|
||||
suitableFor?: string | null;
|
||||
artNo?: string | null;
|
||||
category?: { name?: string } | null;
|
||||
category?: string | { name?: string } | null;
|
||||
}): Record<string, string> {
|
||||
const spec: Record<string, string> = {
|
||||
برند: "کنینا (Canina Pharma Germany)",
|
||||
"کشور سازنده": "آلمان",
|
||||
ضمانت: "اصالت ۱۰۰٪ کالای آلمانی و سلامت فیزیکی",
|
||||
'برند': 'کنینا (Canina Pharma Germany)',
|
||||
'کشور سازنده': 'آلمان',
|
||||
'ضمانت': 'اصالت ۱۰۰٪ کالای آلمانی و سلامت فیزیکی',
|
||||
};
|
||||
|
||||
if (product.suitableFor) {
|
||||
spec["گونه هدف"] =
|
||||
product.suitableFor === "هر دو" ? "سگ و گربه" : product.suitableFor;
|
||||
const pet = normalizeSuitableFor(product.suitableFor);
|
||||
if (pet) {
|
||||
spec['گونه هدف'] = pet;
|
||||
}
|
||||
|
||||
const numSize = Number(product.packageSize);
|
||||
const unit = (product.unit || "").trim();
|
||||
const unit = (product.unit || '').trim();
|
||||
if (numSize && numSize > 0) {
|
||||
spec["حجم / وزن / تعداد"] = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
spec['حجم / وزن / تعداد'] = unit ? `${numSize} ${unit}` : `${numSize}`;
|
||||
}
|
||||
|
||||
if (product.artNo) {
|
||||
spec["کد محصول (Art.No)"] = product.artNo;
|
||||
spec['کد محصول (Art.No)'] = product.artNo;
|
||||
}
|
||||
|
||||
if (product.category?.name) {
|
||||
spec["دسته دارویی و درمانی"] = product.category.name;
|
||||
const catName = typeof product.category === 'object' ? product.category?.name : product.category;
|
||||
if (catName) {
|
||||
spec['دسته دارویی و درمانی'] = catName;
|
||||
}
|
||||
|
||||
return spec;
|
||||
|
||||
Reference in new issue
Block a user