All files / marketing-platform/domain/entities PointPromotion.ts

90% Statements 72/80
94.33% Branches 50/53
77.77% Functions 21/27
90% Lines 72/80

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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334                                                                                                                                                173x         173x       173x   173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x                     173x 170x 2x                 171x 2x                   143x     143x             5x             23x 22x   1x             14x 2x     12x   12x       10x 2x       2x       12x     12x   12x             4x   4x 1x           3x   3x 1x     2x             2x 2x 2x 2x   2x             1x                   5x 5x                         6x 1x     5x       2x     3x       2x     1x         1x       3x       1x       1x                       1x       1x                                       3x       3x       3x       3x      
import { Promotion } from "./Promotion";
import {
  PromotionType,
  DistributionType,
  ProductType,
  ImageType,
  ExhaustionAlarmPercentages,
  YesNo,
  PromotionSavingType,
  ClientLimitType,
} from "../types";
import {
  InvalidPercentageException,
  InsufficientBudgetException,
  MinimumPaymentNotMetException,
  InvalidPointCalculationException,
} from "../exceptions/PromotionExceptions";
 
/**
 * Point Promotion entity
 * Handles percentage-based point rewards for purchases
 */
export class PointPromotion extends Promotion {
  private promotionName: string;
  private promotionBudget: number;
  private promotionSavingType: PromotionSavingType;
  private promotionSavingRate: number | null;
  private promotionSavingPoint: number | null;
  private minimumPaymentPriceYn: YesNo;
  private minimumPaymentPrice: number;
  private maximumSavingPoint: number;
  private clientLimitType: ClientLimitType;
  private clientLimitTerm: number | null;
  private clientLimitCount: number | null;
  private clientLimitPoint: number | null;
  private usedPoint: number;
  private usedPointPercentage: number;
  private remainingPoint: number;
  private remainingPointPercentage: 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[];
    // Point Promotion specific
    promotionName: string;
    promotionBudget: number;
    promotionSavingType: PromotionSavingType;
    promotionSavingRate: number | null;
    promotionSavingPoint: number | null;
    minimumPaymentPriceYn: YesNo;
    minimumPaymentPrice: number;
    maximumSavingPoint: number;
    clientLimitType: ClientLimitType;
    clientLimitTerm: number | null;
    clientLimitCount: number | null;
    clientLimitPoint: number | null;
    usedPoint?: number;
    usedPointPercentage?: number;
    remainingPoint?: number;
    remainingPointPercentage?: number;
  }) {
    super({
      ...params,
      distributionType: "NA",
    });
 
    this.validateSavingRate(
      params.promotionSavingRate,
      params.promotionSavingType
    );
    this.validateBudget(params.promotionBudget);
 
    this.promotionName = params.promotionName;
    this.promotionBudget = params.promotionBudget;
    this.promotionSavingType = params.promotionSavingType;
    this.promotionSavingRate = params.promotionSavingRate;
    this.promotionSavingPoint = params.promotionSavingPoint;
    this.minimumPaymentPriceYn = params.minimumPaymentPriceYn;
    this.minimumPaymentPrice = params.minimumPaymentPrice;
    this.maximumSavingPoint = params.maximumSavingPoint;
    this.clientLimitType = params.clientLimitType;
    this.clientLimitTerm = params.clientLimitTerm;
    this.clientLimitCount = params.clientLimitCount;
    this.clientLimitPoint = params.clientLimitPoint;
    this.usedPoint = params.usedPoint ?? 0;
    this.usedPointPercentage = params.usedPointPercentage ?? 0;
    this.remainingPoint = params.remainingPoint ?? params.promotionBudget;
    this.remainingPointPercentage =
      params.remainingPointPercentage ?? this.calculateRemainingPercentage();
  }
 
  /**
   * Validates the saving rate for fixed rate promotions
   */
  private validateSavingRate(
    rate: number | null,
    type: PromotionSavingType
  ): void {
    if (type === "FIXED_RATE" && rate !== null) {
      if (rate < 0 || rate > 100) {
        throw new InvalidPercentageException(rate);
      }
    }
  }
 
  /**
   * Validates the promotion budget
   */
  private validateBudget(budget: number): void {
    if (budget <= 0) {
      throw new InvalidPointCalculationException(
        "Promotion budget must be positive"
      );
    }
  }
 
  /**
   * Calculates remaining point percentage
   */
  private calculateRemainingPercentage(): number {
    Iif (this.promotionBudget === 0) {
      return 0;
    }
    return (this.remainingPoint / this.promotionBudget) * 100;
  }
 
  /**
   * Checks if there are sufficient points remaining
   */
  public hasSufficientPoints(requiredPoints: number): boolean {
    return this.remainingPoint >= requiredPoints;
  }
 
  /**
   * Checks if the payment amount meets the minimum requirement
   */
  public meetsMinimumPayment(paymentAmount: number): boolean {
    if (this.minimumPaymentPriceYn === "Y") {
      return paymentAmount >= this.minimumPaymentPrice;
    }
    return true;
  }
 
  /**
   * Calculates point reward based on payment amount
   */
  public calculatePointReward(paymentAmount: number): number {
    if (!this.meetsMinimumPayment(paymentAmount)) {
      return 0;
    }
 
    let calculatedPoints = 0;
 
    if (
      this.promotionSavingType === "FIXED_RATE" &&
      this.promotionSavingRate !== null
    ) {
      calculatedPoints = (paymentAmount * this.promotionSavingRate) / 100;
    E} else if (
      this.promotionSavingType === "FIXED_POINT" &&
      this.promotionSavingPoint !== null
    ) {
      calculatedPoints = this.promotionSavingPoint;
    }
 
    // Apply maximum saving point cap
    calculatedPoints = Math.min(calculatedPoints, this.maximumSavingPoint);
 
    // Ensure we don't exceed remaining budget
    calculatedPoints = Math.min(calculatedPoints, this.remainingPoint);
 
    return calculatedPoints;
  }
 
  /**
   * Applies the point reward for a payment
   */
  public applyPointReward(paymentAmount: number): number {
    this.ensureActive();
 
    if (!this.meetsMinimumPayment(paymentAmount)) {
      throw new MinimumPaymentNotMetException(
        this.minimumPaymentPrice,
        paymentAmount
      );
    }
 
    const pointReward = this.calculatePointReward(paymentAmount);
 
    if (pointReward === 0) {
      throw new InsufficientBudgetException("No points available for reward");
    }
 
    Iif (!this.hasSufficientPoints(pointReward)) {
      throw new InsufficientBudgetException(
        `Insufficient points. Required: ${pointReward}, Available: ${this.remainingPoint}`
      );
    }
 
    // Update point tracking
    this.usedPoint += pointReward;
    this.remainingPoint -= pointReward;
    this.usedPointPercentage = (this.usedPoint / this.promotionBudget) * 100;
    this.remainingPointPercentage = this.calculateRemainingPercentage();
 
    return pointReward;
  }
 
  /**
   * Calculates the usage percentage of the promotion budget
   */
  public calculateUsagePercentage(): number {
    return this.usedPointPercentage;
  }
 
  /**
   * Checks if the promotion can be applied to a payment
   */
  public canApply(
    paymentAmount: number,
    currentDate: Date = new Date()
  ): boolean {
    const pointReward = this.calculatePointReward(paymentAmount);
    return (
      this.isWithinValidPeriod(currentDate) &&
      this.meetsMinimumPayment(paymentAmount) &&
      pointReward > 0 &&
      this.hasSufficientPoints(pointReward)
    );
  }
 
  /**
   * Checks if client limit allows another use
   * Note: This is a simplified version. In production, you'd need to track per-user usage
   */
  public canUserApply(userUsageCount: number, userUsedPoints: number): boolean {
    if (this.clientLimitType === "NONE") {
      return true;
    }
 
    if (
      this.clientLimitCount !== null &&
      userUsageCount >= this.clientLimitCount
    ) {
      return false;
    }
 
    if (
      this.clientLimitPoint !== null &&
      userUsedPoints >= this.clientLimitPoint
    ) {
      return false;
    }
 
    return true;
  }
 
  // Getters
  public getPromotionName(): string {
    return this.promotionName;
  }
 
  public getPromotionBudget(): number {
    return this.promotionBudget;
  }
 
  public getPromotionSavingType(): PromotionSavingType {
    return this.promotionSavingType;
  }
 
  public getPromotionSavingRate(): number | null {
    return this.promotionSavingRate;
  }
 
  public getPromotionSavingPoint(): number | null {
    return this.promotionSavingPoint;
  }
 
  public getMinimumPaymentPriceYn(): YesNo {
    return this.minimumPaymentPriceYn;
  }
 
  public getMinimumPaymentPrice(): number {
    return this.minimumPaymentPrice;
  }
 
  public getMaximumSavingPoint(): number {
    return this.maximumSavingPoint;
  }
 
  public getClientLimitType(): ClientLimitType {
    return this.clientLimitType;
  }
 
  public getClientLimitTerm(): number | null {
    return this.clientLimitTerm;
  }
 
  public getClientLimitCount(): number | null {
    return this.clientLimitCount;
  }
 
  public getClientLimitPoint(): number | null {
    return this.clientLimitPoint;
  }
 
  public getUsedPoint(): number {
    return this.usedPoint;
  }
 
  public getRemainingPoint(): number {
    return this.remainingPoint;
  }
 
  public getUsedPointPercentage(): number {
    return this.usedPointPercentage;
  }
 
  public getRemainingPointPercentage(): number {
    return this.remainingPointPercentage;
  }
}