feat(billing): add percentage discount coupons

Admins can create coupon codes that discount specific services (app
runtimes, optional services, managed products, custom-domain addon, or
all) and restrict them to specific users or make them public, with total
and per-user usage caps and an active date window.

Coupons apply in deploy, renewal, and upgrade flows: cost-breakdown lines
are tagged with a service key, the eligible portion is discounted and
capped to the payable amount, the invoice records discountAmount/
discountCode, and the redemption is recorded once when the invoice is
fully paid (covering wallet, gateway, and mixed payments).

- Discount + DiscountRedemption entities; invoice discount columns
- DiscountService (CRUD, validation, redemption) + admin/validate API
- Idempotent schema bootstrap on init so production (synchronize off)
  provisions the tables/columns without a migration runner
- Admin discounts UI, coupon entry in deploy/renewal, invoice discount line
- fa/en strings; discount.service unit spec

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
keyhan
2026-06-20 11:48:45 +03:30
parent b232129ee2
commit 49726f1dfd
21 changed files with 2013 additions and 20 deletions
+64
View File
@@ -655,6 +655,7 @@ const en: Dictionary = {
},
title: 'Invoices',
subtitle: 'Review what each payment was for and pay open invoices.',
subtotal: 'Subtotal',
walletBalance: 'Wallet balance',
toman: 'Toman',
filterAll: 'All',
@@ -1102,6 +1103,69 @@ const en: Dictionary = {
saveFailedShort: 'Failed to save',
hours: 'hours',
days: 'days',
discounts: {
title: 'Discount codes',
subtitle: 'Percentage discounts on different services — public or for specific users.',
add: 'New discount',
empty: 'No discount codes yet',
edit: 'Edit',
delete: 'Delete',
deleteConfirm: 'Delete this discount code?',
code: 'Code',
codePlaceholder: 'NOWRUZ1403',
name: 'Label',
namePlaceholder: 'Nowruz discount',
description: 'Description',
percentOff: 'Percent off',
scope: 'Eligible services',
allServices: 'All services',
selectServices: 'Select specific services',
audience: 'Eligible users',
public: 'Public (all users)',
restricted: 'Specific users',
searchUsers: 'Search users by name or email…',
noUsersSelected: 'No users selected yet',
limits: 'Limits',
maxUses: 'Total usage cap',
maxUsesPerUser: 'Per-user cap',
unlimited: 'Unlimited',
startsAt: 'Start date',
endsAt: 'End date',
active: 'Active',
inactive: 'Inactive',
used: 'Used',
save: 'Save',
saving: 'Saving…',
cancel: 'Cancel',
saved: 'Discount code saved',
deleted: 'Discount code deleted',
saveFailed: 'Failed to save discount code',
groups: {
runtime: 'App runtimes',
optional: 'Optional services',
managed: 'Managed services',
addon: 'Add-ons',
},
coupon: {
label: 'Discount code',
placeholder: 'Enter discount code',
apply: 'Apply',
checking: 'Checking…',
applied: 'Discount applied',
remove: 'Remove',
discountLine: 'Discount',
},
reasons: {
not_found: 'Invalid discount code',
inactive: 'This discount code is inactive',
not_started: 'This code is not active yet',
expired: 'This code has expired',
max_uses_reached: 'This code has reached its usage limit',
max_uses_per_user_reached: 'You have reached your usage limit for this code',
not_eligible_user: 'This code is not available for your account',
no_eligible_services: 'This code does not discount your selected services',
},
},
},
servicesNew: {
steps: ['Service type', 'Configuration', 'Review & pay'],
+64
View File
@@ -654,6 +654,7 @@ const fa = {
},
title: 'فاکتورها',
subtitle: 'ببین هر پرداخت بابت چه بوده و فاکتورهای باز را پرداخت کن.',
subtotal: 'جمع جزء',
walletBalance: 'موجودی کیف‌پول',
toman: 'تومان',
filterAll: 'همه',
@@ -1101,6 +1102,69 @@ const fa = {
saveFailedShort: 'ذخیره ناموفق بود',
hours: 'ساعت',
days: 'روز',
discounts: {
title: 'کدهای تخفیف',
subtitle: 'تخفیف درصدی روی سرویس‌های مختلف؛ عمومی یا مخصوص کاربران خاص.',
add: 'کد تخفیف جدید',
empty: 'هنوز کد تخفیفی تعریف نشده',
edit: 'ویرایش',
delete: 'حذف',
deleteConfirm: 'این کد تخفیف حذف شود؟',
code: 'کد',
codePlaceholder: 'NOWRUZ1403',
name: 'عنوان',
namePlaceholder: 'تخفیف نوروزی',
description: 'توضیحات',
percentOff: 'درصد تخفیف',
scope: 'سرویس‌های مشمول',
allServices: 'همهٔ سرویس‌ها',
selectServices: 'انتخاب سرویس‌های خاص',
audience: 'کاربران مشمول',
public: 'عمومی (همهٔ کاربران)',
restricted: 'کاربران مشخص',
searchUsers: 'جست‌وجوی کاربر بر اساس نام یا ایمیل…',
noUsersSelected: 'هنوز کاربری انتخاب نشده',
limits: 'محدودیت‌ها',
maxUses: 'سقف کل استفاده',
maxUsesPerUser: 'سقف هر کاربر',
unlimited: 'نامحدود',
startsAt: 'تاریخ شروع',
endsAt: 'تاریخ پایان',
active: 'فعال',
inactive: 'غیرفعال',
used: 'استفاده‌شده',
save: 'ذخیره',
saving: 'در حال ذخیره…',
cancel: 'انصراف',
saved: 'کد تخفیف ذخیره شد',
deleted: 'کد تخفیف حذف شد',
saveFailed: 'ذخیرهٔ کد تخفیف ناموفق بود',
groups: {
runtime: 'رانتایم اپلیکیشن',
optional: 'سرویس‌های جانبی',
managed: 'سرویس‌های مدیریت‌شده',
addon: 'افزونه‌ها',
},
coupon: {
label: 'کد تخفیف',
placeholder: 'کد تخفیف را وارد کنید',
apply: 'اعمال',
checking: 'در حال بررسی…',
applied: 'کد تخفیف اعمال شد',
remove: 'حذف کد',
discountLine: 'تخفیف',
},
reasons: {
not_found: 'کد تخفیف نامعتبر است',
inactive: 'این کد تخفیف غیرفعال است',
not_started: 'این کد هنوز فعال نشده است',
expired: 'این کد منقضی شده است',
max_uses_reached: 'ظرفیت استفاده از این کد تمام شده است',
max_uses_per_user_reached: 'سقف استفادهٔ شما از این کد پر شده است',
not_eligible_user: 'این کد برای حساب شما قابل استفاده نیست',
no_eligible_services: 'این کد روی سرویس‌های انتخابی شما تخفیف ندارد',
},
},
},
servicesNew: {
steps: ['نوع سرویس', 'پیکربندی', 'بررسی و پرداخت'],