2023-02-25 09:23:28 +00:00
|
|
|
import { Cart } from "../../../../model/cart";
|
2023-02-25 12:29:20 +00:00
|
|
|
import { SERVICE_LIST, Tariffs } from "../../../../model/tariff";
|
2023-02-25 09:23:28 +00:00
|
|
|
import { User } from "../../../../model/user";
|
|
|
|
|
|
|
|
|
|
|
|
export function calcCartData(
|
|
|
|
user: User,
|
|
|
|
cartItems: Cart.CartItem[],
|
|
|
|
discounts: Cart.AnyDiscount[],
|
2023-02-28 13:15:08 +00:00
|
|
|
coupon?: string,
|
2023-02-25 09:23:28 +00:00
|
|
|
): Cart.CartTotal {
|
|
|
|
const cartTotal: Cart.CartTotal = {
|
|
|
|
items: [],
|
|
|
|
totalPrice: 0,
|
|
|
|
priceByService: {
|
2023-02-28 13:15:08 +00:00
|
|
|
templategen: {
|
|
|
|
defaultTariffs: 0,
|
|
|
|
customTariffs: 0,
|
|
|
|
},
|
|
|
|
squiz: {
|
|
|
|
defaultTariffs: 0,
|
|
|
|
customTariffs: 0,
|
|
|
|
},
|
|
|
|
dwarfener: {
|
|
|
|
defaultTariffs: 0,
|
|
|
|
customTariffs: 0,
|
|
|
|
},
|
2023-02-25 09:23:28 +00:00
|
|
|
},
|
2023-02-27 13:33:15 +00:00
|
|
|
envolvedCartDiscounts: [],
|
2023-02-25 09:23:28 +00:00
|
|
|
};
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
// layer 0
|
2023-02-25 09:23:28 +00:00
|
|
|
for (const discount of discounts) {
|
|
|
|
if (discount.conditionType !== "userType" || discount.condition.userType !== user.Type) continue;
|
|
|
|
|
|
|
|
cartItems.forEach(cartItem => {
|
|
|
|
cartTotal.items.push({
|
|
|
|
envolvedDiscounts: [],
|
2023-02-28 13:15:08 +00:00
|
|
|
tariff: cartItem.tariff,
|
2023-02-25 09:23:28 +00:00
|
|
|
totalPrice: cartItem.price,
|
|
|
|
});
|
2023-02-28 13:15:08 +00:00
|
|
|
|
|
|
|
cartTotal.priceByService[cartItem.tariff.privilege.serviceKey].defaultTariffs += cartItem.price;
|
2023-02-25 09:23:28 +00:00
|
|
|
cartTotal.totalPrice += cartItem.price;
|
|
|
|
});
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
cartTotal.totalPrice *= discount.target.factor;
|
|
|
|
cartTotal.envolvedCartDiscounts.push(discount._id);
|
|
|
|
|
2023-02-25 09:23:28 +00:00
|
|
|
return cartTotal;
|
|
|
|
}
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
const couponDiscount = coupon ? findUserDiscount(discounts, user, coupon) : null;
|
2023-02-25 09:23:28 +00:00
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
// layer 1
|
2023-02-25 09:23:28 +00:00
|
|
|
for (const cartItem of cartItems) {
|
|
|
|
const cartItemTotal: Cart.CartItemTotal = {
|
2023-02-28 13:15:08 +00:00
|
|
|
tariff: cartItem.tariff,
|
2023-02-25 09:23:28 +00:00
|
|
|
envolvedDiscounts: [],
|
|
|
|
totalPrice: cartItem.price,
|
|
|
|
};
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
const tariff = cartItem.tariff;
|
|
|
|
const privilegesAffectedByCoupon: string[] = [];
|
|
|
|
|
|
|
|
couponDiscount?.target.products.forEach(product => {
|
|
|
|
if (
|
|
|
|
product.privilegeId === tariff.privilege.privilegeId &&
|
|
|
|
(tariff.customPricePerUnit === undefined || couponDiscount.overwhelm)
|
|
|
|
) {
|
|
|
|
cartItemTotal.totalPrice *= product.factor;
|
|
|
|
cartItemTotal.envolvedDiscounts.push(couponDiscount._id);
|
|
|
|
privilegesAffectedByCoupon.push(product.privilegeId);
|
|
|
|
}
|
|
|
|
});
|
2023-02-25 09:23:28 +00:00
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
const privilegeDiscount = findMaxApplicablePrivilegeDiscount(discounts, tariff);
|
|
|
|
|
|
|
|
privilegeDiscount?.target.products.forEach(product => {
|
|
|
|
if (
|
|
|
|
product.privilegeId === tariff.privilege.privilegeId &&
|
|
|
|
tariff.customPricePerUnit === undefined &&
|
|
|
|
!privilegesAffectedByCoupon.includes(privilegeDiscount.condition.privilege.id)
|
|
|
|
) {
|
|
|
|
cartItemTotal.totalPrice *= product.factor;
|
|
|
|
cartItemTotal.envolvedDiscounts.push(privilegeDiscount._id);
|
|
|
|
}
|
|
|
|
});
|
2023-02-25 09:23:28 +00:00
|
|
|
|
|
|
|
cartTotal.items.push(cartItemTotal);
|
2023-02-28 13:15:08 +00:00
|
|
|
if (tariff.customPricePerUnit === undefined) cartTotal.priceByService[tariff.privilege.serviceKey].defaultTariffs += cartItemTotal.totalPrice;
|
|
|
|
else cartTotal.priceByService[tariff.privilege.serviceKey].customTariffs += cartItemTotal.totalPrice;
|
2023-02-25 09:23:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 12:29:20 +00:00
|
|
|
// layer 2
|
|
|
|
SERVICE_LIST.forEach(service => {
|
2023-02-28 13:15:08 +00:00
|
|
|
const serviceDiscount = findMaxServiceDiscount(service, discounts, cartTotal.priceByService);
|
2023-02-27 13:33:15 +00:00
|
|
|
if (serviceDiscount) {
|
2023-02-28 13:15:08 +00:00
|
|
|
cartTotal.priceByService[service].defaultTariffs *= serviceDiscount.target.factor;
|
2023-02-27 13:33:15 +00:00
|
|
|
cartTotal.envolvedCartDiscounts.push(serviceDiscount._id);
|
|
|
|
}
|
2023-02-25 12:29:20 +00:00
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
cartTotal.totalPrice += cartTotal.priceByService[service].defaultTariffs;
|
|
|
|
cartTotal.totalPrice += cartTotal.priceByService[service].customTariffs;
|
2023-02-25 12:29:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// layer 3
|
|
|
|
const cartPurchasesAmountDiscount = findMaxCartPurchasesAmountDiscount(discounts, cartTotal);
|
2023-02-27 13:33:15 +00:00
|
|
|
if (cartPurchasesAmountDiscount) {
|
|
|
|
cartTotal.totalPrice *= cartPurchasesAmountDiscount.factor;
|
|
|
|
cartTotal.envolvedCartDiscounts.push(cartPurchasesAmountDiscount._id);
|
|
|
|
}
|
2023-02-25 12:29:20 +00:00
|
|
|
|
|
|
|
// layer 4
|
2023-02-25 13:54:52 +00:00
|
|
|
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user);
|
2023-02-27 13:33:15 +00:00
|
|
|
if (totalPurchasesAmountDiscount) {
|
|
|
|
cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
|
|
|
cartTotal.envolvedCartDiscounts.push(totalPurchasesAmountDiscount._id);
|
|
|
|
}
|
2023-02-25 12:29:20 +00:00
|
|
|
|
2023-02-25 09:23:28 +00:00
|
|
|
return cartTotal;
|
|
|
|
}
|
|
|
|
|
2023-02-25 12:29:20 +00:00
|
|
|
function findMaxApplicablePrivilegeDiscount(discounts: Cart.AnyDiscount[], tariff: Tariffs.Tariff): Cart.PrivilegeDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.PrivilegeDiscount => {
|
2023-02-27 13:33:15 +00:00
|
|
|
return (
|
|
|
|
discount.conditionType === "privilege" &&
|
|
|
|
tariff.privilege.privilegeId === discount.condition.privilege.id &&
|
|
|
|
tariff.amount >= discount.condition.privilege.value
|
|
|
|
);
|
2023-02-25 09:23:28 +00:00
|
|
|
});
|
|
|
|
|
2023-02-25 12:29:20 +00:00
|
|
|
if (!applicableDiscounts.length) return null;
|
2023-02-25 09:23:28 +00:00
|
|
|
|
2023-02-25 12:29:20 +00:00
|
|
|
const maxValueDiscount = applicableDiscounts.reduce(
|
2023-02-25 09:23:28 +00:00
|
|
|
(prev, current) => current.condition.privilege.value > prev.condition.privilege.value ? current : prev
|
|
|
|
);
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
2023-02-25 12:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findMaxCartPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], cartTotal: Cart.CartTotal): Cart.CartPurchasesAmountDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.CartPurchasesAmountDiscount => {
|
2023-02-27 13:33:15 +00:00
|
|
|
return discount.conditionType === "cartPurchasesAmount" && cartTotal.totalPrice >= discount.condition.cartPurchasesAmount;
|
2023-02-25 12:29:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce(
|
|
|
|
(prev, current) => current.condition.cartPurchasesAmount > prev.condition.cartPurchasesAmount ? current : prev
|
|
|
|
);
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:54:52 +00:00
|
|
|
function findMaxTotalPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], user: User): Cart.PurchasesAmountDiscount | null {
|
2023-02-25 12:29:20 +00:00
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.PurchasesAmountDiscount => {
|
2023-02-27 13:33:15 +00:00
|
|
|
return discount.conditionType === "purchasesAmount" && user.PurchasesAmount >= discount.condition.purchasesAmount;
|
2023-02-25 12:29:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce(
|
|
|
|
(prev, current) => current.condition.purchasesAmount > prev.condition.purchasesAmount ? current : prev
|
|
|
|
);
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findMaxServiceDiscount(
|
|
|
|
service: Tariffs.ServiceType,
|
|
|
|
discounts: Cart.AnyDiscount[],
|
2023-02-28 13:15:08 +00:00
|
|
|
priceByService: Cart.ServiceToPriceMap,
|
2023-02-25 12:29:20 +00:00
|
|
|
): Cart.ServiceDiscount | null {
|
|
|
|
const discountsForTariffService = discounts.filter((discount): discount is Cart.ServiceDiscount => {
|
|
|
|
return (
|
|
|
|
discount.conditionType === "service" &&
|
|
|
|
discount.condition.service.id === service &&
|
2023-02-28 13:15:08 +00:00
|
|
|
priceByService[service].defaultTariffs + priceByService[service].customTariffs >= discount.condition.service.value
|
2023-02-25 12:29:20 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!discountsForTariffService.length) return null;
|
|
|
|
|
|
|
|
const maxValueDiscount = discountsForTariffService.reduce((prev, current) => {
|
|
|
|
return current.condition.service.value > prev.condition.service.value ? current : prev;
|
|
|
|
});
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
|
|
|
}
|
2023-02-25 09:23:28 +00:00
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
function findUserDiscount(discounts: Cart.AnyDiscount[], user: User, coupon: string,): Cart.UserDiscount | null {
|
|
|
|
const userDiscount = discounts.find((discount): discount is Cart.UserDiscount => {
|
|
|
|
return (
|
|
|
|
discount.conditionType === "user" &&
|
|
|
|
discount.condition.user === user.ID &&
|
|
|
|
discount.condition.coupon === coupon
|
|
|
|
);
|
2023-02-25 09:23:28 +00:00
|
|
|
});
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
return userDiscount ?? null;
|
2023-02-25 09:23:28 +00:00
|
|
|
}
|