2022-07-12 16:20:18 +03:00

252 lines
6.5 KiB
TypeScript
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 type { ApolloClient, NormalizedCache } from '@apollo/client';
import { gql } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { getDomainName } from 'services/user/tools';
import type { User } from 'services/user/types';
import { normalizeOptions } from 'tools/entity';
import type { GetAddproductTypes } from './__generated__/GetAddproductTypes';
import type { GetInsuranceData } from './__generated__/GetInsuranceData';
import type { GetMainOptions } from './__generated__/GetMainOptions';
import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData';
import type { GetRegions } from './__generated__/GetRegions';
import type { GetSubsidies } from './__generated__/GetSubsidies';
dayjs.extend(utc);
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
selectLead: leads(owner_domainname: $domainname) {
label: fullname
value: leadid
}
selectOpportunity: opportunities(owner_domainname: $domainname) {
label: name
value: opportunityid
}
}
`;
const QUERY_GET_MAIN_OPTIONS = gql`
query GetMainOptions($currentDate: DateTime) {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
selectProduct: evo_baseproducts(
statecode: 0
evo_relation: [100000000]
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_baseproductid
}
selectLeaseObjectType: evo_leasingobject_types(statecode: 0) {
label: evo_name
value: evo_leasingobject_typeid
}
selectBrand: evo_brands(statecode: 0) {
label: evo_name
value: evo_brandid
}
selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) {
label: name
value: accountid
}
selectGPSBrand: evo_gps_brands(statecode: 0) {
label: evo_name
value: evo_gps_brandid
}
}
`;
const QUERY_GET_SUBSIDIES = gql`
query GetSubsidies($currentDate: DateTime) {
evo_subsidies(
statecode: 0
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_subsidyid
evo_subsidy_type
}
}
`;
const QUERY_GET_REGIONS = gql`
query GetRegions {
evo_regions {
label: evo_name
value: evo_regionid
}
}
`;
const QUERY_GET_ADDPRODUCT_TYPES = gql`
query GetAddproductTypes {
evo_addproduct_types(statecode: 0) {
label: evo_name
value: evo_addproduct_typeid
evo_graph_price
evo_product_type
}
}
`;
const QUERY_GET_INSURANCE_DATA = gql`
query GetInsuranceData($evo_account_type: [Int!]) {
osago: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000001]
statecode: 0
) {
value: accountid
label: name
}
kasko: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000000]
statecode: 0
) {
value: accountid
label: name
}
fingap: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000002]
statecode: 0
) {
value: accountid
label: name
}
}
`;
export async function getCRMData(apolloClient: ApolloClient<NormalizedCache>, user: User) {
const {
data: { selectLead, selectOpportunity },
} = await apolloClient.query<GetOwnerData, GetOwnerDataVariables>({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: getDomainName(user),
},
});
// prettier-ignore
const { data: options } = await apolloClient.query<GetMainOptions>({
query: QUERY_GET_MAIN_OPTIONS,
variables: {
currentDate: dayjs().utc().toISOString()
}
});
const {
data: { kasko, osago, fingap },
} = await apolloClient.query<GetInsuranceData>({
query: QUERY_GET_INSURANCE_DATA,
});
const insuranceData = {
osago: {
insuranceCompany: normalizeOptions(osago),
},
kasko: {
insuranceCompany: normalizeOptions(kasko),
},
fingap: {
insuranceCompany: normalizeOptions(fingap),
},
};
const { data: subsidies } = await apolloClient.query<GetSubsidies>({
query: QUERY_GET_SUBSIDIES,
variables: {
currentDate: dayjs().utc().toISOString(),
},
});
const selectSubsidy = subsidies.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type)
);
const selectImportProgram = subsidies.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type)
);
const { data: regions } = await apolloClient.query<GetRegions>({
query: QUERY_GET_REGIONS,
});
const selectRegionRegistration = regions.evo_regions;
const selectObjectRegionRegistration = regions.evo_regions;
const selectLegalClientRegion = regions.evo_regions;
const { data: addproductTypes } = await apolloClient.query<GetAddproductTypes>({
query: QUERY_GET_ADDPRODUCT_TYPES,
});
const selectRegistration = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_001)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTechnicalCard = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_000)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTelematic = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_004)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTracker = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_003)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectInsNSIB = addproductTypes.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_002
);
return {
options: {
selectLead,
selectOpportunity,
...options,
selectSubsidy,
selectImportProgram,
selectRegionRegistration,
selectObjectRegionRegistration,
selectLegalClientRegion,
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
},
tables: {
insurance: insuranceData,
},
};
}