@pax2pay/model-banking 0.1.491 → 0.1.493

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/Backup/Account.ts +24 -0
  2. package/Backup/Base.ts +32 -0
  3. package/Backup/Card.ts +25 -0
  4. package/Backup/Operation.ts +25 -0
  5. package/Backup/Organization.ts +26 -0
  6. package/Backup/Rule.ts +25 -0
  7. package/Backup/Transaction.ts +28 -0
  8. package/Backup/User.ts +25 -0
  9. package/Backup/index.ts +30 -0
  10. package/Card/Meta.ts +1 -2
  11. package/Operation/Changes.ts +2 -5
  12. package/Rule/Reserve.ts +2 -9
  13. package/Rule/State/Transaction.ts +1 -2
  14. package/Rule/index.ts +18 -15
  15. package/Transaction/Amount.ts +41 -0
  16. package/Transaction/index.ts +24 -57
  17. package/dist/cjs/Backup/Account.d.ts +15 -0
  18. package/dist/cjs/Backup/Account.js +20 -0
  19. package/dist/cjs/Backup/Account.js.map +1 -0
  20. package/dist/cjs/Backup/Base.d.ts +29 -0
  21. package/dist/cjs/Backup/Base.js +18 -0
  22. package/dist/cjs/Backup/Base.js.map +1 -0
  23. package/dist/cjs/Backup/Card.d.ts +14 -0
  24. package/dist/cjs/Backup/Card.js +20 -0
  25. package/dist/cjs/Backup/Card.js.map +1 -0
  26. package/dist/cjs/Backup/Operation.d.ts +15 -0
  27. package/dist/cjs/Backup/Operation.js +19 -0
  28. package/dist/cjs/Backup/Operation.js.map +1 -0
  29. package/dist/cjs/Backup/Organization.d.ts +13 -0
  30. package/dist/cjs/Backup/Organization.js +19 -0
  31. package/dist/cjs/Backup/Organization.js.map +1 -0
  32. package/dist/cjs/Backup/Rule.d.ts +17 -0
  33. package/dist/cjs/Backup/Rule.js +19 -0
  34. package/dist/cjs/Backup/Rule.js.map +1 -0
  35. package/dist/cjs/Backup/Transaction.d.ts +25 -0
  36. package/dist/cjs/Backup/Transaction.js +20 -0
  37. package/dist/cjs/Backup/Transaction.js.map +1 -0
  38. package/dist/cjs/Backup/User.d.ts +15 -0
  39. package/dist/cjs/Backup/User.js +18 -0
  40. package/dist/cjs/Backup/User.js.map +1 -0
  41. package/dist/cjs/Backup/index.d.ts +26 -0
  42. package/dist/cjs/Backup/index.js +23 -0
  43. package/dist/cjs/Backup/index.js.map +1 -0
  44. package/dist/cjs/Card/Meta.d.ts +1 -3
  45. package/dist/cjs/Card/Meta.js.map +1 -1
  46. package/dist/cjs/Operation/Changes.d.ts +2 -2
  47. package/dist/cjs/Operation/Changes.js.map +1 -1
  48. package/dist/cjs/Rule/Reserve.js +2 -9
  49. package/dist/cjs/Rule/Reserve.js.map +1 -1
  50. package/dist/cjs/Rule/State/Transaction.d.ts +0 -1
  51. package/dist/cjs/Rule/State/Transaction.js +1 -1
  52. package/dist/cjs/Rule/State/Transaction.js.map +1 -1
  53. package/dist/cjs/Rule/index.js +18 -15
  54. package/dist/cjs/Rule/index.js.map +1 -1
  55. package/dist/cjs/Transaction/Amount.d.ts +14 -0
  56. package/dist/cjs/Transaction/Amount.js +34 -0
  57. package/dist/cjs/Transaction/Amount.js.map +1 -0
  58. package/dist/cjs/Transaction/index.d.ts +3 -13
  59. package/dist/cjs/Transaction/index.js +21 -50
  60. package/dist/cjs/Transaction/index.js.map +1 -1
  61. package/dist/cjs/pax2pay.d.ts +1 -0
  62. package/dist/cjs/pax2pay.js +3 -1
  63. package/dist/cjs/pax2pay.js.map +1 -1
  64. package/dist/mjs/Backup/Account.d.ts +15 -0
  65. package/dist/mjs/Backup/Account.js +17 -0
  66. package/dist/mjs/Backup/Account.js.map +1 -0
  67. package/dist/mjs/Backup/Base.d.ts +29 -0
  68. package/dist/mjs/Backup/Base.js +15 -0
  69. package/dist/mjs/Backup/Base.js.map +1 -0
  70. package/dist/mjs/Backup/Card.d.ts +14 -0
  71. package/dist/mjs/Backup/Card.js +17 -0
  72. package/dist/mjs/Backup/Card.js.map +1 -0
  73. package/dist/mjs/Backup/Operation.d.ts +15 -0
  74. package/dist/mjs/Backup/Operation.js +16 -0
  75. package/dist/mjs/Backup/Operation.js.map +1 -0
  76. package/dist/mjs/Backup/Organization.d.ts +13 -0
  77. package/dist/mjs/Backup/Organization.js +16 -0
  78. package/dist/mjs/Backup/Organization.js.map +1 -0
  79. package/dist/mjs/Backup/Rule.d.ts +17 -0
  80. package/dist/mjs/Backup/Rule.js +16 -0
  81. package/dist/mjs/Backup/Rule.js.map +1 -0
  82. package/dist/mjs/Backup/Transaction.d.ts +25 -0
  83. package/dist/mjs/Backup/Transaction.js +17 -0
  84. package/dist/mjs/Backup/Transaction.js.map +1 -0
  85. package/dist/mjs/Backup/User.d.ts +15 -0
  86. package/dist/mjs/Backup/User.js +15 -0
  87. package/dist/mjs/Backup/User.js.map +1 -0
  88. package/dist/mjs/Backup/index.d.ts +26 -0
  89. package/dist/mjs/Backup/index.js +20 -0
  90. package/dist/mjs/Backup/index.js.map +1 -0
  91. package/dist/mjs/Card/Meta.d.ts +1 -3
  92. package/dist/mjs/Card/Meta.js.map +1 -1
  93. package/dist/mjs/Operation/Changes.d.ts +2 -2
  94. package/dist/mjs/Operation/Changes.js.map +1 -1
  95. package/dist/mjs/Rule/Reserve.js +2 -9
  96. package/dist/mjs/Rule/Reserve.js.map +1 -1
  97. package/dist/mjs/Rule/State/Transaction.d.ts +0 -1
  98. package/dist/mjs/Rule/State/Transaction.js +1 -1
  99. package/dist/mjs/Rule/State/Transaction.js.map +1 -1
  100. package/dist/mjs/Rule/index.js +18 -15
  101. package/dist/mjs/Rule/index.js.map +1 -1
  102. package/dist/mjs/Transaction/Amount.d.ts +14 -0
  103. package/dist/mjs/Transaction/Amount.js +31 -0
  104. package/dist/mjs/Transaction/Amount.js.map +1 -0
  105. package/dist/mjs/Transaction/index.d.ts +3 -13
  106. package/dist/mjs/Transaction/index.js +21 -50
  107. package/dist/mjs/Transaction/index.js.map +1 -1
  108. package/dist/mjs/pax2pay.d.ts +1 -0
  109. package/dist/mjs/pax2pay.js +1 -0
  110. package/dist/mjs/pax2pay.js.map +1 -1
  111. package/package.json +4 -4
  112. package/pax2pay.ts +1 -0
