From ce4a8ad2c12f6d23a57c7f3400f8fc65889f9329 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 25 Jan 2023 13:58:05 +0300 Subject: [PATCH] =?UTF-8?q?selectBrand:=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/graphql/crm.types.ts | 28 ++- .../leasing-object/reactions/common.ts | 197 +++++++++++++++++- 2 files changed, 206 insertions(+), 19 deletions(-) diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index ebc0c63..c071d90 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -231,33 +231,45 @@ export type GetQuoteUrlQueryVariables = Exact<{ export type GetQuoteUrlQuery = { __typename?: 'Query', entity: { __typename?: 'quote', link: string | null } | null }; -export type GetModelsQueryVariables = Exact<{ +export type GetModels_ProcessLeasingObjectQueryVariables = Exact<{ brandid: Scalars['Uuid']; }>; -export type GetModelsQuery = { __typename?: 'Query', evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null, evo_vehicle_type: number | null, label: string | null, value: string | null } | null> | null }; +export type GetModels_ProcessLeasingObjectQuery = { __typename?: 'Query', evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null, evo_vehicle_type: number | null, label: string | null, value: string | null } | null> | null }; -export type GetModelsFilterDataLeaseObjectTypeQueryVariables = Exact<{ +export type GetLeaseObjectType_ProcessLeasingObjectQueryVariables = Exact<{ leaseObjectTypeId: Scalars['Uuid']; }>; -export type GetModelsFilterDataLeaseObjectTypeQuery = { __typename?: 'Query', leaseObjectType: { __typename?: 'evo_leasingobject_type', evo_vehicle_type: Array | null } | null }; +export type GetLeaseObjectType_ProcessLeasingObjectQuery = { __typename?: 'Query', leaseObjectType: { __typename?: 'evo_leasingobject_type', evo_vehicle_type: Array | null } | null }; -export type GetModelsFilterDataSubsidyQueryVariables = Exact<{ +export type GetSubsidy_ProcessLeasingObjectQueryVariables = Exact<{ subsidyId: Scalars['Uuid']; }>; -export type GetModelsFilterDataSubsidyQuery = { __typename?: 'Query', subsidy: { __typename?: 'evo_subsidy', evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null } | null> | null } | null }; +export type GetSubsidy_ProcessLeasingObjectQuery = { __typename?: 'Query', subsidy: { __typename?: 'evo_subsidy', evo_brands: Array<{ __typename?: 'evo_brand', evo_brandid: string | null } | null> | null, evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null } | null> | null } | null }; -export type GetModelsFilterDataImportProgramQueryVariables = Exact<{ +export type GetImportProgram_ProcessLeasingObjectQueryVariables = Exact<{ importProgramId: Scalars['Uuid']; }>; -export type GetModelsFilterDataImportProgramQuery = { __typename?: 'Query', importProgram: { __typename?: 'evo_subsidy', evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null } | null> | null } | null }; +export type GetImportProgram_ProcessLeasingObjectQuery = { __typename?: 'Query', importProgram: { __typename?: 'evo_subsidy', evo_brands: Array<{ __typename?: 'evo_brand', evo_brandid: string | null } | null> | null, evo_models: Array<{ __typename?: 'evo_model', evo_modelid: string | null } | null> | null } | null }; + +export type GetBrands_ProcessLeasingObjectQueryVariables = Exact<{ [key: string]: never; }>; + + +export type GetBrands_ProcessLeasingObjectQuery = { __typename?: 'Query', evo_brands: Array<{ __typename?: 'evo_brand', evo_brandid: string | null, evo_vehicle_type: Array | null, label: string | null, value: string | null } | null> | null }; + +export type GetProduct_ProcessLeasingObjectQueryVariables = Exact<{ + productId: Scalars['Uuid']; +}>; + + +export type GetProduct_ProcessLeasingObjectQuery = { __typename?: 'Query', evo_baseproduct: { __typename?: 'evo_baseproduct', evo_brands: Array<{ __typename?: 'evo_brand', evo_brandid: string | null } | null> | null } | null }; export type GetLeasingWithoutKaskoOptionsQueryVariables = Exact<{ [key: string]: never; }>; diff --git a/apps/web/process/leasing-object/reactions/common.ts b/apps/web/process/leasing-object/reactions/common.ts index b41e7ba..897bab6 100644 --- a/apps/web/process/leasing-object/reactions/common.ts +++ b/apps/web/process/leasing-object/reactions/common.ts @@ -3,10 +3,11 @@ import { gql } from '@apollo/client'; import type * as CRMTypes from 'graphql/crm.types'; import { autorun } from 'mobx'; import type { ReactionsContext } from 'process/types'; +import { intersects } from 'radash'; import { normalizeOptions } from 'tools'; const QUERY_GET_MODELS = gql` - query GetModels($brandid: Uuid!) { + query GetModels_ProcessLeasingObject($brandid: Uuid!) { evo_models(statecode: 0, evo_brandid: $brandid) { label: evo_name value: evo_modelid @@ -17,7 +18,7 @@ const QUERY_GET_MODELS = gql` `; const QUERY_GET_LEASE_OBJECT_TYPE = gql` - query GetModelsFilterDataLeaseObjectType($leaseObjectTypeId: Uuid!) { + query GetLeaseObjectType_ProcessLeasingObject($leaseObjectTypeId: Uuid!) { leaseObjectType: evo_leasingobject_type(evo_leasingobject_typeid: $leaseObjectTypeId) { evo_vehicle_type } @@ -25,8 +26,11 @@ const QUERY_GET_LEASE_OBJECT_TYPE = gql` `; const QUERY_GET_SUBSIDY = gql` - query GetModelsFilterDataSubsidy($subsidyId: Uuid!) { + query GetSubsidy_ProcessLeasingObject($subsidyId: Uuid!) { subsidy: evo_subsidy(evo_subsidyid: $subsidyId) { + evo_brands { + evo_brandid + } evo_models { evo_modelid } @@ -35,8 +39,11 @@ const QUERY_GET_SUBSIDY = gql` `; const QUERY_GET_IMPORT_PROGRAM = gql` - query GetModelsFilterDataImportProgram($importProgramId: Uuid!) { + query GetImportProgram_ProcessLeasingObject($importProgramId: Uuid!) { importProgram: evo_subsidy(evo_subsidyid: $importProgramId) { + evo_brands { + evo_brandid + } evo_models { evo_modelid } @@ -44,6 +51,27 @@ const QUERY_GET_IMPORT_PROGRAM = gql` } `; +const QUERY_GET_BRANDS = gql` + query GetBrands_ProcessLeasingObject { + evo_brands(statecode: 0) { + label: evo_name + value: evo_brandid + evo_brandid + evo_vehicle_type + } + } +`; + +const QUERY_GET_PRODUCT = gql` + query GetProduct_ProcessLeasingObject($productId: Uuid!) { + evo_baseproduct(evo_baseproductid: $productId) { + evo_brands { + evo_brandid + } + } + } +`; + export default function commonReactions({ store, apolloClient }: ReactionsContext) { const { $calculation } = store; @@ -63,7 +91,10 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex const { data: { evo_models }, - } = await apolloClient.query({ + } = await apolloClient.query< + CRMTypes.GetModels_ProcessLeasingObjectQuery, + CRMTypes.GetModels_ProcessLeasingObjectQueryVariables + >({ query: QUERY_GET_MODELS, variables: { brandid: brandId, @@ -82,8 +113,8 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex const { data: { leaseObjectType }, } = await apolloClient.query< - CRMTypes.GetModelsFilterDataLeaseObjectTypeQuery, - CRMTypes.GetModelsFilterDataLeaseObjectTypeQueryVariables + CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQuery, + CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQueryVariables >({ query: QUERY_GET_LEASE_OBJECT_TYPE, variables: { @@ -107,8 +138,8 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex const { data: { subsidy }, } = await apolloClient.query< - CRMTypes.GetModelsFilterDataSubsidyQuery, - CRMTypes.GetModelsFilterDataSubsidyQueryVariables + CRMTypes.GetSubsidy_ProcessLeasingObjectQuery, + CRMTypes.GetSubsidy_ProcessLeasingObjectQueryVariables >({ query: QUERY_GET_SUBSIDY, variables: { @@ -135,8 +166,8 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex const { data: { importProgram }, } = await apolloClient.query< - CRMTypes.GetModelsFilterDataImportProgramQuery, - CRMTypes.GetModelsFilterDataImportProgramQueryVariables + CRMTypes.GetImportProgram_ProcessLeasingObjectQuery, + CRMTypes.GetImportProgram_ProcessLeasingObjectQueryVariables >({ query: QUERY_GET_IMPORT_PROGRAM, variables: { @@ -166,4 +197,148 @@ export default function commonReactions({ store, apolloClient }: ReactionsContex delay: 10, } ); + + autorun( + async () => { + const { + product: productId, + subsidy: subsidyId, + importProgram: importProgramId, + leaseObjectType: leaseObjectTypeId, + } = $calculation.$values.values; + + const { + data: { evo_brands }, + } = await apolloClient.query< + CRMTypes.GetBrands_ProcessLeasingObjectQuery, + CRMTypes.GetBrands_ProcessLeasingObjectQueryVariables + >({ + query: QUERY_GET_BRANDS, + }); + + let brands = evo_brands; + + if (!brands) { + $calculation.element('selectBrand').reset(); + + return; + } + + if (productId) { + const { + data: { evo_baseproduct }, + } = await apolloClient.query< + CRMTypes.GetProduct_ProcessLeasingObjectQuery, + CRMTypes.GetProduct_ProcessLeasingObjectQueryVariables + >({ + query: QUERY_GET_PRODUCT, + variables: { + productId, + }, + }); + + brands = brands?.filter((brand) => { + if ( + !evo_baseproduct?.evo_brands?.length || + evo_baseproduct.evo_brands.filter( + (baseproduct_evo_brand) => baseproduct_evo_brand?.evo_brandid === brand?.evo_brandid + )?.length + ) { + return true; + } + + return false; + }); + } + + if (leaseObjectTypeId) { + const { + data: { leaseObjectType }, + } = await apolloClient.query< + CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQuery, + CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQueryVariables + >({ + query: QUERY_GET_LEASE_OBJECT_TYPE, + variables: { + leaseObjectTypeId, + }, + }); + + brands = brands?.filter((brand) => { + if ( + brand?.evo_vehicle_type?.length && + leaseObjectType?.evo_vehicle_type && + intersects( + brand.evo_vehicle_type?.filter((x) => x > 0), + leaseObjectType?.evo_vehicle_type + ) + ) { + return true; + } + + return false; + }); + } + + if (subsidyId) { + const { + data: { subsidy }, + } = await apolloClient.query< + CRMTypes.GetSubsidy_ProcessLeasingObjectQuery, + CRMTypes.GetSubsidy_ProcessLeasingObjectQueryVariables + >({ + query: QUERY_GET_SUBSIDY, + variables: { + subsidyId, + }, + }); + + brands = brands?.filter((brand) => { + if ( + !subsidy?.evo_brands?.length || + subsidy?.evo_brands?.filter( + (subsidy_evo_brand) => subsidy_evo_brand?.evo_brandid === brand?.evo_brandid + )?.length + ) { + return true; + } + + return false; + }); + } + + if (importProgramId) { + const { + data: { importProgram }, + } = await apolloClient.query< + CRMTypes.GetImportProgram_ProcessLeasingObjectQuery, + CRMTypes.GetImportProgram_ProcessLeasingObjectQueryVariables + >({ + query: QUERY_GET_IMPORT_PROGRAM, + variables: { + importProgramId, + }, + }); + + brands = brands?.filter((brand) => { + if ( + !importProgram?.evo_brands?.length || + importProgram?.evo_brands?.filter( + (importProgram_evo_brand) => + importProgram_evo_brand?.evo_brandid === brand?.evo_brandid + )?.length + ) { + return true; + } + + return false; + }); + } + + $calculation.element('selectBrand').setOptions(normalizeOptions(brands)); + }, + { + delay: 10, + } + ); }