ELT: add initial row obj

This commit is contained in:
vchikalkin 2023-07-31 13:42:01 +03:00
parent 94a39925d5
commit 80a0105a5a
4 changed files with 49 additions and 68 deletions

View File

@ -8,6 +8,7 @@ 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 { defaultRow } from '@/stores/tables/elt/default-values';
import { useApolloClient } from '@apollo/client';
import type { QueryFunctionContext } from '@tanstack/react-query';
import { useQueries } from '@tanstack/react-query';
@ -16,6 +17,18 @@ import { Flex } from 'ui/grid';
const storeSelector: StoreSelector = ({ kasko }) => kasko;
const initialData = {
...defaultRow,
error: null,
kaskoSum: 0,
paymentPeriods: [
{
kaskoSum: 0,
},
],
sum: 0,
};
export const Kasko = observer(() => {
const store = useStore();
const { $tables, $calculation } = store;
@ -26,22 +39,12 @@ export const Kasko = observer(() => {
const queries = useQueries({
queries: $tables.elt.kasko.getRows.map((row) => {
const { id, key } = row;
const { id, key, name } = row;
return {
cacheTime: 0,
enabled: false,
initialData: {
...row,
error: '',
kaskoSum: 0,
paymentPeriods: [
{
kaskoSum: 0,
},
],
sum: 0,
},
initialData: { ...initialData, id, key, name },
queryFn: async (context: QueryFunctionContext) => {
const payload = await makeEltKaskoRequest({ apolloClient, store }, row);
const res = await getEltKasko(payload, context);
@ -49,27 +52,10 @@ export const Kasko = observer(() => {
if (res) {
const companyRes = res?.[id];
return { ...companyRes, id, key };
return { ...companyRes, id, key, name };
}
return {
error: null,
id,
kaskoSum: 0,
key,
message: null,
numCalc: 0,
paymentPeriods: [
{
kaskoSum: 0,
},
],
requestId: '',
skCalcId: '',
status: null,
sum: 0,
totalFranchise: 0,
};
return { ...initialData, id, key, name };
},
queryKey: ['elt', 'kasko', id],
refetchOnWindowFocus: false,
@ -152,14 +138,10 @@ export const Kasko = observer(() => {
.catch((error) => {
if (data?.key)
$tables.elt.kasko.setRow({
...defaultRow,
key: data?.key,
message: error,
numCalc: 0,
requestId: '',
skCalcId: '',
status: 'error',
sum: 0,
totalFranchise: 0,
});
});
});

View File

@ -8,6 +8,7 @@ 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 { defaultRow } from '@/stores/tables/elt/default-values';
import { useApolloClient } from '@apollo/client';
import type { QueryFunctionContext } from '@tanstack/react-query';
import { useQueries } from '@tanstack/react-query';
@ -16,6 +17,13 @@ import { Flex } from 'ui/grid';
const storeSelector: StoreSelector = ({ osago }) => osago;
const initialData = {
...defaultRow,
error: null,
premiumSum: 0,
sum: 0,
};
export const Osago = observer(() => {
const store = useStore();
const { $tables } = store;
@ -26,12 +34,12 @@ export const Osago = observer(() => {
const queries = useQueries({
queries: $tables.elt.osago.getRows.map((row) => {
const { id, key } = row;
const { id, key, name } = row;
return {
cacheTime: 0,
enabled: false,
initialData: { ...row, error: '', premiumSum: 0 },
initialData: { ...initialData, id, key, name },
queryFn: async (context: QueryFunctionContext) => {
const payload = await makeEltOsagoRequest({ apolloClient, store }, row);
const res = await getEltOsago(payload, context);
@ -39,20 +47,10 @@ export const Osago = observer(() => {
if (res) {
const companyRes = res?.[id];
return { ...companyRes, id, key };
return { ...companyRes, id, key, name };
}
return {
error: null,
id,
key,
message: null,
numCalc: 0,
premiumSum: 0,
skCalcId: '',
status: null,
sum: 0,
};
return { ...initialData, id, key, name };
},
queryKey: ['elt', 'osago', id],
refetchOnWindowFocus: false,
@ -71,7 +69,7 @@ export const Osago = observer(() => {
.map((x) => x.value);
queries
.filter(({ data }) => data?.key && osagoCompanyIds.includes(data.key))
.filter(({ data }) => data?.key && osagoCompanyIds.includes(data?.key))
.forEach(({ refetch, data }) => {
if (data?.key) $tables.elt.osago.setRow({ key: data?.key, status: 'fetching' });
@ -114,12 +112,10 @@ export const Osago = observer(() => {
.catch((error) => {
if (data?.key)
$tables.elt.osago.setRow({
...defaultRow,
key: data?.key,
message: error,
numCalc: 0,
skCalcId: '',
status: 'error',
sum: 0,
});
});
});

View File

@ -1,6 +1,7 @@
import type { ProcessContext } from '../../types';
import type { Row } from '@/Components/Calculation/Form/ELT/types';
import * as CRMTypes from '@/graphql/crm.types';
import { defaultRow } from '@/stores/tables/elt/default-values';
export default function helper({
apolloClient,
@ -39,34 +40,22 @@ export default function helper({
: Boolean(x?.evo_id_elt)
)
.map((x) => ({
...defaultRow,
id:
evo_leasingobject_type?.evo_id &&
['6', '9', '10'].includes(evo_leasingobject_type?.evo_id)
? x?.evo_id_elt_smr
: x?.evo_id_elt,
key: x?.value,
message: null,
name: x?.label,
numCalc: 0,
requestId: '',
skCalcId: '',
status: null,
sum: 0,
totalFranchise: 0,
})) || []) as Row[],
osago: (accounts
?.filter((x) => x?.evo_type_ins_policy?.includes(100_000_001) && x?.evo_id_elt_osago)
.map((x) => ({
...defaultRow,
id: x?.evo_id_elt_osago,
key: x?.value,
message: null,
name: x?.label,
numCalc: 0,
requestId: '',
skCalcId: '',
status: null,
sum: 0,
totalFranchise: 0,
})) || []) as Row[],
};
},

View File

@ -0,0 +1,14 @@
import type * as ELT from '@/Components/Calculation/Form/ELT/types';
export const defaultRow: ELT.Row = {
id: '',
key: '',
message: null,
name: '',
numCalc: 0,
requestId: '',
skCalcId: '',
status: null,
sum: 0,
totalFranchise: 0,
};