import * as gracely from "gracely" import * as isoly from "isoly" import * as selectively from "selectively" import { Account as ReconciliationAccount } from "../Account" import { Fee } from "../Fee" import { Transaction as ReconciliationTransaction } from "./Transaction" export interface Reconciliation { account: ReconciliationAccount | { [currency in isoly.Currency | "default"]?: ReconciliationAccount } costPlus?: true fees: Fee //Intergiro Fee reserves?: { percentage: number days?: number } // payoutDelay in days payoutDelay?: number // Actual scheme/network fee network?: Fee.Network payout?: { currency: isoly.Currency foreignExchangeFee: number // In percentage } } export namespace Reconciliation { export function is(value: any | Reconciliation): value is Reconciliation { return ( typeof value == "object" && (ReconciliationAccount.is(value.account) || (typeof value.account == "object" && Object.entries(value.account).every( ([currency, account]) => (isoly.Currency.is(currency) || currency == "default") && ReconciliationAccount.is(account) ))) && (value.costPlus == undefined || value.costPlus == true) && Fee.is(value.fees) && (value.reserves == undefined || (typeof value.reserves == "object" && typeof value.reserves.percentage == "number" && (value.reserves.days == undefined || typeof value.reserves.days == "number"))) && (value.payoutDelay == undefined || (typeof value.payoutDelay == "number" && Number.isInteger(value.payoutDelay) && value.payoutDelay >= 0)) && (value.payout == undefined || (value.payout && typeof value.payout == "object" && isoly.Currency.is(value.payout.currency) && typeof value.payout.foreignExchangeFee == "number")) ) } export function flaw(value: any | Reconciliation): gracely.Flaw { return { type: "Merchant.Reconciliation", flaws: typeof value != "object" ? undefined : ([ ReconciliationAccount.is(value.account) || (typeof value.account == "object" && Object.entries(value.account).every( ([currency, account]) => (isoly.Currency.is(currency) || currency == "default") && ReconciliationAccount.is(account) )) || { property: "account", type: 'Merchant.Account | { [currency in isoly.Currency | "default"]?: Merchant.Account }', }, value.costPlus == undefined || value.costPlus == true || { property: "costPlus", type: "undefined | true" }, Fee.is(value.fees) || { property: "fees", type: "Merchant.Fee" }, typeof value.reserves == "object" || { property: "reserves", type: "object" }, typeof value.reserves.percentage == "number" || { property: "reserves.percentage", type: "number" }, value.reserves.days == undefined || typeof value.reserves.days == "number" || { property: "reserves.days", type: "number | undefined" }, value.payoutDelay == undefined || (typeof value.payoutDelay == "number" && Number.isInteger(value.payoutDelay) && value.payoutDelay >= 0) || { property: "payoutDelay", type: "positive integer | undefined" }, value.payout == undefined || (value.payout && typeof value.payout == "object" && isoly.Currency.is(value.payout.currency) && typeof value.payout.foreignExchangeFee == "number") || { property: "payout", type: "{ currency: isoly.Currency; foreignExchangeFee: number } | undefined", }, ].filter(gracely.Flaw.is) as gracely.Flaw[]), } } export const template = new selectively.Type.Object({ account: new selectively.Type.Union([ ReconciliationAccount.template, new selectively.Type.Object( Object.fromEntries( [...isoly.Currency.types, "default"].map(currency => [currency, ReconciliationAccount.template]) ) ), ]), costPlus: new selectively.Type.Boolean(), payoutDelay: new selectively.Type.Number(), }) export type Transaction = ReconciliationTransaction export namespace Transaction { export const is = ReconciliationTransaction.is export const toCsv = ReconciliationTransaction.toCsv } export type Account = ReconciliationAccount export namespace Account { export const is = ReconciliationAccount.is } }