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

95.71% Statements 67/70
96.87% Branches 31/32
93.75% Functions 30/32
97.05% Lines 66/68

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                      4x                                                                                                     166x   166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x                   168x 3x                   3x       4x       15x       14x       3x       1x       1x       2x                       2x       3x       3x       3x                             1x 1x 1x 1x             4x 2x   2x 1x   1x             1x                   1x             1x                             3x 9x 9x 9x                       2x             1x 3x               5x             3x             3x               2x 1x                             4x 1x           3x 3x 3x 1x           2x   2x 2x               4x 2x 2x                          
import {
  PromotionType,
  DistributionType,
  ProductType,
  ImageType,
  ExhaustionAlarmPercentages,
  YesNo,
  ExposureProduct,
} from "../types";
import { InvalidPromotionDateException } from "../exceptions/PromotionExceptions";
 
const MAX_TITLE_LENGTH = 300;
 
/**
 * Abstract base class for all promotions
 * Contains common properties and behaviors shared across all promotion types
 * Private properties ensure even subclasses cannot access and modify them directly
 * Private properties can only be updated via methods defined in this class
 * to enfrce consistant validation and business rules
 */
export abstract class Promotion {
  protected readonly id: string;
 
  // Basic details
  protected title: string;
  private startDate: Date;
  private endDate: Date;
 
  // Prmotion Categories
  private readonly promotionType: PromotionType;
  private readonly distributionType: DistributionType;
  private readonly productType: ProductType;
 
  // Image management
  protected imageType: ImageType;
  protected imageObsId: string;
  protected imageObsHash: string;
  protected imageUrl: string;
 
  // Exhaustion alarm settings
  protected exhaustionAlarmYn: YesNo;
  protected exhaustionAlarmPercentageList: ExhaustionAlarmPercentages[];
 
