@voltro/plugin-economy 0.0.0-stage → 0.88.0

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 (177) hide show
  1. package/CHANGELOG.md +24492 -0
  2. package/LICENSE +61 -0
  3. package/README.md +2719 -2
  4. package/SECURITY.md +56 -0
  5. package/THIRD-PARTY-NOTICES.md +9841 -0
  6. package/dist/amount.js +20 -0
  7. package/dist/appleIap.server.js +262 -0
  8. package/dist/appleIapNotifications.server.js +162 -0
  9. package/dist/appleIapTrust.server.js +30 -0
  10. package/dist/bundleValue.server.js +112 -0
  11. package/dist/canonicalDataJson.js +10 -0
  12. package/dist/capture.js +53 -0
  13. package/dist/codePort.fixture.js +123 -0
  14. package/dist/codeStore.server.js +439 -0
  15. package/dist/codeTables.entity.js +43 -0
  16. package/dist/codes.d.ts +150 -0
  17. package/dist/codes.js +82 -0
  18. package/dist/components.d.ts +397 -0
  19. package/dist/components.js +283 -0
  20. package/dist/consistency.server.js +115 -0
  21. package/dist/containerStore.server.js +687 -0
  22. package/dist/containerStore.testFixtures.js +75 -0
  23. package/dist/containerTables.entity.js +45 -0
  24. package/dist/containers.d.ts +311 -0
  25. package/dist/containers.fixture.js +128 -0
  26. package/dist/containers.js +214 -0
  27. package/dist/containers.server.js +39 -0
  28. package/dist/containersPort.fixture.js +74 -0
  29. package/dist/currency.js +32 -0
  30. package/dist/declarationFixtures.js +61 -0
  31. package/dist/economy-app/events/economy.refundReversed.event.ts +1 -0
  32. package/dist/economy-app/events/economy.refunded.event.ts +1 -0
  33. package/dist/economyPort.fixture.js +124 -0
  34. package/dist/errors.d.ts +14 -0
  35. package/dist/errors.js +21 -0
  36. package/dist/events.d.ts +46 -0
  37. package/dist/events.js +1 -0
  38. package/dist/googleIap.server.js +391 -0
  39. package/dist/googleIapNotifications.server.js +130 -0
  40. package/dist/googleIapSubscription.server.js +183 -0
  41. package/dist/iap.d.ts +309 -0
  42. package/dist/iap.js +303 -0
  43. package/dist/iap.testFixtures.js +144 -0
  44. package/dist/iapAccountTables.entity.js +39 -0
  45. package/dist/iapAccounts.server.js +200 -0
  46. package/dist/iapBinding.server.js +523 -0
  47. package/dist/iapCheckout.server.js +97 -0
  48. package/dist/iapCompletion.server.js +55 -0
  49. package/dist/iapCurrentState.server.js +107 -0
  50. package/dist/iapCurrentState.testFixtures.js +22 -0
  51. package/dist/iapCurrentStatePort.fixture.js +63 -0
  52. package/dist/iapEntitlements.d.ts +79 -0
  53. package/dist/iapEntitlements.js +57 -0
  54. package/dist/iapEntitlements.server.js +48 -0
  55. package/dist/iapEntitlementsBinding.server.js +105 -0
  56. package/dist/iapEntitlementsPort.fixture.js +80 -0
  57. package/dist/iapInstalledEntitlements.testFixtures.js +75 -0
  58. package/dist/iapInstalledNotifications.testFixtures.js +93 -0
  59. package/dist/iapInventoryRefunds.server.js +261 -0
  60. package/dist/iapNotificationCustodyPort.fixture.js +67 -0
  61. package/dist/iapNotificationPort.fixture.js +73 -0
  62. package/dist/iapNotificationProcessor.server.js +134 -0
  63. package/dist/iapNotificationProcessor.testFixtures.js +63 -0
  64. package/dist/iapNotificationProcessorPort.fixture.js +55 -0
  65. package/dist/iapNotificationSchedule.server.js +69 -0
  66. package/dist/iapNotificationStore.server.js +352 -0
  67. package/dist/iapNotificationStore.testFixtures.js +31 -0
  68. package/dist/iapNotificationTables.entity.js +48 -0
  69. package/dist/iapNotificationTarget.server.js +36 -0
  70. package/dist/iapNotificationTarget.testFixtures.js +29 -0
  71. package/dist/iapNotifications.d.ts +644 -0
  72. package/dist/iapNotifications.js +6 -0
  73. package/dist/iapNotifications.server.js +95 -0
  74. package/dist/iapNotificationsBinding.server.js +232 -0
  75. package/dist/iapNotificationsSystemPort.fixture.js +50 -0
  76. package/dist/iapPlugin.testFixtures.js +59 -0
  77. package/dist/iapPort.fixture.js +74 -0
  78. package/dist/iapProviderCapture.server.js +17 -0
  79. package/dist/iapRecovery.d.ts +146 -0
  80. package/dist/iapRecovery.js +89 -0
  81. package/dist/iapRefundEventDelivery.server.js +82 -0
  82. package/dist/iapRefundEvents.js +44 -0
  83. package/dist/iapRefundEventsPort.fixture.js +59 -0
  84. package/dist/iapRefundPort.fixture.js +54 -0
  85. package/dist/iapRefundTables.entity.js +55 -0
  86. package/dist/iapRefunds.d.ts +755 -0
  87. package/dist/iapRefunds.js +6 -0
  88. package/dist/iapRefunds.server.js +553 -0
  89. package/dist/iapRefunds.testFixtures.js +64 -0
  90. package/dist/iapSpendAdmission.server.js +98 -0
  91. package/dist/iapStore.server.js +740 -0
  92. package/dist/iapStore.testFixtures.js +127 -0
  93. package/dist/iapTables.entity.js +80 -0
  94. package/dist/iapVerification.server.js +120 -0
  95. package/dist/index.d.ts +2984 -0
  96. package/dist/index.js +15 -0
  97. package/dist/intentionRecovery.js +133 -0
  98. package/dist/ledger.js +77 -0
  99. package/dist/loyalty.d.ts +274 -0
  100. package/dist/loyalty.fixture.js +92 -0
  101. package/dist/loyalty.js +311 -0
  102. package/dist/loyaltyBinding.server.js +203 -0
  103. package/dist/loyaltyPort.fixture.js +86 -0
  104. package/dist/loyaltyShopSource.server.js +90 -0
  105. package/dist/loyaltyStore.server.js +552 -0
  106. package/dist/loyaltyStore.testFixtures.js +82 -0
  107. package/dist/loyaltyTables.entity.js +49 -0
  108. package/dist/offerMerchandising.fixture.js +114 -0
  109. package/dist/offerMerchandising.js +282 -0
  110. package/dist/offerPolicies.d.ts +441 -0
  111. package/dist/offerPolicies.fixture.js +53 -0
  112. package/dist/offerPolicies.js +267 -0
  113. package/dist/piggyBanks.d.ts +272 -0
  114. package/dist/piggyBanks.fixture.js +70 -0
  115. package/dist/piggyBanks.js +200 -0
  116. package/dist/piggyBinding.server.js +204 -0
  117. package/dist/piggyPort.fixture.js +72 -0
  118. package/dist/piggyStore.server.js +610 -0
  119. package/dist/piggyStore.testFixtures.js +97 -0
  120. package/dist/piggyTables.entity.js +40 -0
  121. package/dist/plugin.js +1047 -0
  122. package/dist/port.js +12 -0
  123. package/dist/price.d.ts +99 -0
  124. package/dist/price.js +143 -0
  125. package/dist/purchaseRecovery.js +55 -0
  126. package/dist/reports.server.js +114 -0
  127. package/dist/rpc.d.ts +3201 -0
  128. package/dist/rpc.js +1200 -0
  129. package/dist/sanctionsTradeAuthority.server.js +79 -0
  130. package/dist/server.d.ts +3856 -0
  131. package/dist/server.js +43 -0
  132. package/dist/shop.d.ts +279 -0
  133. package/dist/shop.js +195 -0
  134. package/dist/shop.testFixtures.js +113 -0
  135. package/dist/shopPort.fixture.js +71 -0
  136. package/dist/shopPresentation.js +67 -0
  137. package/dist/shopStore.server.js +623 -0
  138. package/dist/shopTables.entity.js +62 -0
  139. package/dist/shopUiCopy.js +56 -0
  140. package/dist/spendLimits.d.ts +140 -0
  141. package/dist/spendLimits.fixture.js +52 -0
  142. package/dist/spendLimits.js +154 -0
  143. package/dist/spendLimitsPort.fixture.js +119 -0
  144. package/dist/subscriptionBinding.server.js +210 -0
  145. package/dist/subscriptionStore.server.js +642 -0
  146. package/dist/subscriptionStore.testFixtures.js +71 -0
  147. package/dist/subscriptionTables.entity.js +36 -0
  148. package/dist/subscriptions.d.ts +394 -0
  149. package/dist/subscriptions.fixture.js +96 -0
  150. package/dist/subscriptions.js +212 -0
  151. package/dist/subscriptionsPort.fixture.js +75 -0
  152. package/dist/systemFixtures.js +88 -0
  153. package/dist/tables.entity.js +82 -0
  154. package/dist/testing.d.ts +4615 -0
  155. package/dist/testing.js +29 -0
  156. package/dist/tradeAuthority.server.js +171 -0
  157. package/dist/tradeBinding.server.js +200 -0
  158. package/dist/tradeComponents.js +341 -0
  159. package/dist/tradeEscrow.server.js +231 -0
  160. package/dist/tradeEscrow.testFixtures.js +132 -0
  161. package/dist/tradeExpiry.server.js +42 -0
  162. package/dist/tradeRecovery.js +47 -0
  163. package/dist/tradeStore.server.js +749 -0
  164. package/dist/tradeStore.testFixtures.js +72 -0
  165. package/dist/tradeTables.entity.js +65 -0
  166. package/dist/tradeUiCopy.js +78 -0
  167. package/dist/trades.d.ts +246 -0
  168. package/dist/trades.fixture.js +102 -0
  169. package/dist/trades.js +288 -0
  170. package/dist/tradesPort.fixture.js +93 -0
  171. package/dist/walletEscrow.js +105 -0
  172. package/dist/walletEscrow.server.js +375 -0
  173. package/dist/walletKeys.server.js +4 -0
  174. package/dist/walletStore.js +570 -0
  175. package/dist/web.d.ts +2257 -0
  176. package/dist/web.js +8 -0
  177. package/package.json +161 -4
