138 lines
3.9 KiB
TypeScript
138 lines
3.9 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
||
import type { ProcessContext } from '@/process/types';
|
||
import { reaction } from 'mobx';
|
||
import { normalizeOptions } from 'tools/entity';
|
||
import { makeDisposable } from 'tools/mobx';
|
||
|
||
export default function reactions({ store, apolloClient }: ProcessContext) {
|
||
const { $calculation, $process } = store;
|
||
|
||
/**
|
||
* Если lead содержит данные, то в opportunity подгружается значение из поля Интереса
|
||
* Лизинговая сделка (lead.evo_opportunityid → opportunity),
|
||
* и в списке для выбора только эта ЛС указывается
|
||
* Иначе ничего не указывается
|
||
*/
|
||
|
||
makeDisposable(
|
||
() =>
|
||
reaction(
|
||
() => $calculation.element('selectLead').getValue(),
|
||
async (leadid) => {
|
||
if (!leadid) {
|
||
$calculation.element('selectOpportunity').resetValue();
|
||
|
||
return;
|
||
}
|
||
|
||
const {
|
||
data: { lead },
|
||
} = await apolloClient.query({
|
||
query: CRMTypes.GetLeadDocument,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
if (lead?.evo_opportunityidData?.value) {
|
||
$calculation.element('selectOpportunity').setValue(lead?.evo_opportunityidData?.value);
|
||
} else {
|
||
$calculation.element('selectOpportunity').resetValue();
|
||
}
|
||
}
|
||
),
|
||
() => $process.has('LoadKP')
|
||
);
|
||
|
||
reaction(
|
||
() => $calculation.element('selectOpportunity').getValue(),
|
||
async (opportunityid) => {
|
||
const leadid = $calculation.element('selectLead').getValue();
|
||
|
||
if (leadid) {
|
||
const {
|
||
data: { lead },
|
||
} = await apolloClient.query({
|
||
query: CRMTypes.GetLeadDocument,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
if (!opportunityid && lead?.evo_opportunityidData?.value) {
|
||
$calculation.element('selectLead').resetValue();
|
||
}
|
||
}
|
||
|
||
if (opportunityid) {
|
||
const {
|
||
data: { opportunity },
|
||
} = await apolloClient.query({
|
||
query: CRMTypes.GetOpportunityDocument,
|
||
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.
|
||
*/
|
||
|
||
reaction(
|
||
() => $calculation.element('selectLead').getValue(),
|
||
async (leadid) => {
|
||
if (leadid) {
|
||
const {
|
||
data: { quotes },
|
||
} = await apolloClient.query({
|
||
fetchPolicy: 'network-only',
|
||
query: CRMTypes.GetQuotesDocument,
|
||
variables: {
|
||
leadid,
|
||
},
|
||
});
|
||
|
||
$calculation.element('selectQuote').setOptions(normalizeOptions(quotes));
|
||
} else {
|
||
$calculation.element('selectQuote').reset();
|
||
}
|
||
}
|
||
);
|
||
|
||
reaction(
|
||
() => $calculation.element('selectLead').getValue(),
|
||
async (leadid) => {
|
||
if (!leadid) {
|
||
$calculation.element('tbxINNForCalc').resetValue();
|
||
|
||
return;
|
||
}
|
||
|
||
const {
|
||
data: { lead },
|
||
} = await apolloClient.query({
|
||
query: CRMTypes.GetLeadDocument,
|
||
variables: { leadid },
|
||
});
|
||
|
||
if (lead?.evo_inn) {
|
||
$calculation.element('tbxINNForCalc').setValue(Number.parseInt(lead?.evo_inn, 10));
|
||
} else {
|
||
$calculation.element('tbxINNForCalc').resetValue();
|
||
}
|
||
}
|
||
);
|
||
}
|