2023-02-03 08:30:24 +03:00

205 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable import/prefer-default-export */
import { gql, useApolloClient } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import * as CRMTypes from 'graphql/crm.types';
import { useEffect } from 'react';
import { useStore } from 'stores/hooks';
import { normalizeOptions } from 'tools';
dayjs.extend(utc);
const QUERY_GET_ADDPRODUCT_TYPES = gql`
query GetAddproductTypes($currentDate: DateTime) {
evo_addproduct_types(
statecode: 0
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_addproduct_typeid
evo_graph_price
evo_product_type
}
}
`;
const currentDate = dayjs().utc(false).toISOString();
/**
*
* @param {import('@apollo/client').ApolloClient['query']} query
* @param {*} onCompleted
*/
function getMainData(query, onCompleted) {
query({
query: CRMTypes.GetTransactionCurrenciesDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectSupplierCurrency: data?.transactioncurrencies,
});
});
query({
query: CRMTypes.GetLeaseObjectTypesDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectLeaseObjectType: data?.evo_baseproducts,
});
});
query({
query: CRMTypes.GetGpsBrandsDocument,
}).then(({ data }) => {
onCompleted({
selectGPSBrand: data?.evo_gps_brands,
});
});
query({
query: CRMTypes.GetProductsDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectProduct: data?.evo_baseproducts,
});
});
query({
query: CRMTypes.GetSubsidiesDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
const selectSubsidy = data?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type)
);
const selectImportProgram = data?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type)
);
onCompleted({
selectSubsidy,
selectImportProgram,
});
});
query({
query: CRMTypes.GetRegionsDocument,
}).then(({ data }) => {
const selectRegionRegistration = data?.evo_regions;
const selectObjectRegionRegistration = data?.evo_regions;
const selectLegalClientRegion = data?.evo_regions;
onCompleted({
selectRegionRegistration,
selectObjectRegionRegistration,
selectLegalClientRegion,
});
});
query({
query: CRMTypes.GetBrandsDocument,
}).then(({ data }) => {
onCompleted({
selectBrand: data?.evo_brands,
});
});
query({
query: CRMTypes.GetDealersDocument,
}).then(({ data }) => {
onCompleted({
selectDealer: data?.dealers,
});
});
query({
query: QUERY_GET_ADDPRODUCT_TYPES,
variables: {
currentDate,
},
}).then(({ data }) => {
const selectRegistration = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_001)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTechnicalCard = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_000)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTelematic = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_004)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTracker = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_003)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectInsNSIB = data.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_002
);
const selectLeasingWithoutKasko = data.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_007
);
onCompleted({
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
selectLeasingWithoutKasko,
});
});
query({
query: CRMTypes.GetTarifsDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectTarif: data?.evo_tarifs,
});
});
}
export function useMainData() {
const { $calculation } = useStore();
const { query } = useApolloClient();
function handleOnCompleted(options) {
Object.keys(options).forEach((elementName) => {
const elementOptions = options[elementName];
$calculation.element(elementName).setOptions(normalizeOptions(elementOptions));
});
}
useEffect(() => {
getMainData(query, handleOnCompleted);
}, []);
}