2023-07-12 11:14:13 +03:00

96 lines
2.6 KiB
TypeScript

import type { ProcessContext } from '../types';
import { notification } from '@/Components/Common/Notification';
import * as CRMTypes from '@/graphql/crm.types';
import { normalizeOptions } from '@/utils/entity';
import { toJS } from 'mobx';
const key = 'ACTION_CREATEKP';
const errorMessage = 'Ошибка во время создания КП!';
const successMessage = 'КП создано!';
export function action({ store, trpcClient, apolloClient }: ProcessContext) {
const { $calculation, $tables, $results, $process } = store;
$process.add('CreateKP');
$results.clear();
const values = $calculation.$values.getValues();
const insurance = {
fingap: toJS($tables.insurance.row('fingap').getValues()),
kasko: toJS($tables.insurance.row('kasko').getValues()),
osago: toJS($tables.insurance.row('osago').getValues()),
};
const fingap = $tables.fingap.getSelectedRisks();
const paymentRelations = toJS($tables.payments.values);
const paymentSums = toJS($tables.payments.sums);
const elt = {
kasko: toJS($tables.elt.kasko.getSelectedRow),
osago: toJS($tables.elt.osago.getSelectedRow),
};
trpcClient.createQuote
.mutate({
elt,
fingap,
insurance: { values: insurance },
payments: { sums: paymentSums, values: paymentRelations },
values,
})
.then(async (res) => {
if (res.success === false) {
notification.error({
description: res.error,
key,
message: errorMessage,
placement: 'bottomRight',
});
} else {
$results.setPayments(res.data.resultPayments);
$results.setValues(res.data.resultValues);
$calculation.$values.setValues({ ...res.data.values, recalcWithRevision: false });
notification.success({
key,
message: successMessage,
placement: 'bottomRight',
});
const leadid = $calculation.element('selectLead').getValue();
if (leadid) {
const {
data: { quotes },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetQuotesDocument,
variables: {
leadid,
},
});
$calculation
.element('selectQuote')
.setOptions(normalizeOptions(quotes))
.setValue(values.quote);
}
}
})
.catch((error_) => {
const error = error_ as Error;
notification.error({
description: error.message,
key,
message: errorMessage,
placement: 'bottomRight',
});
})
.finally(() => {
$process.delete('CreateKP');
});
}