2023-04-21 10:59:25 +02:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
|
|
|
import { captureException } from '@sentry/browser';
|
|
|
|
import { mmiActionsFactory } from '../../store/institutional/institution-background';
|
|
|
|
|
|
|
|
const name = 'institutionalFeatures';
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
historicalReports: {},
|
|
|
|
complianceProjectId: '',
|
|
|
|
complianceClientId: '',
|
|
|
|
reportsInProgress: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const slice = createSlice({
|
|
|
|
name,
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
setHistoricalReports(state, action) {
|
|
|
|
state.historicalReports[action.payload.address] = [
|
|
|
|
...action.payload.reports,
|
|
|
|
];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { actions, reducer } = slice;
|
|
|
|
|
|
|
|
export default reducer;
|
|
|
|
|
|
|
|
export const getComplianceProjectId = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.complianceProjectId;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const getComplianceClientId = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.complianceClientId;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const getComplianceTenantSubdomain = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.complianceTenantSubdomain;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const getComplianceHistoricalReports = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.historicalReports;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const getComplianceReportsInProgress = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.reportsInProgress;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const getInstitutionalConnectRequests = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
state.metamask[name]?.connectRequests;
|
2023-04-21 10:59:25 +02:00
|
|
|
export const complianceActivated = (state) =>
|
2023-05-05 14:07:51 +02:00
|
|
|
Boolean(state.metamask[name]?.complianceProjectId);
|
2023-04-21 10:59:25 +02:00
|
|
|
|
|
|
|
export const getComplianceHistoricalReportsByAddress = (address) =>
|
|
|
|
createSelector(getComplianceHistoricalReports, (reports) =>
|
|
|
|
reports ? reports[address] : [],
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getComplianceReportsInProgressByAddress = (address) =>
|
|
|
|
createSelector(getComplianceReportsInProgress, (reports) =>
|
|
|
|
reports ? reports[address.toLowerCase()] : undefined,
|
|
|
|
);
|
|
|
|
|
|
|
|
export const fetchHistoricalReports = (address, testProjectId = undefined) => {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const mmiActions = mmiActionsFactory();
|
|
|
|
|
|
|
|
let projectId;
|
|
|
|
|
|
|
|
// testProjectId is provided to make a test request, which checks if projectId is correct
|
|
|
|
if (!testProjectId) {
|
|
|
|
projectId = getComplianceProjectId(state);
|
|
|
|
if (!projectId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await dispatch(
|
|
|
|
mmiActions.getComplianceHistoricalReportsByAddress(address, projectId),
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
mmiActions.syncReportsInProgress({
|
|
|
|
address,
|
|
|
|
historicalReports: result.items ? result.items : [],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
dispatch(
|
|
|
|
actions.setHistoricalReports({
|
|
|
|
address,
|
|
|
|
reports: result.items
|
|
|
|
? result.items.filter((report) => report.status !== 'inProgress')
|
|
|
|
: [],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
captureException(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export function generateComplianceReport(address) {
|
|
|
|
return (dispatch, _getState) => {
|
|
|
|
const mmiActions = mmiActionsFactory();
|
|
|
|
dispatch(mmiActions.generateComplianceReport(address));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { setHistoricalReports } = actions;
|
|
|
|
|
|
|
|
export { setHistoricalReports };
|