161 lines
4.7 KiB
TypeScript
161 lines
4.7 KiB
TypeScript
/* eslint-disable implicit-arrow-linebreak */
|
||
import { gql } from '@apollo/client';
|
||
import type * as CRMTypes from 'graphql/crm.types';
|
||
import { reaction } from 'mobx';
|
||
import type { ReactionsContext } from 'process/types';
|
||
import { normalizeOptions } from 'tools/entity';
|
||
import { makeDisposable } from 'tools/mobx';
|
||
|
||
export default function commonReactions({ store, apolloClient }: ReactionsContext) {
|
||
const { $calculation, $process } = store;
|
||
|
||
/**
|
||
* Если lead содержит данные, то в opportunity подгружается значение из поля Интереса
|
||
* Лизинговая сделка (lead.evo_opportunityid → opportunity),
|
||
* и в списке для выбора только эта ЛС указывается
|
||
* Иначе ничего не указывается
|
||
*/
|
||
|
||
const QUERY_GET_OPPORTUNITY_BY_LEAD = gql`
|
||
query GetOpportunityByLead($leadid: Uuid!) {
|
||
lead(leadid: $leadid) {
|
||
evo_opportunityidData {
|
||
label: name
|
||
value: opportunityid
|
||
}
|
||
}
|
||
}
|
||
`;
|
||
|
||
makeDisposable(
|
||
() =>
|
||
reaction(
|
||
() => $calculation.element('selectLead').getValue(),
|
||
async (leadid) => {
|
||
if (!leadid) {
|
||
$calculation.element('selectOpportunity').resetValue();
|
||
|
||
return;
|
||
}
|
||
|
||
const {
|
||
data: { lead },
|
||
} = await apolloClient.query<
|
||
CRMTypes.GetOpportunityByLeadQuery,
|
||
CRMTypes.GetOpportunityByLeadQueryVariables
|
||
>({
|
||
query: QUERY_GET_OPPORTUNITY_BY_LEAD,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
if (lead?.evo_opportunityidData?.value) {
|
||
$calculation.element('selectOpportunity').setValue(lead?.evo_opportunityidData?.value);
|
||
} else {
|
||
$calculation.element('selectOpportunity').resetValue();
|
||
}
|
||
}
|
||
),
|
||
() => $process.has('LoadKP')
|
||
);
|
||
|
||
/**
|
||
* если opportunity содержит данные,
|
||
то в lead подгружается значение из поля Интерес
|
||
выбранной карточки Лизинговая сделка (opportunity.evo_leadid)
|
||
иначе ничего не делать
|
||
*/
|
||
const QUERY_GET_LEADID_BY_OPPORTUNITY = gql`
|
||
query GetLeadidByOpportunity($opportunityid: Uuid!) {
|
||
opportunity(opportunityid: $opportunityid) {
|
||
evo_leadid
|
||
}
|
||
}
|
||
`;
|
||
|
||
reaction(
|
||
() => $calculation.element('selectOpportunity').getValue(),
|
||
async (opportunityid) => {
|
||
const leadid = $calculation.element('selectLead').getValue();
|
||
|
||
if (leadid) {
|
||
const {
|
||
data: { lead },
|
||
} = await apolloClient.query<
|
||
CRMTypes.GetOpportunityByLeadQuery,
|
||
CRMTypes.GetOpportunityByLeadQueryVariables
|
||
>({
|
||
query: QUERY_GET_OPPORTUNITY_BY_LEAD,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
if (!opportunityid && lead?.evo_opportunityidData?.value) {
|
||
$calculation.element('selectLead').resetValue();
|
||
}
|
||
}
|
||
|
||
if (opportunityid) {
|
||
const {
|
||
data: { opportunity },
|
||
} = await apolloClient.query<
|
||
CRMTypes.GetLeadidByOpportunityQuery,
|
||
CRMTypes.GetLeadidByOpportunityQueryVariables
|
||
>({
|
||
query: QUERY_GET_LEADID_BY_OPPORTUNITY,
|
||
variables: {
|
||
opportunityid,
|
||
},
|
||
});
|
||
|
||
if (opportunity?.evo_leadid) {
|
||
$calculation.element('selectLead').setValue(opportunity.evo_leadid);
|
||
} else {
|
||
$calculation.element('selectLead').resetValue();
|
||
}
|
||
}
|
||
}
|
||
);
|
||
|
||
/**
|
||
* Если lead содержит данные,
|
||
То в quote подгружается список Предложений CRM (сущность quote),
|
||
у которых в поле Интерес (quote.evo_leadid) записана ссылка на выбранный интерес.
|
||
Иначе очищать поле калькулятора quote.
|
||
*/
|
||
|
||
const QUERY_GET_QUOTES_BY_LEAD = gql`
|
||
query GetQuotesByLead($leadid: Uuid!) {
|
||
quotes(evo_leadid: $leadid) {
|
||
label: evo_quotename
|
||
value: quoteid
|
||
}
|
||
}
|
||
`;
|
||
|
||
reaction(
|
||
() => $calculation.element('selectLead').getValue(),
|
||
async (leadid) => {
|
||
if (leadid) {
|
||
const {
|
||
data: { quotes },
|
||
} = await apolloClient.query<
|
||
CRMTypes.GetQuotesByLeadQuery,
|
||
CRMTypes.GetQuotesByLeadQueryVariables
|
||
>({
|
||
query: QUERY_GET_QUOTES_BY_LEAD,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
$calculation.element('selectQuote').setOptions(normalizeOptions(quotes));
|
||
} else {
|
||
$calculation.element('selectQuote').reset();
|
||
}
|
||
}
|
||
);
|
||
}
|