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

100% Statements 11/11
100% Branches 1/1
100% Functions 8/8
100% Lines 11/11

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                      18x               6x                     5x 5x 14x                       6x 6x 24x                   5x 5x 20x        
import {
  IPromotionApplicationRepository,
  PromotionApplication,
} from "../../domain";
 
/**
 * Use case for retrieving all promotion applications
 * This follows the command/query separation principle - it's a pure query operation
 */
export class GetAllPromotionsUseCase {
  constructor(
    private readonly promotionApplicationRepository: IPromotionApplicationRepository
  ) {}
 
  /**
   * Executes the use case to fetch all promotion applications
   * @returns Promise resolving to an array of all promotion applications
   */
  async execute(): Promise<PromotionApplication[]> {
    return await this.promotionApplicationRepository.findAll();
  }
 
  /**
   * Gets only active promotion applications
   * @param currentDate Optional date to check against (defaults to now)
   * @returns Promise resolving to an array of active promotion applications
   */
  async getActivePromotions(
    currentDate: Date = new Date()
  ): Promise<PromotionApplication[]> {
    const allPromotions = await this.promotionApplicationRepository.findAll();
    return allPromotions.filter((promotion) =>
      promotion.isActive(currentDate)
    );
  }
 
  /**
   * Gets promotion applications by status
   * @param status The application status to filter by
   * @returns Promise resolving to an array of promotion applications with the specified status
   */
  async getByStatus(
    status: "APPLYING" | "IN_SERVICE" | "COMPLETED" | "CANCELLED"
  ): Promise<PromotionApplication[]> {
    const allPromotions = await this.promotionApplicationRepository.findAll();
    return allPromotions.filter(
      (promotion) => promotion.getApplicationStatus() === status
    );
  }
 
  /**
   * Gets promotion applications for a specific merchant
   * @param merchantId The merchant ID to filter by
   * @returns Promise resolving to an array of promotion applications for the merchant
   */
  async getByMerchant(merchantId: string): Promise<PromotionApplication[]> {
    const allPromotions = await this.promotionApplicationRepository.findAll();
    return allPromotions.filter(
      (promotion) => promotion.getMerchantId() === merchantId
    );
  }
}