diff --git a/process/agents/lib/fill-agents-from-lead.ts b/process/agents/lib/fill-agents-from-lead.ts index 93feb1d..93dc6fc 100644 --- a/process/agents/lib/fill-agents-from-lead.ts +++ b/process/agents/lib/fill-agents-from-lead.ts @@ -5,6 +5,7 @@ import type { Elements } from 'Components/Calculation/config/map/values'; import type RootStore from 'stores/root'; import { normalizeOptions } from 'tools/entity'; import QUERY_GET_AGENT from './query/get-agent'; +import QUERY_GET_BROKER_ACCOUNTID from './query/get-broker-accountid'; import type { GetAgent } from './query/__generated__/GetAgent'; function makeFillAgent( @@ -99,14 +100,6 @@ export const fillCalcDoubleAgent = makeFillAgent( * иначе очищать поле калькулятора calcBroker */ -const QUERY_GET_BROKER_ACCOUNTID = gql` - query GetBrokerAccountId($leadid: Uuid!) { - lead(leadid: $leadid) { - agentid: evo_broker_accountid - } - } -`; - export const fillCalcBroker = makeFillAgent( 'selectCalcBroker', QUERY_GET_BROKER_ACCOUNTID, diff --git a/process/agents/lib/__generated__/GetBrokerAccountId.ts b/process/agents/lib/query/__generated__/GetBrokerAccountId.ts similarity index 100% rename from process/agents/lib/__generated__/GetBrokerAccountId.ts rename to process/agents/lib/query/__generated__/GetBrokerAccountId.ts diff --git a/process/agents/lib/query/get-broker-accountid.js b/process/agents/lib/query/get-broker-accountid.js new file mode 100644 index 0000000..d971faa --- /dev/null +++ b/process/agents/lib/query/get-broker-accountid.js @@ -0,0 +1,11 @@ +import { gql } from '@apollo/client'; + +const QUERY_GET_BROKER_ACCOUNTID = gql` + query GetBrokerAccountId($leadid: Uuid!) { + lead(leadid: $leadid) { + agentid: evo_broker_accountid + } + } +`; + +export default QUERY_GET_BROKER_ACCOUNTID; diff --git a/process/agents/reactions/__generated__/GetBrokerAccountIdFromDealer.ts b/process/agents/reactions/__generated__/GetBrokerAccountIdFromDealer.ts new file mode 100644 index 0000000..87b70b3 --- /dev/null +++ b/process/agents/reactions/__generated__/GetBrokerAccountIdFromDealer.ts @@ -0,0 +1,21 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetBrokerAccountIdFromDealer +// ==================================================== + +export interface GetBrokerAccountIdFromDealer_dealer { + __typename: "account"; + evo_broker_accountid: any | null; +} + +export interface GetBrokerAccountIdFromDealer { + dealer: GetBrokerAccountIdFromDealer_dealer | null; +} + +export interface GetBrokerAccountIdFromDealerVariables { + dealerId: any; +} diff --git a/process/agents/reactions/__generated__/GetDealerPerson.ts b/process/agents/reactions/__generated__/GetDealerPerson.ts new file mode 100644 index 0000000..08b9b89 --- /dev/null +++ b/process/agents/reactions/__generated__/GetDealerPerson.ts @@ -0,0 +1,25 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetDealerPerson +// ==================================================== + +export interface GetDealerPerson_salon_providers { + __typename: "account"; + label: string | null; + value: any | null; +} + +export interface GetDealerPerson { + /** + * Поставщики ЮЛ салона. statecode по умолчанию 0 + */ + salon_providers: (GetDealerPerson_salon_providers | null)[] | null; +} + +export interface GetDealerPersonVariables { + dealerId: any; +} diff --git a/process/agents/reactions/common.ts b/process/agents/reactions/common.ts index a2daa7e..127b5b0 100644 --- a/process/agents/reactions/common.ts +++ b/process/agents/reactions/common.ts @@ -1,10 +1,20 @@ import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; import { reaction } from 'mobx'; import type RootStore from 'stores/root'; +import { normalizeOptions } from 'tools/entity'; import * as fillAgentsFromLead from '../lib/fill-agents-from-lead'; +import QUERY_GET_AGENT from '../lib/query/get-agent'; +import type { GetAgent } from '../lib/query/__generated__/GetAgent'; +import type { GetBrokerAccountIdFromDealer } from './__generated__/GetBrokerAccountIdFromDealer'; +import type { GetDealerPerson } from './__generated__/GetDealerPerson'; export default function commonReactions(store: RootStore, apolloClient: ApolloClient) { const { $calculation } = store; + + /** + * Заполняем агентов из Интереса + */ reaction( () => $calculation.getElementValue('selectLead'), (leadid) => { @@ -14,4 +24,100 @@ export default function commonReactions(store: RootStore, apolloClient: ApolloCl fillAgentsFromLead.fillFinDepartment(store, apolloClient, leadid); } ); + + /** + * Заполняем selectDealerPerson + * На изменение Салон приобретения формируем список в поле ЮЛ поставщика - записи Контрагент, + * у которых статус = активный И Поставщик = Да И Тип поставщика = Юридическое лицо + * И связаны с карточкой Контрагент из поля "Салон приобретения" по связи Салон-ЮЛ + */ + + const QUERY_GET_DEALER_PERSON = gql` + query GetDealerPerson($dealerId: Uuid!) { + salon_providers(statecode: 0, salonaccountid: $dealerId) { + label: name + value: accountid + } + } + `; + + reaction( + () => $calculation.getElementValue('selectDealer'), + async (dealerId) => { + if (!dealerId) { + $calculation.resetElement('selectDealerPerson'); + + return; + } + + const { + // eslint-disable-next-line @typescript-eslint/naming-convention + data: { salon_providers }, + } = await apolloClient.query({ + query: QUERY_GET_DEALER_PERSON, + variables: { + dealerId, + }, + }); + + if (salon_providers?.length) { + $calculation.setElementOptions('selectDealerPerson', normalizeOptions(salon_providers)); + $calculation.setElementValue('selectDealerPerson', salon_providers[0]?.value); + } + } + ); + + /** + * Заполняем selectDealerBroker + */ + + const QUERY_GET_BROKER_ACCOUNTID = gql` + query GetBrokerAccountIdFromDealer($dealerId: Uuid!) { + dealer: account(accountid: $dealerId) { + evo_broker_accountid + } + } + `; + + reaction( + () => $calculation.getElementValue('selectDealerPerson'), + async (dealerPersonId) => { + if (!dealerPersonId) { + $calculation.resetElement('selectDealerBroker'); + + return; + } + + // prettier-ignore + const { data: { dealer } } = await apolloClient.query({ + query: QUERY_GET_BROKER_ACCOUNTID, + variables: { + dealerId: dealerPersonId, + }, + }); + + if (dealer?.evo_broker_accountid) { + // prettier-ignore + const { data: { agent: dealerBroker } } = await apolloClient.query({ + query: QUERY_GET_AGENT, + variables: { + agentid: dealer?.evo_broker_accountid, + }, + }); + + if (dealerBroker) { + $calculation.setElementOptions('selectDealerBroker', normalizeOptions([dealerBroker])); + $calculation.setElementValue('selectDealerBroker', dealerBroker.value); + } + } + } + ); + + /** + * Заполняем selectDealerRewardCondition + */ + + /** + * Заполняем selectDealerBrokerRewardCondition + */ } diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts index 8d56c96..101e45e 100644 --- a/stores/calculation/index.ts +++ b/stores/calculation/index.ts @@ -24,7 +24,7 @@ export default class CalculationStore { resetElement = (elementName: Values.Elements) => { this.resetElementValue(elementName); - this.$options.resetOption(elementName); + this.$options.resetOptions(elementName); this.$status.resetStatus(elementName); this.$validation.clearErrors(elementName); }; @@ -60,6 +60,10 @@ export default class CalculationStore { this.$options.setOptions(elementName, options); }; + resetElementOptions = (elementName: T) => { + this.$options.resetOptions(elementName); + }; + getElementOption(elementName: T) { const value = this.getElementValue(elementName); diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index 09a5be7..0ea7283 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -40,6 +40,20 @@ export default class OptionsStore { return options; } + private checkValueInOptions = (elementName: Elements) => { + /** + * Проверяем, что значение есть в новом списке, иначе сбрасываем значение + */ + const value = this.root.$calculation.getElementValue(elementName); + if ( + // eslint-disable-next-line operator-linebreak + !this.options[elementName]?.length || + this.options[elementName].some((x) => x.value === value) + ) { + this.root.$calculation.resetElementValue(elementName); + } + }; + setOptions = (elementName: T, options: BaseOption[]) => { /** * TODO: use T instead of any in BaseOption type @@ -48,13 +62,7 @@ export default class OptionsStore { */ this.options[elementName] = options as BaseOption[]; - /** - * Проверяем, что значение есть в новом списке, иначе сбрасываем значение - */ - const value = this.root.$calculation.getElementValue(elementName); - if (!options?.length || options.some((x) => x.value === value)) { - this.root.$calculation.resetElementValue(elementName); - } + this.checkValueInOptions(elementName); /** * Если в новом списке одна запись, то указываем ее @@ -69,8 +77,9 @@ export default class OptionsStore { } }; - resetOption = (elementName: T) => { + resetOptions = (elementName: T) => { this.options[elementName] = defaultOptions[elementName]; + this.checkValueInOptions(elementName); }; setManyOptions = ( @@ -81,7 +90,7 @@ export default class OptionsStore { (Object.keys(defaultOptions) as Elements[]) .filter((elementName) => !settings?.exclude.includes(elementName)) .forEach((elementName) => { - this.resetOption(elementName); + this.resetOptions(elementName); }); }