116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import type { CalculationValues } from '@/stores/calculation/values/types';
|
|
import { gql } from '@apollo/client';
|
|
|
|
const QUERY_GET_QUOTE_REGION_TOWN = gql`
|
|
query GetQuoteRegionTown($quoteId: Uuid!) {
|
|
quote(quoteId: $quoteId) {
|
|
evo_regionid
|
|
evo_townid
|
|
evo_legal_regionid
|
|
evo_legal_townid
|
|
}
|
|
}
|
|
`;
|
|
|
|
export default function helper({ apolloClient }: Pick<ProcessContext, 'apolloClient'>) {
|
|
async function getRegionByFias(evo_region_fias_id: string) {
|
|
const {
|
|
data: { evo_regions },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetRegionsDocument,
|
|
});
|
|
|
|
return evo_regions?.find((x) => x?.evo_fias_id === evo_region_fias_id);
|
|
}
|
|
|
|
async function getTownByFias(regionId: string, evo_town_fias_id: string) {
|
|
const {
|
|
data: { evo_towns },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetTownsDocument,
|
|
variables: {
|
|
regionId,
|
|
},
|
|
});
|
|
|
|
return evo_towns?.find((x) => x?.evo_fias_id === evo_town_fias_id);
|
|
}
|
|
|
|
return {
|
|
async getData({
|
|
lead: leadid,
|
|
opportunity: opportunityid,
|
|
quote: quoteId,
|
|
}: Pick<CalculationValues, 'lead' | 'opportunity' | 'quote'>) {
|
|
let lead: CRMTypes.GetLeadQuery['lead'] = null;
|
|
let opportunity: CRMTypes.GetOpportunityQuery['opportunity'] = null;
|
|
let quote: CRMTypes.GetQuoteRegionTownQuery['quote'] = null;
|
|
|
|
if (leadid) {
|
|
const { data } = await apolloClient.query({
|
|
query: CRMTypes.GetLeadDocument,
|
|
variables: { leadid },
|
|
});
|
|
({ lead } = data);
|
|
}
|
|
|
|
if (opportunityid) {
|
|
const { data } = await apolloClient.query({
|
|
query: CRMTypes.GetOpportunityDocument,
|
|
variables: { opportunityid },
|
|
});
|
|
|
|
({ opportunity } = data);
|
|
}
|
|
|
|
if (quoteId) {
|
|
const { data } = await apolloClient.query<
|
|
CRMTypes.GetQuoteRegionTownQuery,
|
|
CRMTypes.GetQuoteRegionTownQueryVariables
|
|
>({
|
|
query: QUERY_GET_QUOTE_REGION_TOWN,
|
|
variables: { quoteId },
|
|
});
|
|
({ quote } = data);
|
|
}
|
|
|
|
// region
|
|
let region: Awaited<ReturnType<typeof getRegionByFias>> = null;
|
|
const evo_region_fias_id =
|
|
lead?.accountidData?.evo_address_legalidData?.evo_region_fias_id ||
|
|
opportunity?.accountidData?.evo_address_legalidData?.evo_region_fias_id;
|
|
|
|
if (evo_region_fias_id) {
|
|
region = await getRegionByFias(evo_region_fias_id);
|
|
}
|
|
|
|
// town
|
|
let town: Awaited<ReturnType<typeof getTownByFias>> = null;
|
|
const evo_city_fias_id =
|
|
lead?.accountidData?.evo_address_legalidData?.evo_city_fias_id ||
|
|
opportunity?.accountidData?.evo_address_legalidData?.evo_city_fias_id;
|
|
|
|
if (region?.value && evo_city_fias_id) {
|
|
town = await getTownByFias(region?.value, evo_city_fias_id);
|
|
}
|
|
|
|
return {
|
|
account: {
|
|
evo_legal_regionid: region?.value,
|
|
evo_legal_townid: town?.value,
|
|
evo_regionid: region?.value,
|
|
evo_townid: town?.value,
|
|
},
|
|
quote: {
|
|
evo_legal_regionid: quote?.evo_legal_regionid,
|
|
evo_legal_townid: quote?.evo_legal_townid,
|
|
evo_regionid: quote?.evo_regionid,
|
|
evo_townid: quote?.evo_townid,
|
|
},
|
|
};
|
|
},
|
|
};
|
|
}
|