forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-customer.epics.ts
193 lines (181 loc) · 7.48 KB
/
search-customer.epics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import { ApiService } from '../core/account/api.service';
import { ApiScheme } from '../core/account/interfaces/api-scheme';
import {
SearchCustomerActionTypeKeys, IGetSingleCustomerSucceeded, IClearSearchCustomerState,
IPerformSearch,
IPerformSearchSucceeded,
IPerformSearchFailed
} from './search-customer.actions';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators/map';
import { concat } from 'rxjs/observable/concat';
import { ActionsObservable } from 'redux-observable';
import { StringConstants } from '../shared/string-constants';
import { CustomerResult } from '../core/account/interfaces/api-customer';
import { RoutesActionTypeKeys } from '../shared/routes/routes.actions';
import { Store } from 'redux';
import { IRootState } from '../store/root.state';
import { SearchLoadingState } from './search-customer.state';
import { isSearchCustomerStep } from '../shared/routes/current-step-check';
import { AppActions } from '../store/app.actions';
import {
OverlayProperties, OverlayLabels,
OverlayTypes
} from '../shared/overlay/overlay-properties';
/**
* Epic that orchestrates search customer actions
*
* @export
* @class SearchCustomerEpics
*/
@Injectable()
export class SearchCustomerEpics {
constructor(private service: ApiService) { }
searchCustomers = (action$: ActionsObservable<IPerformSearch>, store: any):
Observable<IPerformSearchSucceeded | IPerformSearchFailed> => {
return action$.ofType(SearchCustomerActionTypeKeys.PERFORM_SEARCH)
.filter(() => store.getState().searchCustomerReducer.searchKey)
.mergeMap((action: IPerformSearch) => {
const payload: ApiScheme.IPartiesRequestArguments = this.getPartiesRequestArguments(
store.getState().searchCustomerReducer.searchType,
action.payload
);
const partyRequest = this.service.getParties(payload);
return partyRequest.pipe(
map(result => {
const customers: CustomerResult.ISearchCustomerResult[] = result.data.map(
customer => this.getCustomer(customer)
);
return <IPerformSearchSucceeded>{
type: SearchCustomerActionTypeKeys.PERFORM_SEARCH_SUCCEEDED,
payload: customers
};
})
).catch(error => Observable.of<IPerformSearchFailed>({
type: SearchCustomerActionTypeKeys.PERFORM_SEARCH_FAILED
}));
});
}
private getPartiesRequestArguments(urlType: string, searchValue: string):
ApiScheme.IPartiesRequestArguments {
const idType: string = StringConstants.getUniqueIdentifiers(urlType);
const request: ApiScheme.IPartiesRequestArguments = { urlType, searchValue, idType };
return request;
}
private getCustomer(customer): CustomerResult.ISearchCustomerResult {
return {
id: customer.uniqueIdentifier,
name: customer.name,
address: customer.address,
postalCode: customer.address.postalCode,
partyId: customer.partyId
};
}
getSingleCustomer = (action$: ActionsObservable<any>, store: Store<IRootState>) => {
return action$.ofType(SearchCustomerActionTypeKeys.GET_SINGLE_CUSTOMER)
.mergeMap((action) => {
const application = store.getState().appReducer.accountData.selectedApplication;
return concat(
this.service.getPartyById(action.payload).pipe(
map(result => {
const customer = this.mapCustomerResult(result);
return {
type: SearchCustomerActionTypeKeys.GET_SINGLE_CUSTOMER_SUCCEEDED,
payload: customer
};
})
).catch(error => Observable.of({
type: SearchCustomerActionTypeKeys.GET_SINGLE_CUSTOMER_FAILED
})),
Observable.of({
type: AppActions.DISPLAY_APP_OVERLAY,
payload: application.assessment &&
application.assessment.result === StringConstants.creditCheckStatus.approved ?
new OverlayProperties(OverlayLabels.dealLoadTitle, OverlayTypes.loading) : null
}));
});
}
private mapCustomerResult(raw: CustomerResult.IGetCustomerResult):
CustomerResult.ISearchCustomerResult {
const address = raw.company.addresses.registeredAddress;
return {
id: raw.company.uniqueIdentifier,
name: raw.company.commercialName,
address,
postalCode: address.postalCode,
partyId: raw.partyId
};
}
mapResultToCustomerState = (action$: ActionsObservable<any>, store: Store<IRootState>) => {
return action$.ofType(SearchCustomerActionTypeKeys.GET_SINGLE_CUSTOMER_SUCCEEDED)
.mergeMap((action: IGetSingleCustomerSucceeded) => concat(
Observable.of({
type: SearchCustomerActionTypeKeys.SET_SEARCH_KEY,
payload: action.payload.id
}),
Observable.of({
type: SearchCustomerActionTypeKeys.PERFORM_SEARCH_SUCCEEDED,
payload: [action.payload]
}),
Observable.of({
type: SearchCustomerActionTypeKeys.SET_CUSTOMER,
payload: action.payload
}),
Observable.of({
type: SearchCustomerActionTypeKeys.SELECT_CONSENT_CHECK_BOX,
payload: true
}),
Observable.of({
type: SearchCustomerActionTypeKeys.SET_LOADING_STATE,
payload: SearchLoadingState.complete
})
));
}
/**
* Listens to routes SHOW_VALIDATION_ERROR
* and if that action has a relevant errorID as payload
* fire proper actions to trigger UI changes (like opening tooltips)
*
* @returns Observable of SET_CUSTOMER_TOOLTIP_DISPLAY in case of not selected customer
* @returns Observable of SET_CUSTOMER_CONSENT_TOOLTIP in case consent checkbox is not selected
*/
watchSearchCustomerErrors = (action$: ActionsObservable<any>, store: Store<IRootState>) => {
return action$.ofType(RoutesActionTypeKeys.SHOW_VALIDATION_ERROR)
.filter(() => isSearchCustomerStep(store.getState()))
.switchMap((action) => {
if (action.payload === StringConstants.validationErrors.customerID
&& store.getState().appReducer.site === StringConstants.siteName.NL) {
return Observable.of({
type: SearchCustomerActionTypeKeys.SET_CUSTOMER_TOOLTIP_DISPLAY,
payload: true
});
} else if (store.getState().appReducer.contractData.selectedCustomer.id &&
store.getState().appReducer.site === StringConstants.siteName.NL &&
action.payload === StringConstants.validationErrors.consentSelected) {
return Observable.of({
type: SearchCustomerActionTypeKeys.SET_CUSTOMER_CONSENT_TOOLTIP,
payload: true
});
} else {
return Observable.of({
type: SearchCustomerActionTypeKeys.NO_CUSTOMER_SELECTED
});
}
});
}
clearSearchCustomerState =
(action$: ActionsObservable<any>, store: Store<IRootState>):
Observable<IClearSearchCustomerState> => {
return action$.ofType(AppActions.CLEAR_STATE)
.filter(() => !!store.getState().appReducer.locale)
.map(() => {
return <IClearSearchCustomerState>{
type: SearchCustomerActionTypeKeys.CLEAR_STATE
};
});
}
}