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

0% Statements 0/24
0% Branches 0/28
0% Functions 0/4
0% Lines 0/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                                                                                                                                                             
import {
  isAbsoluteTx,
  isRelativeTx,
  LedgerEventDlq,
} from "../../tr8-script/domain/ledger-event.js";
import {
  isAbsoluteTxDto,
  isRelativeTxDto,
  LedgerEventDtoDlq,
} 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 LedgerEventMapperDlq {
  static toDTO(domain: LedgerEventDlq): LedgerEventDtoDlq {
    const firstTx = domain.txs[0];
    if (!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 {
      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);
        } else if (isRelativeTx(tx)) {
          return LedgerEventRelativeRefTxMapper.toDTO(tx);
        }
 
        throw new Error("LedgerEventMapper: unknown tx type");
      }),
      typeTxs,
    };
  }
 
  static fromDTO(dto: LedgerEventDtoDlq): LedgerEventDlq {
    const { partial_notif_id, ...rest } = dto;
    const result: LedgerEventDlq = {
      ...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);
        } else if (isRelativeTxDto(tx)) {
          return LedgerEventRelativeRefTxMapper.fromDTO(tx);
        }
 
        throw new Error("LedgerEventMapper: unknown tx type");
      }),
    };
 
    if (partial_notif_id !== null) {
      result.partial_notif_id = partial_notif_id;
    }
 
    return result;
  }
}
 
export { LedgerEventMapperDlq };