@jsdev_ninja/core 0.11.9 → 0.12.1

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.
@@ -31,16 +31,7 @@ export declare const DiscountSchema: z.ZodObject<{
31
31
  requiredQuantity: number;
32
32
  discountPrice: number;
33
33
  }>]>;
34
- image: z.ZodOptional<z.ZodObject<{
35
- url: z.ZodString;
36
- id: z.ZodString;
37
- }, "strip", z.ZodTypeAny, {
38
- id: string;
39
- url: string;
40
- }, {
41
- id: string;
42
- url: string;
43
- }>>;
34
+ images: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
44
35
  }, "strip", z.ZodTypeAny, {
45
36
  type: "Discount";
46
37
  id: string;
@@ -57,10 +48,7 @@ export declare const DiscountSchema: z.ZodObject<{
57
48
  requiredQuantity: number;
58
49
  discountPrice: number;
59
50
  };
60
- image?: {
61
- id: string;
62
- url: string;
63
- } | undefined;
51
+ images?: string[] | undefined;
64
52
  }, {
65
53
  type: "Discount";
66
54
  id: string;
@@ -77,10 +65,7 @@ export declare const DiscountSchema: z.ZodObject<{
77
65
  requiredQuantity: number;
78
66
  discountPrice: number;
79
67
  };
80
- image?: {
81
- id: string;
82
- url: string;
83
- } | undefined;
68
+ images?: string[] | undefined;
84
69
  }>;
85
70
  export type TDiscount = z.infer<typeof DiscountSchema>;
86
71
  //# sourceMappingURL=Discount.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Discount.d.ts","sourceRoot":"","sources":["../../lib/entities/Discount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgBzB,CAAC;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC"}
1
+ {"version":3,"file":"Discount.d.ts","sourceRoot":"","sources":["../../lib/entities/Discount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgBzB,CAAC;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC"}
@@ -16,5 +16,5 @@ export const DiscountSchema = z.object({
16
16
  discountPrice: z.number().positive(),
17
17
  }),
18
18
  ]),
19
- image: z.object({ url: z.string().url(), id: z.string() }).optional(),
19
+ images: z.array(z.string().nonempty()).optional(),
20
20
  });
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAY,MAAM,EAAE,MAAM,aAAa,CAAC;AA6BjE,wBAAgB,WAAW,CAAC,EAC3B,IAAI,EACJ,SAAS,EACT,KAAK,GACL,EAAE;IACF,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACrB,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;CACd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmJA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAY,MAAM,EAAE,MAAM,aAAa,CAAC;AA6BjE,wBAAgB,WAAW,CAAC,EAC3B,IAAI,EACJ,SAAS,EACT,KAAK,GACL,EAAE;IACF,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACrB,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;CACd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmIA"}
@@ -6,7 +6,7 @@ function calculateDiscount(product) {
6
6
  return (product.price * (product.discount.value ?? 100)) / 100;
7
7
  }
8
8
  if (product.discount?.type === "number") {
9
- return product.price - (product.discount.value ?? 0);
9
+ return product.discount.value ?? 0;
10
10
  }
11
11
  return 0;
12
12
  }
@@ -29,8 +29,8 @@ export function getCartCost({ cart, discounts, store, }) {
29
29
  amount: item.amount,
30
30
  product: { ...item.product },
31
31
  originalPrice: item.product.price,
32
- finalPrice: item.product.price,
33
- finalDiscount: 0,
32
+ finalPrice: getPriceAfterDiscount(item.product),
33
+ finalDiscount: calculateDiscount(item.product),
34
34
  };
35
35
  });
36
36
  const activeDiscounts = discounts.filter((discount) => {
@@ -71,10 +71,7 @@ export function getCartCost({ cart, discounts, store, }) {
71
71
  const totalDiscount = (price * discount.variant.requiredQuantity - discount.variant.discountPrice) * times;
72
72
  const originalPrice = productsTotal * price;
73
73
  const discountPriceFinal = originalPrice - totalDiscount;
74
- console.log("totalDiscount", totalDiscount);
75
- console.log("discountPriceFinal", discountPriceFinal, originalPrice);
76
74
  const averagePrice = Number((discountPriceFinal / productsTotal).toFixed(2));
77
- console.log("averagePrice", averagePrice);
78
75
  result = result.map((item) => {
79
76
  if (discount.variant.productsId.includes(item.product.id)) {
80
77
  return {
@@ -82,36 +79,32 @@ export function getCartCost({ cart, discounts, store, }) {
82
79
  finalPrice: averagePrice,
83
80
  originalPrice: item.product.price,
84
81
  discountPrice: price,
85
- finalDiscount: item.product.price - averagePrice,
82
+ finalDiscount: item.finalDiscount + (item.product.price - averagePrice),
86
83
  };
87
84
  }
88
85
  return item;
89
86
  });
90
- console.log("averagePrice", averagePrice);
91
- console.log("yes", times, discount.variant.requiredQuantity, discount.variant.discountPrice);
92
- console.log("dis", productsTotal, products);
93
87
  // find average price
94
88
  }
95
89
  });
96
- console.log("result", result);
97
90
  const cartDetails = result.reduce((acc, item) => {
98
91
  const { product, amount, finalPrice, finalDiscount } = item;
99
- console.log("isVatIncludedInPrice", isVatIncludedInPrice);
100
92
  let productVatValue = 0;
101
93
  if (product.vat) {
94
+ let vat = 0;
102
95
  if (isVatIncludedInPrice) {
103
96
  const vat_amount = finalPrice * (CONFIG.VAT / (100 + CONFIG.VAT));
104
97
  productVatValue = Number(vat_amount.toFixed(2));
105
98
  productVatValue = productVatValue * amount;
106
- acc.vat += Number(productVatValue.toFixed(2));
99
+ vat = Number(productVatValue.toFixed(2));
107
100
  }
108
101
  else {
109
102
  productVatValue = (finalPrice * CONFIG.VAT) / 100;
110
103
  productVatValue = productVatValue * amount;
111
- acc.vat += Number(productVatValue.toFixed(2));
104
+ vat = Number(productVatValue.toFixed(2));
112
105
  }
106
+ acc.vat = Number((acc.vat + vat).toFixed(2));
113
107
  }
114
- console.log("finalDiscount", finalDiscount);
115
108
  acc.cost += amount * finalPrice;
116
109
  acc.discount += finalDiscount ? amount * finalDiscount : finalDiscount;
117
110
  acc.finalCost += amount * finalPrice + (isVatIncludedInPrice ? 0 : productVatValue);
@@ -18,6 +18,6 @@ export const DiscountSchema = z.object({
18
18
  discountPrice: z.number().positive(),
19
19
  }),
20
20
  ]),
21
- image: z.object({ url: z.string().url(), id: z.string() }).optional(),
21
+ images: z.array(z.string().nonempty()).optional(),
22
22
  });
