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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, IsNull, Repository } from 'typeorm';
import { Payment, Rule, type Company } from '@amalia/core/models';
import { assert } from '@amalia/ext/typescript';
import { RuleType } from '@amalia/payout-definition/plans/types';
import { search } from '../common/search';
/**
* Service rule.
*/
@Injectable()
export class RulesService {
public constructor(
@InjectRepository(Rule)
private readonly ruleRepository: Repository<Rule>,
@InjectRepository(Payment)
private readonly paymentsRepository: Repository<Payment>,
) {}
/**
* Fin all rules attached to a company
* @param company company
*/
public async findAll(company: Company): Promise<Rule[]> {
return this.ruleRepository.find({
where: { company: { id: company.id } },
});
}
/**
* Find all rules attached to a company with a searchKey present in the formula
* @param company company
* @param searchKey searchKey
*/
public async findUsages(company: Company, searchKey: string): Promise<Rule[]> {
return this.ruleRepository.find(search(company, 'formula', searchKey));
}
/**
* Find all rules attached to a company that use a variable in their configuration
* @param company
* @param variableId
* @returns
*/
public async findViaVariableInConfiguration(company: Company, variableId: string): Promise<Rule[]> {
const queryBuilder = this.ruleRepository.createQueryBuilder();
queryBuilder.where('"companyId" = :companyId', { companyId: company.id });
queryBuilder.andWhere(
'"releaseConditionVariableId" = :variableId OR "commissionVariableId" = :variableId OR configuration::text ilike :variableSearchId',
{ variableSearchId: `%${variableId}%`, variableId },
);
return queryBuilder.getMany();
}
/**
* Find all rules according to it type.
*
* @param company company
* @param type according to RuleType Enum
*/
public async findByType(company: Company, type: RuleType): Promise<Rule[]> {
return this.ruleRepository.findBy({
company: { id: company.id },
type,
});
}
/**
* Find rule by id.
* @param company
* @param ruleId
* @param relations
* @throws {NotFoundException}
*/
public async findById(company: Pick<Company, 'id'>, ruleId: Rule['id'], relations: string[] = []): Promise<Rule> {
const rule = await this.ruleRepository.findOne({
where: { company: { id: company.id }, id: ruleId },
relations,
});
assert(rule, new NotFoundException(`Rule with id ${ruleId} was not found`));
return rule;
}
/**
* Find rules with an id list.
* @param company
* @param ids
*/
public async findByIds(company: Company, ids: string[]): Promise<Rule[]> {
return this.ruleRepository.find({
where: {
company: { id: company.id },
id: In(ids),
},
});
}
/**
* Find all payments non paid yet linked to a rule
* @param company
* @param ruleId
* @returns
*/
public async findPaymentsPaidByRuleId(company: Company, ruleId: string): Promise<Payment[]> {
return this.paymentsRepository.findBy({
company: { id: company.id },
ruleId,
paymentPeriodId: IsNull(),
});
}
}
|