import { CartData, Discount, Tariff } from "@frontend/kitui"; import { calcCart } from "./calcCart"; describe("Cart calculations", () => { describe("without discounts", () => { it("calculates cart with 1 item", () => { const cart = calcCart([templategenTariff1], [], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("calculates cart with 3 items", () => { const cart = calcCart([templategenTariff1, squizTariff, reducerTariff], [], 0); const expectedCart: CartData = { itemCount: 3, priceAfterDiscounts: 100 * 100 + 200 * 200 + 300 * 300, priceBeforeDiscounts: 100 * 100 + 200 * 200 + 300 * 300, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, { serviceKey: "squiz", price: 200 * 200, privileges: [ { description: "d2", price: 200 * 200, privilegeId: "p2", serviceKey: "squiz", tariffId: "t2", appliedPrivilegeDiscount: null, tariffName: "squizTariff", }, ], appliedServiceDiscount: null, }, { serviceKey: "reducer", price: 300 * 300, privileges: [ { description: "d3", price: 300 * 300, privilegeId: "p3", serviceKey: "reducer", tariffId: "t3", appliedPrivilegeDiscount: null, tariffName: "reducerTariff", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("calculates cart with items for the same service", () => { const cart = calcCart([templategenTariff1, templategenTariff2, reducerTariff], [], 0); const expectedCart: CartData = { itemCount: 3, priceAfterDiscounts: 100 * 100 + 600 * 600 + 300 * 300, priceBeforeDiscounts: 100 * 100 + 600 * 600 + 300 * 300, services: [ { serviceKey: "templategen", price: 100 * 100 + 600 * 600, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, { description: "d5", price: 600 * 600, privilegeId: "p5", serviceKey: "templategen", tariffId: "t5", appliedPrivilegeDiscount: null, tariffName: "templategenTariff2", }, ], appliedServiceDiscount: null, }, { serviceKey: "reducer", price: 300 * 300, privileges: [ { description: "d3", price: 300 * 300, privilegeId: "p3", serviceKey: "reducer", tariffId: "t3", appliedPrivilegeDiscount: null, tariffName: "reducerTariff", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("returns blank cart when no tariffs", () => { const cart = calcCart([], [], 0); const expectedCart: CartData = { itemCount: 0, priceAfterDiscounts: 0, priceBeforeDiscounts: 0, services: [], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); }); describe("with single discount", () => { it("applies privilege discount to tariff 1", () => { const cart = calcCart([templategenTariff1], [templategenP1PrivilegeDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100 * 0.9, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100 * 0.9, privileges: [ { description: "d1", price: 100 * 100 * 0.9, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies privilege discount to tariff 2", () => { const cart = calcCart([reducerTariff], [reducerPrivilegeDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 300 * 300 * 0.95, priceBeforeDiscounts: 300 * 300, services: [ { serviceKey: "reducer", price: 300 * 300 * 0.95, privileges: [ { description: "d3", price: 300 * 300 * 0.95, privilegeId: "p3", serviceKey: "reducer", tariffId: "t3", appliedPrivilegeDiscount: reducerPrivilegeDiscount, tariffName: "reducerTariff", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [reducerPrivilegeDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies service discount to tariffs", () => { const cart = calcCart([templategenTariff1, templategenTariff2], [templategenServiceDiscount], 0); const expectedCart: CartData = { itemCount: 2, priceAfterDiscounts: (100 * 100 + 600 * 600) * 0.8, priceBeforeDiscounts: 100 * 100 + 600 * 600, services: [ { serviceKey: "templategen", price: (100 * 100 + 600 * 600) * 0.8, privileges: [ { description: "d1", price: 100 * 100 * 0.8, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, { description: "d5", price: 600 * 600 * 0.8, privilegeId: "p5", serviceKey: "templategen", tariffId: "t5", appliedPrivilegeDiscount: null, tariffName: "templategenTariff2", }, ], appliedServiceDiscount: templategenServiceDiscount, }, ], allAppliedDiscounts: [templategenServiceDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies privilege discount to 2 tariffs", () => { const cart = calcCart([templategenTariff1, templategenTariff2], [templategenP1PrivilegeDiscount], 0); const expectedCart: CartData = { itemCount: 2, priceAfterDiscounts: 100 * 100 * 0.9 + 600 * 600, priceBeforeDiscounts: 100 * 100 + 600 * 600, services: [ { serviceKey: "templategen", price: 100 * 100 * 0.9 + 600 * 600, privileges: [ { description: "d1", price: 100 * 100 * 0.9, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, { description: "d5", price: 600 * 600, privilegeId: "p5", serviceKey: "templategen", tariffId: "t5", appliedPrivilegeDiscount: null, tariffName: "templategenTariff2", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies cart purchases discount to tariff", () => { const cart = calcCart([templategenTariff1], [cartPurchasesDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100 * 0.7, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [cartPurchasesDiscount], appliedCartPurchasesDiscount: cartPurchasesDiscount, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("doesn't apply cart purchases discount when cartPurchasesAmount is not enough", () => { const cart = calcCart([templategenTariff1], [highAmountCartPurchasesDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies loyalty discount to tariff", () => { const cart = calcCart([templategenTariff1], [loyaltyDiscount], 1001); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100 * 0.6, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [loyaltyDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: loyaltyDiscount, }; expect(cart).toStrictEqual(expectedCart); }); it("doesn't apply loyalty discount when purchasesAmount is not enough", () => { const cart = calcCart([templategenTariff1], [loyaltyDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100, privileges: [ { description: "d1", price: 100 * 100, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: null, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); }); describe("with multiple discounts", () => { it("applies privilege and service discounts to tariff", () => { const cart = calcCart([templategenTariff1], [templategenP1PrivilegeDiscount, templategenServiceDiscount], 0); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100 * 0.9 * 0.8, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100 * 0.9 * 0.8, privileges: [ { description: "d1", price: 100 * 100 * 0.9 * 0.8, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: templategenServiceDiscount, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount, templategenServiceDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies all types of discounts to tariff", () => { const cart = calcCart( [templategenTariff1], [templategenP1PrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount], 1001 ); const expectedCart: CartData = { itemCount: 1, priceAfterDiscounts: 100 * 100 * 0.9 * 0.8 * 0.7 * 0.6, priceBeforeDiscounts: 100 * 100, services: [ { serviceKey: "templategen", price: 100 * 100 * 0.9 * 0.8, privileges: [ { description: "d1", price: 100 * 100 * 0.9 * 0.8, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: templategenServiceDiscount, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount], appliedCartPurchasesDiscount: cartPurchasesDiscount, appliedLoyaltyDiscount: loyaltyDiscount, }; expect(cart).toStrictEqual(expectedCart); }); it("applies different discounts to different tariffs", () => { const cart = calcCart( [templategenTariff1, reducerTariff], [templategenP1PrivilegeDiscount, reducerPrivilegeDiscount], 1001 ); const expectedCart: CartData = { itemCount: 2, priceAfterDiscounts: 100 * 100 * 0.9 + 300 * 300 * 0.95, priceBeforeDiscounts: 100 * 100 + 300 * 300, services: [ { serviceKey: "templategen", price: 100 * 100 * 0.9, privileges: [ { description: "d1", price: 100 * 100 * 0.9, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, ], appliedServiceDiscount: null, }, { serviceKey: "reducer", price: 300 * 300 * 0.95, privileges: [ { description: "d3", price: 300 * 300 * 0.95, privilegeId: "p3", serviceKey: "reducer", tariffId: "t3", appliedPrivilegeDiscount: reducerPrivilegeDiscount, tariffName: "reducerTariff", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount, reducerPrivilegeDiscount], appliedCartPurchasesDiscount: null, appliedLoyaltyDiscount: null, }; expect(cart).toStrictEqual(expectedCart); }); it("applies all types of discounts to multiple tariffs", () => { const cart = calcCart( [templategenTariff1, templategenTariff2, squizTariff, reducerTariff], [templategenP1PrivilegeDiscount, reducerPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount], 1001 ); const expectedCart: CartData = { itemCount: 4, priceAfterDiscounts: ((100 * 100 * 0.9 + 600 * 600) * 0.8 + 200 * 200 + 300 * 300 * 0.95) * 0.7 * 0.6, priceBeforeDiscounts: 100 * 100 + 600 * 600 + 200 * 200 + 300 * 300, services: [ { serviceKey: "templategen", price: (100 * 100 * 0.9 + 600 * 600) * 0.8, privileges: [ { description: "d1", price: 100 * 100 * 0.9 * 0.8, privilegeId: "p1", serviceKey: "templategen", tariffId: "t1", appliedPrivilegeDiscount: templategenP1PrivilegeDiscount, tariffName: "templategenTariff1", }, { description: "d5", price: 600 * 600 * 0.8, privilegeId: "p5", serviceKey: "templategen", tariffId: "t5", appliedPrivilegeDiscount: null, tariffName: "templategenTariff2", }, ], appliedServiceDiscount: templategenServiceDiscount, }, { serviceKey: "squiz", price: 200 * 200, privileges: [ { description: "d2", price: 200 * 200, privilegeId: "p2", serviceKey: "squiz", tariffId: "t2", appliedPrivilegeDiscount: null, tariffName: "squizTariff", }, ], appliedServiceDiscount: null, }, { serviceKey: "reducer", price: 300 * 300 * 0.95, privileges: [ { description: "d3", price: 300 * 300 * 0.95, privilegeId: "p3", serviceKey: "reducer", tariffId: "t3", appliedPrivilegeDiscount: reducerPrivilegeDiscount, tariffName: "reducerTariff", }, ], appliedServiceDiscount: null, }, ], allAppliedDiscounts: [templategenP1PrivilegeDiscount, reducerPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount], appliedCartPurchasesDiscount: cartPurchasesDiscount, appliedLoyaltyDiscount: loyaltyDiscount, }; expect(cart).toStrictEqual(expectedCart); }); }); }); const templategenTariff1: Tariff = { _id: "t1", name: "templategenTariff1", price: 0, isCustom: false, privilegies: [ { name: "n1", privilegeId: "p1", serviceKey: "templategen", description: "d1", type: "count", value: "МБ", price: 100, amount: 100, }, ], isDeleted: false, createdAt: "", updatedAt: "" }; const templategenTariff2: Tariff = { _id: "t5", name: "templategenTariff2", price: 0, isCustom: false, privilegies: [ { name: "n5", privilegeId: "p5", serviceKey: "templategen", description: "d5", type: "count", value: "МБ", price: 600, amount: 600, }, ], isDeleted: false, createdAt: "", updatedAt: "" }; const squizTariff: Tariff = { _id: "t2", name: "squizTariff", price: 0, isCustom: false, privilegies: [ { name: "n2", privilegeId: "p2", serviceKey: "squiz", description: "d2", type: "count", value: "МБ", price: 200, amount: 200, }, ], isDeleted: false, createdAt: "", updatedAt: "" }; const reducerTariff: Tariff = { _id: "t3", name: "reducerTariff", price: 0, isCustom: false, privilegies: [ { name: "n3", privilegeId: "p3", serviceKey: "reducer", description: "d3", type: "count", value: "МБ", price: 300, amount: 300, }, ], isDeleted: false, createdAt: "", updatedAt: "" }; const templategenP1PrivilegeDiscount: Discount = { ID: "id1", Name: "n1", Layer: 1, Description: "d1", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 0, CartPurchasesAmount: 0, Product: "p1", Term: "1000", Usage: "0", PriceFrom: 0, Group: "templategen" }, Target: { Products: [ { ID: "p1", Factor: 0.9, Overhelm: false } ], Factor: 0.9, TargetScope: "Sum", TargetGroup: "templategen", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false }; const templategenServiceDiscount: Discount = { ID: "id2", Name: "n2", Layer: 2, Description: "d2", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 0, CartPurchasesAmount: 0, Product: "", Term: "1000", Usage: "0", PriceFrom: 0, Group: "templategen" }, Target: { Products: [ { ID: "", Factor: 0.8, Overhelm: false } ], Factor: 0.8, TargetScope: "Sum", TargetGroup: "templategen", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false }; const reducerPrivilegeDiscount: Discount = { ID: "id11", Name: "n11", Layer: 1, Description: "d11", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 0, CartPurchasesAmount: 0, Product: "p3", Term: "1000", Usage: "0", PriceFrom: 0, Group: "reducer" }, Target: { Products: [ { ID: "p1", Factor: 0.95, Overhelm: false } ], Factor: 0.95, TargetScope: "Sum", TargetGroup: "reducer", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false }; const cartPurchasesDiscount: Discount = { ID: "id3", Name: "n3", Layer: 3, Description: "d3", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 0, CartPurchasesAmount: 1000, Product: "", Term: "0", Usage: "0", PriceFrom: 0, Group: "templategen" }, Target: { Products: [ { ID: "p1", Factor: 0.7, Overhelm: false } ], Factor: 0.7, TargetScope: "Sum", TargetGroup: "templategen", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false }; const highAmountCartPurchasesDiscount: Discount = { ID: "id3", Name: "n3", Layer: 3, Description: "d3", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 0, CartPurchasesAmount: 1000000, Product: "", Term: "0", Usage: "0", PriceFrom: 0, Group: "templategen" }, Target: { Products: [ { ID: "p1", Factor: 0.7, Overhelm: false } ], Factor: 0.7, TargetScope: "Sum", TargetGroup: "templategen", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false }; const loyaltyDiscount: Discount = { ID: "id3", Name: "n3", Layer: 4, Description: "d3", Condition: { Period: { From: "", To: "" }, User: "", UserType: "", Coupon: "", PurchasesAmount: 1000, CartPurchasesAmount: 0, Product: "", Term: "0", Usage: "0", PriceFrom: 0, Group: "templategen" }, Target: { Products: [ { ID: "p1", Factor: 0.6, Overhelm: false } ], Factor: 0.6, TargetScope: "Sum", TargetGroup: "templategen", Overhelm: false }, Audit: { UpdatedAt: "", CreatedAt: "", Deleted: false }, Deprecated: false };