All files / approval-engine-2/mappers source-constraint.mapper.ts

60% Statements 6/10
50% Branches 4/8
100% Functions 2/2
60% Lines 6/10

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                2x                 2x                 2x                 2x             2x             2x                
import { SourceConstraint } from "../../tr8-script/domain/ast.js";
import { isRelativeValue } from "../../tr8-script/domain/value.js";
import { SourceConstraintDto } from "../dtos/ledger-event.dto.js";
import { AbsoluteFixedValueMapper } from "./absolute-fixed-value.mapper.js";
import { RelativeValueMapper } from "./relative-value.mapper.js";
 
class SourceConstraintMapper {
  static toDTO(domain: SourceConstraint): SourceConstraintDto {
    Iif (domain.amount === "unbounded") {
      return {
        amount: {
          UnboundedValue: "unbounded",
        },
        constraint: domain.constraint,
      };
    }
 
    Iif (isRelativeValue(domain.amount)) {
      return {
        amount: {
          RelativeValue: RelativeValueMapper.toDTO(domain.amount),
        },
        constraint: domain.constraint,
      };
    }
 
    return {
      amount: {
        AbsoluteFixedValue: AbsoluteFixedValueMapper.toDTO(domain.amount),
      },
      constraint: domain.constraint,
    };
  }
 
  static fromDTO(dto: SourceConstraintDto): SourceConstraint {
    Iif ("UnboundedValue" in dto.amount) {
      return {
        amount: "unbounded",
        constraint: dto.constraint,
      };
    }
 
    Iif ("RelativeValue" in dto.amount) {
      return {
        amount: RelativeValueMapper.fromDTO(dto.amount.RelativeValue),
        constraint: dto.constraint,
      };
    }
 
    return {
      amount: AbsoluteFixedValueMapper.fromDTO(dto.amount.AbsoluteFixedValue),
      constraint: dto.constraint,
    };
  }
}
 
export { SourceConstraintMapper };