@@ -0,0 +1,283 @@
1
+ import { dataList as e } from "./capture.js";
2
+ import { captureShopOrderReceipt as t } from "./shop.js";
3
+ import { captureShopOfferView as n, shopOfferAvailable as r, shopPurchaseTerms as i } from "./shopPresentation.js";
4
+ import { shopUiCopy as a } from "./shopUiCopy.js";
5
+ import { tradeUiCopy as o } from "./tradeUiCopy.js";
6
+ import { TradeAcceptDialog as s, TradeIntentionRecovery as c, TradeList as l, TradeTerms as u } from "./tradeComponents.js";
7
+ import { useEffect as d, useId as f, useRef as p } from "react";
8
+ import { Button as m, Dialog as h, DialogDescription as g, DialogFooter as _, DialogHeader as v, DialogTitle as y } from "@voltro/ui-shadcn";
9
+ import { jsx as b, jsxs as x } from "react/jsx-runtime";
10
+ // src/components.tsx
11
+ var S = (e) => e.copy ?? a[e.locale ?? "en"], C = (e, t, n) => e.kind === "free" ? t.free : `${e.amount} ${n?.(e.currency) ?? e.currency}`, w = (e) => {
12
+ let t = S(e), a, o;
13
+ try {
14
+ a = n(e.view), o = r(a, e.at);
15
+ } catch {
16
+ return /* @__PURE__ */ b("article", { children: /* @__PURE__ */ b("p", {
17
+ role: "alert",
18
+ children: t.invalid
19
+ }) });
20
+ }
21
+ let s = a.price.kind === "virtual" ? a.price.options : a.price.kind === "mixed" ? a.price.virtual : [];
22
+ return /* @__PURE__ */ x("article", {
23
+ className: "flex min-w-0 flex-col gap-4 rounded-xl border border-border p-5",
24
+ children: [
25
+ /* @__PURE__ */ b("h3", {
26
+ className: "break-words text-lg font-semibold",
27
+ children: e.title(a.offer.titleKey)
28
+ }),
29
+ e.renderRewards ? e.renderRewards(a) : /* @__PURE__ */ x("ul", {
30
+ className: "space-y-1 text-sm",
31
+ children: [a.offer.items.map((e) => /* @__PURE__ */ x("li", { children: [
32
+ e.qty,
33
+ " × ",
34
+ e.itemId
35
+ ] }, e.itemId)), a.offer.currencies.map((t) => /* @__PURE__ */ x("li", {
36
+ className: "tabular-nums",
37
+ children: [
38
+ t.amount,
39
+ " ",
40
+ e.currencyLabel?.(t.currency) ?? t.currency
41
+ ]
42
+ }, t.currency))]
43
+ }),
44
+ a.offer.oneTime ? /* @__PURE__ */ b("p", {
45
+ className: "text-sm text-muted-foreground",
46
+ children: t.oneTime
47
+ }) : null,
48
+ o && a.offer.until !== null ? /* @__PURE__ */ b("p", {
49
+ className: "text-sm tabular-nums",
50
+ children: /* @__PURE__ */ b("time", {
51
+ dateTime: new Date(a.offer.until).toISOString(),
52
+ children: t.endsIn(Math.max(0, Math.ceil((a.offer.until - e.at) / 1e3)))
53
+ })
54
+ }) : null,
55
+ o ? null : /* @__PURE__ */ b("p", {
56
+ className: "text-sm text-muted-foreground",
57
+ children: t.closed
58
+ }),
59
+ /* @__PURE__ */ x("div", {
60
+ className: "mt-auto flex flex-col gap-3",
61
+ children: [a.price.kind === "free" ? /* @__PURE__ */ x(m, {
62
+ type: "button",
63
+ "aria-disabled": !o || e.busy,
64
+ className: "h-auto min-h-11 whitespace-normal",
65
+ onClick: () => {
66
+ o && !e.busy && e.onSelect(a, void 0);
67
+ },
68
+ children: [
69
+ t.choose,
70
+ " · ",
71
+ t.free
72
+ ]
73
+ }) : s.map((n) => {
74
+ let r = i(a, {
75
+ offerId: a.offer.id,
76
+ offerVersion: a.offer.version,
77
+ offerDigest: a.offerDigest,
78
+ requestId: "display",
79
+ currency: n.currency
80
+ });
81
+ return /* @__PURE__ */ x("div", {
82
+ className: "space-y-2",
83
+ children: [r.discountPercent > 0 ? /* @__PURE__ */ b("p", {
84
+ className: "text-sm text-muted-foreground",
85
+ children: t.discount(r.discountPercent)
86
+ }) : null, /* @__PURE__ */ x(m, {
87
+ type: "button",
88
+ "aria-disabled": !o || e.busy,
89
+ className: "h-auto min-h-11 w-full whitespace-normal",
90
+ onClick: () => {
91
+ o && !e.busy && e.onSelect(a, n.currency);
92
+ },
93
+ children: [
94
+ t.choose,
95
+ " · ",
96
+ C(r.current, t, e.currencyLabel)
97
+ ]
98
+ })]
99
+ }, n.currency);
100
+ }), a.price.kind === "real" ? /* @__PURE__ */ b("p", {
101
+ className: "text-sm text-muted-foreground",
102
+ children: t.providersMissing
103
+ }) : null]
104
+ })
105
+ ]
106
+ });
107
+ }, T = (t) => {
108
+ let r = S(t), i = f(), a, o = !1;
109
+ try {
110
+ a = t.source.data === void 0 ? void 0 : e(t.source.data, 256).map((e) => n(e)), o = a !== void 0 && new Set(a.map((e) => e.offer.id)).size !== a.length;
111
+ } catch {
112
+ o = !0;
113
+ }
114
+ return /* @__PURE__ */ x("section", {
115
+ "aria-labelledby": i,
116
+ className: "min-w-0 space-y-5",
117
+ children: [/* @__PURE__ */ b("h2", {
118
+ id: i,
119
+ className: "text-2xl font-semibold",
120
+ children: r.shop
121
+ }), t.source.error !== void 0 || o ? /* @__PURE__ */ b("p", {
122
+ role: "alert",
123
+ children: r.unavailable
124
+ }) : t.source.loading || a === void 0 ? /* @__PURE__ */ b("p", {
125
+ role: "status",
126
+ children: r.loading
127
+ }) : a.length === 0 ? /* @__PURE__ */ b("p", { children: r.empty }) : /* @__PURE__ */ b("div", {
128
+ className: "grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-3",
129
+ children: a.map((e) => /* @__PURE__ */ b(w, {
130
+ view: e,
131
+ at: t.at,
132
+ busy: t.busy,
133
+ locale: t.locale,
134
+ copy: t.copy,
135
+ title: t.title,
136
+ currencyLabel: t.currencyLabel,
137
+ renderRewards: t.renderRewards,
138
+ onSelect: t.onSelect
139
+ }, e.offer.id))
140
+ })]
141
+ });
142
+ }, E = (e) => {
143
+ let t = S(e);
144
+ return e.reference ? /* @__PURE__ */ x("section", {
145
+ "aria-label": t.retry,
146
+ className: "min-w-0 space-y-4 border-y border-border py-5",
147
+ children: [
148
+ /* @__PURE__ */ b("p", {
149
+ role: "status",
150
+ className: "max-w-prose text-sm leading-relaxed",
151
+ children: e.busy ? t.sending : t.uncertain
152
+ }),
153
+ /* @__PURE__ */ x("dl", {
154
+ className: "space-y-2 text-sm",
155
+ children: [/* @__PURE__ */ x("div", { children: [/* @__PURE__ */ b("dt", {
156
+ className: "text-muted-foreground",
157
+ children: t.originalOffer
158
+ }), /* @__PURE__ */ b("dd", {
159
+ className: "break-all",
160
+ children: e.reference.request.offerId
161
+ })] }), /* @__PURE__ */ x("div", { children: [/* @__PURE__ */ b("dt", {
162
+ className: "text-muted-foreground",
163
+ children: t.originalRequest
164
+ }), /* @__PURE__ */ b("dd", {
165
+ className: "break-all font-mono",
166
+ children: e.reference.request.requestId
167
+ })] })]
168
+ }),
169
+ e.error === void 0 ? null : /* @__PURE__ */ b("p", {
170
+ role: "alert",
171
+ children: t.uncertain
172
+ }),
173
+ /* @__PURE__ */ b(m, {
174
+ type: "button",
175
+ "aria-disabled": e.busy,
176
+ className: "h-auto min-h-11 whitespace-normal",
177
+ onClick: () => {
178
+ !e.busy && e.reference && e.onRetry(e.reference.request);
179
+ },
180
+ children: t.retry
181
+ })
182
+ ]
183
+ }) : null;
184
+ }, D = (e) => {
185
+ let n = S(e), a = p(null), o = f(), s = f();
186
+ d(() => {
187
+ let t = a.current;
188
+ t && (e.open && !t.open ? t.showModal() : !e.open && t.open && t.close());
189
+ }, [e.open]);
190
+ let c;
191
+ try {
192
+ c = i(e.view, e.intention), r(e.view, e.at) || (c = void 0);
193
+ } catch {
194
+ c = void 0;
195
+ }
196
+ let l;
197
+ if (e.receipt !== void 0) try {
198
+ let n = t(e.receipt);
199
+ c && n.offerId === c.request.offerId && n.offerVersion === c.request.offerVersion && n.offerDigest === c.request.offerDigest && n.quantity === c.request.qty && (c.maximum.kind === "free" ? n.pricePaid.kind === "free" : n.pricePaid.kind === "virtual" && n.pricePaid.currency === c.maximum.currency && BigInt(n.pricePaid.amount) <= BigInt(c.maximum.amount)) && (l = n);
200
+ } catch {
201
+ l = void 0;
202
+ }
203
+ let u = c !== void 0 && !e.busy && e.error === void 0 && e.receipt === void 0;
204
+ return /* @__PURE__ */ x(h, {
205
+ ref: a,
206
+ "aria-labelledby": o,
207
+ "aria-describedby": s,
208
+ className: "max-h-[calc(100dvh-2rem)] max-w-[min(32rem,calc(100vw-2rem))] overflow-y-auto motion-reduce:animate-none",
209
+ onCancel: (t) => {
210
+ t.preventDefault(), e.onClose();
211
+ },
212
+ children: [
213
+ /* @__PURE__ */ x(v, { children: [/* @__PURE__ */ b(y, {
214
+ id: o,
215
+ children: e.title(e.view.offer.titleKey)
216
+ }), /* @__PURE__ */ b(g, {
217
+ id: s,
218
+ children: n.pricePolicy
219
+ })] }),
220
+ c ? /* @__PURE__ */ x("dl", {
221
+ className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)] gap-x-4 gap-y-3 text-sm",
222
+ children: [
223
+ /* @__PURE__ */ b("dt", { children: n.quantity }),
224
+ /* @__PURE__ */ b("dd", {
225
+ className: "tabular-nums",
226
+ children: c.request.qty
227
+ }),
228
+ /* @__PURE__ */ b("dt", { children: n.currentPrice }),
229
+ /* @__PURE__ */ b("dd", {
230
+ className: "break-words text-right tabular-nums",
231
+ children: C(c.current, n, e.currencyLabel)
232
+ }),
233
+ /* @__PURE__ */ b("dt", {
234
+ className: "font-medium",
235
+ children: n.maximumPrice
236
+ }),
237
+ /* @__PURE__ */ b("dd", {
238
+ className: "break-words text-right font-medium tabular-nums",
239
+ children: C(c.maximum, n, e.currencyLabel)
240
+ })
241
+ ]
242
+ }) : /* @__PURE__ */ b("p", {
243
+ role: "alert",
244
+ children: n.invalid
245
+ }),
246
+ e.busy ? /* @__PURE__ */ b("p", {
247
+ role: "status",
248
+ className: "mt-4 text-sm",
249
+ children: n.sending
250
+ }) : null,
251
+ e.error === void 0 ? null : /* @__PURE__ */ b("p", {
252
+ role: "alert",
253
+ className: "mt-4 text-sm",
254
+ children: n.uncertain
255
+ }),
256
+ l ? /* @__PURE__ */ b("p", {
257
+ role: "status",
258
+ className: "mt-4 font-medium",
259
+ children: n.complete
260
+ }) : e.receipt === void 0 ? null : /* @__PURE__ */ b("p", {
261
+ role: "alert",
262
+ className: "mt-4",
263
+ children: n.invalid
264
+ }),
265
+ /* @__PURE__ */ x(_, { children: [/* @__PURE__ */ b(m, {
266
+ type: "button",
267
+ variant: "outline",
268
+ className: "h-auto min-h-11 whitespace-normal",
269
+ onClick: e.onClose,
270
+ children: n.cancel
271
+ }), /* @__PURE__ */ b(m, {
272
+ type: "button",
273
+ "aria-disabled": !u,
274
+ className: "h-auto min-h-11 whitespace-normal",
275
+ onClick: () => {
276
+ u && c && e.onConfirm(c.request);
277
+ },
278
+ children: n.confirm
279
+ })] })
280
+ ]
281
+ });
282
+ };
283
+ export { w as OfferCard, D as PurchaseDialog, E as PurchaseRecovery, T as ShopGrid, s as TradeAcceptDialog, c as TradeIntentionRecovery, l as TradeList, u as TradeTerms, n as captureShopOfferView, r as shopOfferAvailable, i as shopPurchaseTerms, a as shopUiCopy, o as tradeUiCopy };
@@ -0,0 +1,115 @@
1
+ import { EconomyError as e, economyError as t } from "./errors.js";
2
+ import { captureBoundMethods as n, dataList as r, dataObject as i, identifier as a } from "./capture.js";
3
+ import { nonNegativeAmount as o } from "./amount.js";
4
+ import { captureAccount as s, economyAccountKey as c } from "./ledger.js";
5
+ import { economyLedgerTable as l, economyWalletsTable as u } from "./tables.entity.js";
6
+ import { economyWalletKey as d, readEconomyTransferEvidence as f } from "./walletStore.js";
7
+ import { Effect as p } from "effect";
8
+ import { systemSubject as m } from "@voltro/protocol";
9
+ import { eq as h, queryFor as g } from "@voltro/database";
10
+ import { defineSchedule as _ } from "@voltro/runtime";
11
+ // src/consistency.server.ts
12
+ var v = (e) => {
13
+ let n = i(e, [
14
+ "tenantId",
15
+ "maxLedgerRows",
16
+ "maxWallets"
17
+ ]), r = n.maxLedgerRows ?? 1e4, o = n.maxWallets ?? 1e3;
18
+ if (typeof r != "number" || !Number.isSafeInteger(r) || r < 2 || r > 1e5 || typeof o != "number" || !Number.isSafeInteger(o) || o < 1 || o > 1e4) throw t("invalid_input");
19
+ return Object.freeze({
20
+ tenantId: a(n.tenantId),
21
+ maxLedgerRows: r,
22
+ maxWallets: o
23
+ });
24
+ }, y = (n, i, m, _, y) => {
25
+ try {
26
+ let b = v(y), x = structuredClone(_), S = new Set(r(m, 128).map((e) => a(e)));
27
+ if (S.size !== m.length) throw t("invalid_input");
28
+ if (x.type !== "system") throw t("forbidden");
29
+ return p.gen(function* () {
30
+ let r = yield* p.tryPromise({
31
+ try: () => n.transactional(async (e) => {
32
+ let n = await e.query(g(l).where(h("tenantId", b.tenantId)).orderBy("id", "asc").limit(b.maxLedgerRows + 1).descriptor), r = await e.query(g(u).where(h("tenantId", b.tenantId)).orderBy("id", "asc").limit(b.maxWallets + 1).descriptor);
33
+ if (n.length > b.maxLedgerRows || r.length > b.maxWallets) throw t("limit_exceeded");
34
+ let i = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), p = (e, n, r) => {
35
+ if (!S.has(e)) throw t("invalid_currency");
36
+ let i = s({
37
+ kind: "wallet",
38
+ ownerKind: n,
39
+ ownerId: r
40
+ });
41
+ if (i.kind !== "wallet") throw t("invalid_record");
42
+ if (o.set(JSON.stringify([c(i), e]), {
43
+ account: i,
44
+ currency: e
45
+ }), o.size > b.maxWallets) throw t("limit_exceeded");
46
+ };
47
+ for (let e of n) {
48
+ let t = a(e.currency), n = a(e.txId), r = i.get(t);
49
+ r || (r = /* @__PURE__ */ new Set(), i.set(t, r)), r.add(n);
50
+ }
51
+ for (let [t, n] of i) {
52
+ let r = await f(e, b.tenantId, [...n], t);
53
+ for (let { pair: e } of r.values()) for (let n of e) n.accountKind === "wallet" && n.ownerId !== null && p(t, n.ownerKind, n.ownerId);
54
+ }
55
+ for (let e of r) {
56
+ let n = a(e.currency), r = s({
57
+ kind: "wallet",
58
+ ownerKind: e.ownerKind,
59
+ ownerId: e.ownerId
60
+ });
61
+ if (e.tenantId !== b.tenantId || e.id !== d(b.tenantId, r, n)) throw t("invalid_record");
62
+ p(n, e.ownerKind, e.ownerId);
63
+ }
64
+ return {
65
+ accounts: [...o.entries()].sort(([e], [t]) => e < t ? -1 : +(e > t)).map(([, e]) => e),
66
+ ledgerRows: n.length
67
+ };
68
+ }),
69
+ catch: (n) => n instanceof e ? n : t("unavailable")
70
+ }), m = 0, _ = 0;
71
+ for (let { currency: n, account: a } of r.accounts) {
72
+ let r = yield* i.auditWallet(x, b.tenantId, a, n);
73
+ yield* p.try({
74
+ try: () => {
75
+ let e = o(r.balance), i = o(r.ledgerBalance);
76
+ if (r.currency !== n || typeof r.matches != "boolean" || r.matches !== (e === i) || !Number.isSafeInteger(r.ledgerRows) || r.ledgerRows < 0) throw t("invalid_record");
77
+ },
78
+ catch: (n) => n instanceof e ? n : t("invalid_record")
79
+ }), m++, r.matches || _++;
80
+ }
81
+ return Object.freeze({
82
+ checked: m,
83
+ mismatches: _,
84
+ matches: _ === 0,
85
+ ledgerRows: r.ledgerRows
86
+ });
87
+ });
88
+ } catch (n) {
89
+ return p.fail(n instanceof e ? n : t("invalid_input"));
90
+ }
91
+ }, b = (e) => {
92
+ let r = i(e, [
93
+ "tenantId",
94
+ "maxLedgerRows",
95
+ "maxWallets",
96
+ "runner",
97
+ "name",
98
+ "cron",
99
+ "timezone"
100
+ ]), o = v({
101
+ tenantId: r.tenantId,
102
+ ...r.maxLedgerRows === void 0 ? {} : { maxLedgerRows: r.maxLedgerRows },
103
+ ...r.maxWallets === void 0 ? {} : { maxWallets: r.maxWallets }
104
+ }), s = n(r.runner, ["checkConsistency"]), c = a(r.name ?? "economy.checkConsistency"), l = r.cron ?? "0 2 * * *", u = r.timezone ?? "UTC";
105
+ if (typeof l != "string" || l.length > 120 || typeof u != "string" || u.length > 120) throw t("invalid_input");
106
+ return _({
107
+ name: c,
108
+ cron: l,
109
+ timezone: u,
110
+ handler: () => p.gen(function* () {
111
+ if (!(yield* s.checkConsistency(m("economy.consistency.schedule"), o)).matches) return yield* p.fail(t("conflict"));
112
+ })
113
+ });
114
+ };
115
+ export { v as captureEconomyConsistencyInput, y as checkSqlEconomyConsistency, b as economyConsistencySchedule };