All files / approval-engine-2/mappers ledger-event.mapper.ts

79.16% Statements 19/24
46.42% Branches 13/28
100% Functions 4/4
79.16% Lines 19/24

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                              2x 2x           2x 1x 1x 1x         2x         2x 1x 1x 1x                   2x 2x                   2x 1x 1x 1x             2x       2x          
import {
  isAbsoluteTx,
  isRelativeTx,
  LedgerEvent,
} from "../../tr8-script/domain/ledger-event.js";
import {
  isAbsoluteTxDto,
  isRelativeTxDto,
  LedgerEventDto,
} from "../dtos/ledger-event.dto.js";
import { LedgerEventAbsoluteRefTxMapper } from "./ledger-event-absolute-ref-tx.mapper.js";
import { LedgerEventRelativeRefTxMapper } from "./ledger-event-relative-ref-tx.mapper.js";
 
class LedgerEventMapper {
  static toDTO(domain: LedgerEvent): LedgerEventDto {
    const firstTx = domain.txs[0];
    Iif (!firstTx?.refValue?.type) {
      throw new Error("LedgerEventMapper: refValue is not defined");
    }
 
    let typeTxs: "abs" | "rel";
 
    if (isAbsoluteTx(firstTx)) {
      typeTxs = "abs";
    } else if (isRelativeTx(firstTx)) {
      typeTxs = "rel";
    } else E{
      throw new Error("LedgerEventMapper: unknown tx type");
    }
 
    return {
      ...domain,
      partial_notif_id:
        domain.partial_notif_id === undefined ? null : domain.partial_notif_id,
      txs: domain.txs.map((tx) => {
        if (isAbsoluteTx(tx)) {
          return LedgerEventAbsoluteRefTxMapper.toDTO(tx);
        E} else if (isRelativeTx(tx)) {
          return LedgerEventRelativeRefTxMapper.toDTO(tx);
        }
 
        throw new Error("LedgerEventMapper: unknown tx type");
      }),
      typeTxs,
    };
  }
 
  static fromDTO(dto: LedgerEventDto): LedgerEvent {
    const { partial_notif_id, ...rest } = dto;
    const result: LedgerEvent = {
      ...rest,
      typeTxs:
        dto.typeTxs ??
        (dto.txs[0] && isAbsoluteTxDto(dto.txs[0])
          ? "abs"
          : dto.txs[0] && isRelativeTxDto(dto.txs[0])
          ? "rel"
          : undefined),
      txs: dto.txs.map((tx) => {
        if (isAbsoluteTxDto(tx)) {
          return LedgerEventAbsoluteRefTxMapper.fromDTO(tx);
        E} else if (isRelativeTxDto(tx)) {
          return LedgerEventRelativeRefTxMapper.fromDTO(tx);
        }
 
        throw new Error("LedgerEventMapper: unknown tx type");
      }),
    };
 
    Iif (partial_notif_id !== null) {
      result.partial_notif_id = partial_notif_id;
    }
 
    return result;
  }
}
 
export { LedgerEventMapper };