85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import type { CalculationValues } from '@/stores/calculation/values/types';
|
|
|
|
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,
|
|
}: Pick<CalculationValues, 'lead' | 'opportunity'>) {
|
|
let lead: CRMTypes.GetLeadQuery['lead'] = null;
|
|
let opportunity: CRMTypes.GetOpportunityQuery['opportunity'] = 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);
|
|
}
|
|
|
|
// 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,
|
|
},
|
|
};
|
|
},
|
|
};
|
|
}
|