  // Exposure products for marketing campaigns
  protected exposureProductList: ExposureProduct[];
 
  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[];
    exposureProductList?: ExposureProduct[];
  }) {
    this.validateDates(params.startDate, params.endDate);
 
    this.id = params.id;
    this.title = params.title;
    this.startDate = params.startDate;
    this.endDate = params.endDate;
    this.promotionType = params.promotionType;
    this.distributionType = params.distributionType;
    this.productType = params.productType;
    this.imageType = params.imageType;
    this.imageObsId = params.imageObsId;
    this.imageObsHash = params.imageObsHash;
    this.imageUrl = params.imageUrl;
    this.exhaustionAlarmYn = params.exhaustionAlarmYn;
    this.exhaustionAlarmPercentageList = params.exhaustionAlarmPercentageList;
    this.exposureProductList = params.exposureProductList ?? [];
  }
 
  // -------------------------------------------
  // VALIDATIONS
  // -------------------------------------------
  /**
   * Validates that start date is before end date
   */
  protected validateDates(startDate: Date, endDate: Date): void {
    if (startDate >= endDate) {
      throw new InvalidPromotionDateException(
        `Start date (${startDate.toISOString()}) must be before end date (${endDate.toISOString()})`
      );
    }
  }
 
  // -------------------------------------------
  // GETTERS
  // -------------------------------------------
  public getId(): string {
    return this.id;
  }
 
  public getTitle(): string {
    return this.title;
  }
 
  public getStartDate(): Date {
    return new Date(this.startDate);
  }
 
  public getEndDate(): Date {
    return new Date(this.endDate);
  }
 
  public getPromotionType(): PromotionType {
    return this.promotionType;
  }
 
  public getDistributionType(): DistributionType {
    return this.distributionType;
  }
 
  public getProductType(): ProductType {
    return this.productType;
  }
 
  public getImageType(): ImageType {
    return this.imageType;
  }
 
  public getImageObsId(): string {
    return this.imageObsId;
  }
 
  public getImageObsHash(): string {
    return this.imageObsHash;
  }
 
  public getImageUrl(): string {
    return this.imageUrl;
  }
 
  public getExhaustionAlarmYn(): YesNo {
    return this.exhaustionAlarmYn;
  }
 
  public getExhaustionAlarmPercentageList(): ExhaustionAlarmPercentages[] {
    return [...this.exhaustionAlarmPercentageList];
  }
 
  public getExposureProductList(): ExposureProduct[] {
    return [...this.exposureProductList];
  }
 
  // -------------------------------------------
  // SETTERS
  // -------------------------------------------
  /**
   * Updates the image information
   */
  public updateImage(
    imageType: ImageType,
    imageObsId: string,
    imageObsHash: string,
    imageUrl: string
  ): void {
    this.imageType = imageType;
    this.imageObsId = imageObsId;
    this.imageObsHash = imageObsHash;
    this.imageUrl = imageUrl;
  }
 
  /**
   * Updates the promotion title
   */
  public updateTitle(title: string): void {
    if (!title || title.trim().length === 0) {
      throw new Error("Title cannot be empty");
    }
    if (title.length > MAX_TITLE_LENGTH) {
      throw new Error("Title cannot exceed 300 characters.");
    }
    this.title = title;
  }
 
  /**
   * Sets the exhaustion alarm status
   */
  public setExhaustionAlarmYn(value: YesNo): void {
    this.exhaustionAlarmYn = value;
  }
 
  /**
   * Sets the exhaustion alarm percentage list
   */
  public setExhaustionAlarmPercentageList(
    list: ExhaustionAlarmPercentages[]
  ): void {
    // Optionally add validation logic here, e.g., for non-empty array or valid percentages
    this.exhaustionAlarmPercentageList = list;
  }
 
  /**
   * Sets the exposure product list
   */
  public setExposureProductList(list: ExposureProduct[]): void {
    this.exposureProductList = list;
  }
 
  
 
  // -------------------------------------------
  // ADDITIONAL BEHAVIORS
  // -------------------------------------------
 
  /**
   * Gets active exposure products for the promotion
   */
  public getActiveExposureProducts(
    currentDate: Date = new Date()
  ): ExposureProduct[] {
    return this.exposureProductList.filter((product) => {
      const productStartDate = new Date(product.startDate);
      const productEndDate = new Date(product.endDate);
      return (
        product.exposureStatus === "ON" &&
        currentDate >= productStartDate &&
        currentDate <= productEndDate
      );
    });
  }
 
  /**
   * Checks if promotion has any active exposure products
   */
  public hasActiveExposureProducts(currentDate: Date = new Date()): boolean {
    return this.getActiveExposureProducts(currentDate).length > 0;
  }
 
  /**
   * Gets exposure products by type
   */
  public getExposureProductsByType(exposureType: string): ExposureProduct[] {
    return this.exposureProductList.filter(
      (product) => product.exposureType === exposureType
    );
  }
 
  /**
   * Checks if the promotion is within its valid date range
   */
  public isWithinValidPeriod(currentDate: Date = new Date()): boolean {
    return currentDate >= this.startDate && currentDate <= this.endDate;
  }
 
  /**
   * Checks if the promotion has started
   */
  public hasStarted(currentDate: Date = new Date()): boolean {
    return currentDate >= this.startDate;
  }
 
  /**
   * Checks if the promotion has ended
   */
  public hasEnded(currentDate: Date = new Date()): boolean {
    return currentDate > this.endDate;
  }
 
  /**
   * Ensures that the promotion is currently active
   * @throws Error if the promotion is not active
   */
  protected ensureActive(currentDate: Date = new Date()): void {
    if (!this.isWithinValidPeriod(currentDate)) {
      throw new Error(
        `Promotion is not active. Valid period: ${this.startDate.toISOString()} - ${this.endDate.toISOString()}`
      );
    }
  }
 
  // -------------------------------------------
  // BUSINESS LOGIC
  // -------------------------------------------
  public reschedulePromotion(
    newStartDate: Date,
    newEndDate: Date,
    today: Date = new Date()
  ): void {
    // Business rule 1: newStartDate > today
    if (newStartDate <= today) {
      throw new InvalidPromotionDateException(
        "New start date must be in the future."
      );
    }
 
    // Business rule 2: newEndDate is not over 365 days from today
    const maxEndDate = new Date(today);
    maxEndDate.setDate(maxEndDate.getDate() + 365);
    if (newEndDate > maxEndDate) {
      throw new InvalidPromotionDateException(
        "New end date cannot be more than 365 days from today."
      );
    }
 
    // Also need to validate that newStartDate < newEndDate
    this.validateDates(newStartDate, newEndDate);
 
    this.startDate = newStartDate;
    this.endDate = newEndDate;
  }
 
  /**
   * Entity equality is based on identity, not attributes
   * Two promotions are equal if they have the same id
   */
  public equals(other: Promotion | null | undefined): boolean {
    if (!other) return false;
    Iif (this === other) return true;
    return this.id === other.getId();
  }
 
  // -------------------------------------------
  // ABSTRACT METHODS
  // -------------------------------------------
 
  /**
   * Abstract method to calculate usage percentage
   * Must be implemented by subclasses
   */
  public abstract calculateUsagePercentage(): number;
}