diff --git a/apps/web/process/gibdd/get-kp-data.ts b/apps/web/process/gibdd/get-kp-data.ts index 3afc3ef..5a1ce6f 100644 --- a/apps/web/process/gibdd/get-kp-data.ts +++ b/apps/web/process/gibdd/get-kp-data.ts @@ -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) { + 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) { - 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 & { - 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) { - 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 & { - 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> = 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> = 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, + }, + }; }, }; } diff --git a/apps/web/process/gibdd/reactions.ts b/apps/web/process/gibdd/reactions.ts index 8298bd7..dd7927c 100644 --- a/apps/web/process/gibdd/reactions.ts +++ b/apps/web/process/gibdd/reactions.ts @@ -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, } );