293 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable @typescript-eslint/naming-convention */
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import type * as CRMTypes from 'graphql/crm.types';
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';
dayjs.extend(utc);
export default function commonReactions(store: RootStore, apolloClient: ApolloClient<object>) {
const { $calculation } = store;
/**
* Заполняем агентов из Интереса
*/
reaction(
() => $calculation.getElementValue('selectLead'),
(leadid) => {
fillAgentsFromLead.fillIndAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcDoubleAgent(store, apolloClient, leadid);
fillAgentsFromLead.fillCalcBroker(store, apolloClient, leadid);
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');
$calculation.resetElement('selectDealerBroker');
return;
}
const {
data: { salon_providers },
} = await apolloClient.query<
CRMTypes.GetDealerPersonQuery,
CRMTypes.GetDealerPersonQueryVariables
>({
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_FROM_DEALER = gql`
query GetBrokerAccountIdFromDealer($dealerId: Uuid!) {
dealer: account(accountid: $dealerId) {
evo_broker_accountid
}
}
`;
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
return;
}
const {
data: { dealer },
} = await apolloClient.query<
CRMTypes.GetBrokerAccountIdFromDealerQuery,
CRMTypes.GetBrokerAccountIdFromDealerQueryVariables
>({
query: QUERY_GET_BROKER_ACCOUNTID_FROM_DEALER,
variables: {
dealerId: dealerPersonId,
},
});
if (dealer?.evo_broker_accountid) {
const {
data: { agent: dealerBroker },
} = await apolloClient.query<CRMTypes.GetAgentQuery, CRMTypes.GetAgentQueryVariables>({
query: QUERY_GET_AGENT,
variables: {
agentid: dealer?.evo_broker_accountid,
},
});
if (dealerBroker) {
$calculation.setElementOptions('selectDealerBroker', normalizeOptions([dealerBroker]));
$calculation.setElementValue('selectDealerBroker', dealerBroker.value);
}
} else {
$calculation.resetElement('selectDealerBroker');
}
}
);
/**
* Заполняем selectDealerRewardCondition
*/
const QUERY_GET_REWARD_CONDITIONS = gql`
query GetRewardConditions($agentid: Uuid!, $currentDate: DateTime) {
evo_reward_conditions(
evo_agent_accountid: $agentid
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
statecode: 0
evo_agency_agreementid_param: { has: true }
) {
label: evo_name
value: evo_reward_conditionid
evo_reward_summ
}
}
`;
reaction(
() => $calculation.getElementValue('selectDealerPerson'),
async (dealerPersonId) => {
if (!dealerPersonId) {
$calculation.resetElement('selectDealerRewardCondition');
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerPersonId,
currentDate: dayjs().toISOString(),
},
});
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
);
const QUERY_GET_REWARD_SUMM = gql`
query GetRewardSumm($conditionId: Uuid!) {
evo_reward_condition(evo_reward_conditionid: $conditionId) {
evo_reward_summ
}
}
`;
reaction(
() => $calculation.getElementValue('selectDealerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerRewardSumm');
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
$calculation.setElementValue('tbxDealerRewardSumm', evo_reward_condition?.evo_reward_summ);
$calculation.unblockElement('tbxDealerRewardSumm');
}
);
/**
* Заполняем selectDealerBrokerRewardCondition
*/
reaction(
() => $calculation.getElementValue('selectDealerBroker'),
async (dealerBrokerId) => {
if (!dealerBrokerId) {
$calculation.resetElement('selectDealerBrokerRewardCondition');
return;
}
const {
data: { evo_reward_conditions },
} = await apolloClient.query<
CRMTypes.GetRewardConditionsQuery,
CRMTypes.GetRewardConditionsQueryVariables
>({
query: QUERY_GET_REWARD_CONDITIONS,
variables: {
agentid: dealerBrokerId,
currentDate: dayjs().toISOString(),
},
});
if (evo_reward_conditions?.length) {
$calculation.setElementOptions(
'selectDealerBrokerRewardCondition',
normalizeOptions(evo_reward_conditions)
);
}
}
);
reaction(
() => $calculation.getElementValue('selectDealerBrokerRewardCondition'),
async (rewardConditionId) => {
if (!rewardConditionId) {
$calculation.resetElement('tbxDealerBrokerRewardSumm');
return;
}
const {
data: { evo_reward_condition },
} = await apolloClient.query<
CRMTypes.GetRewardSummQuery,
CRMTypes.GetRewardSummQueryVariables
>({
query: QUERY_GET_REWARD_SUMM,
variables: {
conditionId: rewardConditionId,
},
});
$calculation.setElementValue(
'tbxDealerBrokerRewardSumm',
evo_reward_condition?.evo_reward_summ
);
$calculation.unblockElement('tbxDealerBrokerRewardSumm');
}
);
reaction(
() => $calculation.getElementValue('selectDealerRewardCondition'),
(dealerRewardConditionId) => {
if (dealerRewardConditionId) {
$calculation.resetElementValue('selectDealerBroker');
}
}
);
reaction(
() => $calculation.getElementValue('selectDealerBrokerRewardCondition'),
(dealerBrokerRewardConditionId) => {
if (dealerBrokerRewardConditionId) {
$calculation.resetElementValue('selectDealerPerson');
}
}
);
}