@@ -0,0 +1,24 @@
1
+ import { isoly } from "isoly"
2
+ import { Account as modelAccount } from "../Account"
3
+ import { Base } from "./Base"
4
+
5
+ export interface Account extends Base<modelAccount> {
6
+ entityType: "account"
7
+ entity: string
8
+ organization: string
9
+ account: string
10
+ meta: { key: string }
11
+ }
12
+ export namespace Account {
13
+ export const create: Base.Create<modelAccount, Account> = (value: modelAccount, action: Base.Action) => ({
14
+ entityType: "account",
15
+ entity: value.id,
16
+ organization: value.organization,
17
+ account: value.id,
18
+ action,
19
+ created: isoly.DateTime.now(),
20
+ meta: { key: value.key ?? "" },
21
+ value,
22
+ })
23
+ export const addSender = Base.pipeToSender(create)
24
+ }
package/Backup/Base.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { isoly } from "isoly"
2
+ import { Realm } from "../Realm"
3
+
4
+ export type Base<T> = {
5
+ realm?: Realm
6
+ organization?: string
7
+ account?: string
8
+ entity: string
9
+ entityType: string
10
+ action: Base.Action
11
+ created: isoly.DateTime
12
+ isError?: true
13
+ meta?: any
14
+ value: T
15
+ }
16
+ export namespace Base {
17
+ export type Action = typeof Action.values[number]
18
+ export namespace Action {
19
+ export const values = ["created", "updated", "cancelled", "removed"] as const
20
+ }
21
+ export type Data = { realm?: Realm; organization?: string; account?: string; isError?: true; meta?: any }
22
+ export type Create<T, B extends Base<T>, D extends Data = Record<string, never>> = (...args: [T, Action, D?]) => B
23
+ export function pipeToSender<T, B extends Base<T>, D extends Data = Record<string, never>>(
24
+ mapper: Base.Create<T, B, D>
25
+ ): (sender: (backup: B) => any | Promise<any>) => (...args: Parameters<typeof mapper>) => ReturnType<typeof sender> {
26
+ return sender =>
27
+ (...args) => {
28
+ const backup = mapper(...args)
29
+ return sender(backup)
30
+ }
31
+ }
32
+ }
package/Backup/Card.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { isoly } from "isoly"
2
+ import { Card as modelCard } from "../Card"
3
+ import { Realm } from "../Realm"
4
+ import { Base } from "./Base"
5
+
6
+ export interface Card extends Base<modelCard> {
7
+ realm: Realm
8
+ entityType: "card"
9
+ entity: string
10
+ organization: string
11
+ account: string
12
+ }
13
+ export namespace Card {
14
+ export const create: Base.Create<modelCard, Card> = (value: modelCard, action: Base.Action) => ({
15
+ realm: value.realm,
16
+ entityType: "card",
17
+ entity: value.id,
18
+ organization: value.organization,
19
+ account: value.account,
20
+ action,
21
+ created: isoly.DateTime.now(),
22
+ value,
23
+ })
24
+ export const addSender = Base.pipeToSender(create)
25
+ }
@@ -0,0 +1,25 @@
1
+ import { isoly } from "isoly"
2
+ import { Operation as modelOperation } from "../Operation"
3
+ import { Base } from "./Base"
4
+
5
+ export interface Operation extends Base<modelOperation> {
6
+ entityType: "operation"
7
+ entity: string
8
+ account: string
9
+ }
10
+ export namespace Operation {
11
+ export const create: Base.Create<modelOperation, Operation, { organization: string }> = (
12
+ value: modelOperation,
13
+ action: Base.Action,
14
+ data?: { organization: string }
15
+ ) => ({
16
+ entityType: "operation",
17
+ entity: value.signature ?? "",
18
+ ...data,
19
+ account: value.account,
20
+ action,
21
+ created: isoly.DateTime.now(),
22
+ value,
23
+ })
24
+ export const addSender = Base.pipeToSender(create)
25
+ }
@@ -0,0 +1,26 @@
1
+ import { isoly } from "isoly"
2
+ import { Organization as modelOrganization } from "../Organization"
3
+ import { Realm } from "../Realm"
4
+ import { Base } from "./Base"
5
+
6
+ export interface Organization extends Base<modelOrganization> {
7
+ realm: Realm
8
+ entityType: "organization"
9
+ entity: string
10
+ organization: string
11
+ }
12
+ export namespace Organization {
13
+ export const create: Base.Create<modelOrganization, Organization> = (
14
+ value: modelOrganization,
15
+ action: Base.Action
16
+ ) => ({
17
+ realm: value.realm,
18
+ entityType: "organization",
19
+ entity: value.code,
20
+ organization: value.code,
21
+ action,
22
+ created: isoly.DateTime.now(),
23
+ value,
24
+ })
25
+ export const addSender = Base.pipeToSender(create)
26
+ }
package/Backup/Rule.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { isoly } from "isoly"
2
+ import { Rule as modelRule } from "../Rule"
3
+ import { Base } from "./Base"
4
+
5
+ export interface Rule extends Base<modelRule> {
6
+ entityType: "rule"
7
+ entity: string
8
+ meta: string
9
+ }
10
+ export namespace Rule {
11
+ export const create: Base.Create<modelRule, Rule, { organization?: string; account?: string }> = (
12
+ value: modelRule,
13
+ action: Base.Action,
14
+ data?: { organization?: string; account?: string }
15
+ ) => ({
16
+ entityType: "rule",
17
+ entity: value.code,
18
+ ...data,
19
+ action,
20
+ created: isoly.DateTime.now(),
21
+ meta: `${value.category}.${value.type}.${value.action}`,
22
+ value,
23
+ })
24
+ export const addSender = Base.pipeToSender(create)
25
+ }
@@ -0,0 +1,28 @@
1
+ import { isoly } from "isoly"
2
+ import { Transaction as modelTransaction } from "../Transaction"
3
+ import { Base } from "./Base"
4
+
5
+ export interface Transaction extends Base<modelTransaction & { state?: any }> {
6
+ entityType: "transaction"
7
+ entity: string
8
+ organization: string
9
+ account: string
10
+ isError?: true
11
+ }
12
+ export namespace Transaction {
13
+ export const create: Base.Create<modelTransaction & { state?: any }, Transaction, { isError?: true; meta?: any }> = (
14
+ value: modelTransaction & { state?: any },
15
+ action: Base.Action,
16
+ data?: { isError?: true; meta?: any }
17
+ ) => ({
18
+ entityType: "transaction",
19
+ entity: value.id,
20
+ organization: value.organization,
21
+ account: value.accountId,
22
+ action,
23
+ ...data,
24
+ created: isoly.DateTime.now(),
25
+ value,
26
+ })
27
+ export const addSender = Base.pipeToSender(create)
28
+ }
package/Backup/User.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { isoly } from "isoly"
2
+ import { userwidgets } from "@userwidgets/model"
3
+ import { Realm } from "../Realm"
4
+ import { Base } from "./Base"
5
+
6
+ export type User = Base<userwidgets.User> & {
7
+ entityType: "user"
8
+ entity: string
9
+ }
10
+
11
+ export namespace User {
12
+ export const create: Base.Create<userwidgets.User, User, { realm: Realm }> = (
13
+ value: userwidgets.User,
14
+ action: Base.Action,
15
+ data?: { realm: Realm }
16
+ ) => ({
17
+ ...data,
18
+ entityType: "user",
19
+ entity: value.email,
20
+ action,
21
+ created: isoly.DateTime.now(),
22
+ value,
23
+ })
24
+ export const addSender = Base.pipeToSender(create)
25
+ }
@@ -0,0 +1,30 @@
1
+ import { Realm } from "../Realm"
2
+ import { Account as AccountBackup } from "./Account"
3
+ import { Base as BaseBackup } from "./Base"
4
+ import { Card as CardBackup } from "./Card"
5
+ import { Operation as OperationBackup } from "./Operation"
6
+ import { Organization as OrganizationBackup } from "./Organization"
7
+ import { Rule as RuleBackup } from "./Rule"
8
+ import { Transaction as TransactionBackup } from "./Transaction"
9
+ import { User as UserBackup } from "./User"
10
+
11
+ export type Backup = Backup.Partial & Backup.Identifier
12
+ export namespace Backup {
13
+ export type Identifier = { realm: Realm; version: string; source: string }
14
+ export type Partial =
15
+ | TransactionBackup
16
+ | OperationBackup
17
+ | OrganizationBackup
18
+ | RuleBackup
19
+ | AccountBackup
20
+ | CardBackup
21
+ | UserBackup
22
+ export import Base = BaseBackup
23
+ export import Transaction = TransactionBackup
24
+ export import Account = AccountBackup
25
+ export import Organization = OrganizationBackup
26
+ export import Rule = RuleBackup
27
+ export import Operation = OperationBackup
28
+ export import User = UserBackup
29
+ export import Card = CardBackup
30
+ }
package/Card/Meta.ts CHANGED
@@ -1,6 +1,5 @@
1
- type Value = Meta | Value[] | undefined | number | string | boolean
2
1
  export interface Meta {
3
- [key: string]: Value
2
+ [key: string]: any
4
3
  }
