2023-05-12 12:38:35 +03:00

88 lines
2.6 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';
type GetTarifInputValues = Pick<
CalculationValues,
| 'deliveryTime'
| 'firstPaymentPerc'
| 'lastPaymentPerc'
| 'leaseObjectType'
| 'leaseObjectUsed'
| 'leasingPeriod'
| 'product'
>;
export default function helper({ apolloClient }: Pick<ProcessContext, 'apolloClient'>) {
return {
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 };
},
};
}