136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import type { CalculationValues } from '@/stores/calculation/values/types';
|
|
import dayjs from 'dayjs';
|
|
import { first, sort } from 'radash';
|
|
|
|
type GetTarifInputValues = Pick<
|
|
CalculationValues,
|
|
| 'deliveryTime'
|
|
| 'firstPaymentPerc'
|
|
| 'lastPaymentPerc'
|
|
| 'leaseObjectType'
|
|
| 'leaseObjectUsed'
|
|
| 'leasingPeriod'
|
|
| 'product'
|
|
>;
|
|
|
|
type GetRatesInputValues = Pick<CalculationValues, 'tarif'>;
|
|
|
|
export default function helper({ apolloClient }: Pick<ProcessContext, 'apolloClient'>) {
|
|
return {
|
|
async getRates({ tarif: tarifId }: GetRatesInputValues) {
|
|
const currentDate = dayjs().utc(false).toISOString();
|
|
|
|
const {
|
|
data: { evo_rates },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetRatesDocument,
|
|
variables: {
|
|
currentDate,
|
|
},
|
|
});
|
|
|
|
if (!tarifId) {
|
|
return {
|
|
evo_rates,
|
|
};
|
|
}
|
|
|
|
const {
|
|
data: { evo_tarif },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetTarifDocument,
|
|
variables: { tarifId },
|
|
});
|
|
|
|
const evo_tarif_evo_rate =
|
|
evo_tarif?.evo_rates &&
|
|
first(sort(evo_tarif?.evo_rates, (x) => dayjs(x?.evo_datefrom).date()));
|
|
|
|
if (evo_tarif_evo_rate) {
|
|
return {
|
|
evo_rate: evo_tarif_evo_rate,
|
|
evo_rates,
|
|
};
|
|
}
|
|
|
|
const evo_rate = evo_rates?.find((x) => x?.evo_id === 'BASE');
|
|
|
|
return {
|
|
evo_rate,
|
|
evo_rates,
|
|
};
|
|
},
|
|
|
|
async getTarifs({
|
|
deliveryTime,
|
|
firstPaymentPerc,
|
|
lastPaymentPerc,
|
|
leaseObjectType: leaseObjectTypeId,
|
|
leaseObjectUsed,
|
|
leasingPeriod,
|
|
product,
|
|
}: GetTarifInputValues) {
|
|
const currentDate = dayjs().utc(false).toISOString();
|
|
|
|
const {
|
|
data: { evo_tarifs = [] },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetTarifsDocument,
|
|
variables: {
|
|
currentDate,
|
|
},
|
|
});
|
|
|
|
const evo_tarif = evo_tarifs
|
|
?.filter(
|
|
(tarif) =>
|
|
tarif &&
|
|
product &&
|
|
tarif.evo_baseproductid === product &&
|
|
tarif.evo_min_period !== null &&
|
|
tarif.evo_min_period <= leasingPeriod &&
|
|
tarif.evo_max_period !== null &&
|
|
tarif.evo_max_period >= leasingPeriod &&
|
|
tarif.evo_delivery_time?.includes(deliveryTime) &&
|
|
tarif.evo_min_first_payment !== null &&
|
|
tarif.evo_min_first_payment <= firstPaymentPerc &&
|
|
tarif.evo_max_first_payment !== null &&
|
|
tarif.evo_max_first_payment >= firstPaymentPerc &&
|
|
tarif.evo_min_last_payment !== null &&
|
|
tarif.evo_min_last_payment <= lastPaymentPerc &&
|
|
tarif.evo_max_last_payment !== null &&
|
|
tarif.evo_max_last_payment >= lastPaymentPerc
|
|
)
|
|
.filter(
|
|
(tarif) =>
|
|
!tarif?.evo_leasingobject_types?.length ||
|
|
(leaseObjectTypeId &&
|
|
tarif.evo_leasingobject_types
|
|
.map((x) => x?.evo_leasingobject_typeid)
|
|
.includes(leaseObjectTypeId))
|
|
)
|
|
.filter((tarif) => {
|
|
if (leaseObjectUsed === true) {
|
|
if (!tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_001)) {
|
|
return true;
|
|
}
|
|
} else if (
|
|
!tarif?.evo_pl_use_type?.length ||
|
|
tarif.evo_pl_use_type.includes(100_000_000)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
})
|
|
.at(0);
|
|
|
|
return { evo_tarif, evo_tarifs };
|
|
},
|
|
};
|
|
}
|