From d1665d3995caf595f48f462ba0c6ad399b32d9bb Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 10 May 2023 12:17:37 +0300 Subject: [PATCH] load-kp: pass elt data --- apps/web/graphql/crm.types.ts | 8 +++ apps/web/process/elt/get-kp-data.ts | 66 ++++++++++++++++++++++++ apps/web/process/elt/index.ts | 1 + apps/web/process/elt/lib/helper.ts | 5 +- apps/web/process/elt/reactions/common.ts | 38 ++++++++++++-- apps/web/process/load-kp/reactions.ts | 19 ++++++- apps/web/server/routers/quote/index.ts | 4 ++ apps/web/server/routers/quote/types.ts | 17 ++++++ 8 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 apps/web/process/elt/get-kp-data.ts diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index ecc7abb..04b79a6 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -511,6 +511,13 @@ export type GetQuoteCreateKpDataQueryVariables = Exact<{ export type GetQuoteCreateKpDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_price_with_discount: boolean | null, evo_price_without_discount_quote: boolean | null, evo_cost_increace: boolean | null, evo_insurance: boolean | null, evo_registration_quote: boolean | null, evo_card_quote: boolean | null, evo_nsib_quote: boolean | null, evo_redemption_graph: boolean | null, evo_fingap_quote: boolean | null, evo_contact_name: string | null, evo_gender: number | null, evo_last_payment_redemption: boolean | null } | null }; +export type GetQuoteEltDataQueryVariables = Exact<{ + quoteId: Scalars['Uuid']; +}>; + + +export type GetQuoteEltDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_kasko_accountid: string | null, evo_kasko_price: number | null, evo_id_elt_kasko: string | null, evo_id_kasko_calc: string | null, evo_franchise: number | null, evo_osago_accountid: string | null, evo_id_elt_osago: string | null, evo_osago_price: number | null, evo_leasingobject_typeid: string | null } | null }; + export type GetQuoteFingapDataQueryVariables = Exact<{ quoteId: Scalars['Uuid']; }>; @@ -661,6 +668,7 @@ export const GetQuoteAddProductDataDocument = {"kind":"Document","definitions":[ 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 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; export const GetFinGapAddProductTypesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetFinGAPAddProductTypes"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"DateTime"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_types"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"statecode"},"value":{"kind":"IntValue","value":"0"}},{"kind":"Argument","name":{"kind":"Name","value":"evo_datefrom_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"lte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}},{"kind":"Argument","name":{"kind":"Name","value":"evo_dateto_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"gte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}},{"kind":"Argument","name":{"kind":"Name","value":"evo_product_type"},"value":{"kind":"IntValue","value":"100000006"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_name"}},{"kind":"Field","name":{"kind":"Name","value":"evo_type_calc_cerebellum"}},{"kind":"Field","name":{"kind":"Name","value":"evo_cost_service_provider_withoutnds"}},{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_types"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}}]}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteGibddDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteGibddData"},"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_db_accept_registration"}},{"kind":"Field","name":{"kind":"Name","value":"evo_object_registration"}},{"kind":"Field","name":{"kind":"Name","value":"evo_pts_type"}},{"kind":"Field","name":{"kind":"Name","value":"evo_vehicle_tax_year"}},{"kind":"Field","name":{"kind":"Name","value":"evo_vehicle_tax_approved"}},{"kind":"Field","name":{"kind":"Name","value":"evo_category_tr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_vehicle_type_tax"}},{"kind":"Field","name":{"kind":"Name","value":"evo_regionid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_townid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_legal_regionid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_legal_townid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_registration_regionid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_req_telematic"}},{"kind":"Field","name":{"kind":"Name","value":"evo_req_telematic_accept"}}]}}]}}]} as unknown as DocumentNode; diff --git a/apps/web/process/elt/get-kp-data.ts b/apps/web/process/elt/get-kp-data.ts new file mode 100644 index 0000000..abec183 --- /dev/null +++ b/apps/web/process/elt/get-kp-data.ts @@ -0,0 +1,66 @@ +import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types'; +import initializeApollo from '@/apollo/client'; +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_ELT_DATA = gql` + query GetQuoteEltData($quoteId: Uuid!) { + quote(quoteId: $quoteId) { + evo_kasko_accountid + evo_kasko_price + evo_id_elt_kasko + evo_id_kasko_calc + evo_franchise + evo_osago_accountid + evo_id_elt_osago + evo_osago_price + evo_leasingobject_typeid + } + } +`; + +export async function getKPData({ + values: { quote: quoteId }, +}: GetQuoteInputData): Promise { + const apolloClient = initializeApollo(); + + const { + data: { quote }, + } = await apolloClient.query({ + query: CRMTypes.GetQuoteEltDataDocument, + variables: { + quoteId, + }, + }); + + const elt: NonNullable = { kasko: undefined, osago: undefined }; + + if ( + quote?.evo_kasko_accountid && + quote?.evo_id_elt_kasko && + quote?.evo_id_kasko_calc && + quote?.evo_kasko_price && + quote?.evo_franchise + ) { + elt.kasko = { + key: quote?.evo_kasko_accountid, + requestId: quote?.evo_id_elt_kasko, + skCalcId: quote?.evo_id_kasko_calc, + sum: quote?.evo_kasko_price, + totalFranchise: quote?.evo_franchise, + }; + } + + if (quote?.evo_osago_accountid && quote?.evo_id_elt_osago && quote?.evo_osago_price) { + elt.osago = { + key: quote?.evo_osago_accountid, + numCalc: Number.parseInt(quote?.evo_id_elt_osago, 10), + sum: quote?.evo_osago_price, + }; + } + + return { + elt, + }; +} diff --git a/apps/web/process/elt/index.ts b/apps/web/process/elt/index.ts index 88c740a..e05ea2d 100644 --- a/apps/web/process/elt/index.ts +++ b/apps/web/process/elt/index.ts @@ -1 +1,2 @@ +export * from './get-kp-data'; export * as reactions from './reactions'; diff --git a/apps/web/process/elt/lib/helper.ts b/apps/web/process/elt/lib/helper.ts index 8cc0cb4..2661274 100644 --- a/apps/web/process/elt/lib/helper.ts +++ b/apps/web/process/elt/lib/helper.ts @@ -2,7 +2,10 @@ import type { ProcessContext } from '../../types'; import type { Row } from '@/Components/Calculation/Form/ELT/types'; import * as CRMTypes from '@/graphql/crm.types'; -export default function helper({ apolloClient, store }: ProcessContext) { +export default function helper({ + apolloClient, + store, +}: Pick) { const { $calculation } = store; return { diff --git a/apps/web/process/elt/reactions/common.ts b/apps/web/process/elt/reactions/common.ts index 530d9ef..f361ac7 100644 --- a/apps/web/process/elt/reactions/common.ts +++ b/apps/web/process/elt/reactions/common.ts @@ -1,7 +1,7 @@ import type { ProcessContext } from '../../types'; import helper from '../lib/helper'; import { disposableReaction } from '@/utils/mobx'; -import { toJS } from 'mobx'; +import { comparer, toJS } from 'mobx'; export default function reactions(context: ProcessContext) { const { store } = context; @@ -12,8 +12,6 @@ export default function reactions(context: ProcessContext) { disposableReaction( () => $process.has('ELT') || $process.has('LoadKP'), () => ({ - kasko: toJS($tables.insurance.row('kasko').getValues()), - osago: toJS($tables.insurance.row('osago').getValues()), values: $calculation.$values.getValues([ 'leaseObjectType', // osago @@ -43,7 +41,41 @@ export default function reactions(context: ProcessContext) { } }, { + delay: 10, + equals: comparer.shallow, fireImmediately: true, } ); + + disposableReaction( + () => $process.has('ELT') || $process.has('LoadKP'), + () => { + const { insCost, insuranceCompany } = toJS($tables.insurance.row('kasko').getValues()); + + return { insCost, insuranceCompany }; + }, + () => { + $tables.elt.kasko.resetSelectedKey(); + }, + { + delay: 10, + equals: comparer.shallow, + } + ); + + disposableReaction( + () => $process.has('ELT') || $process.has('LoadKP'), + () => { + const { insCost, insuranceCompany } = toJS($tables.insurance.row('osago').getValues()); + + return { insCost, insuranceCompany }; + }, + () => { + $tables.elt.osago.resetSelectedKey(); + }, + { + delay: 10, + equals: comparer.shallow, + } + ); } diff --git a/apps/web/process/load-kp/reactions.ts b/apps/web/process/load-kp/reactions.ts index ae02c0f..902b315 100644 --- a/apps/web/process/load-kp/reactions.ts +++ b/apps/web/process/load-kp/reactions.ts @@ -1,3 +1,5 @@ +/* eslint-disable sonarjs/cognitive-complexity */ +import eltHelper from '../elt/lib/helper'; import type { ProcessContext } from '@/process/types'; import { reaction } from 'mobx'; import { omit } from 'radash'; @@ -5,9 +7,11 @@ import { message } from 'ui/elements'; const key = 'KP_LOADING_INFO'; -export function common({ store, trpcClient }: ProcessContext) { +export function common({ store, trpcClient, apolloClient }: ProcessContext) { const { $calculation, $process, $tables } = store; + const { init: initElt } = eltHelper({ apolloClient, store }); + reaction( () => $calculation.$values.getValue('quote'), () => { @@ -29,7 +33,7 @@ export function common({ store, trpcClient }: ProcessContext) { ...$calculation.$values.getValues(['lead', 'opportunity', 'recalcWithRevision']), }, }) - .then(({ values, payments, insurance, fingap }) => { + .then(({ values, payments, insurance, fingap, elt }) => { $calculation.$values.setValues( omit(values, [ 'lead', @@ -59,6 +63,17 @@ export function common({ store, trpcClient }: ProcessContext) { if (fingap) $tables.fingap.setSelectedKeys(fingap.keys); + initElt().then(() => { + if (elt?.kasko) { + $tables.elt.kasko.setRow(elt.kasko); + $tables.elt.kasko.setSelectedKey(elt.kasko.key); + } + if (elt?.osago) { + $tables.elt.osago.setRow(elt.osago); + $tables.elt.osago.setSelectedKey(elt.osago.key); + } + }); + message.success({ content: `КП ${quote.label} загружено`, key, diff --git a/apps/web/server/routers/quote/index.ts b/apps/web/server/routers/quote/index.ts index fae060b..87ce14b 100644 --- a/apps/web/server/routers/quote/index.ts +++ b/apps/web/server/routers/quote/index.ts @@ -24,6 +24,7 @@ import * as addProduct from '@/process/add-product'; import * as bonuses from '@/process/bonuses'; import * as configurator from '@/process/configurator'; import * as createKpProcess from '@/process/create-kp'; +import * as eltProcess from '@/process/elt'; import * as fingapProcess from '@/process/fingap'; import * as gibdd from '@/process/gibdd'; import * as insuranceProcess from '@/process/insurance'; @@ -69,6 +70,7 @@ export const quoteRouter = router({ insuranceProcess, addProduct, createKpProcess, + eltProcess, ].map(({ getKPData }) => getKPData(input)) ); @@ -76,12 +78,14 @@ export const quoteRouter = router({ const payments = processData.find((x) => x.payments)?.payments ?? defaultPayments; const insurance = processData.find((x) => x.insurance)?.insurance ?? defaultInsurance; const fingap = processData.find((x) => x.fingap)?.fingap ?? defaultFingap; + const elt = processData.find((x) => x.elt)?.elt; return { values, payments, insurance, fingap, + elt, }; }), diff --git a/apps/web/server/routers/quote/types.ts b/apps/web/server/routers/quote/types.ts index a6af4ee..f83d61a 100644 --- a/apps/web/server/routers/quote/types.ts +++ b/apps/web/server/routers/quote/types.ts @@ -1,5 +1,6 @@ /* eslint-disable zod/require-strict */ import { CalculateInputSchema } from '../calculate/types'; +import { RowSchema as EltRowSchema } from '@/config/schema/elt'; import { RiskSchema } from '@/config/schema/fingap'; import { KeysSchema, RowSchema } from '@/config/schema/insurance'; import PaymentsSchema from '@/config/schema/payments'; @@ -33,6 +34,22 @@ const InsuranceSchema = z.object({ export const GetQuoteOutputDataSchema = z .object({ + elt: z + .object({ + kasko: EltRowSchema.pick({ + key: true, + requestId: true, + skCalcId: true, + sum: true, + totalFranchise: true, + }).optional(), + osago: EltRowSchema.pick({ + key: true, + numCalc: true, + sum: true, + }).optional(), + }) + .optional(), fingap: FinGAPSchema, insurance: InsuranceSchema, payments: PaymentsSchema,