fixes according to fixed types
This commit is contained in:
parent
e905e12ca3
commit
56d0e535ec
@ -7,7 +7,6 @@ export function calcCartData(
|
|||||||
user: User,
|
user: User,
|
||||||
cartItems: Cart.CartItem[],
|
cartItems: Cart.CartItem[],
|
||||||
discounts: Cart.AnyDiscount[],
|
discounts: Cart.AnyDiscount[],
|
||||||
privileges: Tariffs.Privilege[],
|
|
||||||
promocode?: Cart.Promocode,
|
promocode?: Cart.Promocode,
|
||||||
): Cart.CartTotal {
|
): Cart.CartTotal {
|
||||||
const cartTotal: Cart.CartTotal = {
|
const cartTotal: Cart.CartTotal = {
|
||||||
@ -38,7 +37,7 @@ export function calcCartData(
|
|||||||
return cartTotal;
|
return cartTotal;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO layer 1
|
// TODO layer 1-user
|
||||||
if (promocode) {
|
if (promocode) {
|
||||||
for (const discount of discounts) {
|
for (const discount of discounts) {
|
||||||
if (discount.conditionType !== "user" || discount.condition.user !== user.ID || discount.condition.coupon !== promocode.name) continue;
|
if (discount.conditionType !== "user" || discount.condition.user !== user.ID || discount.condition.coupon !== promocode.name) continue;
|
||||||
@ -54,6 +53,7 @@ export function calcCartData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// layer 1-promo
|
||||||
for (const cartItem of cartItems) {
|
for (const cartItem of cartItems) {
|
||||||
const cartItemTotal: Cart.CartItemTotal = {
|
const cartItemTotal: Cart.CartItemTotal = {
|
||||||
item: cartItem.item,
|
item: cartItem.item,
|
||||||
@ -72,7 +72,7 @@ export function calcCartData(
|
|||||||
const maxPrivilegeDiscount = findMaxApplicablePrivilegeDiscount(discounts, tariff);
|
const maxPrivilegeDiscount = findMaxApplicablePrivilegeDiscount(discounts, tariff);
|
||||||
if (maxPrivilegeDiscount) {
|
if (maxPrivilegeDiscount) {
|
||||||
maxPrivilegeDiscount.target.products.forEach(product => {
|
maxPrivilegeDiscount.target.products.forEach(product => {
|
||||||
if (product.productId === tariff.id) { // TODO productId - ид продукта(тариф или пакет тарифов) или привилегии? Если второе, то откуда берется привилегия?
|
if (product.productId === tariff.privilege.privilegeId) {
|
||||||
cartItemTotal.totalPrice *= product.factor;
|
cartItemTotal.totalPrice *= product.factor;
|
||||||
cartItemTotal.envolvedDiscounts.push(maxPrivilegeDiscount._id);
|
cartItemTotal.envolvedDiscounts.push(maxPrivilegeDiscount._id);
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ export function calcCartData(
|
|||||||
}
|
}
|
||||||
|
|
||||||
cartTotal.items.push(cartItemTotal);
|
cartTotal.items.push(cartItemTotal);
|
||||||
cartTotal.priceByService[tariff.service] += cartItemTotal.totalPrice;
|
cartTotal.priceByService[tariff.privilege.serviceKey] += cartItemTotal.totalPrice;
|
||||||
}
|
}
|
||||||
|
|
||||||
// layer 2
|
// layer 2
|
||||||
@ -96,7 +96,7 @@ export function calcCartData(
|
|||||||
if (cartPurchasesAmountDiscount) cartTotal.totalPrice *= cartPurchasesAmountDiscount.factor;
|
if (cartPurchasesAmountDiscount) cartTotal.totalPrice *= cartPurchasesAmountDiscount.factor;
|
||||||
|
|
||||||
// layer 4
|
// layer 4
|
||||||
const totalPurchasesAmountDiscount = findMaxPurchasesAmountDiscount(discounts, user);
|
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user);
|
||||||
if (totalPurchasesAmountDiscount) cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
if (totalPurchasesAmountDiscount) cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
||||||
|
|
||||||
return cartTotal;
|
return cartTotal;
|
||||||
@ -130,7 +130,7 @@ function findMaxCartPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], cartT
|
|||||||
return maxValueDiscount;
|
return maxValueDiscount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findMaxPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], user: User): Cart.PurchasesAmountDiscount | null {
|
function findMaxTotalPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], user: User): Cart.PurchasesAmountDiscount | null {
|
||||||
const applicableDiscounts = discounts.filter((discount): discount is Cart.PurchasesAmountDiscount => {
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.PurchasesAmountDiscount => {
|
||||||
return discount.conditionType === "purchasesAmount" && discount.condition.purchasesAmount >= user.PurchasesAmount;
|
return discount.conditionType === "purchasesAmount" && discount.condition.purchasesAmount >= user.PurchasesAmount;
|
||||||
});
|
});
|
||||||
@ -186,13 +186,13 @@ function calcCartItemWithPromocode(
|
|||||||
const tariff = cartItem.item;
|
const tariff = cartItem.item;
|
||||||
|
|
||||||
discount.target.products.forEach(product => {
|
discount.target.products.forEach(product => {
|
||||||
if (product.productId === tariff.id) { // TODO productId - ид продукта(тариф или пакет тарифов) или привилегии? Если второе, то откуда берется привилегия?
|
if (product.productId === tariff.privilege.privilegeId) {
|
||||||
cartItemTotal.totalPrice *= product.factor;
|
cartItemTotal.totalPrice *= product.factor;
|
||||||
cartItemTotal.envolvedDiscounts.push(discount._id);
|
cartItemTotal.envolvedDiscounts.push(discount._id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
priceByService[tariff.service] = cartItemTotal.totalPrice;
|
priceByService[tariff.privilege.serviceKey] = cartItemTotal.totalPrice;
|
||||||
|
|
||||||
return cartItemTotal;
|
return cartItemTotal;
|
||||||
}
|
}
|
||||||
@ -200,7 +200,7 @@ function calcCartItemWithPromocode(
|
|||||||
export function packTariffs(tariffs: Tariffs.Tariff[], id: string, name: string): Tariffs.Package {
|
export function packTariffs(tariffs: Tariffs.Tariff[], id: string, name: string): Tariffs.Package {
|
||||||
const services: Tariffs.ServiceType[] = [];
|
const services: Tariffs.ServiceType[] = [];
|
||||||
tariffs.forEach(tariff => {
|
tariffs.forEach(tariff => {
|
||||||
if (!services.includes(tariff.service)) services.push(tariff.service);
|
if (!services.includes(tariff.privilege.serviceKey)) services.push(tariff.privilege.serviceKey);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user