2023-03-06 13:25:33 +00:00
|
|
|
import { CartItem, AnyDiscount, CartTotal, CartItemTotal, PrivilegeDiscount, CartPurchasesAmountDiscount, PurchasesAmountDiscount, ServiceToPriceMap, ServiceDiscount, UserDiscount } from "@root/model/cart";
|
2023-03-08 10:52:16 +00:00
|
|
|
import { ServiceType, SERVICE_LIST, Tariff } from "../../model/tariff";
|
|
|
|
import { User } from "../../model/user";
|
2023-02-25 09:23:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
export function calcCartData(
|
|
|
|
user: User,
|
2023-03-06 13:25:33 +00:00
|
|
|
cartItems: CartItem[],
|
|
|
|
discounts: AnyDiscount[],
|
2023-02-28 13:15:08 +00:00
|
|
|
coupon?: string,
|
2023-03-06 13:25:33 +00:00
|
|
|
): CartTotal {
|
|
|
|
const cartTotal: CartTotal = {
|
2023-02-25 09:23:28 +00:00
|
|
|
items: [],
|
|
|
|
totalPrice: 0,
|
|
|
|
priceByService: {
|
2023-03-08 10:52:16 +00:00
|
|
|
templategen: 0,
|
|
|
|
squiz: 0,
|
|
|
|
dwarfener: 0,
|
2023-02-25 09:23:28 +00:00
|
|
|
},
|
2023-03-08 10:52:16 +00:00
|
|
|
discountsByService: {
|
|
|
|
templategen: null,
|
|
|
|
squiz: null,
|
|
|
|
dwarfener: null,
|
|
|
|
},
|
|
|
|
envolvedCartDiscountIds: [],
|
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({
|
2023-03-08 10:52:16 +00:00
|
|
|
envolvedDiscountIds: [],
|
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
|
|
|
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.priceByService[cartItem.tariff.privilege.serviceKey] += 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;
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.envolvedCartDiscountIds.push(discount._id);
|
2023-02-28 13:15:08 +00:00
|
|
|
|
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) {
|
2023-03-06 13:25:33 +00:00
|
|
|
const cartItemTotal: CartItemTotal = {
|
2023-02-28 13:15:08 +00:00
|
|
|
tariff: cartItem.tariff,
|
2023-03-08 10:52:16 +00:00
|
|
|
envolvedDiscountIds: [],
|
2023-02-25 09:23:28 +00:00
|
|
|
totalPrice: cartItem.price,
|
|
|
|
};
|
|
|
|
|
2023-02-28 13:15:08 +00:00
|
|
|
const tariff = cartItem.tariff;
|
|
|
|
const privilegesAffectedByCoupon: string[] = [];
|
|
|
|
|
|
|
|
couponDiscount?.target.products.forEach(product => {
|
2023-03-08 10:52:16 +00:00
|
|
|
if (product.privilegeId !== tariff.privilege.privilegeId) return;
|
|
|
|
if (tariff.customPricePerUnit !== undefined && !couponDiscount.overwhelm) return;
|
|
|
|
|
|
|
|
cartItemTotal.totalPrice *= product.factor;
|
|
|
|
cartItemTotal.envolvedDiscountIds.push(couponDiscount._id);
|
|
|
|
privilegesAffectedByCoupon.push(product.privilegeId);
|
2023-02-28 13:15:08 +00:00
|
|
|
});
|
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 => {
|
2023-03-08 10:52:16 +00:00
|
|
|
if (product.privilegeId !== tariff.privilege.privilegeId) return;
|
|
|
|
if (tariff.customPricePerUnit !== undefined) return;
|
|
|
|
if (privilegesAffectedByCoupon.includes(privilegeDiscount.condition.privilege.id)) return;
|
|
|
|
|
|
|
|
cartItemTotal.totalPrice *= product.factor;
|
|
|
|
cartItemTotal.envolvedDiscountIds.push(privilegeDiscount._id);
|
2023-02-28 13:15:08 +00:00
|
|
|
});
|
2023-02-25 09:23:28 +00:00
|
|
|
|
|
|
|
cartTotal.items.push(cartItemTotal);
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.priceByService[tariff.privilege.serviceKey] += cartItemTotal.totalPrice;
|
2023-02-25 09:23:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 12:29:20 +00:00
|
|
|
// layer 2
|
2023-03-06 13:25:33 +00:00
|
|
|
SERVICE_LIST.map(service => service.serviceKey).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-03-08 10:52:16 +00:00
|
|
|
cartTotal.priceByService[service] *= serviceDiscount.target.factor;
|
|
|
|
cartTotal.discountsByService[service] = serviceDiscount;
|
2023-02-27 13:33:15 +00:00
|
|
|
}
|
2023-02-25 12:29:20 +00:00
|
|
|
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.totalPrice += cartTotal.priceByService[service];
|
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;
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.envolvedCartDiscountIds.push(cartPurchasesAmountDiscount._id);
|
2023-02-27 13:33:15 +00:00
|
|
|
}
|
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;
|
2023-03-08 10:52:16 +00:00
|
|
|
cartTotal.envolvedCartDiscountIds.push(totalPurchasesAmountDiscount._id);
|
2023-02-27 13:33:15 +00:00
|
|
|
}
|
2023-02-25 12:29:20 +00:00
|
|
|
|
2023-02-25 09:23:28 +00:00
|
|
|
return cartTotal;
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:25:33 +00:00
|
|
|
function findMaxApplicablePrivilegeDiscount(discounts: AnyDiscount[], tariff: Tariff): PrivilegeDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is 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
|
|
|
}
|
|
|
|
|
2023-03-06 13:25:33 +00:00
|
|
|
function findMaxCartPurchasesAmountDiscount(discounts: AnyDiscount[], cartTotal: CartTotal): CartPurchasesAmountDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is 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-03-06 13:25:33 +00:00
|
|
|
function findMaxTotalPurchasesAmountDiscount(discounts: AnyDiscount[], user: User): PurchasesAmountDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is 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(
|
2023-03-06 13:25:33 +00:00
|
|
|
service: ServiceType,
|
|
|
|
discounts: AnyDiscount[],
|
|
|
|
priceByService: ServiceToPriceMap,
|
|
|
|
): ServiceDiscount | null {
|
|
|
|
const discountsForTariffService = discounts.filter((discount): discount is ServiceDiscount => {
|
2023-02-25 12:29:20 +00:00
|
|
|
return (
|
|
|
|
discount.conditionType === "service" &&
|
|
|
|
discount.condition.service.id === service &&
|
2023-03-08 10:52:16 +00:00
|
|
|
priceByService[service] >= 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-03-06 13:25:33 +00:00
|
|
|
function findUserDiscount(discounts: AnyDiscount[], user: User, coupon: string,): UserDiscount | null {
|
|
|
|
const userDiscount = discounts.find((discount): discount is UserDiscount => {
|
2023-02-28 13:15:08 +00:00
|
|
|
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-03-06 13:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createCartItem(tariff: Tariff): CartItem {
|
|
|
|
const pricePerUnit = tariff.customPricePerUnit ?? tariff.privilege.pricePerUnit;
|
|
|
|
const price = pricePerUnit * tariff.amount;
|
|
|
|
|
|
|
|
return { tariff, price, id: "someId" };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findDiscountById(discounts: AnyDiscount[], id: string) {
|
|
|
|
const discount = discounts.find(discount => discount._id === id);
|
|
|
|
if (!discount) throw new Error("Discount not found by id");
|
|
|
|
|
|
|
|
return discount;
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:39:46 +00:00
|
|
|
export function findDiscountFactor(discount: AnyDiscount, privilegeId?: string) {
|
2023-03-06 13:25:33 +00:00
|
|
|
switch (discount.conditionType) {
|
2023-03-06 17:30:35 +00:00
|
|
|
case "cartPurchasesAmount":
|
|
|
|
return discount.factor;
|
|
|
|
case "purchasesAmount":
|
2023-03-06 13:25:33 +00:00
|
|
|
return discount.factor;
|
|
|
|
case "privilege": {
|
|
|
|
const product = discount.target.products.find(product => product.privilegeId === privilegeId);
|
|
|
|
if (!product) throw new Error("Discount target product not found");
|
|
|
|
|
|
|
|
return product.factor;
|
|
|
|
}
|
|
|
|
case "user": {
|
|
|
|
const product = discount.target.products.find(product => product.privilegeId === privilegeId);
|
|
|
|
if (!product) throw new Error("Discount target product not found");
|
|
|
|
|
|
|
|
return product.factor;
|
|
|
|
}
|
|
|
|
case "service":
|
|
|
|
return discount.target.factor;
|
|
|
|
case "userType":
|
|
|
|
return discount.target.factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDiscountFactor(factor: number): string {
|
|
|
|
return `${((1 - factor) * 100).toFixed(1)}%`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PositiveInput = (event: any) => {
|
|
|
|
const numberInput = parseInt(event.target.value);
|
|
|
|
if (isNaN(numberInput) || numberInput < 0) {
|
|
|
|
event.target.value = '0';
|
|
|
|
}
|
|
|
|
};
|