Chika f85373689a add trpc server
get calculation values types from zod schema
add loadKP reaction
get base agents data from kp
2022-11-02 13:18:33 +03:00

164 lines
5.1 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 createReactions from '../lib/create-reactions';
import * as query from '../lib/query';
dayjs.extend(utc);
export function commonReactions(store: RootStore, apolloClient: ApolloClient<object>) {
const { $calculation, $process } = store;
/**
* Заполняем selectDealerPerson
* На изменение Салон приобретения формируем список в поле ЮЛ поставщика - записи Контрагент,
* у которых статус = активный И Поставщик = Да И Тип поставщика = Юридическое лицо
* И связаны с карточкой Контрагент из поля "Салон приобретения" по связи Салон-ЮЛ
*/
const QUERY_GET_DEALER_PERSON = gql`
query GetDealerPerson($dealerId: Uuid!) {
salon_providers(statecode: 0, salonaccountid: $dealerId) {
label: name
value: accountid
}
}
`;
reaction(
() => $calculation.element('selectDealer').getValue(),
async (dealerId) => {
if (!dealerId) {
$calculation.element('selectDealerPerson').reset();
$calculation.element('selectDealerBroker').reset();
return;
}
const {
data: { salon_providers },
} = await apolloClient.query<
CRMTypes.GetDealerPersonQuery,
CRMTypes.GetDealerPersonQueryVariables
>({
query: QUERY_GET_DEALER_PERSON,
variables: {
dealerId,
},
});
if (salon_providers?.length) {
$calculation.element('selectDealerPerson').setOptions(normalizeOptions(salon_providers));
if (!$process.has('LoadKP')) {
$calculation.element('selectDealerPerson').setValue(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.element('selectDealerPerson').getValue(),
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.QUERY_GET_AGENT,
variables: {
agentid: dealer?.evo_broker_accountid,
},
});
if (dealerBroker) {
$calculation.element('selectDealerBroker').setOptions(normalizeOptions([dealerBroker]));
if (!$process.has('LoadKP')) {
$calculation.element('selectDealerBroker').setValue(dealerBroker.value);
}
}
} else {
$calculation.element('selectDealerBroker').reset();
}
}
);
// Заполняем selectDealerRewardCondition
createReactions.fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectDealerPerson',
rewardConditionField: 'selectDealerRewardCondition',
});
// Заполняем tbxDealerRewardSumm
createReactions.fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectDealerRewardCondition',
rewardSummField: 'tbxDealerRewardSumm',
});
// Заполняем selectDealerBrokerRewardCondition
createReactions.fillAgentRewardReaction(store, apolloClient, {
agentField: 'selectDealerBroker',
rewardConditionField: 'selectDealerBrokerRewardCondition',
});
// Заполняем tbxDealerBrokerRewardSumm
createReactions.fillAgentRewardSummReaction(store, apolloClient, {
rewardConditionField: 'selectDealerBrokerRewardCondition',
rewardSummField: 'tbxDealerBrokerRewardSumm',
});
reaction(
() => $calculation.element('selectDealerRewardCondition').getValue(),
(dealerRewardConditionId) => {
if (dealerRewardConditionId) {
$calculation.element('selectDealerBroker').resetValue();
}
}
);
reaction(
() => $calculation.element('selectDealerBrokerRewardCondition').getValue(),
(dealerBrokerRewardConditionId) => {
if (dealerBrokerRewardConditionId) {
$calculation.element('selectDealerPerson').resetValue();
}
}
);
}
export function validationReactions(store: RootStore, apolloClient: ApolloClient<object>) {}