2022-07-11 16:44:01 +03:00

109 lines
2.7 KiB
TypeScript

/* eslint-disable import/prefer-default-export */
import type { ApolloClient, NormalizedCache } from '@apollo/client';
import { gql } from '@apollo/client';
import { getDomainName } from 'services/user/tools';
import type { User } from 'services/user/types';
import { normalizeOptions } from 'tools/entity';
import type { GetInsuranceData } from './__generated__/GetInsuranceData';
import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData';
import type { GetTransactionCurrencies } from './__generated__/GetTransactionCurrencies';
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_TRANSACTION_CURRENCIES = gql`
query GetTransactionCurrencies {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
}
`;
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: { selectSupplierCurrency } } = await apolloClient.query<GetTransactionCurrencies>({
query: QUERY_GET_TRANSACTION_CURRENCIES,
});
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),
},
};
return {
options: {
selectLead,
selectOpportunity,
selectSupplierCurrency,
},
tables: {
insurance: insuranceData,
},
};
}