feat(admin): add product buy price field, dual price/percentage margin calculator, and security sanitization [TASK-BUYPRICE]

This commit is contained in:
parsa aghaei committed 2026-08-03 12:04:27 +03:30
1 parent 38261c2f76
commit c6aa77f7c9
2 files changed
+206 -37

No files matched your search

+13 -6
View File
@@ -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() {
+193 -31
View File
@@ -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 */}
<div className={activeTab === 'pricing' ? 'block space-y-4' : 'hidden'}>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="bg-amber-50/60 border border-amber-200 p-4 rounded-xl text-xs text-amber-800 flex items-start gap-2">
<HelpCircle className="w-4 h-4 text-amber-600 shrink-0 mt-0.5" />
<div>
<p className="font-bold mb-1">امنیت و حریم خصوصی قیمت خرید (سود و زیان)</p>
<p className="leading-relaxed text-[11px]">
قیمت خرید و درصد‌های سود به هیچ عنوان در پاسخ‌های API عمومی فرانت‌اند سایت منتشر نمی‌شوند و فقط جهت محاسبات حسابداری و گزارش‌گیری سود/زیان در پنل ادمین استفاده می‌شوند.
</p>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{/* Buy Price */}
<div className="space-y-2">
<label className="text-sm font-bold text-gray-700">قیمت اصلی (تومان) *</label>
<label className="text-sm font-bold text-amber-900 flex items-center gap-1.5">
<span>قیمت خرید ما (تومان)</span>
<div className="group relative inline-block">
<HelpCircle className="w-3.5 h-3.5 text-amber-500 cursor-help" />
<div className="absolute top-full right-1/2 translate-x-1/2 mt-2 hidden group-hover:block w-52 p-2.5 bg-gray-900 text-white text-[10px] rounded-lg shadow-xl text-center z-50 font-vazir leading-relaxed">
مبلغ تمام شده خرید کالا برای مبنای محاسبات سود و محاسبه خودکار قیمت فروش با درصد.
</div>
</div>
</label>
<input
required
type="number"
min="0"
value={formData.priceValue}
placeholder="مثال: 500000"
value={formData.buyPrice}
onChange={(e) => {
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"
/>
</div>
{/* Retail Price & Margin */}
<div className="space-y-2">
<label className="text-sm font-bold text-purple-700 flex items-center gap-1">
<span>قیمت اختصاصی B2B (تومان)</span>
<span className="text-[10px] font-normal text-gray-400">(اختیاری)</span>
</label>
<input type="number" min="0" placeholder="در صورت عدم ورود، درصد کلی اعمال می‌شود" value={formData.wholesalePrice} onChange={(e) => 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" />
<div className="flex justify-between items-center">
<label className="text-sm font-bold text-gray-700">قیمت فروش تک‌فروشی (تومان) *</label>
<span className="text-[10px] text-purple-600 font-bold">یا سود ٪ تک‌فروشی</span>
</div>
<div className="grid grid-cols-5 gap-2">
<input
required
type="number"
min="0"
value={formData.priceValue}
onChange={(e) => {
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"
/>
<div className="col-span-2 relative">
<input
type="number"
placeholder="٪ سود"
value={formData.priceValueMarginPercent}
onChange={(e) => {
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"
/>
<span className="absolute left-2 top-1/2 -translate-y-1/2 text-gray-400 text-xs pointer-events-none">٪</span>
</div>
</div>
</div>
{/* Wholesale B2B Price & Margin */}
<div className="space-y-2">
<div className="flex justify-between items-center">
<label className="text-sm font-bold text-purple-700">قیمت اختصاصی B2B (تومان)</label>
<span className="text-[10px] text-purple-600 font-bold">یا سود ٪ B2B</span>
</div>
<div className="grid grid-cols-5 gap-2">
<input
type="number"
min="0"
placeholder="قیمت B2B"
value={formData.wholesalePrice}
onChange={(e) => {
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"
/>
<div className="col-span-2 relative">
<input
type="number"
placeholder="٪ سود"
value={formData.wholesaleMarginPercent}
onChange={(e) => {
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"
/>
<span className="absolute left-2 top-1/2 -translate-y-1/2 text-gray-400 text-xs pointer-events-none">٪</span>
</div>
</div>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 pt-2">
<div className="space-y-2">
<label className="text-sm font-bold text-gray-700">موجودی *</label>
<input required type="number" min="0" value={formData.packageSize} onChange={(e) => 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" />
</div>
<div className="space-y-2 col-span-1 md:col-span-2">
<label className="text-sm font-bold text-gray-700 flex items-center justify-between">
<span>منطق و دوز دقیق مصرفی (Dosage Calculator Fields)</span>
<span className="text-xs text-purple-600 font-bold">فرمت JSON یا راهنمای متنی</span>
</label>
<textarea
rows={3}
value={formData.dosageLogic}
onChange={(e) => setFormData({ ...formData, dosageLogic: e.target.value })}
placeholder='{"baseDosage": 1, "perKg": 10, "unit": "قرص", "maxPerDay": 3}'
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-mono text-xs"
/>
<p className="text-[11px] text-gray-400">
این مقادیر توسط محاسبه‌گر هوشمند دوز مکمل در فرانت‌اند بخوانده می‌شود.
</p>
<div className="space-y-2">
<label className="text-sm font-bold text-gray-700">واحد سنجش</label>
<input type="text" placeholder="مثال: عدد / بسته" value={formData.unit} onChange={(e) => setFormData({ ...formData, unit: e.target.value })} className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm" />
</div>
</div>
<div className="space-y-2 col-span-1 md:col-span-2 pt-2">
<label className="text-sm font-bold text-gray-700 flex items-center justify-between">
<span>منطق و دوز دقیق مصرفی (Dosage Calculator Fields)</span>
<span className="text-xs text-purple-600 font-bold">فرمت JSON یا راهنمای متنی</span>
</label>
<textarea
rows={3}
value={formData.dosageLogic}
onChange={(e) => setFormData({ ...formData, dosageLogic: e.target.value })}
placeholder='{"baseDosage": 1, "perKg": 10, "unit": "قرص", "maxPerDay": 3}'
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-mono text-xs"
/>
<p className="text-[11px] text-gray-400">
این مقادیر توسط محاسبه‌گر هوشمند دوز مکمل در فرانت‌اند خوانده می‌شود.
</p>
</div>
</div>