Files
monorepo/LCU.WebApps.Collections.RepoClient/src/containers/account-page/account-page-tab-view/AccountPageTabView.tsx
T
2026-03-17 11:09:46 -05:00

334 lines
14 KiB
TypeScript

/* eslint-disable eqeqeq */
import Axios from "axios";
import moment from "moment/moment";
import React from "react";
import {connect} from "react-redux";
import {toast} from "react-toastify";
import TabView from "../../../components/tab-view/TabView";
import {IRepoCompany} from "../../../reducers/RepoCompanyReducer";
import {IExternalUser} from "../../../reducers/SettingsReducer";
import {HistorySource, ITemenosHistory} from "../../../services/service-types/AccountHistoryTypes";
import {IRepoAccount} from "../../../services/service-types/RepoAccountTypes";
import Configuration from "../../../shared/configuration/Configuration";
import DateConversions from "../../../shared/conversions/DateConversions";
import AsyncState from "../../../shared/enumerations/AsyncState";
import AxiosMessageFormatting from "../../../shared/formatting/AxiosMessageFormatting";
import UrlFormatting from "../../../shared/formatting/UrlFormatting";
import ToastDefaults from "../../../shared/notifications/ToastDefaults";
import IAsyncStateContainer from "../../../shared/types/IAsyncStateContainer";
import {IAppState} from "../../../store/Store";
import AccountActivity from "../../account-activity/AccountActivity";
import AuctionTab from "../../auction-tab/AuctionTab";
import RefundStatusTab from "../../refund-status-tab/RefundStatusTab";
import RepoHistoryTab from "../../repo-history-tab/RepoHistoryTab";
import RepoInfoTab from "../../repo-info-tab/RepoInfoTab";
import RepoStatusChangeHistoryTable from "../../repo-status-change-history-table/RepoStatusChangeHistoryTable";
import TransactionHistoryTable, {ITransactionHistory} from "../../transaction-history-table/TransactionHistoryTable";
import {getExternalUserMap, getRepoTypeMap} from "./../../../selectors/SettingsSelector";
interface IAccountPageTabViewProps {
selectedAccount?: IRepoAccount;
}
interface IPropsFromState {
readonly repoCompanies: IAsyncStateContainer<IRepoCompany[]>;
readonly externalUserMap: IExternalUser[];
readonly repoTypeMap: string[];
readonly transactionTypeCodes: string[];
readonly transactionTypeCodeQueryPrefix: string;
readonly transactionHistoryParameterDateFormat: string;
readonly transactionHistoryStartDateOffset: number;
}
const mapStateToProps = (state: IAppState): IPropsFromState => {
return {
repoCompanies: state.repoCompanyState.repoCompanies,
externalUserMap: getExternalUserMap(state),
repoTypeMap: getRepoTypeMap(state),
transactionTypeCodes: state.settingsState.transactionTypeCodes,
transactionTypeCodeQueryPrefix: state.settingsState.transactionTypeCodeQueryPrefix,
transactionHistoryParameterDateFormat: state.settingsState.transactionHistoryParameterDateFormat,
transactionHistoryStartDateOffset: state.settingsState.transactionHistoryStartDateOffset
};
};
interface IAccountPageState {
caseHistories: IAsyncStateContainer<ITemenosHistory[]>;
accountHistories: IAsyncStateContainer<ITemenosHistory[]>;
transactionHistories: IAsyncStateContainer<ITransactionHistory[]>;
}
type Props = IAccountPageTabViewProps & IPropsFromState;
class AccountPageTabView extends React.Component<Props, IAccountPageState> {
constructor(props: Props) {
super(props);
this.state = {
caseHistories: {state: AsyncState.Inactive},
accountHistories: {state: AsyncState.Inactive},
transactionHistories: {state: AsyncState.Inactive},
};
this.updateCaseHistory = this.updateCaseHistory.bind(this);
this.updateAccountHistory = this.updateAccountHistory.bind(this);
this.updateTransactionHistory = this.updateTransactionHistory.bind(this);
this.updateHistories = this.updateHistories.bind(this);
}
componentDidUpdate(prevProps: Props) {
// Update letters if the Selected Account has updated
if (this.props.selectedAccount !== prevProps.selectedAccount) {
this.updateHistories();
}
}
async updateCaseHistory() {
if (!this.props.selectedAccount) {
return;
}
if (
this.state.caseHistories.state === AsyncState.Inactive ||
this.state.caseHistories.state === AsyncState.Failure
) {
this.setState({
caseHistories: {
state: AsyncState.Loading,
},
});
if (this.props.selectedAccount.referenceCaseId) {
const caseHistories = await this.getCaseHistory(this.props.selectedAccount.referenceCaseId);
this.setState({
caseHistories: caseHistories,
});
} else {
this.setState({
caseHistories: {
state: AsyncState.Failure
}
});
}
}
}
async updateAccountHistory() {
if (!this.props.selectedAccount) {
return;
}
if (
this.state.accountHistories.state === AsyncState.Inactive ||
this.state.accountHistories.state === AsyncState.Failure
) {
this.setState({
accountHistories: {
state: AsyncState.Loading,
},
});
if (this.props.selectedAccount.referenceId) {
const accountHistories = await this.getAccountHistory(this.props.selectedAccount.referenceId);
this.setState({
accountHistories: accountHistories,
});
} else {
this.setState({
accountHistories: {
state: AsyncState.Failure
}
});
}
}
}
async updateTransactionHistory() {
if (!this.props.selectedAccount) {
return;
}
if (
this.state.transactionHistories.state === AsyncState.Inactive ||
this.state.transactionHistories.state === AsyncState.Failure
) {
this.setState({
transactionHistories: {
state: AsyncState.Loading,
},
});
const transactionHistories = await this.getTransactionHistory(this.props.selectedAccount.accountNo);
this.setState({
transactionHistories: transactionHistories,
});
}
}
async getCaseHistory(caseId: string): Promise<IAsyncStateContainer<ITemenosHistory[]>> {
const parameterizedUrl = UrlFormatting.ReplaceURLParams(
Configuration.PrefixCollectionsUrl(Configuration.Get.urls.collections.case_history),
{key: "caseId", value: caseId},
);
try {
const result = await Axios.get(parameterizedUrl);
const historyResult = result.data as ITemenosHistory[];
historyResult.map((x) => {
x.Created = DateConversions.utcToMoment(x.Created);
x.Source = HistorySource.Case;
return x;
});
return {
data: historyResult,
state: AsyncState.Success,
};
} catch (err) {
toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams);
window.console.error(err);
return {
state: AsyncState.Failure,
};
}
}
async getAccountHistory(accountNo: string): Promise<IAsyncStateContainer<ITemenosHistory[]>> {
const parameterizedUrl = UrlFormatting.ReplaceURLParams(
Configuration.PrefixCollectionsUrl(Configuration.Get.urls.collections.account_history),
{key: "accountNo", value: accountNo},
);
try {
const result = await Axios.get(parameterizedUrl);
const historyResult = result.data as ITemenosHistory[];
historyResult.map((x) => {
x.Created = DateConversions.utcToMoment(x.Created);
x.Source = HistorySource.Account;
return x;
});
return {
data: historyResult,
state: AsyncState.Success,
};
} catch (err) {
toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams);
window.console.error(err);
return {
state: AsyncState.Failure,
};
}
}
async getTransactionHistory(accountNo: string): Promise<IAsyncStateContainer<ITransactionHistory[]>> {
const parameterizedUrl = this.getTransactionHistoriesURL(accountNo);
if (!parameterizedUrl) {
return {
state: AsyncState.Failure,
};
}
try {
const result = await Axios.get(parameterizedUrl);
if (result.data.length < 1)
{
return result.data;
}
const transactionHistoryResult = result.data as ITransactionHistory[];
transactionHistoryResult.map((x) => {
x.postingDate = DateConversions.utcToMoment(x.postingDate);
x.effectiveDate = DateConversions.utcToMoment(x.effectiveDate);
return x;
});
return {
data: transactionHistoryResult,
state: AsyncState.Success,
};
} catch (err) {
toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams);
window.console.error(err);
return {
state: AsyncState.Failure,
};
}
}
getTransactionHistoriesURL(accountNo: string): string | undefined {
return UrlFormatting.ReplaceURLParams(
Configuration.PrefixCoreUrl(Configuration.Get.urls.core.loan_transactions),
{key: "loanNo", value: accountNo},
{key: "startDate", value: moment().add(this.props.transactionHistoryStartDateOffset, "d").format(this.props.transactionHistoryParameterDateFormat)},
{key: "endDate", value: moment().format(this.props.transactionHistoryParameterDateFormat)}
) + UrlFormatting.FormatArrayValuesIntoQuery(this.props.transactionTypeCodes, this.props.transactionTypeCodeQueryPrefix);
}
updateHistories() {
this.updateCaseHistory();
this.updateAccountHistory();
}
render() {
if (!this.props.selectedAccount) {
return (
<div data-test="account-page-tab-view-empty-tabs">
<TabView
pages={[
{
header: "Activity",
element: (<div/>),
},
]}
/>
</div>
);
} else {
return (
<div data-test="account-page-tab-view-loaded-tabs">
<TabView
pages={[
{
header: "Activity",
element: <AccountActivity selectedAccount={this.props.selectedAccount}
caseHistories={this.state.caseHistories}
accountHistories={this.state.accountHistories}
externalUserMap={this.props.externalUserMap}/>,
tabOpenCallback: this.updateHistories,
},
{
header: "Repo Info",
element: <RepoInfoTab selectedAccount={this.props.selectedAccount}
repoCompanies={this.props.repoCompanies.data}
repoTypeMap={this.props.repoTypeMap}/>,
},
{
header: "Auction Info",
element: <AuctionTab selectedAccount={this.props.selectedAccount}
caseHistories={this.state.caseHistories}
accountHistories={this.state.accountHistories}/>,
tabOpenCallback: this.updateHistories,
},
{
header: "Repo History",
element: <RepoHistoryTab accountNo={this.props.selectedAccount.accountNo}
accountHistories={this.state.accountHistories}
caseHistories={this.state.caseHistories}
externalUserMap={this.props.externalUserMap}/>,
tabOpenCallback: this.updateHistories
},
{
header: "Transaction History",
element: <TransactionHistoryTable accountNo={this.props.selectedAccount.accountNo}
transactionHistories={this.state.transactionHistories}/>,
tabOpenCallback: this.updateTransactionHistory
},
{
header: "Repo Status Changes",
element: <RepoStatusChangeHistoryTable accountNo={this.props.selectedAccount.accountNo}
repoStatusChanges={this.props.selectedAccount.repoStatusChanges}/>,
},
{
header: "Refund Status",
element: <RefundStatusTab selectedAccount={this.props.selectedAccount}/>
}
]}
/>
</div>
);
}
}
}
export const _AccountPageTabView = AccountPageTabView;
export default connect<IPropsFromState, null, {}, IAppState>(mapStateToProps, null)(AccountPageTabView);