Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 3x 50x 1x 49x 1x 48x 2x 46x 1x 8x 11x 8x 3x 6x 2x 4x 2x 6x 1x 5x 2x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 1x | import { Promotion } from "./Promotion";
import {
PromotionType,
DistributionType,
ProductType,
ImageType,
ExhaustionAlarmPercentages,
YesNo,
FlexibleDaysType,
} from "../types";
import {
InvalidCouponQuantityException,
InsufficientBudgetException,
MinimumPaymentNotMetException,
} from "../exceptions/PromotionExceptions";
/**
* Abstract base class for all coupon types
* Extends Promotion and adds common coupon-specific functionality
*/
export abstract class Coupon extends Promotion {
protected couponDiscountPrice: number;
protected purchasedCouponQuantity: number;
protected usedCouponQuantity: number;
protected remainingCouponQuantity: number;
protected fullPaymentYn: YesNo;
protected fullPaymentMinPrice: number;
// Validity period management (common for all coupons)
protected validityPeriodType: FlexibleDaysType;
protected validityPeriodDays: number;
// Received/Downloaded coupon quantity tracking (common for all coupons)
protected receivedCouponQuantity: number;
constructor(params: {
id: string;
title: string;
startDate: Date;
endDate: Date;
promotionType: PromotionType;
distributionType: DistributionType;
productType: ProductType;
imageType: ImageType;
imageObsId: string;
imageObsHash: string;
imageUrl: string;
exhaustionAlarmYn: YesNo;
exhaustionAlarmPercentageList: ExhaustionAlarmPercentages[];
// Coupon-specific properties
couponDiscountPrice: number;
purchasedCouponQuantity: number;
usedCouponQuantity: number;
remainingCouponQuantity: number;
fullPaymentYn: YesNo;
fullPaymentMinPrice: number;
// Shared property for Downloadable/Reward Coupon
validityPeriodType: FlexibleDaysType;
validityPeriodDays: number;
receivedCouponQuantity: number;
}) {
super(params);
this.validateCouponQuantities(
params.purchasedCouponQuantity,
params.usedCouponQuantity,
params.remainingCouponQuantity
);
this.validateDiscountPrice(params.couponDiscountPrice);
this.validateMinimumPrice(params.fullPaymentMinPrice);
this.couponDiscountPrice = params.couponDiscountPrice;
this.purchasedCouponQuantity = params.purchasedCouponQuantity;
this.usedCouponQuantity = params.usedCouponQuantity;
this.remainingCouponQuantity = params.remainingCouponQuantity;
this.fullPaymentYn = params.fullPaymentYn;
this.fullPaymentMinPrice = params.fullPaymentMinPrice;
this.validityPeriodType = params.validityPeriodType;
this.validityPeriodDays = params.validityPeriodDays;
this.receivedCouponQuantity = params.receivedCouponQuantity ?? 0;
}
/**
* Validates that coupon quantities are consistent
*/
private validateCouponQuantities(
purchased: number,
used: number,
remaining: number
): void {
if (purchased < 0 || used < 0 || remaining < 0) {
throw new InvalidCouponQuantityException(
"Coupon quantities cannot be negative"
);
}
if (used > purchased) {
throw new InvalidCouponQuantityException(
`Used quantity (${used}) cannot exceed purchased quantity (${purchased})`
);
}
if (remaining > purchased) {
throw new InvalidCouponQuantityException(
`Remaining quantity (${remaining}) cannot exceed purchased quantity (${purchased})`
);
}
}
/**
* Validates that discount price is positive
*/
private validateDiscountPrice(price: number): void {
if (price <= 0) {
throw new InvalidCouponQuantityException(
"Discount price must be positive"
);
}
}
/**
* Validates that minimum price is non-negative
*/
private validateMinimumPrice(price: number): void {
if (price < 0) {
throw new InvalidCouponQuantityException(
"Minimum price cannot be negative"
);
}
}
/**
* Checks if there are coupons available
*/
public hasAvailableCoupons(): boolean {
return this.remainingCouponQuantity > 0;
}
/**
* Checks if the payment amount meets the minimum requirement
*/
public meetsMinimumPayment(paymentAmount: number): boolean {
if (this.fullPaymentYn === "Y") {
return paymentAmount >= this.fullPaymentMinPrice;
}
return true;
}
/**
* Validates that a coupon can be used with the given payment amount
*/
protected validateCouponUsage(paymentAmount: number): void {
if (!this.hasAvailableCoupons()) {
throw new InsufficientBudgetException("No coupons available");
}
if (!this.meetsMinimumPayment(paymentAmount)) {
throw new MinimumPaymentNotMetException(
this.fullPaymentMinPrice,
paymentAmount
);
}
}
/**
* Calculates the discount amount for a given payment
*/
public calculateDiscount(paymentAmount: number): number {
if (!this.meetsMinimumPayment(paymentAmount)) {
return 0;
}
// Return the full discount amount if minimum is met
return Math.min(this.couponDiscountPrice, paymentAmount);
}
/**
* Calculates the usage percentage of coupons
*/
public calculateUsagePercentage(): number {
if (this.purchasedCouponQuantity === 0) {
return 0;
}
return (this.usedCouponQuantity / this.purchasedCouponQuantity) * 100;
}
/**
* Gets the final payment amount after discount
*/
public getFinalPaymentAmount(paymentAmount: number): number {
const discount = this.calculateDiscount(paymentAmount);
return Math.max(0, paymentAmount - discount);
}
// Getters
public getCouponDiscountPrice(): number {
return this.couponDiscountPrice;
}
public getPurchasedCouponQuantity(): number {
return this.purchasedCouponQuantity;
}
public getUsedCouponQuantity(): number {
return this.usedCouponQuantity;
}
public getRemainingCouponQuantity(): number {
return this.remainingCouponQuantity;
}
public getFullPaymentYn(): YesNo {
return this.fullPaymentYn;
}
public getFullPaymentMinPrice(): number {
return this.fullPaymentMinPrice;
}
public getValidityPeriodType(): FlexibleDaysType {
return this.validityPeriodType;
}
public getValidityPeriodDays(): number {
return this.validityPeriodDays;
}
public getReceivedCouponQuantity(): number {
return this.receivedCouponQuantity;
}
/**
* Increments the received coupon quantity
* Used when a coupon is issued/downloaded
*/
protected incrementReceivedCouponQuantity(): void {
this.receivedCouponQuantity++;
}
/**
* Abstract method to calculate coupon expiration date
* Each subclass implements its own business logic for expiration calculation
* @param referenceDate - The date to calculate expiration from (e.g., issue date, download date)
* @returns The calculated expiration date
*/
public abstract calculateCouponExpirationDate(referenceDate: Date): Date;
}
|