diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index 04b79a6..ac01d3b 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -502,7 +502,7 @@ export type GetQuoteConfiguratorDataQueryVariables = Exact<{ }>; -export type GetQuoteConfiguratorDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_client_typeid: string | null, evo_msfo_irr: number | null } | null }; +export type GetQuoteConfiguratorDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_client_typeid: string | null, evo_msfo_irr: number | null, evo_delivery_time: number | null, evo_first_payment_perc: number | null, evo_last_payment_perc: number | null, evo_leasingobject_typeid: string | null, evo_leasingobject_used: boolean | null, evo_period: number | null, evo_accept_period: number | null } | null }; export type GetQuoteCreateKpDataQueryVariables = Exact<{ quoteId: Scalars['Uuid']; @@ -666,7 +666,7 @@ export const GetInsuranceCompaniesDocument = {"kind":"Document","definitions":[{ export const GetRolesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetRoles"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"roleName"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"roles"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"name"},"value":{"kind":"Variable","name":{"kind":"Name","value":"roleName"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"systemusers"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"fullname"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"domainname"}}]}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteAddProductDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteAddProductData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_types"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_product_type"}},{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}}]}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteBonusDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteBonusData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_sale_bonus"}}]}}]}}]} as unknown as DocumentNode; -export const GetQuoteConfiguratorDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteConfiguratorData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_client_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_msfo_irr"}}]}}]}}]} as unknown as DocumentNode; +export const GetQuoteConfiguratorDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteConfiguratorData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_client_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_msfo_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_delivery_time"}},{"kind":"Field","name":{"kind":"Name","value":"evo_first_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_used"}},{"kind":"Field","name":{"kind":"Name","value":"evo_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_accept_period"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteCreateKpDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteCreateKPData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_price_with_discount"}},{"kind":"Field","name":{"kind":"Name","value":"evo_price_without_discount_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_cost_increace"}},{"kind":"Field","name":{"kind":"Name","value":"evo_insurance"}},{"kind":"Field","name":{"kind":"Name","value":"evo_registration_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_card_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_nsib_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_redemption_graph"}},{"kind":"Field","name":{"kind":"Name","value":"evo_fingap_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_contact_name"}},{"kind":"Field","name":{"kind":"Name","value":"evo_gender"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_redemption"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteEltDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteEltData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_kasko_accountid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_kasko_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_elt_kasko"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_kasko_calc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_franchise"}},{"kind":"Field","name":{"kind":"Name","value":"evo_osago_accountid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_elt_osago"}},{"kind":"Field","name":{"kind":"Name","value":"evo_osago_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_typeid"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteFingapDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteFingapData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_product_risks"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}}]}}]}}]}}]} as unknown as DocumentNode; diff --git a/apps/web/process/configurator/get-kp-data.ts b/apps/web/process/configurator/get-kp-data.ts index cb590fc..9422b74 100644 --- a/apps/web/process/configurator/get-kp-data.ts +++ b/apps/web/process/configurator/get-kp-data.ts @@ -1,21 +1,30 @@ import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types'; +import helper from './lib/helper'; import initializeApollo from '@/apollo/client'; import defaultValues from '@/config/default-values'; import * as CRMTypes from '@/graphql/crm.types'; import { gql } from '@apollo/client'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars const QUERY_GET_QUOTE_CONFIGURATOR_DATA = gql` query GetQuoteConfiguratorData($quoteId: Uuid!) { quote(quoteId: $quoteId) { evo_baseproductid evo_client_typeid evo_msfo_irr + evo_delivery_time + evo_first_payment_perc + evo_last_payment_perc + evo_leasingobject_typeid + evo_leasingobject_used + evo_period + evo_accept_period } } `; export async function getKPData({ - values: { quote: quoteId }, + values: { quote: quoteId, recalcWithRevision }, }: GetQuoteInputData): Promise { const apolloClient = initializeApollo(); @@ -28,11 +37,46 @@ export async function getKPData({ }, }); + const leasingPeriod = recalcWithRevision + ? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0) + : quote?.evo_period ?? 0; + + const { getTarifs } = helper({ apolloClient }); + + let tarif = defaultValues.tarif; + if ( + quote?.evo_delivery_time !== null && + quote?.evo_delivery_time !== undefined && + quote?.evo_first_payment_perc !== undefined && + quote?.evo_first_payment_perc !== null && + quote?.evo_last_payment_perc !== undefined && + quote?.evo_last_payment_perc !== null && + quote?.evo_leasingobject_typeid !== undefined && + quote?.evo_leasingobject_typeid !== null && + quote?.evo_leasingobject_used !== undefined && + quote?.evo_leasingobject_used !== null + ) { + const { evo_tarif } = await getTarifs({ + deliveryTime: quote?.evo_delivery_time, + firstPaymentPerc: quote?.evo_first_payment_perc, + lastPaymentPerc: quote?.evo_last_payment_perc, + leaseObjectType: quote?.evo_leasingobject_typeid, + leaseObjectUsed: quote?.evo_leasingobject_used, + leasingPeriod, + product: quote?.evo_baseproductid, + }); + + if (evo_tarif?.evo_tarifid) { + tarif = evo_tarif.evo_tarifid; + } + } + return { values: { IRR_Perc: quote?.evo_msfo_irr ?? defaultValues.IRR_Perc, clientType: quote?.evo_client_typeid, product: quote?.evo_baseproductid, + tarif, }, }; } diff --git a/apps/web/process/configurator/lib/helper.ts b/apps/web/process/configurator/lib/helper.ts new file mode 100644 index 0000000..55b54bb --- /dev/null +++ b/apps/web/process/configurator/lib/helper.ts @@ -0,0 +1,87 @@ +import * as CRMTypes from '@/graphql/crm.types'; +import type { ProcessContext } from '@/process/types'; +import type { CalculationValues } from '@/stores/calculation/values/types'; +import dayjs from 'dayjs'; + +type GetTarifInputValues = Pick< + CalculationValues, + | 'deliveryTime' + | 'firstPaymentPerc' + | 'lastPaymentPerc' + | 'leaseObjectType' + | 'leaseObjectUsed' + | 'leasingPeriod' + | 'product' +>; + +export default function helper({ apolloClient }: Pick) { + return { + async getTarifs({ + deliveryTime, + firstPaymentPerc, + lastPaymentPerc, + leaseObjectType: leaseObjectTypeId, + leaseObjectUsed, + leasingPeriod, + product, + }: GetTarifInputValues) { + const currentDate = dayjs().utc(false).toISOString(); + + const { + data: { evo_tarifs = [] }, + } = await apolloClient.query({ + fetchPolicy: 'network-only', + query: CRMTypes.GetTarifsDocument, + variables: { + currentDate, + }, + }); + + const evo_tarif = evo_tarifs + ?.filter( + (tarif) => + tarif && + product && + tarif.evo_baseproductid === product && + tarif.evo_min_period !== null && + tarif.evo_min_period <= leasingPeriod && + tarif.evo_max_period !== null && + tarif.evo_max_period >= leasingPeriod && + tarif.evo_delivery_time?.includes(deliveryTime) && + tarif.evo_min_first_payment !== null && + tarif.evo_min_first_payment <= firstPaymentPerc && + tarif.evo_max_first_payment !== null && + tarif.evo_max_first_payment >= firstPaymentPerc && + tarif.evo_min_last_payment !== null && + tarif.evo_min_last_payment <= lastPaymentPerc && + tarif.evo_max_last_payment !== null && + tarif.evo_max_last_payment >= lastPaymentPerc + ) + .filter( + (tarif) => + !tarif?.evo_leasingobject_types?.length || + (leaseObjectTypeId && + tarif.evo_leasingobject_types + .map((x) => x?.evo_leasingobject_typeid) + .includes(leaseObjectTypeId)) + ) + .filter((tarif) => { + if (leaseObjectUsed === true) { + if (!tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_001)) { + return true; + } + } else if ( + !tarif?.evo_pl_use_type?.length || + tarif.evo_pl_use_type.includes(100_000_000) + ) { + return true; + } + + return false; + }) + .at(0); + + return { evo_tarif, evo_tarifs }; + }, + }; +} diff --git a/apps/web/process/configurator/reactions/values.ts b/apps/web/process/configurator/reactions/values.ts index a4a7888..76493ee 100644 --- a/apps/web/process/configurator/reactions/values.ts +++ b/apps/web/process/configurator/reactions/values.ts @@ -1,11 +1,12 @@ +import helper from '../lib/helper'; import type { Elements } from '@/Components/Calculation/config/map/values'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import { normalizeOptions } from '@/utils/entity'; -import { debouncedReaction, disposableReaction } from '@/utils/mobx'; +import { disposableReaction } from '@/utils/mobx'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; -import { reaction } from 'mobx'; +import { comparer, reaction } from 'mobx'; import { first, sort } from 'radash'; dayjs.extend(utc); @@ -13,7 +14,10 @@ dayjs.extend(utc); export default function valuesReactions({ store, apolloClient }: ProcessContext) { const { $calculation, $process } = store; - debouncedReaction( + const { getTarifs } = helper({ apolloClient }); + + disposableReaction( + () => $process.has('LoadKP'), () => $calculation.$values.getValues([ 'product', @@ -24,80 +28,16 @@ export default function valuesReactions({ store, apolloClient }: ProcessContext) 'lastPaymentPerc', 'leaseObjectType', ]), - async ({ - product, - leasingPeriod, - leaseObjectUsed, - deliveryTime, - firstPaymentPerc, - lastPaymentPerc, - leaseObjectType: leaseObjectTypeId, - }) => { - const currentDate = dayjs().utc(false).toISOString(); - - let { - data: { evo_tarifs = [] }, - } = await apolloClient.query({ - fetchPolicy: 'network-only', - query: CRMTypes.GetTarifsDocument, - variables: { - currentDate, - }, - }); + async (values) => { + const { evo_tarif, evo_tarifs } = await getTarifs(values); $calculation.element('selectTarif').setOptions(normalizeOptions(evo_tarifs)).resetValue(); - - if (evo_tarifs) { - evo_tarifs = evo_tarifs - ?.filter( - (tarif) => - tarif && - product && - tarif.evo_baseproductid === product && - tarif.evo_min_period !== null && - tarif.evo_min_period <= leasingPeriod && - tarif.evo_max_period !== null && - tarif.evo_max_period >= leasingPeriod && - tarif.evo_delivery_time?.includes(deliveryTime) && - tarif.evo_min_first_payment !== null && - tarif.evo_min_first_payment <= firstPaymentPerc && - tarif.evo_max_first_payment !== null && - tarif.evo_max_first_payment >= firstPaymentPerc && - tarif.evo_min_last_payment !== null && - tarif.evo_min_last_payment <= lastPaymentPerc && - tarif.evo_max_last_payment !== null && - tarif.evo_max_last_payment >= lastPaymentPerc - ) - .filter( - (tarif) => - !tarif?.evo_leasingobject_types?.length || - (leaseObjectTypeId && - tarif.evo_leasingobject_types - .map((x) => x?.evo_leasingobject_typeid) - .includes(leaseObjectTypeId)) - ) - .filter((tarif) => { - if (leaseObjectUsed === true) { - if (!tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_001)) { - return true; - } - } else if ( - !tarif?.evo_pl_use_type?.length || - tarif.evo_pl_use_type.includes(100_000_000) - ) { - return true; - } - - return false; - }); - } - - $calculation.element('selectTarif').setValue(evo_tarifs?.at(0)?.evo_tarifid || null); + $calculation.element('selectTarif').setValue(evo_tarif?.evo_tarifid || null); }, { delay: 10, + equals: comparer.shallow, fireImmediately: true, - wait: 50, } );