@zeniai/client-epic-state 5.0.82-betaAK4 → 5.0.82-betaAK6

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.
@@ -3,9 +3,50 @@ import { reduceAllFetchState } from '../../../commonStateTypes/reduceFetchState'
3
3
  import { getAllFundingAccounts, } from '../commonSetup/setupViewSelector';
4
4
  import { getAllDepositAccounts } from '../zeniAccounts/depositAccountList/depositAccountListSelector';
5
5
  import { getAllPaymentAccounts } from '../zeniAccounts/paymentAccountList/paymentAccountListSelector';
6
+ /**
7
+ * Lifts an `AutoSweepFlowState` into an `AutoTransferRule` so the auto-sweep
8
+ * config surfaces alongside the user's other auto-transfer rules.
9
+ *
10
+ * Mapping notes:
11
+ * - `autoTransferRuleId` reuses `settingsId` from the cash-management
12
+ * settings record. (Different system, but it's the closest stable id we
13
+ * have. Returns `undefined` when no settings have been saved yet.)
14
+ * - `'biweekly'` collapses to `'weekly'` — `AutoTransferRuleFrequency`
15
+ * doesn't model biweekly.
16
+ * - `ruleType` is fixed to `'cash_management'` so the rules-list UI can
17
+ * special-case the row (icon, title, action menu) without confusing it
18
+ * for a regular recurring rule.
19
+ * - Destination is the singleton treasury account, so the id is `null` and
20
+ * the type is `'treasury_account'`.
21
+ */
22
+ export const mapAutoSweepFlowStateToAutoTransferRule = (state) => {
23
+ if (state.settingsId == null) {
24
+ return undefined;
25
+ }
26
+ return {
27
+ amount: state.bufferAmount.amount,
28
+ autoTransferRuleId: state.settingsId,
29
+ currencyCode: state.bufferAmount.currencyCode,
30
+ currencySymbol: state.bufferAmount.currencySymbol,
31
+ destinationBankAccountId: null,
32
+ destinationBankAccountType: 'treasury_account',
33
+ frequency: state.frequency === 'biweekly' ? 'weekly' : state.frequency,
34
+ isActive: true,
35
+ isDeleted: false,
36
+ lastRunTime: null,
37
+ ruleType: 'cash_management',
38
+ sourceBankAccountId: state.primaryFundingAccountId ?? '',
39
+ sourceBankAccountType: 'deposit_account',
40
+ };
41
+ };
6
42
  export const getAutoTransferRules = (state) => {
7
- const { autotransferRulesState, depositAccountState, paymentAccountState, depositAccountListState, paymentAccountListState, treasuryDetailState, } = state;
43
+ const { autotransferRulesState, autoSweepFlowState, depositAccountState, paymentAccountState, depositAccountListState, paymentAccountListState, treasuryDetailState, } = state;
8
44
  const { rules, error, historyByRuleId, ruleUpdateLocalData, ruleUpdateStatusById, ruleDeleteStatusById, createRuleFetchState, autoTransferReviewDetail, } = autotransferRulesState;
45
+ // Surface the auto-sweep config as an additional rule alongside the
46
+ // server-returned rules. Only present when a `settingsId` has been
47
+ // assigned (i.e. the user has saved auto-sweep at least once).
48
+ const autoSweepRule = mapAutoSweepFlowStateToAutoTransferRule(autoSweepFlowState);
49
+ const mergedRules = autoSweepRule != null ? [...rules, autoSweepRule] : rules;
9
50
  const depositAccountsView = getAllDepositAccounts(depositAccountState, depositAccountListState);
10
51
  const paymentAccountsView = getAllPaymentAccounts(paymentAccountState, paymentAccountListState);
11
52
  let transferMoneyParties = [];
@@ -30,7 +71,7 @@ export const getAutoTransferRules = (state) => {
30
71
  };
31
72
  return {
32
73
  autoTransferReviewDetail,
33
- rules,
74
+ rules: mergedRules,
34
75
  ruleUpdateLocalData,
35
76
  ruleUpdateStatusById,
36
77
  ruleDeleteStatusById,
@@ -3,16 +3,20 @@ import { catchError, filter, mergeMap, switchMap } from 'rxjs/operators';
3
3
  import { createZeniAPIStatus, isSuccessResponse, } from '../../../../../responsePayload';
4
4
  import { toSaveAutoSweepSettingsBody, } from '../autoSweepFlowPayload';
5
5
  import { saveAutoSweepSettings, updateAutoSweepSettingsFetchStatus, } from '../autoSweepFlowReducer';
6
+ const SETTINGS_ENDPOINT = 'https://dev.api.zeni.ai/cash-management-agent/1.0/cash-management/settings';
6
7
  export const saveAutoSweepSettingsEpic = (actions$, state$, zeniAPI) => actions$.pipe(filter(saveAutoSweepSettings.match), switchMap(() => {
7
- const { bufferAmount, frequency, memo } = state$.value.autoSweepFlowState;
8
+ const { bufferAmount, frequency, memo, settingsId } = state$.value.autoSweepFlowState;
8
9
  const payload = toSaveAutoSweepSettingsBody({
9
10
  bufferAmount,
10
11
  frequency,
11
12
  memo,
12
13
  });
13
- return zeniAPI
14
- .putAndGetJSON(`https://dev.api.zeni.ai/cash-management-agent/1.0/cash-management/settings`, payload)
15
- .pipe(mergeMap((response) => {
14
+ // First-time setup (no settings id from the GET yet) → POST a new
15
+ // record. Subsequent edits update the existing one via PUT.
16
+ const request$ = settingsId == null
17
+ ? zeniAPI.postAndGetJSON(SETTINGS_ENDPOINT, payload)
18
+ : zeniAPI.putAndGetJSON(SETTINGS_ENDPOINT, payload);
19
+ return request$.pipe(mergeMap((response) => {
16
20
  if (isSuccessResponse(response)) {
17
21
  return of(updateAutoSweepSettingsFetchStatus({ fetchState: 'Completed' }));
18
22
  }
@@ -92,25 +92,27 @@ export const getCashManagementOverview = (state) => {
92
92
  const buildProjectionData = (cashBalance, inflowEvents, outflowEvents) => {
93
93
  const events = [
94
94
  ...inflowEvents.map((e) => ({
95
+ amount: e.amount,
95
96
  date: e.paymentDate,
96
- delta: e.amount.amount,
97
97
  isInflow: true,
98
98
  })),
99
99
  ...outflowEvents.map((e) => ({
100
+ amount: e.amount,
100
101
  date: e.paymentDate,
101
- delta: -e.amount.amount,
102
- isOutflow: true,
102
+ isInflow: false,
103
103
  })),
104
104
  ].sort((a, b) => a.date.valueOf() - b.date.valueOf());
105
- let balance = cashBalance.amount;
105
+ let balance = cashBalance;
106
106
  const points = [{ balance, date: 'Today' }];
107
107
  events.forEach((event) => {
108
- balance += event.delta;
108
+ balance = event.isInflow
109
+ ? addAmounts(balance, event.amount)
110
+ : subtractAmounts(balance, event.amount);
109
111
  points.push({
110
112
  balance,
111
113
  date: event.date.format('MMM D'),
112
- isInflow: event.isInflow,
113
- isOutflow: event.isOutflow,
114
+ isInflow: event.isInflow ? true : undefined,
115
+ isOutflow: event.isInflow ? undefined : true,
114
116
  });
115
117
  });
116
118
  return points;
@@ -4,7 +4,7 @@ import { ZeniDate } from '../../../zeniDayJS';
4
4
  import { FundingAccount } from '../commonSetup/setupViewSelector';
5
5
  import { TreasuryAccount } from '../treasury/treasuryTransferMoney/treasuryTransferMoneyState';
6
6
  import { AutoTransferRule } from './autoTransferRulesState';
7
- export type AutoTransferRuleType = 'minimum_balance' | 'recurring' | 'percentage_of_balance';
7
+ export type AutoTransferRuleType = 'minimum_balance' | 'recurring' | 'percentage_of_balance' | 'cash_management';
8
8
  export type AutoTransferRuleFrequency = 'daily' | 'weekly' | 'monthly';
9
9
  export interface DestinationBankAccountMetadataPayload {
10
10
  account_id: string;
@@ -1,12 +1,30 @@
1
1
  import { FetchState, FetchStateAndError, ID } from '../../../commonStateTypes/common';
2
2
  import { SelectorView } from '../../../commonStateTypes/viewAndReport/viewAndReport';
3
3
  import { RootState } from '../../../reducer';
4
+ import { AutoSweepFlowState } from '../cashManagement/autoSweepFlow/autoSweepFlowState';
4
5
  import { CommonHistoryView } from '../commonHistoryView/commonHistory';
5
6
  import { FundingAccount } from '../commonSetup/setupViewSelector';
6
7
  import { TreasuryAccount } from '../treasury/treasuryTransferMoney/treasuryTransferMoneyState';
7
8
  import { ReviewTransferDetail } from '../zeniAccounts/transferDetail/transferDetailState';
8
9
  import { AutoTransferRuleLocalData } from './autoTransferRulesPayload';
9
10
  import { AutoTransferRule } from './autoTransferRulesState';
11
+ /**
12
+ * Lifts an `AutoSweepFlowState` into an `AutoTransferRule` so the auto-sweep
13
+ * config surfaces alongside the user's other auto-transfer rules.
14
+ *
15
+ * Mapping notes:
16
+ * - `autoTransferRuleId` reuses `settingsId` from the cash-management
17
+ * settings record. (Different system, but it's the closest stable id we
18
+ * have. Returns `undefined` when no settings have been saved yet.)
19
+ * - `'biweekly'` collapses to `'weekly'` — `AutoTransferRuleFrequency`
20
+ * doesn't model biweekly.
21
+ * - `ruleType` is fixed to `'cash_management'` so the rules-list UI can
22
+ * special-case the row (icon, title, action menu) without confusing it
23
+ * for a regular recurring rule.
24
+ * - Destination is the singleton treasury account, so the id is `null` and
25
+ * the type is `'treasury_account'`.
26
+ */
27
+ export declare const mapAutoSweepFlowStateToAutoTransferRule: (state: AutoSweepFlowState) => AutoTransferRule | undefined;
10
28
  export interface AutoTransferRulesSelectorView extends SelectorView {
11
29
  autoTransferReviewDetail: ReviewTransferDetail;
12
30
  createRuleFetchState: FetchStateAndError;
@@ -3,15 +3,57 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAutoTransferRuleHistory = exports.getAutoTransferRuleById = exports.getAutoTransferRules = void 0;
6
+ exports.getAutoTransferRuleHistory = exports.getAutoTransferRuleById = exports.getAutoTransferRules = exports.mapAutoSweepFlowStateToAutoTransferRule = void 0;
7
7
  const get_1 = __importDefault(require("lodash/get"));
8
8
  const reduceFetchState_1 = require("../../../commonStateTypes/reduceFetchState");
9
9
  const setupViewSelector_1 = require("../commonSetup/setupViewSelector");
10
10
  const depositAccountListSelector_1 = require("../zeniAccounts/depositAccountList/depositAccountListSelector");
11
11
  const paymentAccountListSelector_1 = require("../zeniAccounts/paymentAccountList/paymentAccountListSelector");
12
+ /**
13
+ * Lifts an `AutoSweepFlowState` into an `AutoTransferRule` so the auto-sweep
14
+ * config surfaces alongside the user's other auto-transfer rules.
15
+ *
16
+ * Mapping notes:
17
+ * - `autoTransferRuleId` reuses `settingsId` from the cash-management
18
+ * settings record. (Different system, but it's the closest stable id we
19
+ * have. Returns `undefined` when no settings have been saved yet.)
20
+ * - `'biweekly'` collapses to `'weekly'` — `AutoTransferRuleFrequency`
21
+ * doesn't model biweekly.
22
+ * - `ruleType` is fixed to `'cash_management'` so the rules-list UI can
23
+ * special-case the row (icon, title, action menu) without confusing it
24
+ * for a regular recurring rule.
25
+ * - Destination is the singleton treasury account, so the id is `null` and
26
+ * the type is `'treasury_account'`.
27
+ */
28
+ const mapAutoSweepFlowStateToAutoTransferRule = (state) => {
29
+ if (state.settingsId == null) {
30
+ return undefined;
31
+ }
32
+ return {
33
+ amount: state.bufferAmount.amount,
34
+ autoTransferRuleId: state.settingsId,
35
+ currencyCode: state.bufferAmount.currencyCode,
36
+ currencySymbol: state.bufferAmount.currencySymbol,
37
+ destinationBankAccountId: null,
38
+ destinationBankAccountType: 'treasury_account',
39
+ frequency: state.frequency === 'biweekly' ? 'weekly' : state.frequency,
40
+ isActive: true,
41
+ isDeleted: false,
42
+ lastRunTime: null,
43
+ ruleType: 'cash_management',
44
+ sourceBankAccountId: state.primaryFundingAccountId ?? '',
45
+ sourceBankAccountType: 'deposit_account',
46
+ };
47
+ };
48
+ exports.mapAutoSweepFlowStateToAutoTransferRule = mapAutoSweepFlowStateToAutoTransferRule;
12
49
  const getAutoTransferRules = (state) => {
13
- const { autotransferRulesState, depositAccountState, paymentAccountState, depositAccountListState, paymentAccountListState, treasuryDetailState, } = state;
50
+ const { autotransferRulesState, autoSweepFlowState, depositAccountState, paymentAccountState, depositAccountListState, paymentAccountListState, treasuryDetailState, } = state;
14
51
  const { rules, error, historyByRuleId, ruleUpdateLocalData, ruleUpdateStatusById, ruleDeleteStatusById, createRuleFetchState, autoTransferReviewDetail, } = autotransferRulesState;
52
+ // Surface the auto-sweep config as an additional rule alongside the
53
+ // server-returned rules. Only present when a `settingsId` has been
54
+ // assigned (i.e. the user has saved auto-sweep at least once).
55
+ const autoSweepRule = (0, exports.mapAutoSweepFlowStateToAutoTransferRule)(autoSweepFlowState);
56
+ const mergedRules = autoSweepRule != null ? [...rules, autoSweepRule] : rules;
15
57
  const depositAccountsView = (0, depositAccountListSelector_1.getAllDepositAccounts)(depositAccountState, depositAccountListState);
16
58
  const paymentAccountsView = (0, paymentAccountListSelector_1.getAllPaymentAccounts)(paymentAccountState, paymentAccountListState);
17
59
  let transferMoneyParties = [];
@@ -36,7 +78,7 @@ const getAutoTransferRules = (state) => {
36
78
  };
37
79
  return {
38
80
  autoTransferReviewDetail,
39
- rules,
81
+ rules: mergedRules,
40
82
  ruleUpdateLocalData,
41
83
  ruleUpdateStatusById,
42
84
  ruleDeleteStatusById,
@@ -6,16 +6,20 @@ const operators_1 = require("rxjs/operators");
6
6
  const responsePayload_1 = require("../../../../../responsePayload");
7
7
  const autoSweepFlowPayload_1 = require("../autoSweepFlowPayload");
8
8
  const autoSweepFlowReducer_1 = require("../autoSweepFlowReducer");
9
+ const SETTINGS_ENDPOINT = 'https://dev.api.zeni.ai/cash-management-agent/1.0/cash-management/settings';
9
10
  const saveAutoSweepSettingsEpic = (actions$, state$, zeniAPI) => actions$.pipe((0, operators_1.filter)(autoSweepFlowReducer_1.saveAutoSweepSettings.match), (0, operators_1.switchMap)(() => {
10
- const { bufferAmount, frequency, memo } = state$.value.autoSweepFlowState;
11
+ const { bufferAmount, frequency, memo, settingsId } = state$.value.autoSweepFlowState;
11
12
  const payload = (0, autoSweepFlowPayload_1.toSaveAutoSweepSettingsBody)({
12
13
  bufferAmount,
13
14
  frequency,
14
15
  memo,
15
16
  });
16
- return zeniAPI
17
- .putAndGetJSON(`https://dev.api.zeni.ai/cash-management-agent/1.0/cash-management/settings`, payload)
18
- .pipe((0, operators_1.mergeMap)((response) => {
17
+ // First-time setup (no settings id from the GET yet) → POST a new
18
+ // record. Subsequent edits update the existing one via PUT.
19
+ const request$ = settingsId == null
20
+ ? zeniAPI.postAndGetJSON(SETTINGS_ENDPOINT, payload)
21
+ : zeniAPI.putAndGetJSON(SETTINGS_ENDPOINT, payload);
22
+ return request$.pipe((0, operators_1.mergeMap)((response) => {
19
23
  if ((0, responsePayload_1.isSuccessResponse)(response)) {
20
24
  return (0, rxjs_1.of)((0, autoSweepFlowReducer_1.updateAutoSweepSettingsFetchStatus)({ fetchState: 'Completed' }));
21
25
  }
@@ -6,14 +6,8 @@ import { CashManagementMovement, UpcomingPaymentEvent } from './cashManagementOv
6
6
  export interface CashManagementOverviewBannerSelectorView extends SelectorView, CashManagementMovement {
7
7
  cashManagementSettingsId?: ID;
8
8
  }
9
- /**
10
- * One point on the cash-balance projection line. The first point is today's
11
- * `cashBalance`; each subsequent point is the running balance after applying
12
- * the next inflow / outflow event (in chronological order). The `isInflow` /
13
- * `isOutflow` flags drive the colored dot markers in the chart.
14
- */
15
9
  export interface CashProjectionPoint {
16
- balance: number;
10
+ balance: Amount;
17
11
  /** Pre-formatted display label (e.g. `"Today"`, `"May 27"`). */
18
12
  date: string;
19
13
  isInflow?: boolean;
@@ -97,25 +97,27 @@ exports.getCashManagementOverview = getCashManagementOverview;
97
97
  const buildProjectionData = (cashBalance, inflowEvents, outflowEvents) => {
98
98
  const events = [
99
99
  ...inflowEvents.map((e) => ({
100
+ amount: e.amount,
100
101
  date: e.paymentDate,
101
- delta: e.amount.amount,
102
102
  isInflow: true,
103
103
  })),
104
104
  ...outflowEvents.map((e) => ({
105
+ amount: e.amount,
105
106
  date: e.paymentDate,
106
- delta: -e.amount.amount,
107
- isOutflow: true,
107
+ isInflow: false,
108
108
  })),
109
109
  ].sort((a, b) => a.date.valueOf() - b.date.valueOf());
110
- let balance = cashBalance.amount;
110
+ let balance = cashBalance;
111
111
  const points = [{ balance, date: 'Today' }];
112
112
  events.forEach((event) => {
113
- balance += event.delta;
113
+ balance = event.isInflow
114
+ ? addAmounts(balance, event.amount)
115
+ : subtractAmounts(balance, event.amount);
114
116
  points.push({
115
117
  balance,
116
118
  date: event.date.format('MMM D'),
117
- isInflow: event.isInflow,
118
- isOutflow: event.isOutflow,
119
+ isInflow: event.isInflow ? true : undefined,
120
+ isOutflow: event.isInflow ? undefined : true,
119
121
  });
120
122
  });
121
123
  return points;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeniai/client-epic-state",
3
- "version": "5.0.82-betaAK4",
3
+ "version": "5.0.82-betaAK6",
4
4
  "description": "Shared module between Web & Mobile containing required abstractions for state management, async network communication. ",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/esm/index.js",