@fabriktor/fx 0.0.131 → 0.0.133

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.
@@ -1,4 +1,4 @@
1
- import { PaymentEntityTypeBill, PaymentEntityTypePayroll, type Bill, type Payroll, PSPPaymentStatusPending, PSPPaymentStatusPartial, PSPPaymentStatusFull, type CreditLedgerEntry, type Payment, type PmtAccount, type PmtBankAccount, type PmtCard, type PmtCustomer, type PmtPublicSession, type PmtRefund } from "@fabriktor/schema";
1
+ import { PaymentEntityTypeBill, PaymentEntityTypePayroll, type Bill, type Payroll, PSPPaymentStatusPending, PSPPaymentStatusPartial, PSPPaymentStatusFull, type CreditLedgerEntry, type Payment, type PmtAccount, type PmtBankAccount, type PmtCard, type PmtCustomer, type PmtPublicSession, type PmtRefund, type SortStripeBalanceResult } from "@fabriktor/schema";
2
2
  import { type PaymentsOperator, type BillableOperator, type PayrollsOperator } from "@fabriktor/client";
3
3
  import { type SearchOptions, type OwnerSearchOptions, type SearchPage } from "../core/search.js";
4
4
  import { EventBus } from "../core/events.js";
@@ -69,6 +69,7 @@ export type PayableEntityBalance = EntityPayments & {
69
69
  export type SearchPayableEntitiesOptions = SearchOptions & {
70
70
  owner_id: string;
71
71
  type: PayableEntityType;
72
+ include_pending?: boolean;
72
73
  };
73
74
  export type ResolvePayableEntityOptions = {
74
75
  owner_id: string;
@@ -149,6 +150,11 @@ export type GetPmtAccountAccessOptions = {
149
150
  owner_id: string;
150
151
  signal?: AbortSignal;
151
152
  };
153
+ export type GetPayoutBalanceOptions = GetPmtAccountAccessOptions;
154
+ export type PayoutBalance = {
155
+ access: PmtAccountAccess;
156
+ balance: SortStripeBalanceResult | null;
157
+ };
152
158
  export declare function isPmtAccountReady(account: PmtAccount | null): boolean;
153
159
  export declare function currentPmtAccount(accounts: readonly PmtAccount[]): PmtAccount | null;
154
160
  export declare function pmtAccountAccess(account: PmtAccount | null): PmtAccountAccess;
@@ -200,6 +206,7 @@ export interface PaymentsFXOperator {
200
206
  replacePmtAccount(opts: ReplacePmtAccountOptions): Promise<PmtAccount>;
201
207
  searchPmtAccounts(opts: SearchPmtAccountsOptions): Promise<SearchPmtAccountsPage>;
202
208
  getPmtAccountAccess(opts: GetPmtAccountAccessOptions): Promise<PmtAccountAccess>;
209
+ getPayoutBalance(opts: GetPayoutBalanceOptions): Promise<PayoutBalance>;
203
210
  createPmtBankAccount(opts: CreatePmtBankAccountOptions): Promise<PmtBankAccount>;
204
211
  replacePmtBankAccount(opts: ReplacePmtBankAccountOptions): Promise<PmtBankAccount>;
205
212
  searchPmtBankAccounts(opts: SearchPmtBankAccountsOptions): Promise<SearchPmtBankAccountsPage>;
@@ -247,6 +254,7 @@ export declare class PaymentsFX implements PaymentsFXOperator {
247
254
  replacePmtAccount(opts: ReplacePmtAccountOptions): Promise<PmtAccount>;
248
255
  searchPmtAccounts(opts: SearchPmtAccountsOptions): Promise<SearchPmtAccountsPage>;
249
256
  getPmtAccountAccess(opts: GetPmtAccountAccessOptions): Promise<PmtAccountAccess>;
257
+ getPayoutBalance(opts: GetPayoutBalanceOptions): Promise<PayoutBalance>;
250
258
  createPmtBankAccount(opts: CreatePmtBankAccountOptions): Promise<PmtBankAccount>;
251
259
  replacePmtBankAccount(opts: ReplacePmtBankAccountOptions): Promise<PmtBankAccount>;
252
260
  searchPmtBankAccounts(opts: SearchPmtBankAccountsOptions): Promise<SearchPmtBankAccountsPage>;
@@ -14,6 +14,7 @@ const publicAmountPattern = /^\d+$/;
14
14
  const resourcePayment = "payment";
15
15
  const resourcePmtAccount = "pmt_account";
16
16
  const resourcePmtAccountOnboarding = "pmt_account_onboarding";
17
+ const resourcePayoutBalance = "payout_balance";
17
18
  const resourcePmtBankAccount = "pmt_bank_account";
18
19
  const resourcePmtCard = "pmt_card";
19
20
  const resourcePmtRefund = "pmt_refund";
@@ -37,6 +38,7 @@ const msgCreatedPaymentIDMissing = "Created payment response did not contain an
37
38
  const msgPaymentMissing = "Payment could not be resolved after the mutation.";
38
39
  const msgCreatedPmtAccountIDMissing = "Created payment account response did not contain an ID.";
39
40
  const msgPmtAccountMissing = "Payment account could not be resolved after the mutation.";
41
+ const msgPayoutBalanceMissing = "Payout balance response is incomplete.";
40
42
  const msgPmtAccountOnboardingIDMissing = "Payment account onboarding requires a valid account ID.";
41
43
  const msgPmtAccountOnboardingLinkMissing = "Payment account onboarding did not return a link.";
42
44
  const msgCreatedPmtBankAccountIDMissing = "Created payment bank account response did not contain an ID.";
@@ -283,7 +285,8 @@ export class PaymentsFX {
283
285
  entities,
284
286
  signal: opts.signal,
285
287
  });
286
- const results = balances.flatMap((balance, index) => balance.remaining_amount > 0 && !balance.has_pending
288
+ const results = balances.flatMap((balance, index) => balance.remaining_amount > 0 &&
289
+ (opts.include_pending || !balance.has_pending)
287
290
  ? [{ ...balance, entity: entities[index] }]
288
291
  : []);
289
292
  if (results.length || source.done) {
@@ -502,6 +505,31 @@ export class PaymentsFX {
502
505
  page = out.next_page;
503
506
  }
504
507
  }
508
+ async getPayoutBalance(opts) {
509
+ const access = await this.getPmtAccountAccess(opts);
510
+ opts.signal?.throwIfAborted();
511
+ const accountID = resolvedID(access.account?.psp_id);
512
+ if (accountID === undefined) {
513
+ return { access, balance: null };
514
+ }
515
+ // Outstanding funds remain visible when the account needs updated onboarding.
516
+ const out = await this.payments.balance({ psp_account_id: accountID }, { signal: opts.signal });
517
+ opts.signal?.throwIfAborted();
518
+ if (!out?.value) {
519
+ throw errResolveFailed({
520
+ resource: resourcePayoutBalance,
521
+ msg: msgPayoutBalanceMissing,
522
+ });
523
+ }
524
+ return {
525
+ access,
526
+ balance: {
527
+ available: out.value.available ?? [],
528
+ pending: out.value.pending ?? [],
529
+ instant_available: out.value.instant_available ?? [],
530
+ },
531
+ };
532
+ }
505
533
  async createPmtBankAccount(opts) {
506
534
  const out = await this.payments.insertOnePmtBankAccount(opts);
507
535
  const id = requiredOperationID(out, msgCreatedPmtBankAccountIDMissing);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.131",
3
+ "version": "0.0.133",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.92",
20
+ "@fabriktor/client": "0.0.93",
21
21
  "@fabriktor/fx": "0.0.94",
22
22
  "@fabriktor/schema": "0.0.66"
23
23
  }