Implement discount tiers for pricing settings and enhance related functionalities
- Introduced a new `DiscountTierDto` to represent volume discount tiers, allowing roles with a config quota at or above specified thresholds to receive discounts on pricing. - Updated the `PricingSettingsDto` to include a list of discount tiers, enhancing the pricing model to support more flexible pricing strategies. - Modified the `GetPricingSettingsQueryHandler` and `UpdatePricingSettingsCommandHandler` to handle discount tiers, ensuring they are correctly retrieved and updated in the database. - Enhanced validation in `UpdatePricingSettingsCommandValidator` to enforce uniqueness and progressive discount tiers, preventing invalid configurations. - Updated frontend components to support the new discount tier functionality, including forms for adding and managing discount tiers in the admin interface. - Revised API documentation to reflect the new discount tier features and their usage in pricing settings.
This commit is contained in:
@@ -9,6 +9,8 @@ import { HttpError } from '@/shared/api/client'
|
||||
import type { PricingSettingsDto } from '@/shared/api/types'
|
||||
import { updatePricingSettings } from './api'
|
||||
|
||||
type TierRow = { minConfigs: string; discountPercent: string }
|
||||
|
||||
export function PricingSettingsEditor({ settings }: { settings: PricingSettingsDto }) {
|
||||
const { t } = useTranslation()
|
||||
const queryClient = useQueryClient()
|
||||
@@ -21,6 +23,31 @@ export function PricingSettingsEditor({ settings }: { settings: PricingSettingsD
|
||||
const [pricePerConfigPerYear, setPricePerConfigPerYear] = useState(
|
||||
settings.pricePerConfigPerYear != null ? String(settings.pricePerConfigPerYear) : '',
|
||||
)
|
||||
const [tiers, setTiers] = useState<TierRow[]>(
|
||||
settings.discountTiers.map((t) => ({ minConfigs: String(t.minConfigs), discountPercent: String(t.discountPercent) })),
|
||||
)
|
||||
|
||||
const updateTier = (index: number, patch: Partial<TierRow>) =>
|
||||
setTiers((rows) => rows.map((row, i) => (i === index ? { ...row, ...patch } : row)))
|
||||
const removeTier = (index: number) => setTiers((rows) => rows.filter((_, i) => i !== index))
|
||||
const addTier = () => setTiers((rows) => [...rows, { minConfigs: '', discountPercent: '' }])
|
||||
|
||||
// Зеркалит бэкенд-инварианты UpdatePricingSettingsCommandValidator: пороги не повторяются, скидка
|
||||
// на большем пороге не меньше скидки на меньшем (иначе лесенка не прогрессивная).
|
||||
const tierErrors = (() => {
|
||||
if (tiers.some((t) => t.minConfigs === '' || t.discountPercent === '')) return null
|
||||
const parsed = tiers.map((t) => ({ minConfigs: Number(t.minConfigs), discountPercent: Number(t.discountPercent) }))
|
||||
if (parsed.some((t) => !Number.isInteger(t.minConfigs) || t.minConfigs < 1)) return t('admin.pricing.discountTierInvalidMinConfigs')
|
||||
if (parsed.some((t) => !Number.isInteger(t.discountPercent) || t.discountPercent < 1 || t.discountPercent > 99))
|
||||
return t('admin.pricing.discountTierInvalidPercent')
|
||||
if (new Set(parsed.map((t) => t.minConfigs)).size !== parsed.length) return t('admin.pricing.discountTiersDuplicate')
|
||||
const sorted = [...parsed].sort((a, b) => a.minConfigs - b.minConfigs)
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
if (sorted[i].discountPercent < sorted[i - 1].discountPercent) return t('admin.pricing.discountTiersNotProgressive')
|
||||
}
|
||||
return null
|
||||
})()
|
||||
const hasIncompleteTier = tiers.some((t) => t.minConfigs === '' || t.discountPercent === '')
|
||||
|
||||
// Все ставки — цена за конфиг в месяц; итог за более длинный период (ставка × месяцев) не должен
|
||||
// быть дешевле итога за более короткий, иначе выгоднее купить длинный тариф и не продлевать.
|
||||
@@ -48,6 +75,7 @@ export function PricingSettingsEditor({ settings }: { settings: PricingSettingsD
|
||||
pricePerConfigPerQuarter === '' ? null : Number(pricePerConfigPerQuarter),
|
||||
pricePerConfigPerHalfYear === '' ? null : Number(pricePerConfigPerHalfYear),
|
||||
pricePerConfigPerYear === '' ? null : Number(pricePerConfigPerYear),
|
||||
tiers.map((t) => ({ minConfigs: Number(t.minConfigs), discountPercent: Number(t.discountPercent) })),
|
||||
),
|
||||
onSuccess: async () => {
|
||||
toast.success(t('admin.pricing.updated'))
|
||||
@@ -106,8 +134,40 @@ export function PricingSettingsEditor({ settings }: { settings: PricingSettingsD
|
||||
<p className="text-xs text-red-600">{t('admin.pricing.yearCheaperThanQuarter')}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>{t('admin.pricing.discountTiersTitle')}</Label>
|
||||
<p className="text-xs text-muted-foreground">{t('admin.pricing.discountTiersHint')}</p>
|
||||
{tiers.map((tier, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
placeholder={t('admin.pricing.discountTierMinConfigs')}
|
||||
value={tier.minConfigs}
|
||||
onChange={(e) => updateTier(index, { minConfigs: e.target.value })}
|
||||
/>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={99}
|
||||
placeholder={t('admin.pricing.discountTierPercent')}
|
||||
value={tier.discountPercent}
|
||||
onChange={(e) => updateTier(index, { discountPercent: e.target.value })}
|
||||
/>
|
||||
<Button type="button" variant="ghost" size="sm" onClick={() => removeTier(index)}>
|
||||
{t('admin.pricing.removeDiscountTier')}
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
{tierErrors && <p className="text-xs text-red-600">{tierErrors}</p>}
|
||||
<div>
|
||||
<Button type="button" variant="outline" size="sm" onClick={addTier}>
|
||||
{t('admin.pricing.addDiscountTier')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<Button type="submit" disabled={mutation.isPending || hasInvalidCombo}>
|
||||
<Button type="submit" disabled={mutation.isPending || hasInvalidCombo || !!tierErrors || hasIncompleteTier}>
|
||||
{t('admin.roles.save')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user