All files / libs/payout-calculation/statements/core/src/modules/userStatements userStatements.controller.ts

100% Statements 119/119
64% Branches 16/25
100% Functions 3/3
100% Lines 119/119

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 119 1201x 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 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 9x 1x 1x 8x 8x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x  
import { Controller, ForbiddenException, Get, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company as CompanyEntity, Plan, Team, User } from '@amalia/core/models';
import {
  CalculationType,
  StatementStatusType,
  type UserStatementsFacets,
  type UserStatementsPage,
} from '@amalia/core/types';
import { type PaginatedQuery } from '@amalia/core-http-types';
import { BooleanPipe, EnumPipe, Pagination, UuidPipe } from '@amalia/kernel/api';
import {
  AmaliaAuthGuard,
  CheckPolicies,
  CurrentAuthenticatedContext,
  CurrentUserCompany,
  PoliciesGuard,
} from '@amalia/kernel/auth/core';
import { canViewHiddenPlans, canViewStatements, defineAbilityFor } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { PlanIsHiddenQueryChoices } from '@amalia/payout-definition/plans/types';
 
import { GetUserStatementsFacetsUseCase } from './use-cases/get-user-statements-facets.use-case';
import { ListUserStatementsUseCase } from './use-cases/list-user-statements.use-case';
 
/**
 * Provides UserStatements, which is basically a "Statements grouped by user" aggregation.
 *
 * This is needed for the statement list view, because we're showing a list of users, but
 * a user can have multiple statements if he's assigned to multiple plans for that period.
 *
 * This also provides the endpoint for the "facets", which is the different counts displayed
 * on top of the statement list.
 */
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('statements')
@Controller('userStatements')
export class UserStatementsController {
  public constructor(
    private readonly listUserStatementsUseCase: ListUserStatementsUseCase,
    private readonly getUserStatementsFacetsUseCase: GetUserStatementsFacetsUseCase,
  ) {}
 
  @Get()
  @CheckPolicies((ability) => canViewStatements(ability))
  public async list(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Query('periodId', new UuidPipe({ optional: false })) periodId: string,
    @Pagination() pagination: PaginatedQuery,
    @Query('planIds', new UuidPipe({ optional: true, multiple: true })) planIds?: Plan['id'][],
    @Query('teamIds', new UuidPipe({ optional: true, multiple: true })) teamIds?: Team['id'][],
    @Query('userIds', new UuidPipe({ optional: true, multiple: true })) userIds?: User['id'][],
    @Query('statementStatus', new EnumPipe({ enum: StatementStatusType, optional: true }))
    statementStatus?: StatementStatusType,
    @Query('planHiddenStatus', new EnumPipe({ enum: PlanIsHiddenQueryChoices, optional: true }))
    planHiddenStatus?: PlanIsHiddenQueryChoices,
    @Query('isForecast', new BooleanPipe({ optional: true })) isForecast: boolean = false,
  ): Promise<UserStatementsPage> {
    if (
      planHiddenStatus &&
      planHiddenStatus !== PlanIsHiddenQueryChoices.LIVE &&
      !canViewHiddenPlans(defineAbilityFor(authenticatedContext))
    ) {
      throw new ForbiddenException('You cannot query hidden plans.');
    }
 
    return this.listUserStatementsUseCase.execute(
      authenticatedContext,
      isForecast ? CalculationType.FORECAST : CalculationType.STATEMENT,
      periodId,
      {
        userIds,
        teamIds,
        planIds,
        statementStatus,
        planHiddenStatus: planHiddenStatus || PlanIsHiddenQueryChoices.LIVE,
      },
      pagination,
    );
  }
 
  @Get('facets')
  @CheckPolicies((ability) => canViewStatements(ability))
  public async getFacets(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @CurrentUserCompany() company: CompanyEntity,
    @Query('periodId', new UuidPipe({ optional: true })) periodId: string,
    @Query('planIds', new UuidPipe({ optional: true, multiple: true })) planIds?: Plan['id'][],
    @Query('userIds', new UuidPipe({ optional: true, multiple: true })) userIds?: User['id'][],
    @Query('teamIds', new UuidPipe({ optional: true, multiple: true })) teamIds?: Team['id'][],
    @Query('isForecast', new BooleanPipe({ optional: true })) isForecast?: boolean,
    @Query('statementStatus', new EnumPipe({ enum: StatementStatusType, optional: true }))
    statementStatus?: StatementStatusType,
    @Query('planHiddenStatus', new EnumPipe({ enum: PlanIsHiddenQueryChoices, optional: true }))
    planHiddenStatus?: PlanIsHiddenQueryChoices,
  ): Promise<UserStatementsFacets> {
    if (
      planHiddenStatus &&
      planHiddenStatus !== PlanIsHiddenQueryChoices.LIVE &&
      !canViewHiddenPlans(defineAbilityFor(authenticatedContext))
    ) {
      throw new ForbiddenException('You cannot query hidden plans.');
    }
 
    return this.getUserStatementsFacetsUseCase.execute(
      company,
      authenticatedContext,
      periodId,
      planHiddenStatus,
      planIds,
      userIds,
      teamIds,
      statementStatus,
      isForecast,
    );
  }
}