All files / libs/payout-calculation/payments/core/src/lib/paymentFinder paymentsFinder.ts

97.93% Statements 95/97
75% Branches 3/4
100% Functions 2/2
97.93% Lines 95/97

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 981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x     20x 1x  
import { type Repository, type SelectQueryBuilder } from 'typeorm';
 
import { type Payment } from '@amalia/core/models';
import { type CommissionReportFilters } from '@amalia/core/types';
import { PlanIsHiddenQueryChoices } from '@amalia/payout-definition/plans/types';
 
import { FinderQueryBuilder } from './finderQueryBuilder';
 
const MIN_AMOUNT = 0.01;
 
export class PaymentsFinder {
  public static createQueryBuilder(
    paymentRepository: Repository<Payment>,
    companyId: string,
    filters: CommissionReportFilters = {},
    search?: string,
  ): SelectQueryBuilder<Payment> {
    const queryBuilder = paymentRepository.createQueryBuilder('payment');
    queryBuilder.leftJoinAndSelect('payment.paymentPeriod', 'paymentPeriod');
    queryBuilder.leftJoinAndSelect('payment.rule', 'rule');
    queryBuilder.leftJoinAndSelect('rule.commissionVariable', 'commissionVariable');
    queryBuilder.leftJoinAndSelect('commissionVariable.object', 'definition');
    queryBuilder.leftJoinAndSelect('payment.plan', 'plan');
    queryBuilder.leftJoinAndSelect('payment.user', 'user');
    queryBuilder.leftJoinAndSelect('payment.period', 'period');
    queryBuilder.leftJoinAndSelect('payment.adjustment', 'adjustment');
    queryBuilder.leftJoinAndSelect('adjustment.rowDefinition', 'rowDefinition');
 
    queryBuilder.where('payment."companyId" = :companyId', { companyId });
 
    // All payments which amount is -MIN_AMOUNT < amount < MIN_AMOUNT are considered as 0.
    queryBuilder.andWhere('payment."value" NOT BETWEEN :minThreshold AND :maxThreshold', {
      minThreshold: -MIN_AMOUNT,
      maxThreshold: MIN_AMOUNT,
    });
 
    // apply filters
    PaymentsFinder.applyFilterOnQuery(queryBuilder, filters);
 
    // apply searching
    if (search) {
      queryBuilder.andWhere(
        `
     (user.firstName ilike :search
     OR user.lastName ilike :search
     OR plan.name ilike :search
     OR rule.name ilike :search
     OR "adjustment".name ilike :search
     OR "paymentPeriod".name ilike :search
     OR "commissionVariable".name ilike :search
     OR period.name ilike :search
     OR "dealName" ilike :search
     OR "definition".name ilike :search)`,
        { search: `%${search}%` },
      );
    }
 
    return queryBuilder;
  }
 
  /**
   * Apply payment filters on given query
   *
   * @param queryBuilder
   * @param filter
   */
  public static applyFilterOnQuery(
    queryBuilder: SelectQueryBuilder<Payment>,
    {
      userIds,
      periodIds,
      planIds,
      ruleIds,
      paymentPeriodIds,
      planHiddenStatus,
      onlyPaymentsInError,
    }: CommissionReportFilters,
  ) {
    const finderQueryBuilder = new FinderQueryBuilder(queryBuilder);
 
    finderQueryBuilder.applyInCondition('userId', userIds);
    finderQueryBuilder.applyInCondition('periodId', periodIds);
    finderQueryBuilder.applyInCondition('planId', planIds);
    finderQueryBuilder.applyInCondition('ruleId', ruleIds);
    finderQueryBuilder.applyInCondition('paymentPeriodId', paymentPeriodIds);
 
    if (planHiddenStatus !== PlanIsHiddenQueryChoices.BOTH) {
      queryBuilder.andWhere('plan.isHidden = :isHidden', {
        isHidden: planHiddenStatus === PlanIsHiddenQueryChoices.HIDDEN,
      });
    }
 
    if (onlyPaymentsInError) {
      queryBuilder.andWhere('payment.error IS NOT NULL');
    }
  }
}