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

80% Statements 24/30
82.35% Branches 14/17
100% Functions 1/1
80% Lines 24/30

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 311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 4x       4x 1x 1x 1x 4x 1x 4x 1x 4x       1x  
import { Injectable } from '@nestjs/common';
import { isEmpty, last } from 'lodash-es';
 
import { type Statement } from '@amalia/core/models';
import { EngineErrorLevel, StatementStatusType } from '@amalia/core/types';
import { isStatementUnderReview } from '@amalia/payout-collaboration/workflows/types';
 
@Injectable()
export class UserStatementsService {
  public filterByStatus(statements: Statement[], status: StatementStatusType): Statement[] {
    switch (status) {
      case StatementStatusType.REVIEWED:
        return statements.filter((s) => !s.error && s.workflowComplete);
      case StatementStatusType.UNREVIEWED:
        return statements.filter(
          (s) => !s.error && !s.workflowComplete && (isEmpty(s.workflowSteps) || last(s.workflowSteps)?.to === 0),
        );
      case StatementStatusType.UNDER_REVIEW:
        return statements.filter(
          (s) => !s.error && !s.workflowComplete && isStatementUnderReview(s.workflowSteps ?? []),
        );
      case StatementStatusType.ERROR:
        return statements.filter((s) => s.error && s.error.level === EngineErrorLevel.ERROR);
      case StatementStatusType.WARNING:
        return statements.filter((s) => s.error && s.error.level === EngineErrorLevel.WARNING);
    }

    throw new Error('Filtering operation not supported');
  }
}