fix: get fresh insurance companies before request

This commit is contained in:
vchikalkin 2023-05-23 20:49:42 +03:00
parent 3e774ab327
commit a065e49249
4 changed files with 149 additions and 149 deletions

View File

@ -6,6 +6,7 @@ import type { Row, StoreSelector } from './types';
import { getEltKasko } from '@/api/elt/query';
import { STALE_TIME } from '@/constants/request';
import { MAX_FRANCHISE, MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values';
import helper from '@/process/elt/lib/helper';
import { useStore } from '@/stores/hooks';
import { useApolloClient } from '@apollo/client';
import type { QueryFunctionContext } from '@tanstack/react-query';
@ -20,11 +21,14 @@ export function Kasko() {
const apolloClient = useApolloClient();
const { init } = helper({ apolloClient, store });
const queries = useQueries({
queries: $tables.elt.kasko.getRows.map((row) => {
const { id, key } = row;
return {
cacheTime: 0,
enabled: false,
initialData: {
...row,
@ -52,85 +56,90 @@ export function Kasko() {
});
async function handleOnClick() {
const { kasko } = await init();
$tables.elt.kasko.setRows(kasko);
const kaskoCompanyIds = $tables.insurance
.row('kasko')
.getOptions('insuranceCompany')
.map((x) => x.value);
const values = $calculation.$values.getValues();
const fetchingRows = $tables.elt.kasko.getRows.map((x) => ({
...x,
status: 'fetching',
sum: 0,
}));
$tables.elt.kasko.setRows(fetchingRows);
queries.forEach(({ refetch, data }) => {
refetch()
.then((res) => {
if (res.data) {
const {
key,
kaskoSum = 0,
message,
skCalcId,
totalFranchise = 0,
requestId,
paymentPeriods,
} = res.data;
let { error } = res.data;
queries
.filter(({ data }) => data?.key && kaskoCompanyIds.includes(data.key))
.forEach(({ refetch, data }) => {
if (data?.key) $tables.elt.kasko.setRow({ key: data?.key, status: 'fetching' });
if (totalFranchise > MAX_FRANCHISE) {
error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_FRANCHISE)}`;
refetch()
.then((res) => {
if (res.data) {
const {
key,
kaskoSum = 0,
message,
skCalcId,
totalFranchise = 0,
requestId,
paymentPeriods,
} = res.data;
let { error } = res.data;
if (totalFranchise > MAX_FRANCHISE) {
error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_FRANCHISE)}`;
}
if (kaskoSum > MAX_INSURANCE) {
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости КАСКО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_INSURANCE)}`;
}
if (kaskoSum < MIN_INSURANCE) {
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости КАСКО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MIN_INSURANCE)}`;
}
$tables.elt.kasko.setRow({
key,
message: error || message,
numCalc: 0,
requestId,
skCalcId,
status: error ? 'error' : null,
sum: values.leasingPeriod <= 16 ? kaskoSum : paymentPeriods.at(0)?.kaskoSum || 0,
totalFranchise,
});
}
if (kaskoSum > MAX_INSURANCE) {
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости КАСКО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_INSURANCE)}`;
}
if (kaskoSum < MIN_INSURANCE) {
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости КАСКО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MIN_INSURANCE)}`;
}
$tables.elt.kasko.setRow({
key,
message: error || message,
numCalc: 0,
requestId,
skCalcId,
status: error ? 'error' : null,
sum: values.leasingPeriod <= 16 ? kaskoSum : paymentPeriods.at(0)?.kaskoSum || 0,
totalFranchise,
});
}
})
.catch((error) => {
if (data?.key)
$tables.elt.kasko.setRow({
key: data?.key,
message: error,
numCalc: 0,
requestId: '',
skCalcId: '',
status: 'error',
sum: 0,
totalFranchise: 0,
});
});
});
})
.catch((error) => {
if (data?.key)
$tables.elt.kasko.setRow({
key: data?.key,
message: error,
numCalc: 0,
requestId: '',
skCalcId: '',
status: 'error',
sum: 0,
totalFranchise: 0,
});
});
});
}
function handleOnSelectRow(row: Row) {

View File

@ -6,6 +6,7 @@ import type { Row, StoreSelector } from './types';
import { getEltOsago } from '@/api/elt/query';
import { STALE_TIME } from '@/constants/request';
import { MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values';
import helper from '@/process/elt/lib/helper';
import { useStore } from '@/stores/hooks';
import { useApolloClient } from '@apollo/client';
import type { QueryFunctionContext } from '@tanstack/react-query';
@ -20,11 +21,14 @@ export function Osago() {
const apolloClient = useApolloClient();
const { init } = helper({ apolloClient, store });
const queries = useQueries({
queries: $tables.elt.osago.getRows.map((row) => {
const { id, key } = row;
return {
cacheTime: 0,
enabled: false,
initialData: { ...row, error: '', premiumSum: 0 },
queryFn: async (context: QueryFunctionContext) => {
@ -42,62 +46,67 @@ export function Osago() {
});
async function handleOnClick() {
const fetchingRows = $tables.elt.osago.getRows.map((x) => ({
...x,
status: 'fetching',
sum: 0,
}));
$tables.elt.osago.setRows(fetchingRows);
const { osago } = await init();
$tables.elt.osago.setRows(osago);
queries.forEach(({ refetch, data }) => {
refetch()
.then((res) => {
if (res.data) {
const { key, numCalc, premiumSum = 0, message, skCalcId } = res.data;
let { error } = res.data;
const osagoCompanyIds = $tables.insurance
.row('osago')
.getOptions('insuranceCompany')
.map((x) => x.value);
if (premiumSum > MAX_INSURANCE) {
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости ОСАГО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_INSURANCE)}`;
queries
.filter(({ data }) => data?.key && osagoCompanyIds.includes(data.key))
.forEach(({ refetch, data }) => {
if (data?.key) $tables.elt.osago.setRow({ key: data?.key, status: 'fetching' });
refetch()
.then((res) => {
if (res.data) {
const { key, numCalc, premiumSum = 0, message, skCalcId } = res.data;
let { error } = res.data;
if (premiumSum > MAX_INSURANCE) {
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости ОСАГО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MAX_INSURANCE)}`;
}
if (premiumSum < MIN_INSURANCE) {
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости ОСАГО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MIN_INSURANCE)}`;
}
$tables.elt.osago.setRow({
key,
message: error || message,
numCalc,
skCalcId,
status: error ? 'error' : null,
sum: premiumSum,
});
}
if (premiumSum < MIN_INSURANCE) {
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости ОСАГО: ${Intl.NumberFormat(
'ru',
{
currency: 'RUB',
style: 'currency',
}
).format(MIN_INSURANCE)}`;
}
$tables.elt.osago.setRow({
key,
message: error || message,
numCalc,
skCalcId,
status: error ? 'error' : null,
sum: premiumSum,
});
}
})
.catch((error) => {
if (data?.key)
$tables.elt.osago.setRow({
key: data?.key,
message: error,
numCalc: 0,
skCalcId: '',
status: 'error',
sum: 0,
});
});
});
})
.catch((error) => {
if (data?.key)
$tables.elt.osago.setRow({
key: data?.key,
message: error,
numCalc: 0,
skCalcId: '',
status: 'error',
sum: 0,
});
});
});
}
function handleOnSelectRow(row: Row) {

View File

@ -6,7 +6,7 @@ export default function helper({
apolloClient,
store,
}: Pick<ProcessContext, 'apolloClient' | 'store'>) {
const { $calculation, $tables } = store;
const { $calculation } = store;
return {
async init() {
@ -29,16 +29,6 @@ export default function helper({
({ evo_leasingobject_type } = data);
}
const kaskoCompanyIds = $tables.insurance
.row('kasko')
.getOptions('insuranceCompany')
.map((x) => x.value);
const osagoCompanyIds = $tables.insurance
.row('osago')
.getOptions('insuranceCompany')
.map((x) => x.value);
return {
kasko: (accounts
?.filter((x) =>
@ -48,7 +38,6 @@ export default function helper({
? Boolean(x.evo_id_elt_smr)
: Boolean(x?.evo_id_elt)
)
.filter((x) => x?.value && kaskoCompanyIds.includes(x?.value))
.map((x) => ({
id:
evo_leasingobject_type?.evo_id &&
@ -67,7 +56,6 @@ export default function helper({
})) || []) as Row[],
osago: (accounts
?.filter((x) => x?.evo_type_ins_policy?.includes(100_000_001) && x?.evo_id_elt_osago)
.filter((x) => x?.value && osagoCompanyIds.includes(x.value))
.map((x) => ({
id: x?.evo_id_elt_osago,
key: x?.value,

View File

@ -12,9 +12,6 @@ export default function reactions(context: ProcessContext) {
disposableReaction(
() => $process.has('ELT') || $process.has('LoadKP'),
() => ({
kasko: {
options: $tables.insurance.row('kasko').getOptions('insuranceCompany'),
},
values: $calculation.$values.getValues([
'objectRegistration',
'townRegistration',
@ -52,7 +49,7 @@ export default function reactions(context: ProcessContext) {
},
{
delay: 10,
equals: comparer.shallow,
equals: comparer.structural,
fireImmediately: true,
}
);
@ -60,9 +57,6 @@ export default function reactions(context: ProcessContext) {
disposableReaction(
() => $process.has('ELT') || $process.has('LoadKP'),
() => ({
osago: {
options: $tables.insurance.row('osago').getOptions('insuranceCompany'),
},
values: $calculation.$values.getValues([
'legalClientRegion',
'legalClientTown',
@ -107,7 +101,7 @@ export default function reactions(context: ProcessContext) {
},
{
delay: 10,
equals: comparer.shallow,
equals: comparer.structural,
fireImmediately: true,
}
);