@zeniai/client-epic-state 5.2.54-betaRD1 → 5.2.55
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.
- package/lib/entity/billPay/billTransaction/billTransactionReducer.d.ts +5 -1
- package/lib/entity/billPay/billTransaction/billTransactionReducer.js +17 -2
- package/lib/entity/invoicing/plan/planSelector.d.ts +9 -0
- package/lib/entity/invoicing/plan/planSelector.js +3 -2
- package/lib/entity/invoicing/product/productPayload.d.ts +1 -1
- package/lib/entity/invoicing/product/productPayload.js +1 -1
- package/lib/entity/invoicing/product/productState.d.ts +10 -0
- package/lib/entity/jeSchedules/jeSchedulesReducer.d.ts +1 -4
- package/lib/entity/jeSchedules/jeSchedulesReducer.js +5 -79
- package/lib/epic.d.ts +3 -2
- package/lib/epic.js +8 -6
- package/lib/esm/entity/billPay/billTransaction/billTransactionReducer.js +16 -1
- package/lib/esm/entity/invoicing/plan/planSelector.js +1 -1
- package/lib/esm/entity/invoicing/product/productPayload.js +1 -1
- package/lib/esm/entity/jeSchedules/jeSchedulesReducer.js +4 -78
- package/lib/esm/epic.js +8 -6
- package/lib/esm/index.js +5 -5
- package/lib/esm/view/expenseAutomationView/epics/jeSchedule/fetchJeSchedulePageEpic.js +1 -14
- package/lib/esm/view/expenseAutomationView/epics/jeSchedule/fetchJeSchedulesEpic.js +13 -10
- package/lib/esm/view/expenseAutomationView/epics/jeSchedule/initializeJeScheduleLocalDataEpic.js +17 -38
- package/lib/esm/view/expenseAutomationView/epics/jeSchedule/retryJeSchedulesEpic.js +1 -17
- package/lib/esm/view/expenseAutomationView/reducers/jeSchedulesViewReducer.js +1 -74
- package/lib/esm/view/expenseAutomationView/selectors/jeSchedulesViewSelector.js +14 -49
- package/lib/esm/view/invoicing/createInvoice/createInvoiceSelector.js +28 -1
- package/lib/esm/view/invoicing/editInvoicingCatalogItemDetailView/invoicingCatalogItemFormHelper.js +1 -7
- package/lib/esm/view/spendManagement/billPay/billDetailView/billDetailViewReducer.js +87 -1
- package/lib/esm/view/spendManagement/billPay/billDetailView/billDetailViewSelector.js +1 -0
- package/lib/esm/view/spendManagement/billPay/billDetailView/billDetailViewState.js +4 -0
- package/lib/esm/view/spendManagement/billPay/billDetailView/epics/fetchBillRecommendationsEpic.js +97 -0
- package/lib/esm/view/spendManagement/billPay/billDetailView/epics/fetchRecommendationsForBill.js +85 -0
- package/lib/esm/view/spendManagement/billPay/editBillView/editBillViewReducer.js +18 -1
- package/lib/esm/view/spendManagement/billPay/editBillView/editBillViewSelector.js +3 -0
- package/lib/esm/view/spendManagement/billPay/editBillView/epics/applyBillRecommendationsToLocalStoreEpic.js +95 -0
- package/lib/index.d.ts +5 -5
- package/lib/index.js +22 -22
- package/lib/view/expenseAutomationView/epics/jeSchedule/fetchJeSchedulePageEpic.d.ts +2 -2
- package/lib/view/expenseAutomationView/epics/jeSchedule/fetchJeSchedulePageEpic.js +0 -13
- package/lib/view/expenseAutomationView/epics/jeSchedule/fetchJeSchedulesEpic.js +11 -8
- package/lib/view/expenseAutomationView/epics/jeSchedule/initializeJeScheduleLocalDataEpic.js +17 -38
- package/lib/view/expenseAutomationView/epics/jeSchedule/retryJeSchedulesEpic.d.ts +2 -2
- package/lib/view/expenseAutomationView/epics/jeSchedule/retryJeSchedulesEpic.js +1 -17
- package/lib/view/expenseAutomationView/payload/jeSchedulesPayload.d.ts +0 -18
- package/lib/view/expenseAutomationView/reducers/jeSchedulesViewReducer.d.ts +3 -10
- package/lib/view/expenseAutomationView/reducers/jeSchedulesViewReducer.js +3 -76
- package/lib/view/expenseAutomationView/selectorTypes/jeSchedulesSelectorTypes.d.ts +0 -3
- package/lib/view/expenseAutomationView/selectors/jeSchedulesViewSelector.js +14 -49
- package/lib/view/expenseAutomationView/types/jeSchedulesViewState.d.ts +0 -16
- package/lib/view/invoicing/createInvoice/createInvoiceSelector.d.ts +7 -2
- package/lib/view/invoicing/createInvoice/createInvoiceSelector.js +28 -1
- package/lib/view/invoicing/editInvoicingCatalogItemDetailView/invoicingCatalogItemFormHelper.d.ts +2 -2
- package/lib/view/invoicing/editInvoicingCatalogItemDetailView/invoicingCatalogItemFormHelper.js +1 -7
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewPayload.d.ts +19 -0
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewReducer.d.ts +10 -1
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewReducer.js +88 -2
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewSelector.d.ts +1 -0
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewSelector.js +1 -0
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewState.d.ts +7 -0
- package/lib/view/spendManagement/billPay/billDetailView/billDetailViewState.js +4 -0
- package/lib/view/spendManagement/billPay/billDetailView/epics/fetchBillRecommendationsEpic.d.ts +37 -0
- package/lib/view/spendManagement/billPay/billDetailView/epics/fetchBillRecommendationsEpic.js +104 -0
- package/lib/view/spendManagement/billPay/billDetailView/epics/fetchRecommendationsForBill.d.ts +24 -0
- package/lib/view/spendManagement/billPay/billDetailView/epics/fetchRecommendationsForBill.js +92 -0
- package/lib/view/spendManagement/billPay/editBillView/editBillViewReducer.d.ts +4 -1
- package/lib/view/spendManagement/billPay/editBillView/editBillViewReducer.js +19 -2
- package/lib/view/spendManagement/billPay/editBillView/editBillViewSelector.d.ts +1 -0
- package/lib/view/spendManagement/billPay/editBillView/editBillViewSelector.js +3 -0
- package/lib/view/spendManagement/billPay/editBillView/epics/applyBillRecommendationsToLocalStoreEpic.d.ts +18 -0
- package/lib/view/spendManagement/billPay/editBillView/epics/applyBillRecommendationsToLocalStoreEpic.js +102 -0
- package/package.json +1 -1
- package/lib/esm/view/expenseAutomationView/epics/jeSchedule/fetchFailedJePostingsEpic.js +0 -39
- package/lib/tsconfig.mocks.tsbuildinfo +0 -1
- package/lib/tsconfig.typecheck.tsbuildinfo +0 -1
- package/lib/view/expenseAutomationView/epics/jeSchedule/fetchFailedJePostingsEpic.d.ts +0 -20
- package/lib/view/expenseAutomationView/epics/jeSchedule/fetchFailedJePostingsEpic.js +0 -43
|
@@ -210,7 +210,7 @@ export function getExpenseAutomationJESchedulesView(state) {
|
|
|
210
210
|
const accruedSchedules = [];
|
|
211
211
|
const oneTimeAccruals = [];
|
|
212
212
|
const { accountState, accountListState, classState, classListState, expenseAutomationJESchedulesViewState, expenseAutomationViewState, monthEndCloseChecksState, } = state;
|
|
213
|
-
const { jeScheduleTransactionKeysByPeriodAndTab, jeScheduleAccruedKeysByPeriodAndTab, jeScheduleOneTimeAccrualKeysByPeriodAndTab,
|
|
213
|
+
const { jeScheduleTransactionKeysByPeriodAndTab, jeScheduleAccruedKeysByPeriodAndTab, jeScheduleOneTimeAccrualKeysByPeriodAndTab, failedJeScheduleTransactionKeysByPeriodAndTab, jeScheduleLocalDataById, postStatusById, ignoreStatusById, revertStatusById, oneTimeAccrualActionStatusById, uiState, error, fetchState, refreshStatus, newSchedule, } = expenseAutomationJESchedulesViewState;
|
|
214
214
|
const accountSettingsView = getJEAccountSettingsView(state);
|
|
215
215
|
const newScheduleView = newSchedule.selectedDetailScheduleType === 'fixed_assets' ||
|
|
216
216
|
newSchedule.selectedDetailScheduleType === 'prepaid_expenses'
|
|
@@ -249,11 +249,15 @@ export function getExpenseAutomationJESchedulesView(state) {
|
|
|
249
249
|
const jeScheduleTransactionKeysForSelectedTab = selectedPeriodTabKey != null
|
|
250
250
|
? jeScheduleTransactionKeysByPeriodAndTab[selectedPeriodTabKey]
|
|
251
251
|
: undefined;
|
|
252
|
+
const failedJeScheduleTransactionKeysForSelectedTab = selectedPeriodTabKey != null
|
|
253
|
+
? failedJeScheduleTransactionKeysByPeriodAndTab[selectedPeriodTabKey]
|
|
254
|
+
: undefined;
|
|
252
255
|
// Distinguishes "this tab has never been fetched for this period" from "fetched and genuinely
|
|
253
256
|
// empty". Without it the page cannot tell a first visit from a real empty state, and renders an
|
|
254
257
|
// empty table where it should render a skeleton.
|
|
255
258
|
const hasLoadedForSelectedTab = jeScheduleTransactionKeysForSelectedTab != null;
|
|
256
259
|
const jeScheduleTransactionKeys = jeScheduleTransactionKeysForSelectedTab ?? [];
|
|
260
|
+
const failedJeScheduleTransactionKeys = failedJeScheduleTransactionKeysForSelectedTab ?? [];
|
|
257
261
|
const jeScheduleAccruedKeys = (selectedPeriodTabKey != null
|
|
258
262
|
? jeScheduleAccruedKeysByPeriodAndTab[selectedPeriodTabKey]
|
|
259
263
|
: undefined) ?? [];
|
|
@@ -276,57 +280,24 @@ export function getExpenseAutomationJESchedulesView(state) {
|
|
|
276
280
|
accruedSchedules.push(accruedScheduleDetails);
|
|
277
281
|
}
|
|
278
282
|
});
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
failedJePostings.jeScheduleTransactionKeys.forEach((jeScheduleKey) => {
|
|
283
|
-
// getJEScheduledTransactionByJEScheduleTransactionKey throws three different ways: it
|
|
284
|
-
// destructures the result of recordGet without a null check, and it raises explicitly for a
|
|
285
|
-
// missing vendor and for a missing base transaction. Catching rather than pre-checking each
|
|
286
|
-
// one keeps this correct if a fourth is added, and none of the three is recoverable here --
|
|
287
|
-
// the row cannot be drawn either way.
|
|
288
|
-
//
|
|
289
|
-
// This matters more for this bucket than anywhere else on the page. It is tenant-wide, so it
|
|
290
|
-
// names schedules the close-month list never loaded, and the group is defined by deleted
|
|
291
|
-
// vendors and deleted categories -- precisely the rows whose entities are most likely to be
|
|
292
|
-
// missing. Unguarded, one such key takes down the whole JE tab instead of dropping one row.
|
|
293
|
-
try {
|
|
294
|
-
failedJeScheduledTransaction.push(getJEScheduledTransactionByJEScheduleTransactionKey(jeScheduleKey, state));
|
|
295
|
-
}
|
|
296
|
-
catch {
|
|
297
|
-
// Intentionally swallowed: a failed posting we cannot resolve is one missing row, and the
|
|
298
|
-
// alternative is a blank page for every other row too.
|
|
299
|
-
}
|
|
283
|
+
failedJeScheduleTransactionKeys.forEach((jeScheduleKey) => {
|
|
284
|
+
const jeScheduleDetails = getJEScheduledTransactionByJEScheduleTransactionKey(jeScheduleKey, state);
|
|
285
|
+
failedJeScheduledTransaction.push(jeScheduleDetails);
|
|
300
286
|
});
|
|
301
|
-
// What the endpoint last reported as failing. The entity store keeps every month a schedule
|
|
302
|
-
// has and an entry resolved by someone else keeps its old `error` array, so `error` alone would
|
|
303
|
-
// leave a Retry button on a posting that is no longer failing.
|
|
304
|
-
const failedScheduledJournalEntryIDs = new Set(failedJePostings.scheduledJournalEntryIDs);
|
|
305
287
|
const { sortKey, sortOrder, selectedJETypeFilter } = uiState;
|
|
306
|
-
const matchesTypeFilter = (schedule) => selectedJETypeFilter == null ||
|
|
307
|
-
getJEScheduleRowType(schedule) === selectedJETypeFilter;
|
|
308
288
|
const unsortedResolveSchedules = [];
|
|
309
289
|
failedJeScheduledTransaction.forEach((schedule) => {
|
|
310
|
-
|
|
311
|
-
// opening a schedule's detail page or Quick View replaces it with every month the schedule
|
|
312
|
-
// has, posted ones included, so fanning out over all of them turned one failed posting into a
|
|
313
|
-
// row per month -- and a month that never failed has nothing to resolve, so it rendered with
|
|
314
|
-
// an empty action column.
|
|
315
|
-
//
|
|
316
|
-
// Intersected with the ids the endpoint actually returned, not just `error`: the entry array
|
|
317
|
-
// is a cache of every month, and an entry whose failure someone else resolved keeps its stale
|
|
318
|
-
// error array until something overwrites it. Only what the last fetch named is still failing.
|
|
319
|
-
schedule.scheduledJournalEntry
|
|
320
|
-
.filter((journalEntry) => journalEntry.error.length > 0 &&
|
|
321
|
-
failedScheduledJournalEntryIDs.has(journalEntry.scheduledJournalEntryID))
|
|
322
|
-
.forEach((journalEntry) => {
|
|
290
|
+
schedule.scheduledJournalEntry.forEach((journalEntry) => {
|
|
323
291
|
unsortedResolveSchedules.push({
|
|
324
292
|
...schedule,
|
|
325
293
|
scheduledJournalEntry: journalEntry,
|
|
326
294
|
});
|
|
327
295
|
});
|
|
328
296
|
});
|
|
329
|
-
const resolveSchedules = sortJEScheduledTransactions(unsortedResolveSchedules
|
|
297
|
+
const resolveSchedules = sortJEScheduledTransactions(unsortedResolveSchedules, sortKey, sortOrder);
|
|
298
|
+
const validResolveForPending = unsortedResolveSchedules.filter((r) => jeScheduleLocalDataById[r.scheduledJournalEntry.scheduledJournalEntryID] != null);
|
|
299
|
+
const matchesTypeFilter = (schedule) => selectedJETypeFilter == null ||
|
|
300
|
+
getJEScheduleRowType(schedule) === selectedJETypeFilter;
|
|
330
301
|
const isCreatedStatus = (schedule) => {
|
|
331
302
|
// An accrual is "created" once it is not review work: posted, reversing, reversed, or in a
|
|
332
303
|
// transient posting claim.
|
|
@@ -344,6 +315,7 @@ export function getExpenseAutomationJESchedulesView(state) {
|
|
|
344
315
|
];
|
|
345
316
|
const pendingReviewSchedules = sortJEScheduledTransactions([
|
|
346
317
|
...allScheduleRows.filter((schedule) => isPendingReviewRow(schedule) && matchesTypeFilter(schedule)),
|
|
318
|
+
...validResolveForPending,
|
|
347
319
|
], sortKey, sortOrder);
|
|
348
320
|
const createdSchedules = sortJEScheduledTransactions(allScheduleRows.filter((schedule) => isCreatedStatus(schedule) && matchesTypeFilter(schedule)), sortKey, sortOrder);
|
|
349
321
|
// KPI summary — derived from unfiltered schedules so counts are always accurate
|
|
@@ -462,13 +434,6 @@ export function getExpenseAutomationJESchedulesView(state) {
|
|
|
462
434
|
kpiSummary,
|
|
463
435
|
pendingReviewSchedules,
|
|
464
436
|
pendingReviewTotal,
|
|
465
|
-
failedPostings: resolveSchedules,
|
|
466
|
-
// Exposed so a consumer can tell "still loading" and "the lookup failed" from "nothing has
|
|
467
|
-
// failed". The bucket keeps its rows on failure precisely so an error can be shown beside
|
|
468
|
-
// them, which is only possible if the state travels with them.
|
|
469
|
-
failedPostingsFetchState: failedJePostings.fetchState,
|
|
470
|
-
// Deprecated alias of failedPostings. Still read by the section's footer-form effect and by
|
|
471
|
-
// app-ui analytics; remove once both are repointed.
|
|
472
437
|
resolveSchedules: resolveSchedules,
|
|
473
438
|
accountSettingsView,
|
|
474
439
|
newScheduleView,
|
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
import { getAddressByAddressId } from '../../../entity/address/addressSelector';
|
|
2
|
+
import { invoicingPricePointDisplayAmount } from '../../../entity/invoicing/plan/planSelector';
|
|
2
3
|
import { getInvoicingCatalogListView, getInvoicingCatalogPrices, } from '../invoicingCatalogListView/invoicingCatalogListViewSelector';
|
|
3
4
|
import { invoicingFieldHelpForGroup } from '../invoicingConfigView/invoicingConfigViewSelector';
|
|
4
5
|
import { getInvoicingCustomerListView } from '../invoicingCustomerListView/invoicingCustomerListViewSelector';
|
|
5
6
|
import { getInvoicingSettings } from '../settingsView/settingsViewSelector';
|
|
7
|
+
/**
|
|
8
|
+
* Pricing models whose single-unit amount is a true per-unit rate — the only
|
|
9
|
+
* ones a Create Invoice line may be prefilled from. Typed against the entity
|
|
10
|
+
* union so a misspelt member fails to compile; a model added to the union
|
|
11
|
+
* still has to be classified here by hand (it is left out, so it fails closed:
|
|
12
|
+
* no prefill). The complement is `isTieredCatalogPricingModel`.
|
|
13
|
+
*/
|
|
14
|
+
const PER_UNIT_RATE_PRICING_MODELS = [
|
|
15
|
+
'per_unit',
|
|
16
|
+
'flat_fee',
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Whether a price point's single-unit amount is a per-unit rate. A missing
|
|
20
|
+
* model means per-unit: the service defaults `pricing_model` to `per_unit`
|
|
21
|
+
* and older records may omit it.
|
|
22
|
+
*/
|
|
23
|
+
const hasPerUnitRate = (pricingModel) => pricingModel == null ||
|
|
24
|
+
pricingModel === '' ||
|
|
25
|
+
PER_UNIT_RATE_PRICING_MODELS.some((model) => model === pricingModel);
|
|
26
|
+
/**
|
|
27
|
+
* The amount to prefill on a Create Invoice line for a price point, or
|
|
28
|
+
* `undefined` when there is no safe per-unit rate to prefill.
|
|
29
|
+
*/
|
|
30
|
+
const planNamePrefillPrice = (pricePoint) => hasPerUnitRate(pricePoint.pricingModel)
|
|
31
|
+
? invoicingPricePointDisplayAmount(pricePoint)?.amount
|
|
32
|
+
: undefined;
|
|
6
33
|
/**
|
|
7
34
|
* Display name for a customer option: company, else the full name, else the
|
|
8
35
|
* email, else the raw id.
|
|
@@ -47,7 +74,7 @@ export const getCreateInvoiceFormView = (state) => {
|
|
|
47
74
|
.filter((subFamily) => (subFamily.name ?? '') !== '')
|
|
48
75
|
.map((subFamily) => ({
|
|
49
76
|
planName: plan.name ?? '',
|
|
50
|
-
price: subFamily
|
|
77
|
+
price: planNamePrefillPrice(subFamily),
|
|
51
78
|
subFamilyName: subFamily.name ?? '',
|
|
52
79
|
}))),
|
|
53
80
|
products: getInvoicingCatalogPrices(catalogListView.products),
|
package/lib/esm/view/invoicing/editInvoicingCatalogItemDetailView/invoicingCatalogItemFormHelper.js
CHANGED
|
@@ -3,13 +3,7 @@
|
|
|
3
3
|
* for pricing model, tier type, proration or applicability yet, so these stay
|
|
4
4
|
* here until it does. Everything else on this form comes from `/1.0/config`.
|
|
5
5
|
*/
|
|
6
|
-
export const INVOICING_CATALOG_PRICING_MODEL_VALUES = [
|
|
7
|
-
'flat_fee',
|
|
8
|
-
'per_unit',
|
|
9
|
-
'tiered',
|
|
10
|
-
'volume',
|
|
11
|
-
'stairstep',
|
|
12
|
-
];
|
|
6
|
+
export const INVOICING_CATALOG_PRICING_MODEL_VALUES = ['flat_fee', 'per_unit', 'tiered', 'volume', 'stairstep'];
|
|
13
7
|
export const INVOICING_CATALOG_TIER_TYPE_VALUES = [
|
|
14
8
|
'per_unit',
|
|
15
9
|
'flat_fee',
|
|
@@ -18,6 +18,12 @@ const billDetailView = createSlice({
|
|
|
18
18
|
detail.fetchState !== 'In-Progress') {
|
|
19
19
|
draft.billDetailById[key] = {
|
|
20
20
|
...initialBillDetailView,
|
|
21
|
+
// Deliberately independent of the detail fetch: recommendations
|
|
22
|
+
// are requested separately and must not be reset by a detail
|
|
23
|
+
// refresh, which on the edit route lands right after the
|
|
24
|
+
// recommendations request has already started.
|
|
25
|
+
recommendationsFetchStatus: detail?.recommendationsFetchStatus ??
|
|
26
|
+
initialBillDetailView.recommendationsFetchStatus,
|
|
21
27
|
};
|
|
22
28
|
}
|
|
23
29
|
},
|
|
@@ -69,6 +75,86 @@ const billDetailView = createSlice({
|
|
|
69
75
|
};
|
|
70
76
|
},
|
|
71
77
|
},
|
|
78
|
+
fetchBillRecommendations: {
|
|
79
|
+
reducer(draft, action) {
|
|
80
|
+
const key = getBillTransactionDetailKey(action.payload.billId);
|
|
81
|
+
// The edit route can mount before the bill detail entry exists — its
|
|
82
|
+
// screen dispatches this before the parent requests the detail. Seed
|
|
83
|
+
// the entry rather than bailing, or the status would sit at
|
|
84
|
+
// `Not-Started` for the whole wait and round trip.
|
|
85
|
+
const detail = recordGet(draft.billDetailById, key, undefined) ?? {
|
|
86
|
+
...initialBillDetailView,
|
|
87
|
+
billId: action.payload.billId,
|
|
88
|
+
};
|
|
89
|
+
const { fetchState } = detail.recommendationsFetchStatus;
|
|
90
|
+
// Both screens dispatch once per bill — on mount, keyed by bill id,
|
|
91
|
+
// not on every render — so this guard is not what prevents a render
|
|
92
|
+
// loop; the epic's per-bill `exhaustMap` is what collapses the two
|
|
93
|
+
// sites' dispatches into one request.
|
|
94
|
+
//
|
|
95
|
+
// `Error` is deliberately allowed through: a bill whose fetch failed
|
|
96
|
+
// must be able to try again when the screen is next opened.
|
|
97
|
+
if (action.payload.cacheOverride === true ||
|
|
98
|
+
(fetchState !== 'In-Progress' && fetchState !== 'Completed')) {
|
|
99
|
+
draft.billDetailById[key] = {
|
|
100
|
+
...detail,
|
|
101
|
+
recommendationsFetchStatus: {
|
|
102
|
+
fetchState: 'In-Progress',
|
|
103
|
+
error: undefined,
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
prepare(billId, cacheOverride = false) {
|
|
109
|
+
return {
|
|
110
|
+
payload: { billId, cacheOverride },
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
fetchBillRecommendationsSuccess: {
|
|
115
|
+
reducer(draft, action) {
|
|
116
|
+
const key = getBillTransactionDetailKey(action.payload.billId);
|
|
117
|
+
const detail = recordGet(draft.billDetailById, key, undefined);
|
|
118
|
+
if (detail != null) {
|
|
119
|
+
draft.billDetailById[key] = {
|
|
120
|
+
...detail,
|
|
121
|
+
recommendationsFetchStatus: {
|
|
122
|
+
fetchState: 'Completed',
|
|
123
|
+
error: undefined,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
// `didWriteRecommendations` is false on the skip paths, which resolve
|
|
129
|
+
// the status without fetching anything. Consumers that react by
|
|
130
|
+
// recomputing local state must not run then — there is nothing new to
|
|
131
|
+
// apply, and recomputing would reset the line's "update to" selections.
|
|
132
|
+
prepare(billId, didWriteRecommendations = false) {
|
|
133
|
+
return {
|
|
134
|
+
payload: { billId, didWriteRecommendations },
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
fetchBillRecommendationsFailure: {
|
|
139
|
+
reducer(draft, action) {
|
|
140
|
+
const key = getBillTransactionDetailKey(action.payload.billId);
|
|
141
|
+
const detail = recordGet(draft.billDetailById, key, undefined);
|
|
142
|
+
if (detail != null) {
|
|
143
|
+
draft.billDetailById[key] = {
|
|
144
|
+
...detail,
|
|
145
|
+
recommendationsFetchStatus: {
|
|
146
|
+
fetchState: 'Error',
|
|
147
|
+
error: action.payload.error,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
prepare(billId, error) {
|
|
153
|
+
return {
|
|
154
|
+
payload: { billId, error },
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
},
|
|
72
158
|
deleteBill: {
|
|
73
159
|
reducer(draft, action) {
|
|
74
160
|
const key = getBillTransactionDetailKey(action.payload.billId);
|
|
@@ -462,5 +548,5 @@ const billDetailView = createSlice({
|
|
|
462
548
|
});
|
|
463
549
|
},
|
|
464
550
|
});
|
|
465
|
-
export const { fetchBillDetail, updateBillDetail, updateBillDetailFetchState, removeBillDetail, deleteBill, deleteBillSuccessOrFailure, approveOrRejectBill, approveOrRejectBillSuccess, approveOrRejectBillFailure, updateApprovalStatusOnSuccess, cancelAndDeleteBill, cancelAndDeleteBillSuccessOrFailure, waitForVendorByNameThenUpdateBillDetail, fetchEligibleActionsForBill, updateEligibleActionsForBill, updateFetchEligibleActionsFailureState, retryOrRefundBill, retryOrRefundBillSuccess, retryOrRefundBillFailure, } = billDetailView.actions;
|
|
551
|
+
export const { fetchBillDetail, fetchBillRecommendations, fetchBillRecommendationsSuccess, fetchBillRecommendationsFailure, updateBillDetail, updateBillDetailFetchState, removeBillDetail, deleteBill, deleteBillSuccessOrFailure, approveOrRejectBill, approveOrRejectBillSuccess, approveOrRejectBillFailure, updateApprovalStatusOnSuccess, cancelAndDeleteBill, cancelAndDeleteBillSuccessOrFailure, waitForVendorByNameThenUpdateBillDetail, fetchEligibleActionsForBill, updateEligibleActionsForBill, updateFetchEligibleActionsFailureState, retryOrRefundBill, retryOrRefundBillSuccess, retryOrRefundBillFailure, } = billDetailView.actions;
|
|
466
552
|
export default billDetailView.reducer;
|
|
@@ -219,6 +219,7 @@ export const getBillDetailView = (state, billId) => {
|
|
|
219
219
|
fetchState: fetchState.fetchState,
|
|
220
220
|
error: fetchState.error,
|
|
221
221
|
version: 0,
|
|
222
|
+
recommendationsFetchStatus: billTransactionDetails?.recommendationsFetchStatus,
|
|
222
223
|
transactionDetails: transaction,
|
|
223
224
|
createdBy,
|
|
224
225
|
bankAccount,
|
package/lib/esm/view/spendManagement/billPay/billDetailView/epics/fetchBillRecommendationsEpic.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import recordGet from 'lodash/get';
|
|
2
|
+
import { of } from 'rxjs';
|
|
3
|
+
import { exhaustMap, filter, groupBy, mergeMap, take } from 'rxjs/operators';
|
|
4
|
+
import { createZeniAPIStatus } from '../../../../../responsePayload';
|
|
5
|
+
import { fetchBillRecommendations, fetchBillRecommendationsFailure, updateBillDetail, updateBillDetailFetchState, } from '../billDetailViewReducer';
|
|
6
|
+
import { getBillTransactionDetailKey } from '../billDetailViewState';
|
|
7
|
+
import { fetchRecommendationsForBill, getBillTransaction, } from './fetchRecommendationsForBill';
|
|
8
|
+
/**
|
|
9
|
+
* Standalone recommendations fetch for a bill, so neither the bill detail nor
|
|
10
|
+
* the bill edit screen waits on recommendation composition.
|
|
11
|
+
*
|
|
12
|
+
* Which bills need it is decided from the bill's stage: an approval-stage bill
|
|
13
|
+
* gets them from the dedicated endpoint
|
|
14
|
+
* (`GET /1.0/bills/transactions/{transaction_id}/recommendations`), while a
|
|
15
|
+
* draft gets them on the bill detail response itself. The platform makes that
|
|
16
|
+
* split from the bill's status, so neither request carries a flag asking for
|
|
17
|
+
* one behaviour or the other.
|
|
18
|
+
*
|
|
19
|
+
* Only `recommendations` is read — per line, and what drives the "Update to X"
|
|
20
|
+
* checkbox and the Recommended group in the category/class dropdowns. The
|
|
21
|
+
* endpoint returns no entity-level recommendation and the edit form does not
|
|
22
|
+
* render one.
|
|
23
|
+
*
|
|
24
|
+
* Failure is confined to `recommendationsFetchStatus` — no bill-detail action
|
|
25
|
+
* is emitted, so a failed or slow recommendations fetch can neither block nor
|
|
26
|
+
* error out either screen.
|
|
27
|
+
*
|
|
28
|
+
* @precondition Every site dispatching `fetchBillRecommendations` must also
|
|
29
|
+
* cause the bill detail to be requested for that bill. This epic waits for the
|
|
30
|
+
* detail when it is not already in the store, and that wait settles only on a
|
|
31
|
+
* detail write or a detail error. A caller that dispatches without one would
|
|
32
|
+
* leave the wait open, and the per-bill `exhaustMap` would then drop later
|
|
33
|
+
* retries for that bill. Both current callers satisfy this: the bill detail
|
|
34
|
+
* route dispatches `fetchBillDetail` alongside, and the edit route reaches it
|
|
35
|
+
* through `fetchEditBillDetailPage`.
|
|
36
|
+
*/
|
|
37
|
+
export const fetchBillRecommendationsEpic = (actions$, state$, zeniAPI) => actions$.pipe(filter(fetchBillRecommendations.match),
|
|
38
|
+
// The bill detail route requests these and the edit screen asks again on
|
|
39
|
+
// every mount, so repeats are normal. Drop the ones whose bill is already
|
|
40
|
+
// done before they reach the request.
|
|
41
|
+
filter((action) => action.payload.cacheOverride === true ||
|
|
42
|
+
areRecommendationsAlreadyFetched(state$.value, action.payload.billId) === false),
|
|
43
|
+
// Per bill, then `exhaustMap` rather than `switchMap`: a repeat that
|
|
44
|
+
// arrives while the request (or the wait for the bill detail) is still
|
|
45
|
+
// running is ignored, where `switchMap` would cancel the in-flight one and
|
|
46
|
+
// re-issue it. `groupBy` keeps that per bill, so navigating to another
|
|
47
|
+
// bill mid-flight still fetches for the new one.
|
|
48
|
+
groupBy((action) => action.payload.billId), mergeMap((actionsForBill$) => actionsForBill$.pipe(exhaustMap((action) => {
|
|
49
|
+
const { billId, cacheOverride } = action.payload;
|
|
50
|
+
if (isBillReadyForRecommendations(state$.value, billId)) {
|
|
51
|
+
return fetchRecommendationsForBill(state$.value, billId, zeniAPI, cacheOverride);
|
|
52
|
+
}
|
|
53
|
+
// The edit route can mount before the bill detail response lands,
|
|
54
|
+
// and the request needs the vendor / line ids / amount from it. Wait
|
|
55
|
+
// for the detail write for this bill rather than dropping the fetch,
|
|
56
|
+
// and settle on a detail error too.
|
|
57
|
+
//
|
|
58
|
+
// A leftover `Error` on the bill detail is deliberately NOT treated
|
|
59
|
+
// as terminal here. On the edit route this action is dispatched
|
|
60
|
+
// before the parent screen re-requests the detail — React runs the
|
|
61
|
+
// child's effect first — so failing on the stale error would leave
|
|
62
|
+
// recommendations failed even though the detail is about to succeed.
|
|
63
|
+
//
|
|
64
|
+
// The invariant this relies on: every dispatch site also causes the
|
|
65
|
+
// bill detail to be requested. Both do today. A future site that
|
|
66
|
+
// does not would leave this waiting, and `exhaustMap` would drop
|
|
67
|
+
// later retries for that bill.
|
|
68
|
+
const billDetailSettled$ = actions$.pipe(filter((waited) => {
|
|
69
|
+
if (updateBillDetail.match(waited)) {
|
|
70
|
+
return waited.payload.billId === billId;
|
|
71
|
+
}
|
|
72
|
+
return (updateBillDetailFetchState.match(waited) &&
|
|
73
|
+
waited.payload.fetchState === 'Error' &&
|
|
74
|
+
waited.payload.billId === billId);
|
|
75
|
+
}), take(1) // don't listen forever! IMPORTANT!
|
|
76
|
+
);
|
|
77
|
+
return billDetailSettled$.pipe(mergeMap((waited) => {
|
|
78
|
+
if (updateBillDetail.match(waited)) {
|
|
79
|
+
return fetchRecommendationsForBill(state$.value, billId, zeniAPI, cacheOverride);
|
|
80
|
+
}
|
|
81
|
+
// Bill detail failed; there is nothing to recommend against.
|
|
82
|
+
return of(fetchBillRecommendationsFailure(billId, waited.payload.error ??
|
|
83
|
+
createZeniAPIStatus('Unexpected Error', 'Bill detail fetch failed before recommendations could be requested')));
|
|
84
|
+
}));
|
|
85
|
+
}))));
|
|
86
|
+
/**
|
|
87
|
+
* Helper functions
|
|
88
|
+
*/
|
|
89
|
+
function areRecommendationsAlreadyFetched(state, billId) {
|
|
90
|
+
const billDetail = recordGet(state.billDetailViewState.billDetailById, getBillTransactionDetailKey(billId), undefined);
|
|
91
|
+
return billDetail?.recommendationsFetchStatus.fetchState === 'Completed';
|
|
92
|
+
}
|
|
93
|
+
function isBillReadyForRecommendations(state, billId) {
|
|
94
|
+
const billDetail = recordGet(state.billDetailViewState.billDetailById, getBillTransactionDetailKey(billId), undefined);
|
|
95
|
+
return (billDetail?.fetchState === 'Completed' &&
|
|
96
|
+
getBillTransaction(state, billId) != null);
|
|
97
|
+
}
|
package/lib/esm/view/spendManagement/billPay/billDetailView/epics/fetchRecommendationsForBill.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import recordGet from 'lodash/get';
|
|
2
|
+
import { from, of } from 'rxjs';
|
|
3
|
+
import { catchError, mergeMap } from 'rxjs/operators';
|
|
4
|
+
import { transformRecommendation } from '../../../../../commonPayloadTypes/recommendationPayload';
|
|
5
|
+
import { updateBillTransactionRecommendations } from '../../../../../entity/billPay/billTransaction/billTransactionReducer';
|
|
6
|
+
import { createZeniAPIStatus, isSuccessResponse, } from '../../../../../responsePayload';
|
|
7
|
+
import { fetchBillRecommendationsFailure, fetchBillRecommendationsSuccess, } from '../billDetailViewReducer';
|
|
8
|
+
/**
|
|
9
|
+
* Requests the recommendations for one bill and maps them onto the transaction
|
|
10
|
+
* entity, or resolves without a request for a bill that does not need them.
|
|
11
|
+
*
|
|
12
|
+
* Only `recommendations` is read from the response — per line, and what drives
|
|
13
|
+
* the "Update to X" checkbox and the Recommended group in the category/class
|
|
14
|
+
* dropdowns. The endpoint returns no entity-level recommendation and the edit
|
|
15
|
+
* form does not render one.
|
|
16
|
+
*/
|
|
17
|
+
export function fetchRecommendationsForBill(state, billId, zeniAPI, cacheOverride = false) {
|
|
18
|
+
const transaction = getBillTransaction(state, billId);
|
|
19
|
+
const lineIds = transaction?.lines?.map((line) => line.id) ?? [];
|
|
20
|
+
if (transaction == null ||
|
|
21
|
+
shouldSkipRecommendations(transaction, cacheOverride)) {
|
|
22
|
+
return of(fetchBillRecommendationsSuccess(billId));
|
|
23
|
+
}
|
|
24
|
+
// The bill is scoped by the path, so this endpoint takes no query — there is
|
|
25
|
+
// no `is_include_*` flag to turn off the way the bill detail request has.
|
|
26
|
+
return zeniAPI
|
|
27
|
+
.getJSON(`${zeniAPI.apiEndPoints.spendManagementMicroServiceBaseUrl}/1.0/bills/transactions/${billId}/recommendations`)
|
|
28
|
+
.pipe(mergeMap((response) => {
|
|
29
|
+
if (!isSuccessResponse(response) || response.data == null) {
|
|
30
|
+
return of(fetchBillRecommendationsFailure(billId, response.status));
|
|
31
|
+
}
|
|
32
|
+
const actions = [];
|
|
33
|
+
// Positional line <-> recommendation mapping, matching
|
|
34
|
+
// `getRecommendationsPerLine` — the mapping the recommendations went
|
|
35
|
+
// through while the bill detail response still carried them. This
|
|
36
|
+
// endpoint returns them flat, not nested under `transaction`.
|
|
37
|
+
const recommendationPayloads = response.data.recommendations ?? [];
|
|
38
|
+
// The pairing is a convention shared with the platform, not
|
|
39
|
+
// something the payload enforces, so a mismatch would otherwise
|
|
40
|
+
// mis-assign or silently drop recommendations. Degrade safely —
|
|
41
|
+
// extra entries are ignored and unmatched lines get none — but say
|
|
42
|
+
// so, because it means the two sides have diverged.
|
|
43
|
+
if (recommendationPayloads.length !== lineIds.length) {
|
|
44
|
+
console.warn(`Bill ${billId}: ${recommendationPayloads.length} recommendation entries for ${lineIds.length} lines. ` +
|
|
45
|
+
'They are paired by position, so this bill may be scored incorrectly.');
|
|
46
|
+
}
|
|
47
|
+
const recommendationByLineId = {};
|
|
48
|
+
lineIds.forEach((lineId, index) => {
|
|
49
|
+
const recommendationPayload = recommendationPayloads[index];
|
|
50
|
+
if (recommendationPayload != null) {
|
|
51
|
+
recommendationByLineId[lineId] = transformRecommendation(recommendationPayload);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
actions.push(updateBillTransactionRecommendations(billId, recommendationByLineId));
|
|
55
|
+
// Entity writes first, then the view status, so consumers reading on
|
|
56
|
+
// the status flip already see the recommendations.
|
|
57
|
+
actions.push(fetchBillRecommendationsSuccess(billId, true));
|
|
58
|
+
return from(actions);
|
|
59
|
+
}), catchError((error) => of(fetchBillRecommendationsFailure(billId, createZeniAPIStatus('Unexpected Error', `Fetch bill recommendations REST API call errored out ${error}`)))));
|
|
60
|
+
}
|
|
61
|
+
export function getBillTransaction(state, billId) {
|
|
62
|
+
return recordGet(state.billTransactionState.transactionById, billId, undefined);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Helper functions
|
|
66
|
+
*/
|
|
67
|
+
// Awaiting-approval bills are the only ones this request exists for. A draft
|
|
68
|
+
// opens the edit form directly, so its recommendations ride along on the bill
|
|
69
|
+
// detail response; `paid` bills cannot reach the form at all. The platform
|
|
70
|
+
// decides which of those two it is from the bill's status, which is why the
|
|
71
|
+
// request carries no flag asking for either.
|
|
72
|
+
function shouldSkipRecommendations(transaction, cacheOverride) {
|
|
73
|
+
return (transaction.billPayInfo.stage.code !== 'pending_approval' ||
|
|
74
|
+
transaction.isDeleted === true ||
|
|
75
|
+
// No vendor (a draft carrying only a candidate vendor name) means no
|
|
76
|
+
// entity to recommend against — the vendor-selection flow covers that.
|
|
77
|
+
Boolean(transaction.vendor?.id) === false ||
|
|
78
|
+
// The bill detail response already carried them. `toVendorTransaction`
|
|
79
|
+
// maps them straight onto the entity, so requesting again would only
|
|
80
|
+
// re-fetch what is here — this is what lets the platform move the line
|
|
81
|
+
// between embedded and on-demand without a client change. A forced
|
|
82
|
+
// refresh is the one case that still goes to the endpoint.
|
|
83
|
+
(cacheOverride === false &&
|
|
84
|
+
Object.keys(transaction.recommendations ?? {}).length > 0));
|
|
85
|
+
}
|
|
@@ -149,6 +149,23 @@ const editBillView = createSlice({
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
},
|
|
152
|
+
// Applied when the standalone recommendations fetch resolves after the
|
|
153
|
+
// edit local store was already initialized. `initializeBillToLocalStore`
|
|
154
|
+
// is guarded to run once, so without this the late recommendations would
|
|
155
|
+
// never reach the category/class autofill or the "update to" checkbox.
|
|
156
|
+
updateBillLineRecommendationsInLocalStore: {
|
|
157
|
+
reducer(draft, action) {
|
|
158
|
+
const { billId, updatesByLineId } = action.payload;
|
|
159
|
+
const key = getBillTransactionDetailKey(billId);
|
|
160
|
+
const editBill = draft.editBillStateById[key];
|
|
161
|
+
if (editBill != null) {
|
|
162
|
+
editBill.billDetailLocalData.updatesByLineId = Object.assign({}, editBill.billDetailLocalData.updatesByLineId, updatesByLineId);
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
prepare(billId, updatesByLineId) {
|
|
166
|
+
return { payload: { billId, updatesByLineId } };
|
|
167
|
+
},
|
|
168
|
+
},
|
|
152
169
|
discardBillUpdatesInLocalStore(draft, action) {
|
|
153
170
|
if (action.payload.billId != null) {
|
|
154
171
|
const { billId } = action.payload;
|
|
@@ -494,7 +511,7 @@ const editBillView = createSlice({
|
|
|
494
511
|
});
|
|
495
512
|
},
|
|
496
513
|
});
|
|
497
|
-
export const { fetchEditBillDetailPage, fetchBillAndInitializeLocalStore, fetchVendorByNameAndParseInvoice, initializeBillToLocalStore, saveBillUpdatesToLocalStore, discardBillUpdatesInLocalStore, saveBillDetail, saveBillSuccessOrFailure, updateEditBillFetchStateCompleted, parseInvoiceToBill, waitForBillDetailThenInitializeLocalStore, waitForVendorByNameThenParsetoLocalStore, updateInternationalTransferFeeBearerPaymentMethodInLocalData, updateShowAutofill, updateAddBillAutoFields, updateBillUploadFetchState, updateRecommendedVendorId, updateSuggestedVendorIdFromParsedInvoice, updateVendorContact, updateVendorContactSuccessOrFailure, convertAmountToHomeCurrency, currencyConversionValueSuccessOrFailure, updateHomeCurrencyConversion, fetchVendorAndUpdateBillLocalData, waitForVendorByIdThenSaveBillUpdatetoLocalStore, markBillForRetry, updateOutsideZeniPaymentLocalData, discardOutsideZeniPaymentLocalData, updateNewAddedBillPaymentAccount, updateWithdrawFromAccountId, removeBillFileFromLocalStore, replaceBillFileInLocalStore, updateShouldReplaceBillData, } = editBillView.actions;
|
|
514
|
+
export const { fetchEditBillDetailPage, fetchBillAndInitializeLocalStore, fetchVendorByNameAndParseInvoice, initializeBillToLocalStore, saveBillUpdatesToLocalStore, updateBillLineRecommendationsInLocalStore, discardBillUpdatesInLocalStore, saveBillDetail, saveBillSuccessOrFailure, updateEditBillFetchStateCompleted, parseInvoiceToBill, waitForBillDetailThenInitializeLocalStore, waitForVendorByNameThenParsetoLocalStore, updateInternationalTransferFeeBearerPaymentMethodInLocalData, updateShowAutofill, updateAddBillAutoFields, updateBillUploadFetchState, updateRecommendedVendorId, updateSuggestedVendorIdFromParsedInvoice, updateVendorContact, updateVendorContactSuccessOrFailure, convertAmountToHomeCurrency, currencyConversionValueSuccessOrFailure, updateHomeCurrencyConversion, fetchVendorAndUpdateBillLocalData, waitForVendorByIdThenSaveBillUpdatetoLocalStore, markBillForRetry, updateOutsideZeniPaymentLocalData, discardOutsideZeniPaymentLocalData, updateNewAddedBillPaymentAccount, updateWithdrawFromAccountId, removeBillFileFromLocalStore, replaceBillFileInLocalStore, updateShouldReplaceBillData, } = editBillView.actions;
|
|
498
515
|
export default editBillView.reducer;
|
|
499
516
|
function doUpdateRecurringBillLocalData(billDetail, currentConfig, prevConfig) {
|
|
500
517
|
const recurringBillDetail = updateRecurringBillDetails(currentConfig, prevConfig, billDetail.recurringBillInstances, billDetail.recipientTotalAmount?.amount);
|
|
@@ -380,6 +380,9 @@ export const getEditBillDetail = (state, isVendorRecommendationFeatureEnabled, i
|
|
|
380
380
|
classHierarchyList,
|
|
381
381
|
latestApprovedDate: getLatestApprovalDate(billActivities),
|
|
382
382
|
recommendationByLineID: recommendations,
|
|
383
|
+
recommendationsFetchStatus: billId != null
|
|
384
|
+
? billDetailViewState.billDetailById[getBillTransactionDetailKey(billId)]?.recommendationsFetchStatus
|
|
385
|
+
: undefined,
|
|
383
386
|
isRecurring,
|
|
384
387
|
recurringBillInstances,
|
|
385
388
|
recurringConfig,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import recordGet from 'lodash/get';
|
|
2
|
+
import { EMPTY, of } from 'rxjs';
|
|
3
|
+
import { filter, mergeMap } from 'rxjs/operators';
|
|
4
|
+
import { getAccountList, } from '../../../../accountList/accountListSelector';
|
|
5
|
+
import { getClassList, } from '../../../../classList/classListSelector';
|
|
6
|
+
import { fetchBillRecommendationsSuccess } from '../../billDetailView/billDetailViewReducer';
|
|
7
|
+
import { getBillTransactionDetailKey } from '../../billDetailView/billDetailViewState';
|
|
8
|
+
import { updateBillLineRecommendationsInLocalStore } from '../editBillViewReducer';
|
|
9
|
+
import { getLineRecommendationsLocalData } from './initializeBillToLocalStoreEpic';
|
|
10
|
+
/**
|
|
11
|
+
* Back-fills the edit local store once the standalone recommendations fetch
|
|
12
|
+
* resolves. `initializeBillToLocalStore` reads `transaction.recommendations`
|
|
13
|
+
* exactly once and is guarded against re-running, so the recommendation-derived
|
|
14
|
+
* line data (category/class autofill, `recommendedCategory` / `recommendedClass`
|
|
15
|
+
* behind the "update to" checkbox) has to be recomputed here instead.
|
|
16
|
+
*
|
|
17
|
+
* Recomputes from the entity lines with the same helper the initializer uses, so
|
|
18
|
+
* an untouched line ends up exactly as it would have had the recommendations
|
|
19
|
+
* been present at init time.
|
|
20
|
+
*/
|
|
21
|
+
export const applyBillRecommendationsToLocalStoreEpic = (actions$, state$) => actions$.pipe(filter(fetchBillRecommendationsSuccess.match),
|
|
22
|
+
// The skip paths resolve the status without fetching — a draft whose
|
|
23
|
+
// recommendations came on the bill detail response, or a stage that is not
|
|
24
|
+
// served any. `initializeBillToLocalStore` has already read those, so
|
|
25
|
+
// recomputing here would only reset the line's "update to" selections.
|
|
26
|
+
filter((action) => action.payload.didWriteRecommendations), mergeMap((action) => {
|
|
27
|
+
const { billId } = action.payload;
|
|
28
|
+
const state = state$.value;
|
|
29
|
+
const key = getBillTransactionDetailKey(billId);
|
|
30
|
+
const editBillState = recordGet(state.editBillDetailViewState.editBillStateById, key, undefined);
|
|
31
|
+
const transaction = recordGet(state.billTransactionState.transactionById, billId, undefined);
|
|
32
|
+
const recommendations = transaction?.recommendations;
|
|
33
|
+
if (editBillState == null ||
|
|
34
|
+
transaction == null ||
|
|
35
|
+
transaction.lines == null ||
|
|
36
|
+
recommendations == null ||
|
|
37
|
+
Object.keys(recommendations).length === 0) {
|
|
38
|
+
return EMPTY;
|
|
39
|
+
}
|
|
40
|
+
const localData = editBillState.billDetailLocalData;
|
|
41
|
+
// The recommendations were requested for the vendor on the bill. If the
|
|
42
|
+
// user has since switched vendors, `fetchVendorAndUpdateBillLocalData`
|
|
43
|
+
// owns the line data and these are stale — leave it alone.
|
|
44
|
+
//
|
|
45
|
+
// Both sides are QBO ids despite the field names suggesting otherwise.
|
|
46
|
+
// `transaction.vendor` is built by `mapVendorBasePayloadToVendorBase`,
|
|
47
|
+
// whose id is `getVendorPayloadIdentifier` = `vendor_id ?? qbo_id`, and
|
|
48
|
+
// on a bill transaction payload `vendor_id` IS the QBO id — the Zeni id
|
|
49
|
+
// rides separately as `zeni_vendor_id`. Verified against live DEV and QA
|
|
50
|
+
// responses, both of which have `vendor_id === qbo_vendor_id`.
|
|
51
|
+
if (localData.vendor?.qboId !== transaction.vendor?.id) {
|
|
52
|
+
return EMPTY;
|
|
53
|
+
}
|
|
54
|
+
const accountList = getAccountList(state.accountState, state.accountListState, 'billPayAccountList');
|
|
55
|
+
const classList = getClassList(state.classState, state.classListState);
|
|
56
|
+
const updatesByLineId = {};
|
|
57
|
+
transaction.lines.forEach((line) => {
|
|
58
|
+
// A line whose category/class the user already set keeps their input.
|
|
59
|
+
const localLine = localData.updatesByLineId[line.id];
|
|
60
|
+
if (localLine?.isCategoryOrClassUpdated) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// The helper spreads whatever line it is given into its result, and
|
|
64
|
+
// the reducer replaces the entry wholesale, so the local line has to
|
|
65
|
+
// go in or edits that leave `isCategoryOrClassUpdated` untouched — an
|
|
66
|
+
// amount or description changed while this fetch was in flight — get
|
|
67
|
+
// reverted.
|
|
68
|
+
//
|
|
69
|
+
// Category and class are the exception and come from the ENTITY line,
|
|
70
|
+
// exactly as at init. By now the local line may hold a category the
|
|
71
|
+
// vendor default autofilled when no recommendation was available yet,
|
|
72
|
+
// and the helper reads whatever it is given as the line's original
|
|
73
|
+
// value and will not replace one. Passing that back would leave an
|
|
74
|
+
// untouched line on the vendor default instead of the per-line
|
|
75
|
+
// recommendation, so the back-fill would not match what init would
|
|
76
|
+
// have produced had the recommendations been there all along.
|
|
77
|
+
const localLineWithEntityCategory = localLine == null || hasAccountAndClass(line) === false
|
|
78
|
+
? line
|
|
79
|
+
: {
|
|
80
|
+
...localLine,
|
|
81
|
+
account: line.account,
|
|
82
|
+
class: line.class,
|
|
83
|
+
};
|
|
84
|
+
updatesByLineId[line.id] = getLineRecommendationsLocalData(line.id, localLineWithEntityCategory, recommendations, accountList, classList, localData.vendor);
|
|
85
|
+
});
|
|
86
|
+
if (Object.keys(updatesByLineId).length === 0) {
|
|
87
|
+
return EMPTY;
|
|
88
|
+
}
|
|
89
|
+
return of(updateBillLineRecommendationsInLocalStore(billId, updatesByLineId));
|
|
90
|
+
}));
|
|
91
|
+
// Only some `Line` members carry a category and class. A guard rather than a
|
|
92
|
+
// cast so a shape change is a compile error, not a silent `undefined`.
|
|
93
|
+
function hasAccountAndClass(line) {
|
|
94
|
+
return 'account' in line;
|
|
95
|
+
}
|