@jsdev_ninja/core 0.14.5 → 0.14.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAoB,MAAM,aAAa,CAAC;AA8BjE,wBAAgB,WAAW,CAAC,EAC3B,IAAI,EACJ,SAAS,EACT,aAAiB,EACjB,iBAAqB,EACrB,oBAA4B,GAC5B,EAAE;IACF,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACrB,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAC/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+FA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAY,MAAM,aAAa,CAAC;AAmCzD,wBAAgB,WAAW,CAAC,EAC3B,IAAI,EACJ,SAAS,EACT,aAAiB,EACjB,iBAAqB,EACrB,oBAA4B,GAC5B,EAAE;IACF,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACrB,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAC/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqFA"}
@@ -1,8 +1,8 @@
1
- import { DiscountEngine } from "../entities/Discount/engine";
1
+ // import { DiscountEngine } from "../entities/Discount/engine";
2
2
  const CONFIG = {
3
3
  VAT: 18,
4
4
  };
5
- function calculateDiscount(product) {
5
+ function calculateDiscount(product, isVatIncludedInPrice) {
6
6
  if (product.discount?.type === "percent") {
7
7
  return (product.price * (product.discount.value ?? 100)) / 100;
8
8
  }
@@ -11,38 +11,41 @@ function calculateDiscount(product) {
11
11
  }
12
12
  return 0;
13
13
  }
14
- function getPriceAfterDiscount(product) {
14
+ function getPriceAfterDiscount(product, isVatIncludedInPrice) {
15
+ const price = isVatIncludedInPrice && product.vat
16
+ ? product.price + (product.price * CONFIG.VAT) / 100
17
+ : product.price;
15
18
  if (product.discount?.type === "percent") {
16
- const dscountAmount = (product.price * product.discount.value) / 100;
19
+ const dscountAmount = (price * product.discount.value) / 100;
17
20
  return product.price - dscountAmount;
18
21
  }
19
22
  if (product.discount?.type === "number") {
20
- const dscountAmount = product.price - product.discount.value;
23
+ const dscountAmount = price - product.discount.value;
21
24
  return dscountAmount;
22
25
  }
23
- return product.price;
26
+ return price;
24
27
  }
25
28
  // main
26
29
  export function getCartCost({ cart, discounts, deliveryPrice = 0, freeDeliveryPrice = 0, isVatIncludedInPrice = false, }) {
27
30
  // Convert cart items to the format expected by the discount engine
28
- const cartForEngine = cart.map((item) => ({
29
- amount: item.amount,
30
- product: {
31
- id: item.product.id,
32
- price: item.product.price,
33
- },
34
- }));
31
+ // const cartForEngine = cart.map((item) => ({
32
+ // amount: item.amount,
33
+ // product: {
34
+ // id: item.product.id,
35
+ // price: item.product.price,
36
+ // },
37
+ // }));
35
38
  // Apply discounts using the new discount engine
36
- const discountResult = DiscountEngine.calculateDiscounts(cartForEngine, discounts);
39
+ // const discountResult = DiscountEngine.calculateDiscounts(cartForEngine, discounts);
37
40
  // Map the results back to the original format with additional product info
38
41
  const result = cart.map((item, index) => {
39
- const engineItem = discountResult.items[index];
42
+ // const engineItem = discountResult.items[index];
40
43
  return {
41
44
  amount: item.amount,
42
45
  product: { ...item.product },
43
46
  originalPrice: item.product.price,
44
- finalPrice: engineItem ? engineItem.finalPrice : getPriceAfterDiscount(item.product),
45
- finalDiscount: engineItem ? engineItem.finalDiscount : calculateDiscount(item.product),
47
+ finalPrice: getPriceAfterDiscount(item.product, isVatIncludedInPrice),
48
+ finalDiscount: calculateDiscount(item.product, isVatIncludedInPrice),
46
49
  };
47
50
  });
48
51
  // isVatIncludedInPrice = false
@@ -77,13 +80,6 @@ export function getCartCost({ cart, discounts, deliveryPrice = 0, freeDeliveryPr
77
80
  acc.discount = Number(acc.discount.toFixed(2));
78
81
  acc.finalCost = Number(acc.finalCost.toFixed(2));
79
82
  acc.productsCost = Number(acc.productsCost.toFixed(2));
80
- // console.log(
81
- // "product",
82
- // product.name[0].value,
83
- // finalPrice,
84
- // amount,
85
- // amount * roundedFinalPrice + (isVatIncludedInPrice ? 0 : productVatValue)
86
- // );
87
83
  return acc;
88
84
  }, {
89
85
  discount: 0,
@@ -99,6 +95,5 @@ export function getCartCost({ cart, discounts, deliveryPrice = 0, freeDeliveryPr
99
95
  else {
100
96
  cartDetails.finalCost += cartDetails.deliveryPrice;
101
97
  }
102
- console.log("cartDetails", cartDetails);
103
98
  return { items: result, ...cartDetails };
104
99
  }
@@ -34,6 +34,9 @@ describe("storeCalculator", () => {
34
34
  test("return 0 when sale price is 0", () => {
35
35
  expect(storeCalculator.calcMarginFromSalePrice(0, 100)).toBe(0);
36
36
  });
37
+ test("return 24.53 when sale price is less than purchase price", () => {
38
+ expect(storeCalculator.calcMarginFromSalePrice(10.6, 8)).toBe(24.53);
39
+ });
37
40
  test("return 0 when purchase price is 0", () => {
38
41
  expect(storeCalculator.calcMarginFromSalePrice(100, 0)).toBe(0);
39
42
  });