From c6aa77f7c9f4d74d85fce4304a164e7f5c03fad3 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Mon, 3 Aug 2026 12:04:27 +0330 Subject: [PATCH] feat(admin): add product buy price field, dual price/percentage margin calculator, and security sanitization [TASK-BUYPRICE] --- backend/src/products/products.service.ts | 19 +- frontend/admin-panel/src/pages/Products.tsx | 224 +++++++++++++++++--- 2 files changed, 206 insertions(+), 37 deletions(-) diff --git a/backend/src/products/products.service.ts b/backend/src/products/products.service.ts index efd7fc51..d7c051dd 100644 --- a/backend/src/products/products.service.ts +++ b/backend/src/products/products.service.ts @@ -91,12 +91,16 @@ export class ProductsService { const isWholesaleOrAdmin = userRole === 'User_Wholesale' || userRole === 'User_Partner' || userRole === 'ADMIN' || userRole === 'SuperAdmin'; + const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin'; + const data = rawProducts.map((p) => { + // Remove buyPrice for all non-admins + const { buyPrice, ...withoutBuyPrice } = p; if (!isWholesaleOrAdmin) { - const { wholesalePrice, ...rest } = p; - return rest; + const { wholesalePrice, ...publicProduct } = withoutBuyPrice; + return publicProduct; } - return p; + return isAdmin ? p : withoutBuyPrice; }); return { @@ -126,11 +130,14 @@ export class ProductsService { if (!product) return null; const isWholesaleOrAdmin = userRole === 'User_Wholesale' || userRole === 'User_Partner' || userRole === 'ADMIN' || userRole === 'SuperAdmin'; + const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin'; + + const { buyPrice, ...withoutBuyPrice } = product; if (!isWholesaleOrAdmin) { - const { wholesalePrice, ...rest } = product; - return rest; + const { wholesalePrice, ...publicProduct } = withoutBuyPrice; + return publicProduct; } - return product; + return isAdmin ? product : withoutBuyPrice; } async getActiveFilters() { diff --git a/frontend/admin-panel/src/pages/Products.tsx b/frontend/admin-panel/src/pages/Products.tsx index 6c2c8e99..2973e351 100644 --- a/frontend/admin-panel/src/pages/Products.tsx +++ b/frontend/admin-panel/src/pages/Products.tsx @@ -27,6 +27,7 @@ export interface Product { categoryId?: string; priceValue?: number; wholesalePrice?: number; + buyPrice?: number; priceDisplay?: string; unit?: string; packageSize?: number; @@ -111,8 +112,11 @@ export default function Products() { description: '', shortDescription: '', categoryId: '', + buyPrice: '' as number | string, priceValue: 0, wholesalePrice: '' as number | string, + priceValueMarginPercent: '' as number | string, + wholesaleMarginPercent: '' as number | string, priceDisplay: '', unit: '', packageSize: 0, @@ -170,6 +174,13 @@ export default function Products() { setMediaImageError(false); if (product) { setEditingProduct(product); + const bPrice = product.buyPrice ? Number(product.buyPrice) : ''; + const pPrice = Number(product.priceValue || 0); + const wPrice = product.wholesalePrice ? Number(product.wholesalePrice) : ''; + + const pMargin = bPrice && pPrice ? Math.round(((pPrice - Number(bPrice)) / Number(bPrice)) * 100) : ''; + const wMargin = bPrice && wPrice ? Math.round(((wPrice - Number(bPrice)) / Number(bPrice)) * 100) : ''; + setFormData({ artNo: product.artNo, nameFa: product.nameFa || '', @@ -178,8 +189,11 @@ export default function Products() { description: product.description || '', shortDescription: product.shortDescription || '', categoryId: product.categoryId || '', - priceValue: Number(product.priceValue), - wholesalePrice: product.wholesalePrice ? Number(product.wholesalePrice) : '', + buyPrice: bPrice, + priceValue: pPrice, + wholesalePrice: wPrice, + priceValueMarginPercent: pMargin, + wholesaleMarginPercent: wMargin, priceDisplay: product.priceDisplay || '', unit: product.unit || '', packageSize: Number(product.packageSize), @@ -201,7 +215,8 @@ export default function Products() { setEditingProduct(null); setFormData({ artNo: '', nameFa: '', nameEn: '', scientificTagline: '', description: '', shortDescription: '', categoryId: '', - priceValue: 0, wholesalePrice: '', priceDisplay: '', unit: '', packageSize: 0, dosageLogic: '', suitableFor: 'سگ و گربه', + buyPrice: '', priceValue: 0, wholesalePrice: '', priceValueMarginPercent: '', wholesaleMarginPercent: '', + priceDisplay: '', unit: '', packageSize: 0, dosageLogic: '', suitableFor: 'سگ و گربه', imageUrl: '', metaTitle: '', metaDescription: '', keywords: '', canonicalUrl: '', slug: '', images: [], podcastUrl: '', videoUrl: '', pdfUrl: '', symptoms: [] @@ -217,6 +232,8 @@ export default function Products() { const derivedSlug = formData.slug.trim() || slugify(formData.nameEn); const finalPayload = { ...formData, + buyPrice: formData.buyPrice ? Number(formData.buyPrice) : 0, + wholesalePrice: formData.wholesalePrice ? Number(formData.wholesalePrice) : null, slug: derivedSlug, metaTitle: formData.metaTitle.trim() || `${formData.nameFa || 'نام محصول'} | خرید و قیمت مکمل کانینا`, metaDescription: formData.metaDescription.trim() || formData.shortDescription || 'خرید آنلاین مکمل اصل کانینا آلمان با بالاترین کیفیت بالینی...', @@ -635,54 +652,199 @@ export default function Products() { {/* Pricing Tab */}
-
+
+ +
+

امنیت و حریم خصوصی قیمت خرید (سود و زیان)

+

+ قیمت خرید و درصد‌های سود به هیچ عنوان در پاسخ‌های API عمومی فرانت‌اند سایت منتشر نمی‌شوند و فقط جهت محاسبات حسابداری و گزارش‌گیری سود/زیان در پنل ادمین استفاده می‌شوند. +

+
+
+ +
+ {/* Buy Price */}
- + { - const val = Number(e.target.value); + const bPrice = e.target.value ? Number(e.target.value) : ''; + let pPrice = formData.priceValue; + let wPrice = formData.wholesalePrice; + let pMargin = formData.priceValueMarginPercent; + let wMargin = formData.wholesaleMarginPercent; + + if (bPrice && pMargin !== '') { + pPrice = Math.round(Number(bPrice) * (1 + Number(pMargin) / 100)); + } else if (bPrice && pPrice) { + pMargin = Math.round(((pPrice - Number(bPrice)) / Number(bPrice)) * 100); + } + + if (bPrice && wMargin !== '') { + wPrice = Math.round(Number(bPrice) * (1 + Number(wMargin) / 100)); + } else if (bPrice && wPrice) { + wMargin = Math.round(((Number(wPrice) - Number(bPrice)) / Number(bPrice)) * 100); + } + setFormData({ ...formData, - priceValue: val, - priceDisplay: `${val.toLocaleString('fa-IR')} تومان`, + buyPrice: bPrice, + priceValue: pPrice, + wholesalePrice: wPrice, + priceValueMarginPercent: pMargin, + wholesaleMarginPercent: wMargin, + priceDisplay: `${pPrice.toLocaleString('fa-IR')} تومان`, }); }} - className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none" + className="w-full px-4 py-3 rounded-xl border border-amber-300 focus:border-amber-500 outline-none bg-amber-50/20 font-bold" dir="ltr" />
+ + {/* Retail Price & Margin */}
- - setFormData({ ...formData, wholesalePrice: e.target.value ? Number(e.target.value) : '' })} className="w-full px-4 py-3 rounded-xl border border-purple-200 focus:border-purple-600 outline-none font-bold bg-purple-50/20" dir="ltr" /> +
+ + یا سود ٪ تک‌فروشی +
+
+ { + const val = Number(e.target.value); + const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null; + const pMargin = bPrice && val ? Math.round(((val - bPrice) / bPrice) * 100) : ''; + setFormData({ + ...formData, + priceValue: val, + priceValueMarginPercent: pMargin, + priceDisplay: `${val.toLocaleString('fa-IR')} تومان`, + }); + }} + className="col-span-3 px-3 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-bold" + dir="ltr" + /> +
+ { + const margin = e.target.value !== '' ? Number(e.target.value) : ''; + const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null; + let pPrice = formData.priceValue; + if (bPrice && margin !== '') { + pPrice = Math.round(bPrice * (1 + Number(margin) / 100)); + } + setFormData({ + ...formData, + priceValueMarginPercent: margin, + priceValue: pPrice, + priceDisplay: `${pPrice.toLocaleString('fa-IR')} تومان`, + }); + }} + className="w-full px-2 py-3 rounded-xl border border-purple-200 focus:border-purple-500 text-center font-bold text-xs bg-purple-50/30 outline-none" + dir="ltr" + /> + ٪ +
+
+ + {/* Wholesale B2B Price & Margin */} +
+
+ + یا سود ٪ B2B +
+
+ { + const val = e.target.value ? Number(e.target.value) : ''; + const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null; + const wMargin = bPrice && val ? Math.round(((Number(val) - bPrice) / bPrice) * 100) : ''; + setFormData({ + ...formData, + wholesalePrice: val, + wholesaleMarginPercent: wMargin, + }); + }} + className="col-span-3 px-3 py-3 rounded-xl border border-purple-200 focus:border-purple-600 outline-none font-bold bg-purple-50/20" + dir="ltr" + /> +
+ { + const margin = e.target.value !== '' ? Number(e.target.value) : ''; + const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null; + let wPrice = formData.wholesalePrice; + if (bPrice && margin !== '') { + wPrice = Math.round(bPrice * (1 + Number(margin) / 100)); + } + setFormData({ + ...formData, + wholesaleMarginPercent: margin, + wholesalePrice: wPrice, + }); + }} + className="w-full px-2 py-3 rounded-xl border border-purple-200 focus:border-purple-500 text-center font-bold text-xs bg-purple-50/30 outline-none" + dir="ltr" + /> + ٪ +
+
+
+
+ +
setFormData({ ...formData, packageSize: Number(e.target.value) })} className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none" dir="ltr" />
-
- -