41 lines
1.9 KiB
JavaScript
41 lines
1.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.calculateInsuranceCharge = calculateInsuranceCharge;
|
|
exports.applyInsurancesToReservation = applyInsurancesToReservation;
|
|
const prisma_1 = require("../lib/prisma");
|
|
function calculateInsuranceCharge(policy, totalDays, baseRentalAmount) {
|
|
switch (policy.chargeType) {
|
|
case 'PER_DAY': return policy.chargeValue * totalDays;
|
|
case 'PER_RENTAL': return policy.chargeValue;
|
|
case 'PERCENTAGE_OF_RENTAL': return Math.round(baseRentalAmount * policy.chargeValue / 100);
|
|
default: return 0;
|
|
}
|
|
}
|
|
async function applyInsurancesToReservation(reservationId, companyId, selectedPolicyIds, totalDays, baseRentalAmount) {
|
|
const allPolicies = await prisma_1.prisma.insurancePolicy.findMany({ where: { companyId, isActive: true } });
|
|
const required = allPolicies.filter((p) => p.isRequired);
|
|
const selected = allPolicies.filter((p) => selectedPolicyIds.includes(p.id) && !p.isRequired);
|
|
const toApply = [...required, ...selected];
|
|
const records = toApply.map((policy) => ({
|
|
reservationId,
|
|
insurancePolicyId: policy.id,
|
|
policyName: policy.name,
|
|
policyType: policy.type,
|
|
chargeType: policy.chargeType,
|
|
chargeValue: policy.chargeValue,
|
|
totalCharge: calculateInsuranceCharge(policy, totalDays, baseRentalAmount),
|
|
}));
|
|
const insuranceTotal = records.reduce((s, r) => s + r.totalCharge, 0);
|
|
await prisma_1.prisma.$transaction([
|
|
prisma_1.prisma.reservationInsurance.createMany({ data: records }),
|
|
prisma_1.prisma.reservation.update({
|
|
where: { id: reservationId },
|
|
data: {
|
|
insuranceTotal,
|
|
totalAmount: { increment: insuranceTotal },
|
|
},
|
|
}),
|
|
]);
|
|
return { records, insuranceTotal };
|
|
}
|
|
//# sourceMappingURL=insuranceService.js.map
|