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
+50
View File
@@ -704,12 +704,49 @@ export interface CatalogOptionalServiceOption {
label: string;
}
export interface DiscountServiceOption {
value: string;
label: string;
group: string;
}
export interface PricingCatalog {
runtimes: Record<string, PricingRateRow[]>;
optionalServices: Record<string, OptionalServiceCatalogEntry>;
customDomain: CustomDomainCatalogRow;
runtimeOptions: CatalogRuntimeOption[];
optionalServiceOptions: CatalogOptionalServiceOption[];
discountServiceOptions: DiscountServiceOption[];
}
export interface Discount {
id: string;
code: string;
name: string;
description?: string;
percentOff: number;
services: string[];
isPublic: boolean;
allowedUserIds: string[];
maxUses: number | null;
maxUsesPerUser: number | null;
usedCount: number;
startsAt: string | null;
endsAt: string | null;
isActive: boolean;
createdAt: string;
updatedAt: string;
}
/** Result of POST /billing/discounts/validate */
export interface DiscountValidation {
valid: boolean;
reason?: string;
code?: string;
name?: string;
percentOff?: number;
eligibleAmount?: number;
discountAmount?: number;
}
export interface WalletBalance {
@@ -753,6 +790,8 @@ export interface Invoice {
status: InvoiceStatus;
paymentMethod?: PaymentMethod;
subtotal: number;
discountAmount?: number;
discountCode?: string;
total: number;
paidAmount: number;
dueAmount: number;
@@ -794,6 +833,15 @@ export interface DeployExtraChargeLine {
fullPeriodAmount?: number;
}
export interface CouponDiscountPreview {
valid: boolean;
reason?: string;
code?: string;
name?: string;
percentOff?: number;
discountAmount?: number;
}
export interface DeployCostPreview extends CostBreakdown {
cycle: BillingCycle;
fullAmount: number;
@@ -805,6 +853,8 @@ export interface DeployCostPreview extends CostBreakdown {
prepaidCreditUsed: boolean;
prorateRemainingDays?: number;
proratePeriodDays?: number;
couponDiscount?: CouponDiscountPreview | null;
amountDueAfterDiscount?: number;
}
// ─── Snapshot / Rollback types ──────────────────────