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