23
23
  export type TDiscount = z.infer<typeof DiscountSchema>;
@@ -9,7 +9,7 @@ function calculateDiscount(product: TProduct) {
9
9
  return (product.price * (product.discount.value ?? 100)) / 100;
10
10
  }
11
11
  if (product.discount?.type === "number") {
12
- return product.price - (product.discount.value ?? 0);
12
+ return product.discount.value ?? 0;
13
13
  }
14
14
  return 0;
15
15
  }
@@ -42,8 +42,8 @@ export function getCartCost({
42
42
  amount: item.amount,
43
43
  product: { ...item.product },
44
44
  originalPrice: item.product.price,
45
- finalPrice: item.product.price,
46
- finalDiscount: 0,
45
+ finalPrice: getPriceAfterDiscount(item.product),
46
+ finalDiscount: calculateDiscount(item.product),
47
47
  };
48
48
  });
49
49
 
@@ -98,12 +98,7 @@ export function getCartCost({
98
98
  const originalPrice = productsTotal * price;
99
99
  const discountPriceFinal = originalPrice - totalDiscount;
100
100
 
101
- console.log("totalDiscount", totalDiscount);
102
-
103
- console.log("discountPriceFinal", discountPriceFinal, originalPrice);
104
-
105
101
  const averagePrice = Number((discountPriceFinal / productsTotal).toFixed(2));
106
- console.log("averagePrice", averagePrice);
107
102
 
108
103
  result = result.map((item) => {
109
104
  if (discount.variant.productsId.includes(item.product.id)) {
@@ -112,49 +107,38 @@ export function getCartCost({
112
107
  finalPrice: averagePrice,
113
108
  originalPrice: item.product.price,
114
109
  discountPrice: price,
115
- finalDiscount: item.product.price - averagePrice,
110
+ finalDiscount: item.finalDiscount + (item.product.price - averagePrice),
116
111
  };
117
112
  }
118
113
  return item;
119
114
  });
120
115
 
121
- console.log("averagePrice", averagePrice);
122
-
123
- console.log(
124
- "yes",
125
- times,
126
- discount.variant.requiredQuantity,
127
- discount.variant.discountPrice
128
- );
129
-
130
- console.log("dis", productsTotal, products);
131
-
132
116
  // find average price
133
117
  }
134
118
  });
135
119
 
136
- console.log("result", result);
137
-
138
120
  const cartDetails = result.reduce(
139
121
  (acc, item) => {
140
122
  const { product, amount, finalPrice, finalDiscount } = item;
141
- console.log("isVatIncludedInPrice", isVatIncludedInPrice);
142
123
 
143
124
  let productVatValue: number = 0;
144
125
  if (product.vat) {
126
+ let vat = 0;
127
+
145
128
  if (isVatIncludedInPrice) {
146
129
  const vat_amount = finalPrice * (CONFIG.VAT / (100 + CONFIG.VAT));
147
130
  productVatValue = Number(vat_amount.toFixed(2));
148
131
  productVatValue = productVatValue * amount;
149
132
 
150
- acc.vat += Number(productVatValue.toFixed(2));
133
+ vat = Number(productVatValue.toFixed(2));
151
134
  } else {
152
135
  productVatValue = (finalPrice * CONFIG.VAT) / 100;
153
136
  productVatValue = productVatValue * amount;
154
- acc.vat += Number(productVatValue.toFixed(2));
137
+ vat = Number(productVatValue.toFixed(2));
155
138
  }
139
+
140
+ acc.vat = Number((acc.vat + vat).toFixed(2));
156
141
  }
157
- console.log("finalDiscount", finalDiscount);
158
142
 
159
143
  acc.cost += amount * finalPrice;
160
144
  acc.discount += finalDiscount ? amount * finalDiscount : finalDiscount;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsdev_ninja/core",
3
- "version": "0.11.9",
3
+ "version": "0.12.1",
4
4
  "main": "dist/core.cjs.js",
5
5
  "module": "dist/core.es.js",
6
6
  "types": "dist/index.d.ts",