126 lines
3.7 KiB
TypeScript
126 lines
3.7 KiB
TypeScript
import type { ownOsagoRequest } from './request';
|
||
import type * as ELT from '@/api/elt/types';
|
||
import type { Row } from '@/Components/Calculation/Form/ELT/types';
|
||
import { MAX_FRANCHISE, MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values';
|
||
import type { CalculationValues } from '@/stores/calculation/values/types';
|
||
|
||
export function convertEltOsagoResponse(response: ELT.ResponseEltOsago, row: Row): Row {
|
||
const { message, numCalc, premiumSum = 0, skCalcId } = response;
|
||
let { error } = response;
|
||
if (premiumSum > MAX_INSURANCE) {
|
||
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости ОСАГО: ${Intl.NumberFormat(
|
||
'ru',
|
||
{
|
||
currency: 'RUB',
|
||
style: 'currency',
|
||
}
|
||
).format(MAX_INSURANCE)}`;
|
||
}
|
||
if (premiumSum < MIN_INSURANCE) {
|
||
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости ОСАГО: ${Intl.NumberFormat(
|
||
'ru',
|
||
{
|
||
currency: 'RUB',
|
||
style: 'currency',
|
||
}
|
||
).format(MIN_INSURANCE)}`;
|
||
}
|
||
|
||
return {
|
||
...row,
|
||
message: error || message,
|
||
numCalc: `${numCalc}`,
|
||
skCalcId,
|
||
status: error ? 'error' : null,
|
||
sum: premiumSum,
|
||
};
|
||
}
|
||
|
||
type ResponseOwnOsago = Awaited<ReturnType<typeof ownOsagoRequest>>;
|
||
|
||
export function convertOwnOsagoResult(result: ResponseOwnOsago | undefined, row: Row): Row {
|
||
if (!result) {
|
||
return {
|
||
...row,
|
||
message:
|
||
'Для получения расчета ОСАГО следует использовать калькулятор ЭЛТ или Индивидуальный запрос',
|
||
numCalc: '',
|
||
skCalcId: '',
|
||
status: 'error',
|
||
sum: 0,
|
||
totalFranchise: 0,
|
||
};
|
||
}
|
||
|
||
if (!result.evo_id) {
|
||
return {
|
||
...row,
|
||
message: 'Сервер не вернул идентификатор страховки evo_id',
|
||
numCalc: '',
|
||
skCalcId: '',
|
||
status: 'error',
|
||
sum: result.evo_graph_price_withoutnds || 0,
|
||
};
|
||
}
|
||
|
||
return {
|
||
...row,
|
||
message: null,
|
||
numCalc: result.evo_id,
|
||
status: null,
|
||
sum: result.evo_graph_price_withoutnds || 0,
|
||
};
|
||
}
|
||
|
||
export function convertEltKaskoResponse(
|
||
res: ELT.ResponseEltKasko,
|
||
row: Row,
|
||
values: CalculationValues
|
||
): Row {
|
||
const { kaskoSum = 0, message, paymentPeriods, requestId, skCalcId, totalFranchise = 0 } = res;
|
||
let { error } = res;
|
||
|
||
const sum = values.leasingPeriod <= 16 ? kaskoSum : paymentPeriods?.[0]?.kaskoSum || 0;
|
||
|
||
if (totalFranchise > MAX_FRANCHISE) {
|
||
error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat(
|
||
'ru',
|
||
{
|
||
currency: 'RUB',
|
||
style: 'currency',
|
||
}
|
||
).format(MAX_FRANCHISE)}`;
|
||
}
|
||
|
||
if (sum > MAX_INSURANCE) {
|
||
error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости КАСКО: ${Intl.NumberFormat(
|
||
'ru',
|
||
{
|
||
currency: 'RUB',
|
||
style: 'currency',
|
||
}
|
||
).format(MAX_INSURANCE)}`;
|
||
}
|
||
|
||
if (sum < MIN_INSURANCE) {
|
||
error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости КАСКО: ${Intl.NumberFormat(
|
||
'ru',
|
||
{
|
||
currency: 'RUB',
|
||
style: 'currency',
|
||
}
|
||
).format(MIN_INSURANCE)}`;
|
||
}
|
||
|
||
return {
|
||
...row,
|
||
message: error || message,
|
||
numCalc: '0',
|
||
requestId,
|
||
skCalcId,
|
||
status: error ? 'error' : null,
|
||
sum,
|
||
totalFranchise,
|
||
};
|
||
}
|