All files / marketing-platform/application/useCases GetPromotionApplicationDetailsUseCase.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 4/4
100% Lines 9/9

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                      18x                   25x 25x                 25x 8x                           5x   4x 2x         2x      
import {
  IPromotionApplicationRepository,
  PromotionApplication,
} from "../../domain";
 
/**
 * Use case for retrieving a single promotion application by ID
 * This follows the command/query separation principle - it's a pure query operation
 */
export class GetPromotionApplicationDetailsUseCase {
  constructor(
    private readonly promotionApplicationRepository: IPromotionApplicationRepository
  ) {}
 
  /**
   * Executes the use case to fetch a promotion application by its application sequence number
   * @param applySeq The application sequence number (unique identifier)
   * @returns Promise resolving to the promotion application or null if not found
   * @throws Error if applySeq is invalid
   */
  async execute(applySeq: number): Promise<PromotionApplication | null> {
    this.validateApplySeq(applySeq);
    return await this.promotionApplicationRepository.findById(applySeq);
  }
 
  /**
   * Validates the application sequence number
   * @param applySeq The application sequence number to validate
   * @throws Error if applySeq is invalid
   */
  private validateApplySeq(applySeq: number): void {
    if (!Number.isInteger(applySeq) || applySeq <= 0) {
      throw new Error(
        "Invalid application sequence number. Must be a positive integer."
      );
    }
  }
 
  /**
   * Executes the use case and throws an error if not found
   * Useful when you expect the promotion application to exist
   * @param applySeq The application sequence number
   * @returns Promise resolving to the promotion application
   * @throws Error if the promotion application is not found
   */
  async executeOrThrow(applySeq: number): Promise<PromotionApplication> {
    const application = await this.execute(applySeq);
 
    if (!application) {
      throw new Error(
        `Promotion application with ID ${applySeq} not found`
      );
    }
 
    return application;
  }
}