From 44bfb5c593dd69e8dc2fb30ac65a82dbbfbae66a Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 14 Oct 2026 15:32:00 +0000 Subject: [PATCH] fix(torob): normalize suitable-for pet targets and deduplicate package size in product titles --- backend/src/products/torob.controller.ts | 93 ++++++++++++------- .../app/api/torob/products/route.ts | 50 +++++++--- 2 files changed, 100 insertions(+), 43 deletions(-) diff --git a/backend/src/products/torob.controller.ts b/backend/src/products/torob.controller.ts index 4c50b6a9..ff1eedbf 100644 --- a/backend/src/products/torob.controller.ts +++ b/backend/src/products/torob.controller.ts @@ -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 { const spec: Record = { - برند: "کنینا (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; diff --git a/frontend/application/app/api/torob/products/route.ts b/frontend/application/app/api/torob/products/route.ts index 37eb2ed7..0325055f 100644 --- a/frontend/application/app/api/torob/products/route.ts +++ b/frontend/application/app/api/torob/products/route.ts @@ -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);