All files / libs/payout-calculation/badge-awards/core/src/lib badgeAwards.service.ts

100% Statements 38/38
33.33% Branches 3/9
100% Functions 2/2
100% Lines 38/38

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 391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository, type FindOptionsWhere } from 'typeorm';
 
import { BadgeAward } from '@amalia/core/models';
import { canViewThisStatement, defineAbilityFor } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { type Company } from '@amalia/tenants/companies/types';
 
@Injectable()
export class BadgeAwardsService {
  public constructor(
    @InjectRepository(BadgeAward)
    private readonly badgeAwardRepository: Repository<BadgeAward>,
  ) {}
 
  public async getForConditions(
    company: Company,
    authenticatedContext: AuthenticatedContext,
    planIds: string[] | null,
    periodId: string | null,
    userIds: string[] | null,
  ): Promise<BadgeAward[]> {
    const findConditions: FindOptionsWhere<BadgeAward> = {
      company: { id: company.id },
      ...(planIds?.length && { configuration: { planId: In(planIds) } }),
      ...(periodId && { periodId }),
      ...(userIds?.length && { userId: In(userIds) }),
    };
 
    const awards = await this.badgeAwardRepository.find({
      relations: ['configuration', 'statement', 'statement.period', 'statement.plan'],
      where: findConditions,
    });
 
    return awards.filter(({ statement }) => canViewThisStatement(defineAbilityFor(authenticatedContext), statement));
  }
}