fix regions pt.1
This commit is contained in:
parent
28d99c15b4
commit
0e337a498a
@ -1,3 +1,4 @@
|
||||
/* eslint-disable sonarjs/cognitive-complexity */
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
@ -41,34 +42,14 @@ export async function getKPData({ values }: GetQuoteInputData): Promise<GetQuote
|
||||
},
|
||||
});
|
||||
|
||||
const { getLegalRegion, getLegalTown, getRegion, getTown } = helper({
|
||||
const { getData } = helper({
|
||||
apolloClient,
|
||||
});
|
||||
|
||||
const legalRegionId = await getLegalRegion({
|
||||
const { account } = await getData({
|
||||
lead: leadId,
|
||||
opportunity: opportunityId,
|
||||
quote: quoteId,
|
||||
});
|
||||
|
||||
const legalTownId = await getLegalTown({
|
||||
lead: leadId,
|
||||
opportunity: opportunityId,
|
||||
quote: quoteId,
|
||||
regionId: legalRegionId,
|
||||
});
|
||||
|
||||
const regionId = await getRegion({
|
||||
lead: leadId,
|
||||
opportunity: opportunityId,
|
||||
quote: quoteId,
|
||||
});
|
||||
|
||||
const townId = await getTown({
|
||||
lead: leadId,
|
||||
opportunity: opportunityId,
|
||||
quote: quoteId,
|
||||
regionId,
|
||||
quote: null,
|
||||
});
|
||||
|
||||
const objectRegistration = recalcWithRevision
|
||||
@ -77,19 +58,41 @@ export async function getKPData({ values }: GetQuoteInputData): Promise<GetQuote
|
||||
|
||||
const typePTS = objectRegistration === 100_000_000 ? defaultValues.typePTS : quote?.evo_pts_type;
|
||||
|
||||
// region
|
||||
let legalClientRegion = quote?.evo_legal_regionid ?? defaultValues.legalClientRegion;
|
||||
if (account?.evo_legal_regionid) {
|
||||
legalClientRegion = account.evo_legal_regionid;
|
||||
}
|
||||
|
||||
let regionRegistration = quote?.evo_regionid ?? defaultValues.regionRegistration;
|
||||
if (objectRegistration === 100_000_000 && account?.evo_legal_regionid) {
|
||||
regionRegistration = account.evo_legal_regionid;
|
||||
}
|
||||
|
||||
// town
|
||||
let legalClientTown = quote?.evo_legal_townid ?? defaultValues.legalClientTown;
|
||||
if (account?.evo_legal_townid) {
|
||||
legalClientTown = account?.evo_legal_townid;
|
||||
}
|
||||
|
||||
let townRegistration = quote?.evo_townid ?? defaultValues.townRegistration;
|
||||
if (objectRegistration === 100_000_000 && account?.evo_legal_townid) {
|
||||
townRegistration = account?.evo_legal_townid;
|
||||
}
|
||||
|
||||
return {
|
||||
values: {
|
||||
legalClientRegion: legalRegionId,
|
||||
legalClientTown: legalTownId,
|
||||
legalClientRegion,
|
||||
legalClientTown,
|
||||
objectCategoryTax: quote?.evo_category_tr,
|
||||
objectRegionRegistration: quote?.evo_registration_regionid,
|
||||
objectRegistration,
|
||||
objectTypeTax: quote?.evo_vehicle_type_tax,
|
||||
regionRegistration: regionId,
|
||||
regionRegistration,
|
||||
requirementTelematic:
|
||||
(recalcWithRevision ? quote?.evo_req_telematic_accept : quote?.evo_req_telematic) ??
|
||||
defaultValues.requirementTelematic,
|
||||
townRegistration: townId,
|
||||
townRegistration,
|
||||
typePTS,
|
||||
vehicleTaxInYear:
|
||||
(recalcWithRevision ? quote?.evo_vehicle_tax_approved : quote?.evo_vehicle_tax_year) ??
|
||||
|
||||
@ -15,273 +15,101 @@ const QUERY_GET_QUOTE_REGION_TOWN = gql`
|
||||
`;
|
||||
|
||||
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 getLegalRegion({
|
||||
async getData({
|
||||
lead: leadid,
|
||||
opportunity: opportunityid,
|
||||
quote: quoteId,
|
||||
}: Pick<CalculationValues, 'lead' | 'opportunity' | 'quote'>) {
|
||||
let regionId: string | null | undefined;
|
||||
|
||||
const {
|
||||
data: { evo_regions },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetRegionsDocument,
|
||||
});
|
||||
let lead: CRMTypes.GetLeadQuery['lead'] = null;
|
||||
let opportunity: CRMTypes.GetOpportunityQuery['opportunity'] = null;
|
||||
let quote: CRMTypes.GetQuoteRegionTownQuery['quote'] = null;
|
||||
|
||||
if (leadid) {
|
||||
const {
|
||||
data: { lead },
|
||||
} = await apolloClient.query({
|
||||
const { data } = await apolloClient.query({
|
||||
query: CRMTypes.GetLeadDocument,
|
||||
variables: { leadid },
|
||||
});
|
||||
if (lead?.accountidData?.evo_address_legalidData?.evo_region_fias_id) {
|
||||
const region = evo_regions?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id === lead?.accountidData?.evo_address_legalidData?.evo_region_fias_id
|
||||
);
|
||||
regionId = region?.value;
|
||||
}
|
||||
({ lead } = data);
|
||||
}
|
||||
|
||||
if (opportunityid) {
|
||||
const {
|
||||
data: { opportunity },
|
||||
} = await apolloClient.query({
|
||||
const { data } = await apolloClient.query({
|
||||
query: CRMTypes.GetOpportunityDocument,
|
||||
variables: { opportunityid },
|
||||
});
|
||||
|
||||
if (!regionId && opportunity?.accountidData?.evo_address_legalidData?.evo_region_fias_id) {
|
||||
const region = evo_regions?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id ===
|
||||
opportunity?.accountidData?.evo_address_legalidData?.evo_region_fias_id
|
||||
);
|
||||
regionId = region?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!regionId && quoteId) {
|
||||
const {
|
||||
data: { quote },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetQuoteRegionTownQuery,
|
||||
CRMTypes.GetQuoteRegionTownQueryVariables
|
||||
>({
|
||||
query: QUERY_GET_QUOTE_REGION_TOWN,
|
||||
variables: { quoteId },
|
||||
});
|
||||
regionId = quote?.evo_legal_regionid;
|
||||
}
|
||||
|
||||
return regionId;
|
||||
},
|
||||
|
||||
async getLegalTown({
|
||||
lead: leadid,
|
||||
opportunity: opportunityid,
|
||||
quote: quoteId,
|
||||
regionId,
|
||||
}: Pick<CalculationValues, 'lead' | 'opportunity' | 'quote'> & {
|
||||
regionId: string | null | undefined;
|
||||
}) {
|
||||
if (!regionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let townId: string | null | undefined;
|
||||
|
||||
const {
|
||||
data: { evo_towns },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTownsDocument,
|
||||
variables: { regionId },
|
||||
});
|
||||
|
||||
if (leadid) {
|
||||
const {
|
||||
data: { lead },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeadDocument,
|
||||
variables: { leadid },
|
||||
});
|
||||
if (lead?.accountidData?.evo_address_legalidData?.evo_city_fias_id) {
|
||||
const town = evo_towns?.find(
|
||||
(x) => x?.evo_fias_id === lead?.accountidData?.evo_address_legalidData?.evo_city_fias_id
|
||||
);
|
||||
townId = town?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (opportunityid) {
|
||||
const {
|
||||
data: { opportunity },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetOpportunityDocument,
|
||||
variables: { opportunityid },
|
||||
});
|
||||
|
||||
if (!townId && opportunity?.accountidData?.evo_address_legalidData?.evo_city_fias_id) {
|
||||
const town = evo_towns?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id ===
|
||||
opportunity?.accountidData?.evo_address_legalidData?.evo_city_fias_id
|
||||
);
|
||||
townId = town?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!townId && quoteId) {
|
||||
const {
|
||||
data: { quote },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetQuoteRegionTownQuery,
|
||||
CRMTypes.GetQuoteRegionTownQueryVariables
|
||||
>({
|
||||
query: QUERY_GET_QUOTE_REGION_TOWN,
|
||||
variables: { quoteId },
|
||||
});
|
||||
townId = quote?.evo_legal_townid;
|
||||
}
|
||||
|
||||
return townId;
|
||||
},
|
||||
|
||||
async getRegion({
|
||||
lead: leadid,
|
||||
opportunity: opportunityid,
|
||||
quote: quoteId,
|
||||
}: Pick<CalculationValues, 'lead' | 'opportunity' | 'quote'>) {
|
||||
let regionId: string | null | undefined;
|
||||
|
||||
const {
|
||||
data: { evo_regions },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetRegionsDocument,
|
||||
});
|
||||
|
||||
if (leadid) {
|
||||
const {
|
||||
data: { lead },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeadDocument,
|
||||
variables: { leadid },
|
||||
});
|
||||
if (lead?.accountidData?.evo_address_legalidData?.evo_region_fias_id) {
|
||||
const region = evo_regions?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id === lead?.accountidData?.evo_address_legalidData?.evo_region_fias_id
|
||||
);
|
||||
regionId = region?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (opportunityid) {
|
||||
const {
|
||||
data: { opportunity },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetOpportunityDocument,
|
||||
variables: { opportunityid },
|
||||
});
|
||||
|
||||
if (!regionId && opportunity?.accountidData?.evo_address_legalidData?.evo_region_fias_id) {
|
||||
const region = evo_regions?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id ===
|
||||
opportunity?.accountidData?.evo_address_legalidData?.evo_region_fias_id
|
||||
);
|
||||
regionId = region?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!regionId && quoteId) {
|
||||
const {
|
||||
data: { quote },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetQuoteRegionTownQuery,
|
||||
CRMTypes.GetQuoteRegionTownQueryVariables
|
||||
>({
|
||||
query: QUERY_GET_QUOTE_REGION_TOWN,
|
||||
variables: { quoteId },
|
||||
});
|
||||
regionId = quote?.evo_regionid;
|
||||
}
|
||||
|
||||
return regionId;
|
||||
},
|
||||
|
||||
async getTown({
|
||||
lead: leadid,
|
||||
opportunity: opportunityid,
|
||||
quote: quoteId,
|
||||
regionId,
|
||||
}: Pick<CalculationValues, 'lead' | 'opportunity' | 'quote'> & {
|
||||
regionId: string | null | undefined;
|
||||
}) {
|
||||
if (!regionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let townId: string | null | undefined;
|
||||
|
||||
const {
|
||||
data: { evo_towns },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTownsDocument,
|
||||
variables: { regionId },
|
||||
});
|
||||
|
||||
if (leadid) {
|
||||
const {
|
||||
data: { lead },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeadDocument,
|
||||
variables: { leadid },
|
||||
});
|
||||
if (lead?.accountidData?.evo_address_legalidData?.evo_city_fias_id) {
|
||||
const town = evo_towns?.find(
|
||||
(x) => x?.evo_fias_id === lead?.accountidData?.evo_address_legalidData?.evo_city_fias_id
|
||||
);
|
||||
townId = town?.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (opportunityid) {
|
||||
const {
|
||||
data: { opportunity },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetOpportunityDocument,
|
||||
variables: { opportunityid },
|
||||
});
|
||||
|
||||
if (!townId && opportunity?.accountidData?.evo_address_legalidData?.evo_city_fias_id) {
|
||||
const town = evo_towns?.find(
|
||||
(x) =>
|
||||
x?.evo_fias_id ===
|
||||
opportunity?.accountidData?.evo_address_legalidData?.evo_city_fias_id
|
||||
);
|
||||
townId = town?.value;
|
||||
}
|
||||
({ opportunity } = data);
|
||||
}
|
||||
|
||||
if (quoteId) {
|
||||
const {
|
||||
data: { quote },
|
||||
} = await apolloClient.query<
|
||||
const { data } = await apolloClient.query<
|
||||
CRMTypes.GetQuoteRegionTownQuery,
|
||||
CRMTypes.GetQuoteRegionTownQueryVariables
|
||||
>({
|
||||
query: QUERY_GET_QUOTE_REGION_TOWN,
|
||||
variables: { quoteId },
|
||||
});
|
||||
if (!townId && quote?.evo_townid) {
|
||||
townId = quote?.evo_townid;
|
||||
}
|
||||
({ quote } = data);
|
||||
}
|
||||
|
||||
return townId;
|
||||
// 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,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -377,114 +377,30 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) {
|
||||
}
|
||||
);
|
||||
|
||||
const { getLegalRegion, getLegalTown, getRegion, getTown } = helper({ apolloClient });
|
||||
const { getData } = helper({ apolloClient });
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['lead', 'opportunity']),
|
||||
async ({ lead, opportunity }) => {
|
||||
if (!lead && !opportunity) {
|
||||
$calculation.element('selectLegalClientRegion').resetValue().unblock();
|
||||
$calculation.element('selectRegionRegistration').resetValue().unblock();
|
||||
const { account } = await getData({ lead, opportunity, quote: null });
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const legalRegionId = await getLegalRegion({
|
||||
lead,
|
||||
opportunity,
|
||||
quote: null,
|
||||
});
|
||||
if (legalRegionId) {
|
||||
$calculation.element('selectLegalClientRegion').setValue(legalRegionId).block();
|
||||
if (account.evo_legal_regionid) {
|
||||
$calculation
|
||||
.element('selectLegalClientRegion')
|
||||
.setValue(account.evo_legal_regionid)
|
||||
.block();
|
||||
} else {
|
||||
$calculation.element('selectLegalClientRegion').resetValue().unblock();
|
||||
}
|
||||
|
||||
const regionId = await getRegion({
|
||||
lead,
|
||||
opportunity,
|
||||
quote: null,
|
||||
});
|
||||
if (regionId) {
|
||||
$calculation.element('selectRegionRegistration').setValue(regionId).block();
|
||||
if (account.evo_legal_townid) {
|
||||
$calculation.element('selectLegalClientTown').setValue(account.evo_legal_townid).block();
|
||||
} else {
|
||||
$calculation.element('selectRegionRegistration').resetValue().unblock();
|
||||
$calculation.element('selectLegalClientTown').resetValue().unblock();
|
||||
}
|
||||
},
|
||||
{
|
||||
delay: 10,
|
||||
}
|
||||
);
|
||||
|
||||
disposableReaction(
|
||||
() => $process.has('LoadKP'),
|
||||
() => $calculation.element('selectLegalClientRegion').getValue(),
|
||||
async (regionId) => {
|
||||
if (!regionId) {
|
||||
$calculation.element('selectLegalClientTown').resetValue().unblock();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const lead = $calculation.element('selectLead').getValue();
|
||||
const opportunity = $calculation.element('selectOpportunity').getValue();
|
||||
|
||||
const townId = await getLegalTown({
|
||||
lead,
|
||||
opportunity,
|
||||
quote: null,
|
||||
regionId,
|
||||
});
|
||||
if (townId) {
|
||||
$calculation.element('selectLegalClientTown').setValue(townId).block();
|
||||
} else {
|
||||
$calculation.element('selectLegalClientTown').resetValue().unblock();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['regionRegistration', 'objectRegistration']),
|
||||
async ({ regionRegistration: regionId, objectRegistration }) => {
|
||||
if (!regionId) {
|
||||
$calculation.element('selectTownRegistration').resetValue().unblock();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const lead = $calculation.element('selectLead').getValue();
|
||||
const opportunity = $calculation.element('selectOpportunity').getValue();
|
||||
|
||||
const townId = await getTown({
|
||||
lead,
|
||||
opportunity,
|
||||
quote: null,
|
||||
regionId,
|
||||
});
|
||||
|
||||
if (townId && objectRegistration === 100_000_000) {
|
||||
$calculation.element('selectTownRegistration').setValue(townId).block();
|
||||
} else {
|
||||
$calculation.element('selectTownRegistration').resetValue().unblock();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.element('radioObjectRegistration').getValue(),
|
||||
async (objectRegistration) => {
|
||||
const {
|
||||
data: { evo_regions },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetRegionsDocument,
|
||||
});
|
||||
|
||||
if (objectRegistration === 100_000_001) {
|
||||
const regions = evo_regions?.filter((x) => x?.evo_businessunit_evolution === true);
|
||||
$calculation.element('selectRegionRegistration').setOptions(normalizeOptions(regions));
|
||||
} else {
|
||||
$calculation.element('selectRegionRegistration').setOptions(normalizeOptions(evo_regions));
|
||||
}
|
||||
delay: 50,
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user