289 lines
7.9 KiB
TypeScript
289 lines
7.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
|
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'>) {
|
|
return {
|
|
async getLegalRegion({
|
|
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_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;
|
|
}
|
|
}
|
|
|
|
if (quoteId) {
|
|
const {
|
|
data: { quote },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetQuoteRegionTownQuery,
|
|
CRMTypes.GetQuoteRegionTownQueryVariables
|
|
>({
|
|
query: QUERY_GET_QUOTE_REGION_TOWN,
|
|
variables: { quoteId },
|
|
});
|
|
if (!townId && quote?.evo_townid) {
|
|
townId = quote?.evo_townid;
|
|
}
|
|
}
|
|
|
|
return townId;
|
|
},
|
|
};
|
|
}
|