5
4
 
6
5
  export namespace Meta {
@@ -100,10 +100,7 @@ export namespace Changes {
100
100
  export const type = isly.string<Balance>(values)
101
101
  }
102
102
  }
103
- export function fromCapture(
104
- settlement: string | undefined, // FIXME: remove | undefined when we're sure we send the id
105
- amounts: { net: number; fee: number; charge?: number }
106
- ): Changes {
103
+ export function fromCapture(settlement: string, amounts: { net: number; fee: number; charge?: number }): Changes {
107
104
  return {
108
105
  [`${settlement}-net`]: { type: "add" as const, amount: amounts.net, status: "pending" as const },
109
106
  [`${settlement}-fee`]: { type: "add" as const, amount: amounts.fee, status: "pending" as const },
@@ -113,7 +110,7 @@ export namespace Changes {
113
110
  }
114
111
  }
115
112
  export function fromRefund(
116
- settlement: string | undefined, // FIXME: remove | undefined when we're sure we send the id
113
+ settlement: string,
117
114
  refund: Settlement.Entry.Creatable.Refund,
118
115
  charge: number | undefined,
119
116
  sum: Sum
package/Rule/Reserve.ts CHANGED
@@ -21,10 +21,7 @@ export namespace Reserve {
21
21
  }
22
22
  export const type = Base.type.extend<Reserve>({
23
23
  action: isly.string(Action.value),
24
- reserve: isly.object({
25
- percentage: isly.number(),
26
- fixed: Amount.type.optional(),
27
- }),
24
+ reserve: isly.object({ percentage: isly.number(), fixed: Amount.type.optional() }),
28
25
  })
29
26
 
30
27
  export interface Api extends Base {
@@ -63,11 +60,7 @@ export namespace Reserve {
63
60
  macros?: Record<string, selectively.Definition>,
64
61
  table: Exchange.Rates = {}
65
62
  ): { outcomes: Reserve[]; notes: Note[]; reserve: number } {
66
- const result: ReturnType<typeof evaluate> = {
67
- outcomes: [],
68
- notes: [],
69
- reserve: 0,
70
- }
63
+ const result: ReturnType<typeof evaluate> = { outcomes: [], notes: [], reserve: 0 }
71
64
  const now = isoly.DateTime.now()
72
65
  if (
73
66
  state.transaction.stage == "initiate" &&
@@ -13,7 +13,6 @@ export type Transaction = ModelTransaction.Creatable.Resolved & {
13
13
  total: number
14
14
  amount: number
15
15
  charge?: { current: number; total: number }
16
- reserve?: number
17
16
  }
18
17
  }
19
18
  export namespace Transaction {
@@ -31,7 +30,7 @@ export namespace Transaction {
31
30
  transaction.currency,
32
31
  transaction.state?.transaction.original.total ??
33
32
  (typeof transaction.amount == "number" ? transaction.amount : transaction.amount.total),
34
- stage === "finalize" ? transaction.state?.transaction.original.reserve ?? 0 : 0
33
+ stage === "finalize" ? transaction.state?.transaction.original.charge?.total ?? 0 : 0
35
34
  ),
36
35
  ]
37
36
  : [transaction.amount, transaction.amount]
package/Rule/index.ts CHANGED
@@ -2,6 +2,7 @@ import { selectively } from "selectively"
2
2
  import { isly } from "isly"
3
3
  import { Exchange } from "../Exchange"
4
4
  import { Realm } from "../Realm"
5
+ import type { Note } from "../Transaction/Note"
5
6
  import { Base as RuleBase } from "./Base"
6
7
  import { Charge as RuleCharge } from "./Charge"
7
8
  import { control as ruleControl } from "./control"
@@ -63,30 +64,32 @@ export namespace Rule {
63
64
  score: [],
64
65
  reserve: [],
65
66
  }
67
+ const notes: Note[] = []
66
68
  const { other, chargers, scorers, reservers } = sort(rules, state)
67
69
  const scored = Score.evaluate(scorers, state, macros)
70
+ notes.push(...scored.notes)
68
71
  outcomes.score.push(...scored.outcomes)
69
72
  state.transaction.risk = scored.risk
70
73
  const evaluated = Other.evaluate(other, state, macros)
74
+ notes.push(...evaluated.notes)
71
75
  outcomes.flag.push(...evaluated.outcomes.flag)
72
76
  outcomes.review.push(...evaluated.outcomes.review)
73
77
  outcomes.reject.push(...evaluated.outcomes.reject)
74
- const reserved = Reserve.evaluate(reservers, state, macros, table)
75
- outcomes.reserve.push(...reserved.outcomes)
76
- state.transaction.original.reserve = reserved.reserve
77
- state.transaction.original.total = Reserve.apply(reserved.reserve, state)
78
- const charged = Charge.evaluate(chargers, state, macros, table)
79
- outcomes.charge.push(...charged.outcomes)
80
- state.transaction.original.charge = charged.charge
81
- state.transaction.original.total = Charge.apply(charged.charge, state)
82
- const outcome = outcomes.reject.length > 0 ? "reject" : outcomes.review.length > 0 ? "review" : "approve"
83
- return {
84
- ...state,
85
- flags: [...evaluated.flags],
86
- notes: [...scored.notes, ...evaluated.notes, ...reserved.notes, ...charged.notes],
87
- outcomes,
88
- outcome,
78
+ if (state.transaction.stage == "initiate") {
79
+ const reserved = Reserve.evaluate(reservers, state, macros, table)
80
+ state.transaction.original.charge = { current: reserved.reserve, total: reserved.reserve }
81
+ state.transaction.original.total = Reserve.apply(reserved.reserve, state)
82
+ outcomes.reserve.push(...reserved.outcomes)
83
+ notes.push(...reserved.notes)
84
+ } else if (state.transaction.stage == "finalize") {
85
+ const charged = Charge.evaluate(chargers, state, macros, table)
86
+ outcomes.charge.push(...charged.outcomes)
87
+ state.transaction.original.charge = charged.charge
88
+ state.transaction.original.total = Charge.apply(charged.charge, state)
89
+ notes.push(...charged.notes)
89
90
  }
91
+ const outcome = outcomes.reject.length > 0 ? "reject" : outcomes.review.length > 0 ? "review" : "approve"
92
+ return { ...state, flags: [...evaluated.flags], notes, outcomes, outcome }
90
93
  }
91
94
  function sort(
92
95
  rules: Rule[],
@@ -0,0 +1,41 @@
1
+ import { isoly } from "isoly"
2
+ import { isly } from "isly"
3
+ import type { Rule } from "../Rule"
4
+ import { Exchange } from "./Exchange"
5
+
6
+ export interface Amount {
7
+ original: number
8
+ charge: number
9
+ total: number
10
+ exchange?: Exchange
11
+ }
12
+ export namespace Amount {
13
+ export const type = isly.object<Amount>({
14
+ original: isly.number(),
15
+ charge: isly.number(),
16
+ total: isly.number(),
17
+ exchange: Exchange.type.optional(),
18
+ })
19
+ export function fromState(state: Rule.State): Amount {
20
+ const sign = ["outbound", "authorization", "capture"].some(direction => direction == state.transaction.kind)
21
+ ? -1
22
+ : 1
23
+ return {
24
+ original: sign * state.transaction.original.amount,
25
+ charge: sign * (state.transaction.original.charge?.total ?? 0),
26
+ total: sign * state.transaction.original.total,
27
+ exchange: state?.transaction.exchange ?? state.authorization?.exchange,
28
+ }
29
+ }
30
+
31
+ export function change(
32
+ currency: isoly.Currency,
33
+ amount: Amount,
34
+ change: number,
35
+ type: Exclude<keyof Amount, "total" | "exchange">
36
+ ): Amount {
37
+ amount[type] = isoly.Currency.add(currency, amount[type], change)
38
+ amount.total = isoly.Currency.add(currency, amount.total, change)
39
+ return amount
40
+ }
41
+ }
@@ -7,6 +7,7 @@ import { Rail } from "../Rail"
7
7
  import { Report } from "../Report"
8
8
  import type { Rule } from "../Rule"
9
9
  import { Settlement } from "../Settlement"
10
+ import { Amount as TransactionAmount } from "./Amount"
10
11
  import { Creatable as TransactionCreatable } from "./Creatable"
11
12
  import { Exchange as TransactionExchange } from "./Exchange"
12
13
  import { Note as TransactionNote } from "./Note"
@@ -44,58 +45,7 @@ export interface Transaction {
44
45
  }
45
46
  export namespace Transaction {
46
47
  export import Exchange = TransactionExchange
47
- export type Amount = { original: number; reserved: number; charge: number; total: number; exchange?: Exchange }
48
- export namespace Amount {
49
- export const type = isly.object<Amount>({
50
- original: isly.number(),
51
- reserved: isly.number(),
52
- charge: isly.number(),
53
- total: isly.number(),
54
- exchange: Exchange.type.optional(),
55
- })
56
- export function fromState(state: Rule.State): Amount {
57
- const sign = ["outbound", "authorization", "capture"].some(direction => direction == state.transaction.kind)
58
- ? -1
59
- : 1
60
- return {
61
- original: sign * state.transaction.original.amount,
62
- reserved: sign * (state.transaction.original.reserve ?? 0),
63
- charge: sign * (state.transaction.original.charge?.total ?? 0),
64
- total: sign * state.transaction.original.total,
65
- exchange: state?.transaction.exchange ?? state.authorization?.exchange,
66
- }
67
- }
68
- export function fromOperations(
69
- transaction: Transaction | Transaction.Creatable,
70
- operations: Operation[],
71
- state?: Rule.State
72
- ): Amount {
73
- const stateAmount = state && fromState(state)
74
- const changes = Operation.sum(operations)
75
- const reserved = isoly.Currency.add(
76
- transaction.currency,
77
- changes["reserved-incoming"] ?? 0,
78
- changes["reserved-outgoing"] ?? 0
79
- )
80
- return {
81
- original: typeof transaction.amount == "number" ? transaction.amount : transaction.amount.original,
82
- reserved,
83
- charge: stateAmount?.charge ?? 0,
84
- total: changes.available ?? reserved ?? 0,
85
- exchange: state?.transaction.exchange ?? state?.authorization?.exchange,
86
- }
87
- }
88
- export function change(
89
- currency: isoly.Currency,
90
- amount: Amount,
91
- change: number,
92
- type: Exclude<keyof Amount, "total" | "exchange">
93
- ): Amount {
94
- amount[type] = isoly.Currency.add(currency, amount[type], change)
95
- amount.total = isoly.Currency.add(currency, amount.total, change)
96
- return amount
97
- }
98
- }
48
+ export import Amount = TransactionAmount
99
49
  export const types = ["card", "internal", "external", "system"] as const
100
50
  export type Types = typeof types[number]
101
51
  export const directions = ["inbound", "outbound"] as const
@@ -137,7 +87,25 @@ export namespace Transaction {
137
87
  risk: isly.number().optional(),
138
88
  state: isly.any().optional(),
139
89
  })
140
-
90
+ export function amountFromOperations(
91
+ transaction: Transaction | Transaction.Creatable,
92
+ operations: Operation[],
93
+ state?: Rule.State
94
+ ): Amount {
95
+ const stateAmount = state && Amount.fromState(state)
96
+ const changes = Operation.sum(operations)
97
+ const reserved = isoly.Currency.add(
98
+ transaction.currency,
99
+ changes["reserved-incoming"] ?? 0,
100
+ changes["reserved-outgoing"] ?? 0
101
+ )
102
+ return {
103
+ original: typeof transaction.amount == "number" ? transaction.amount : transaction.amount.original,
104
+ charge: stateAmount?.charge ?? 0,
105
+ total: changes.available ?? reserved ?? 0,
106
+ exchange: state?.transaction.exchange ?? state?.authorization?.exchange,
107
+ }
108
+ }
141
109
  export interface Legacy extends Omit<Transaction, "amount"> {
142
110
  amount: number
143
111
  }
@@ -154,7 +122,6 @@ export namespace Transaction {
154
122
  transaction.state?.transaction.original.amount ??
155
123
  isoly.Currency.subtract(transaction.currency, transaction.amount, transaction.charge ?? 0),
156
124
  charge: transaction.state?.transaction.original.charge?.total ?? transaction.charge ?? 0,
157
- reserved: transaction.state?.transaction.original.reserve ?? 0,
158
125
  total: transaction.state?.transaction.original.total ?? transaction.amount,
159
126
  },
160
127
  }
@@ -232,7 +199,7 @@ export namespace Transaction {
232
199
  ): Transaction {
233
200
  return {
234
201
  ...creatable,
235
- amount: { original: creatable.amount, reserved: 0, charge: 0, total: creatable.amount },
202
+ amount: { original: creatable.amount, charge: 0, total: creatable.amount },
236
203
  type: getType(creatable.counterpart, account.name),
237
204
  direction: "inbound",
238
205
  organization: account.organization,
@@ -258,7 +225,7 @@ export namespace Transaction {
258
225
  ): Transaction {
259
226
  return {
260
227
  ...creatable,
261
- amount: { original: 0, reserved: 0, charge: 0, total: 0 },
228
+ amount: { original: 0, charge: 0, total: 0 },
262
229
  type: getType(creatable.counterpart, account.name),
263
230
  direction: "inbound",
264
231
  organization: account.organization,
@@ -292,7 +259,7 @@ export namespace Transaction {
292
259
  name: account.name,
293
260
  organization: account.organization,
294
261
  },
295
- amount: { original: 0, reserved: 0, charge: 0, total: 0 },
262
+ amount: { original: 0, charge: 0, total: 0 },
296
263
  type: "internal",
297
264
  direction: "inbound",
298
265
  organization: account.organization,
@@ -0,0 +1,15 @@
1
+ import { Account as modelAccount } from "../Account";
2
+ import { Base } from "./Base";
3
+ export interface Account extends Base<modelAccount> {
4
+ entityType: "account";
5
+ entity: string;
6
+ organization: string;
7
+ account: string;
8
+ meta: {
9
+ key: string;
10
+ };
11
+ }
12
+ export declare namespace Account {
13
+ const create: Base.Create<modelAccount, Account>;
14
+ const addSender: (sender: (backup: Account) => any | Promise<any>) => (args_0: modelAccount, args_1: "created" | "cancelled" | "updated" | "removed", args_2?: Record<string, never> | undefined) => ReturnType<(backup: Account) => any | Promise<any>>;
15
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Account = void 0;
4
+ const isoly_1 = require("isoly");
5
+ const Base_1 = require("./Base");
6
+ var Account;
7
+ (function (Account) {
8
+ Account.create = (value, action) => ({
9
+ entityType: "account",
10
+ entity: value.id,
11
+ organization: value.organization,
12
+ account: value.id,
13
+ action,
14
+ created: isoly_1.isoly.DateTime.now(),
15
+ meta: { key: value.key ?? "" },
16
+ value,
17
+ });
18
+ Account.addSender = Base_1.Base.pipeToSender(Account.create);
19
+ })(Account || (exports.Account = Account = {}));
20
+ //# sourceMappingURL=Account.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Account.js","sourceRoot":"","sources":["../../../Backup/Account.ts"],"names":[],"mappings":";;;AAAA,iCAA6B;AAE7B,iCAA6B;AAS7B,IAAiB,OAAO,CAYvB;AAZD,WAAiB,OAAO;IACV,cAAM,GAAuC,CAAC,KAAmB,EAAE,MAAmB,EAAE,EAAE,CAAC,CAAC;QACxG,UAAU,EAAE,SAAS;QACrB,MAAM,EAAE,KAAK,CAAC,EAAE;QAChB,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,OAAO,EAAE,KAAK,CAAC,EAAE;QACjB,MAAM;QACN,OAAO,EAAE,aAAK,CAAC,QAAQ,CAAC,GAAG,EAAE;QAC7B,IAAI,EAAE,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,IAAI,EAAE,EAAE;QAC9B,KAAK;KACL,CAAC,CAAA;IACW,iBAAS,GAAG,WAAI,CAAC,YAAY,CAAC,QAAA,MAAM,CAAC,CAAA;AACnD,CAAC,EAZgB,OAAO,uBAAP,OAAO,QAYvB"}
@@ -0,0 +1,29 @@
1
+ import { isoly } from "isoly";
2
+ import { Realm } from "../Realm";
3
+ export type Base<T> = {
4
+ realm?: Realm;
5
+ organization?: string;
6
+ account?: string;
7
+ entity: string;
8
+ entityType: string;
9
+ action: Base.Action;
10
+ created: isoly.DateTime;
11
+ isError?: true;
12
+ meta?: any;
13
+ value: T;
14
+ };
15
+ export declare namespace Base {
16
+ type Action = typeof Action.values[number];
17
+ namespace Action {
18
+ const values: readonly ["created", "updated", "cancelled", "removed"];
19
+ }
20
+ type Data = {
21
+ realm?: Realm;
22
+ organization?: string;
23
+ account?: string;
24
+ isError?: true;
25
+ meta?: any;
26
+ };
27
+ type Create<T, B extends Base<T>, D extends Data = Record<string, never>> = (...args: [T, Action, D?]) => B;
28
+ function pipeToSender<T, B extends Base<T>, D extends Data = Record<string, never>>(mapper: Base.Create<T, B, D>): (sender: (backup: B) => any | Promise<any>) => (...args: Parameters<typeof mapper>) => ReturnType<typeof sender>;
29
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Base = void 0;
4
+ var Base;
5
+ (function (Base) {
6
+ let Action;
7
+ (function (Action) {
8
+ Action.values = ["created", "updated", "cancelled", "removed"];
9
+ })(Action = Base.Action || (Base.Action = {}));
10
+ function pipeToSender(mapper) {
11
+ return sender => (...args) => {
12
+ const backup = mapper(...args);
13
+ return sender(backup);
14
+ };
15
+ }
16
+ Base.pipeToSender = pipeToSender;
17
+ })(Base || (exports.Base = Base = {}));
18
+ //# sourceMappingURL=Base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Base.js","sourceRoot":"","sources":["../../../Backup/Base.ts"],"names":[],"mappings":";;;AAeA,IAAiB,IAAI,CAgBpB;AAhBD,WAAiB,IAAI;IAEpB,IAAiB,MAAM,CAEtB;IAFD,WAAiB,MAAM;QACT,aAAM,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,CAAU,CAAA;IAC9E,CAAC,EAFgB,MAAM,GAAN,WAAM,KAAN,WAAM,QAEtB;IAGD,SAAgB,YAAY,CAC3B,MAA4B;QAE5B,OAAO,MAAM,CAAC,EAAE,CACf,CAAC,GAAG,IAAI,EAAE,EAAE;YACX,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;YAC9B,OAAO,MAAM,CAAC,MAAM,CAAC,CAAA;QACtB,CAAC,CAAA;IACH,CAAC;IARe,iBAAY,eAQ3B,CAAA;AACF,CAAC,EAhBgB,IAAI,oBAAJ,IAAI,QAgBpB"}
@@ -0,0 +1,14 @@
1
+ import { Card as modelCard } from "../Card";
2
+ import { Realm } from "../Realm";
3
+ import { Base } from "./Base";
4
+ export interface Card extends Base<modelCard> {
5
+ realm: Realm;
6
+ entityType: "card";
7
+ entity: string;
8
+ organization: string;
9
+ account: string;
10
+ }
11
+ export declare namespace Card {
12
+ const create: Base.Create<modelCard, Card>;
13
+ const addSender: (sender: (backup: Card) => any | Promise<any>) => (args_0: modelCard, args_1: "created" | "cancelled" | "updated" | "removed", args_2?: Record<string, never> | undefined) => ReturnType<(backup: Card) => any | Promise<any>>;
14
+ }