@q2devel/q2-core 1.0.55 → 1.0.57
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.
- package/dist/api/cart/Cart.service.d.ts +3 -3
- package/dist/api/cart/Cart.service.d.ts.map +1 -1
- package/dist/api/cart/Cart.service.js +70 -52
- package/dist/api/products/getProducts.d.ts +8 -3
- package/dist/api/products/getProducts.d.ts.map +1 -1
- package/dist/api/products/getProducts.js +47 -37
- package/dist/utils/mapProducts.d.ts +1 -1
- package/dist/utils/mapProducts.d.ts.map +1 -1
- package/dist/utils/mapProducts.js +24 -13
- package/package.json +39 -39
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AxiosRequestConfig } from
|
|
2
|
-
import { CoreContext } from
|
|
3
|
-
import { AddToCartRequest, AddToCartResponse, Cart, CartItem, DeleteCartItemRequest,
|
|
1
|
+
import { AxiosRequestConfig } from "axios";
|
|
2
|
+
import { CoreContext } from "../../context";
|
|
3
|
+
import { AddToCartRequest, AddToCartResponse, Cart, CartItem, DeleteCartItemRequest, OrderHistoryResponse, UpdateCartItemRequest } from "./CartService.types";
|
|
4
4
|
export declare const getUserCart: (ctx: CoreContext, config?: AxiosRequestConfig) => Promise<Cart>;
|
|
5
5
|
export declare const getCurrentCart: (ctx: CoreContext, activeToken: string, config?: AxiosRequestConfig) => Promise<Cart>;
|
|
6
6
|
export declare const addToCart: (ctx: CoreContext, activeToken: string | undefined, request: AddToCartRequest, config?: AxiosRequestConfig) => Promise<AddToCartResponse>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Cart.service.d.ts","sourceRoot":"","sources":["../../../api/cart/Cart.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"Cart.service.d.ts","sourceRoot":"","sources":["../../../api/cart/Cart.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA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Q,kBAAuB,KAC9B,OAAO,CAAC,IAAI,CAQd,CAAC;AAEF,eAAO,MAAM,YAAY,GACvB,KAAK,WAAW,EAChB,SAAQ,kBAAuB,KAC9B,OAAO,CAAC,MAAM,CAQhB,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,KAAK,WAAW,EAChB,SAAQ,kBAAuB,KAC9B,OAAO,CAAC,oBAAoB,CAmD9B,CAAC"}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { DrupalJsonApiParams } from
|
|
2
|
-
import { mapIncludedResources } from
|
|
3
|
-
import { mapProduct } from
|
|
1
|
+
import { DrupalJsonApiParams } from "drupal-jsonapi-params";
|
|
2
|
+
import { mapIncludedResources } from "../../utils/mapIncludedResources";
|
|
3
|
+
import { mapProduct } from "../../utils/mapProducts";
|
|
4
4
|
const getHeaders = (ctx, customHeaders = {}) => ({
|
|
5
|
-
|
|
5
|
+
"Content-Type": "application/vnd.api+json",
|
|
6
6
|
...(ctx.accessToken && { Authorization: `Bearer ${ctx.accessToken}` }),
|
|
7
7
|
...customHeaders,
|
|
8
8
|
});
|
|
9
9
|
export const getUserCart = async (ctx, config = {}) => {
|
|
10
10
|
if (!ctx.userId)
|
|
11
|
-
throw new Error(
|
|
11
|
+
throw new Error("Missing userId in context");
|
|
12
12
|
const params = new DrupalJsonApiParams()
|
|
13
|
-
.addFilter(
|
|
14
|
-
.addFilter(
|
|
13
|
+
.addFilter("state", "draft")
|
|
14
|
+
.addFilter("uid.meta", ctx.userId)
|
|
15
15
|
.addInclude([
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
"order_items",
|
|
17
|
+
"order_items.purchased_entity",
|
|
18
|
+
"order_items.purchased_entity.field_image",
|
|
19
|
+
"order_items.purchased_entity.field_image.field_media_image",
|
|
20
|
+
"order_items.purchased_entity.product_id",
|
|
21
21
|
// 'order_items.purchased_entity.product_id.field_image',
|
|
22
22
|
// 'order_items.purchased_entity.product_id.field_image.field_media_image',
|
|
23
23
|
]);
|
|
@@ -25,96 +25,108 @@ export const getUserCart = async (ctx, config = {}) => {
|
|
|
25
25
|
if (!data.data || (Array.isArray(data.data) && data.data.length === 0)) {
|
|
26
26
|
return {
|
|
27
27
|
data: {
|
|
28
|
-
id:
|
|
28
|
+
id: "",
|
|
29
29
|
drupalOrderId: 0,
|
|
30
30
|
cartItems: [],
|
|
31
|
-
total: { number:
|
|
31
|
+
total: { number: "0", currency_code: "CZK", formatted: "0 Kč" },
|
|
32
32
|
},
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
const resolvedOrders = mapIncludedResources(data, 6);
|
|
36
|
-
const orderData = Array.isArray(resolvedOrders)
|
|
36
|
+
const orderData = Array.isArray(resolvedOrders)
|
|
37
|
+
? resolvedOrders[0]
|
|
38
|
+
: resolvedOrders;
|
|
37
39
|
const cartItems = (orderData.order_items || [])
|
|
38
40
|
.map((item) => {
|
|
39
41
|
const defaultProduct = item.purchased_entity?.product_id;
|
|
40
42
|
const variationProduct = item.purchased_entity;
|
|
41
43
|
const variations = defaultProduct?.variations ?? [];
|
|
42
44
|
if (!defaultProduct || !variationProduct) {
|
|
43
|
-
console.warn(
|
|
45
|
+
console.warn("Invalid order item structure", item);
|
|
44
46
|
return null;
|
|
45
47
|
}
|
|
46
48
|
const baseProduct = mapProduct(defaultProduct, variationProduct, ctx);
|
|
47
49
|
return {
|
|
48
50
|
...baseProduct,
|
|
49
|
-
quantity: parseFloat(item.quantity ||
|
|
50
|
-
price: parseFloat(item.unit_price?.number ||
|
|
51
|
+
quantity: parseFloat(item.quantity || "0"),
|
|
52
|
+
price: parseFloat(item.unit_price?.number || "0"), // override price from order item
|
|
51
53
|
drupalOrderItemId: item.drupal_internal__order_item_id || 0,
|
|
52
|
-
order_item_id: item.id ||
|
|
54
|
+
order_item_id: item.id || "",
|
|
53
55
|
};
|
|
54
56
|
})
|
|
55
57
|
.filter(Boolean);
|
|
56
58
|
return {
|
|
57
59
|
data: {
|
|
58
|
-
id: orderData.id ||
|
|
60
|
+
id: orderData.id || "",
|
|
59
61
|
drupalOrderId: orderData.drupal_internal__order_id || 0,
|
|
60
62
|
cartItems,
|
|
61
|
-
total: orderData.total_price || {
|
|
63
|
+
total: orderData.total_price || {
|
|
64
|
+
number: "0",
|
|
65
|
+
currency_code: "CZK",
|
|
66
|
+
formatted: "0 Kč",
|
|
67
|
+
},
|
|
62
68
|
},
|
|
63
69
|
};
|
|
64
70
|
};
|
|
65
71
|
export const getCurrentCart = async (ctx, activeToken, config = {}) => {
|
|
66
72
|
if (!activeToken)
|
|
67
|
-
throw new Error(
|
|
73
|
+
throw new Error("Missing activeToken in context");
|
|
68
74
|
const { data } = await ctx.axios.get(`/jsonapi/carts?cartToken=${activeToken}&include=order_items,order_items.purchased_entity,order_items.purchased_entity.product_id,order_items.purchased_entity.field_image,order_items.purchased_entity.field_image.field_media_image`, { ...config, headers: getHeaders(ctx, config.headers) });
|
|
69
75
|
if (!data.data || data.data.length === 0) {
|
|
70
76
|
return {
|
|
71
77
|
data: {
|
|
72
|
-
id:
|
|
78
|
+
id: "",
|
|
73
79
|
drupalOrderId: 0,
|
|
74
80
|
cartItems: [],
|
|
75
|
-
total: { number:
|
|
81
|
+
total: { number: "0", currency_code: "CZK", formatted: "0 Kč" },
|
|
76
82
|
},
|
|
77
83
|
};
|
|
78
84
|
}
|
|
79
85
|
const resolvedOrders = mapIncludedResources(data, 6);
|
|
80
|
-
const orderData = Array.isArray(resolvedOrders)
|
|
86
|
+
const orderData = Array.isArray(resolvedOrders)
|
|
87
|
+
? resolvedOrders[0]
|
|
88
|
+
: resolvedOrders;
|
|
81
89
|
const cartItems = (orderData.order_items || [])
|
|
82
90
|
.map((item) => {
|
|
83
91
|
const defaultProduct = item.purchased_entity?.product_id;
|
|
84
92
|
const variationProduct = item.purchased_entity;
|
|
85
93
|
const variations = defaultProduct?.variations ?? [];
|
|
86
94
|
if (!defaultProduct || !variationProduct) {
|
|
87
|
-
console.warn(
|
|
95
|
+
console.warn("Invalid order item structure", item);
|
|
88
96
|
return null;
|
|
89
97
|
}
|
|
90
98
|
const baseProduct = mapProduct(defaultProduct, variationProduct, ctx);
|
|
91
99
|
return {
|
|
92
100
|
...baseProduct,
|
|
93
|
-
quantity: parseFloat(item.quantity ||
|
|
94
|
-
price: parseFloat(item.unit_price?.number ||
|
|
101
|
+
quantity: parseFloat(item.quantity || "0"),
|
|
102
|
+
price: parseFloat(item.unit_price?.number || "0"), // override price from order item
|
|
95
103
|
drupalOrderItemId: item.drupal_internal__order_item_id || 0,
|
|
96
|
-
order_item_id: item.id ||
|
|
104
|
+
order_item_id: item.id || "",
|
|
97
105
|
};
|
|
98
106
|
})
|
|
99
107
|
.filter(Boolean);
|
|
100
108
|
return {
|
|
101
109
|
data: {
|
|
102
|
-
id: orderData.id ||
|
|
110
|
+
id: orderData.id || "",
|
|
103
111
|
drupalOrderId: orderData.drupal_internal__order_id || 0,
|
|
104
112
|
cartItems,
|
|
105
|
-
total: orderData.total_price || {
|
|
113
|
+
total: orderData.total_price || {
|
|
114
|
+
number: "0",
|
|
115
|
+
currency_code: "CZK",
|
|
116
|
+
formatted: "0 Kč",
|
|
117
|
+
},
|
|
106
118
|
},
|
|
107
119
|
};
|
|
108
120
|
};
|
|
109
121
|
export const addToCart = async (ctx, activeToken, request, config = {}) => {
|
|
110
122
|
const customHeaders = {};
|
|
111
123
|
if (ctx.accessToken) {
|
|
112
|
-
customHeaders[
|
|
124
|
+
customHeaders["Authorization"] = `Bearer ${ctx.accessToken}`;
|
|
113
125
|
}
|
|
114
126
|
else if (activeToken) {
|
|
115
|
-
customHeaders[
|
|
127
|
+
customHeaders["Commerce-Cart-Token"] = activeToken;
|
|
116
128
|
}
|
|
117
|
-
const { data } = await ctx.axios.post(
|
|
129
|
+
const { data } = await ctx.axios.post("/jsonapi/cart/add", request, {
|
|
118
130
|
...config,
|
|
119
131
|
headers: getHeaders(ctx, { ...config.headers, ...customHeaders }),
|
|
120
132
|
});
|
|
@@ -128,7 +140,7 @@ export const deleteCartItem = async (ctx, orderId, request, config = {}) => {
|
|
|
128
140
|
await ctx.axios.delete(`/jsonapi/carts/${orderId}/items`, {
|
|
129
141
|
...config,
|
|
130
142
|
data: request,
|
|
131
|
-
headers: getHeaders(ctx, { Accept:
|
|
143
|
+
headers: getHeaders(ctx, { Accept: "application/vnd.api+json" }),
|
|
132
144
|
});
|
|
133
145
|
};
|
|
134
146
|
export const clearCart = async (ctx, cartId, config = {}) => {
|
|
@@ -140,7 +152,7 @@ export const clearCart = async (ctx, cartId, config = {}) => {
|
|
|
140
152
|
export const applyCoupon = async (ctx, cartId, couponCode, config = {}) => {
|
|
141
153
|
const { data } = await ctx.axios.post(`/jsonapi/commerce_cart/${cartId}/coupons`, {
|
|
142
154
|
data: {
|
|
143
|
-
type:
|
|
155
|
+
type: "commerce_promotion_coupon--default",
|
|
144
156
|
attributes: { code: couponCode },
|
|
145
157
|
},
|
|
146
158
|
}, { ...config, headers: getHeaders(ctx, config.headers) });
|
|
@@ -154,44 +166,50 @@ export const removeCoupon = async (ctx, cartId, couponId, config = {}) => {
|
|
|
154
166
|
};
|
|
155
167
|
export const getCartToken = async (ctx, config = {}) => {
|
|
156
168
|
if (!ctx.cartToken)
|
|
157
|
-
throw new Error(
|
|
169
|
+
throw new Error("Missing cartToken in context");
|
|
158
170
|
const { data } = await ctx.axios.get(`/cart?_format=json&cartToken=${ctx.cartToken}`, { ...config, headers: getHeaders(ctx, config.headers) });
|
|
159
171
|
return data;
|
|
160
172
|
};
|
|
161
173
|
export const getOrderHistory = async (ctx, config = {}) => {
|
|
162
174
|
if (!ctx.userId)
|
|
163
|
-
throw new Error(
|
|
175
|
+
throw new Error("Missing userId in context");
|
|
164
176
|
const params = new DrupalJsonApiParams()
|
|
165
|
-
.addFilter(
|
|
166
|
-
.addFilter(
|
|
167
|
-
.addInclude([
|
|
177
|
+
.addFilter("state", "completed")
|
|
178
|
+
.addFilter("uid.meta", ctx.userId)
|
|
179
|
+
.addInclude([
|
|
180
|
+
"order_items",
|
|
181
|
+
"order_items.purchased_entity",
|
|
182
|
+
"order_items.purchased_entity.product_id",
|
|
183
|
+
]);
|
|
168
184
|
const { data } = await ctx.axios.get(`/jsonapi/commerce_order/default?${params.getQueryString()}`, { ...config, headers: getHeaders(ctx, config.headers) });
|
|
169
185
|
if (!data.data || (Array.isArray(data.data) && data.data.length === 0)) {
|
|
170
186
|
return { data: [] };
|
|
171
187
|
}
|
|
172
188
|
const resolvedOrders = mapIncludedResources(data);
|
|
173
|
-
const orders = Array.isArray(resolvedOrders)
|
|
174
|
-
|
|
189
|
+
const orders = Array.isArray(resolvedOrders)
|
|
190
|
+
? resolvedOrders
|
|
191
|
+
: [resolvedOrders];
|
|
192
|
+
const processedOrders = orders.map((orderData) => {
|
|
175
193
|
const cartItems = (orderData.order_items || []).map((item) => ({
|
|
176
194
|
id: item.purchased_entity.product_id.drupal_internal__product_id,
|
|
177
195
|
uuid: item.id,
|
|
178
196
|
drupalOrderItemId: item.drupal_internal__order_item_id || 0,
|
|
179
197
|
type: item.type,
|
|
180
|
-
name: item.title ||
|
|
181
|
-
price: parseFloat(item.unit_price?.number ||
|
|
182
|
-
quantity: parseFloat(item.quantity ||
|
|
183
|
-
variationId: item.purchased_entity?.id ||
|
|
198
|
+
name: item.title || "",
|
|
199
|
+
price: parseFloat(item.unit_price?.number || "0"),
|
|
200
|
+
quantity: parseFloat(item.quantity || "0"),
|
|
201
|
+
variationId: item.purchased_entity?.id || "",
|
|
184
202
|
}));
|
|
185
203
|
return {
|
|
186
|
-
id: orderData.id ||
|
|
204
|
+
id: orderData.id || "",
|
|
187
205
|
drupalOrderId: orderData.drupal_internal__order_id || 0,
|
|
188
206
|
cartItems,
|
|
189
207
|
total: orderData.total_price || {
|
|
190
|
-
number:
|
|
191
|
-
currency_code:
|
|
192
|
-
formatted:
|
|
208
|
+
number: "0",
|
|
209
|
+
currency_code: "CZK",
|
|
210
|
+
formatted: "0 Kč",
|
|
193
211
|
},
|
|
194
|
-
completed: orderData.completed ||
|
|
212
|
+
completed: orderData.completed || "",
|
|
195
213
|
};
|
|
196
214
|
});
|
|
197
215
|
return { data: processedOrders };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CoreContext } from
|
|
2
|
-
import { Product } from
|
|
1
|
+
import { CoreContext } from "../../context";
|
|
2
|
+
import { Product } from "../../types/products/Product";
|
|
3
3
|
type GetProductsParams = {
|
|
4
4
|
locale?: string;
|
|
5
5
|
categoryId?: string[];
|
|
@@ -22,8 +22,13 @@ export declare const getProducts: (ctx: CoreContext, { locale, categoryId, produ
|
|
|
22
22
|
type GetProductCountParams = {
|
|
23
23
|
categoryId?: string[];
|
|
24
24
|
tag?: string;
|
|
25
|
+
filters?: Array<{
|
|
26
|
+
path: string;
|
|
27
|
+
value: string | string[] | null;
|
|
28
|
+
operator?: string;
|
|
29
|
+
}>;
|
|
25
30
|
cache?: boolean;
|
|
26
31
|
};
|
|
27
|
-
export declare const getProductCount: (ctx: CoreContext, { categoryId, tag, cache }: GetProductCountParams) => Promise<number>;
|
|
32
|
+
export declare const getProductCount: (ctx: CoreContext, { categoryId, tag, cache, filters, }: GetProductCountParams) => Promise<number>;
|
|
28
33
|
export {};
|
|
29
34
|
//# sourceMappingURL=getProducts.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getProducts.d.ts","sourceRoot":"","sources":["../../../api/products/getProducts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"getProducts.d.ts","sourceRoot":"","sources":["../../../api/products/getProducts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAKvD,KAAK,iBAAiB,GAAG;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAA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2
|
-
import { handleSortChange } from
|
|
3
|
-
import mapIncludedResources from
|
|
4
|
-
import { mapProduct } from
|
|
5
|
-
export const getProducts = async (ctx, { locale, categoryId, productId, searchTerm, limit, offset, filters = [], sort =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { DrupalJsonApiParams } from "drupal-jsonapi-params";
|
|
2
|
+
import { handleSortChange } from "../../utils/generalHelper";
|
|
3
|
+
import mapIncludedResources from "../../utils/mapIncludedResources";
|
|
4
|
+
import { mapProduct } from "../../utils/mapProducts";
|
|
5
|
+
export const getProducts = async (ctx, { locale, categoryId, productId, searchTerm, limit, offset, filters = [], sort = "", tag = "", include = [
|
|
6
|
+
"product_id",
|
|
7
|
+
"product_id.field_category",
|
|
8
|
+
"product_id.field_image",
|
|
9
|
+
"product_id.field_image.field_media_image",
|
|
10
|
+
"field_image.field_media_image",
|
|
11
11
|
], cache = true, mapFnVariations, }) => {
|
|
12
12
|
const currentLocale = locale || ctx.locale;
|
|
13
13
|
const cacheKey = [
|
|
14
|
-
|
|
14
|
+
"products",
|
|
15
15
|
currentLocale,
|
|
16
|
-
categoryId?.length ? `cat:${categoryId.join(
|
|
17
|
-
productId ? `id:${productId}` :
|
|
18
|
-
searchTerm ? `search:${searchTerm}` :
|
|
19
|
-
limit ? `limit:${limit}` :
|
|
20
|
-
offset ? `offset:${offset}` :
|
|
21
|
-
filters.length ? `filters:${JSON.stringify(filters)}` :
|
|
22
|
-
sort ? `sort:${sort}` :
|
|
23
|
-
tag ? `tag:${tag}` :
|
|
16
|
+
categoryId?.length ? `cat:${categoryId.join(",")}` : "",
|
|
17
|
+
productId ? `id:${productId}` : "",
|
|
18
|
+
searchTerm ? `search:${searchTerm}` : "",
|
|
19
|
+
limit ? `limit:${limit}` : "",
|
|
20
|
+
offset ? `offset:${offset}` : "",
|
|
21
|
+
filters.length ? `filters:${JSON.stringify(filters)}` : "",
|
|
22
|
+
sort ? `sort:${sort}` : "",
|
|
23
|
+
tag ? `tag:${tag}` : "",
|
|
24
24
|
]
|
|
25
25
|
.filter(Boolean)
|
|
26
|
-
.join(
|
|
26
|
+
.join(":");
|
|
27
27
|
if (cache && ctx.getCache) {
|
|
28
28
|
const cached = await ctx.getCache(cacheKey);
|
|
29
29
|
if (cached)
|
|
@@ -31,25 +31,30 @@ export const getProducts = async (ctx, { locale, categoryId, productId, searchTe
|
|
|
31
31
|
}
|
|
32
32
|
const params = new DrupalJsonApiParams()
|
|
33
33
|
.addInclude(include)
|
|
34
|
-
.addFilter(
|
|
34
|
+
.addFilter("langcode", currentLocale);
|
|
35
35
|
if (tag)
|
|
36
|
-
params.addFilter(`product_id.${tag}`,
|
|
36
|
+
params.addFilter(`product_id.${tag}`, "1");
|
|
37
37
|
if (categoryId)
|
|
38
|
-
params.addFilter(
|
|
38
|
+
params.addFilter("tid", categoryId, "IN");
|
|
39
39
|
if (productId)
|
|
40
|
-
params.addFilter(
|
|
40
|
+
params.addFilter("variation_id", productId);
|
|
41
41
|
if (searchTerm)
|
|
42
|
-
params.addFilter(
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
params.addFilter("title_1", searchTerm);
|
|
43
|
+
if (filters.length) {
|
|
44
|
+
filters.forEach((filter) => {
|
|
45
|
+
params.addFilter(filter.path, filter.value, filter.operator);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
47
48
|
if (sort) {
|
|
48
49
|
const sortData = handleSortChange(sort);
|
|
49
50
|
if (sortData) {
|
|
50
51
|
params.addSort(sortData.title, sortData.direction);
|
|
51
52
|
}
|
|
52
53
|
}
|
|
54
|
+
if (limit)
|
|
55
|
+
params.addPageLimit(limit);
|
|
56
|
+
if (offset)
|
|
57
|
+
params.addPageOffset(offset);
|
|
53
58
|
const queryString = params.getQueryString();
|
|
54
59
|
const url = currentLocale === ctx.defaultLocale
|
|
55
60
|
? `/jsonapi/index/variation`
|
|
@@ -68,27 +73,32 @@ export const getProducts = async (ctx, { locale, categoryId, productId, searchTe
|
|
|
68
73
|
}
|
|
69
74
|
return mappedProducts;
|
|
70
75
|
};
|
|
71
|
-
export const getProductCount = async (ctx, { categoryId = [], tag =
|
|
76
|
+
export const getProductCount = async (ctx, { categoryId = [], tag = "", cache = true, filters = [], }) => {
|
|
72
77
|
const cacheKey = [
|
|
73
|
-
|
|
78
|
+
"productCount",
|
|
74
79
|
ctx.locale,
|
|
75
|
-
categoryId?.length ? `cat:${categoryId.join(
|
|
76
|
-
tag ? `tag:${tag}` :
|
|
80
|
+
categoryId?.length ? `cat:${categoryId.join(",")}` : "",
|
|
81
|
+
tag ? `tag:${tag}` : "",
|
|
77
82
|
]
|
|
78
83
|
.filter(Boolean)
|
|
79
|
-
.join(
|
|
84
|
+
.join(":");
|
|
80
85
|
// if (cache && ctx.getCache) {
|
|
81
86
|
// const cached = await ctx.getCache<number>(cacheKey);
|
|
82
87
|
// if (cached !== null) return cached;
|
|
83
88
|
// }
|
|
84
89
|
const params = new DrupalJsonApiParams();
|
|
85
90
|
if (categoryId.length > 0) {
|
|
86
|
-
params.addFilter(
|
|
91
|
+
params.addFilter("tid", categoryId, "IN");
|
|
87
92
|
}
|
|
88
93
|
if (tag) {
|
|
89
|
-
params.addFilter(`product_id.${tag}`,
|
|
94
|
+
params.addFilter(`product_id.${tag}`, "1");
|
|
95
|
+
}
|
|
96
|
+
if (filters.length) {
|
|
97
|
+
filters.forEach((filter) => {
|
|
98
|
+
params.addFilter(filter.path, filter.value, filter.operator);
|
|
99
|
+
});
|
|
90
100
|
}
|
|
91
|
-
params.addFilter(
|
|
101
|
+
params.addFilter("langcode", ctx.locale).addPageLimit(1);
|
|
92
102
|
const queryString = params.getQueryString();
|
|
93
103
|
const url = ctx.locale === ctx.defaultLocale
|
|
94
104
|
? `/jsonapi/index/variation`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Product } from
|
|
1
|
+
import { Product } from "../types/products/Product";
|
|
2
2
|
export declare function mapProduct(defaultProduct: any, variationProduct: any, ctx: any): Product;
|
|
3
3
|
export declare function mapProductLite(defaultProduct: any, variationProduct: any, ctx: any): any;
|
|
4
4
|
//# sourceMappingURL=mapProducts.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapProducts.d.ts","sourceRoot":"","sources":["../../utils/mapProducts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAuB,MAAM,2BAA2B,
|
|
1
|
+
{"version":3,"file":"mapProducts.d.ts","sourceRoot":"","sources":["../../utils/mapProducts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAuB,MAAM,2BAA2B,CAAC;AAuJzE,wBAAgB,UAAU,CACxB,cAAc,EAAE,GAAG,EACnB,gBAAgB,EAAE,GAAG,EACrB,GAAG,EAAE,GAAG,GACP,OAAO,CAeT;AAED,wBAAgB,cAAc,CAC5B,cAAc,EAAE,GAAG,EACnB,gBAAgB,EAAE,GAAG,EACrB,GAAG,EAAE,GAAG,GACP,GAAG,CAKL"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getDiscountPercent } from
|
|
1
|
+
import { getDiscountPercent } from "../utils/generalHelper";
|
|
2
2
|
function getImageUrl(img) {
|
|
3
3
|
if (img?.field_media_image?.uri?.url) {
|
|
4
4
|
return img.field_media_image.uri.url;
|
|
@@ -11,11 +11,11 @@ function getImageUrl(img) {
|
|
|
11
11
|
function mapImages(images, ctx) {
|
|
12
12
|
const imagesArray = Array.isArray(images) ? images : images ? [images] : [];
|
|
13
13
|
if (imagesArray.length === 0) {
|
|
14
|
-
return [{ href: ctx.logoUrl ||
|
|
14
|
+
return [{ href: ctx.logoUrl || "/logo.png", alt: "Q2" }];
|
|
15
15
|
}
|
|
16
16
|
const result = imagesArray.map((img) => ({
|
|
17
|
-
href: ctx.makeAbsoluteUrl(getImageUrl(img) || ctx.logoUrl ||
|
|
18
|
-
alt: img.name ??
|
|
17
|
+
href: ctx.makeAbsoluteUrl(getImageUrl(img) || ctx.logoUrl || "/logo.png"),
|
|
18
|
+
alt: img.name ?? "Image",
|
|
19
19
|
}));
|
|
20
20
|
return result;
|
|
21
21
|
}
|
|
@@ -31,21 +31,26 @@ function mapVariations(variations = []) {
|
|
|
31
31
|
discountPercentVariation,
|
|
32
32
|
ean: v.field_ean,
|
|
33
33
|
stock: v?.field_stock_level?.available_stock ?? 0,
|
|
34
|
-
href:
|
|
34
|
+
href: "",
|
|
35
35
|
};
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
function mapProductBase(defaultProduct, variationProduct, ctx) {
|
|
39
39
|
const defaultImages = defaultProduct.field_image;
|
|
40
40
|
const variationImages = variationProduct.field_image;
|
|
41
|
-
const
|
|
41
|
+
const imosImage = variationProduct?.field_image_url || "";
|
|
42
|
+
console.log(defaultImages, variationImages, imosImage);
|
|
43
|
+
const allImages = [imosImage, variationImages, defaultImages]
|
|
44
|
+
.filter(Boolean)
|
|
45
|
+
.flat();
|
|
46
|
+
console.log(allImages);
|
|
42
47
|
const discountPercent = getDiscountPercent(variationProduct.price, variationProduct.list_price);
|
|
43
48
|
const badges = [
|
|
44
49
|
...(variationProduct.field_news
|
|
45
|
-
? [{ text:
|
|
50
|
+
? [{ text: "Novinka", color: "bg-green-100 text-green-800" }]
|
|
46
51
|
: []),
|
|
47
52
|
...(variationProduct.field_aftersale
|
|
48
|
-
? [{ text:
|
|
53
|
+
? [{ text: "Doprodej", color: "bg-red-100 text-red-800" }]
|
|
49
54
|
: []),
|
|
50
55
|
];
|
|
51
56
|
return {
|
|
@@ -55,7 +60,7 @@ function mapProductBase(defaultProduct, variationProduct, ctx) {
|
|
|
55
60
|
variation_name: variationProduct.title,
|
|
56
61
|
sku: variationProduct.sku,
|
|
57
62
|
ean: variationProduct.field_ean,
|
|
58
|
-
description: defaultProduct.field_description?.processed ??
|
|
63
|
+
description: defaultProduct.field_description?.processed ?? "",
|
|
59
64
|
price: variationProduct?.list_price?.number
|
|
60
65
|
? variationProduct.list_price.number
|
|
61
66
|
: variationProduct.price?.number ?? 0,
|
|
@@ -63,7 +68,7 @@ function mapProductBase(defaultProduct, variationProduct, ctx) {
|
|
|
63
68
|
product_id: defaultProduct.drupal_internal__product_id,
|
|
64
69
|
product_name: defaultProduct.title,
|
|
65
70
|
changed: variationProduct.changed,
|
|
66
|
-
category: defaultProduct.field_category ??
|
|
71
|
+
category: defaultProduct.field_category ?? "",
|
|
67
72
|
variation_images: mapImages(variationImages, ctx),
|
|
68
73
|
default_images: mapImages(defaultImages, ctx),
|
|
69
74
|
all_images: mapImages(allImages, ctx),
|
|
@@ -76,13 +81,17 @@ function mapProductBase(defaultProduct, variationProduct, ctx) {
|
|
|
76
81
|
field_features: defaultProduct.field_features ?? [],
|
|
77
82
|
field_returns: defaultProduct.field_returns ?? [],
|
|
78
83
|
field_shipping: defaultProduct.field_shipping ?? [],
|
|
79
|
-
...(variationProduct.packaging
|
|
84
|
+
...(variationProduct.packaging
|
|
85
|
+
? { packaging: variationProduct.packaging }
|
|
86
|
+
: {}),
|
|
80
87
|
...(variationProduct.country ? { country: variationProduct.country } : {}),
|
|
81
88
|
...(variationProduct.weight &&
|
|
82
89
|
variationProduct.weight.number &&
|
|
83
90
|
variationProduct.weight.unit
|
|
84
91
|
? {
|
|
85
|
-
weight: `${Number(variationProduct.weight.number)
|
|
92
|
+
weight: `${Number(variationProduct.weight.number)
|
|
93
|
+
.toFixed(2)
|
|
94
|
+
.toString()} ${variationProduct.weight.unit}`,
|
|
86
95
|
}
|
|
87
96
|
: {}),
|
|
88
97
|
...(variationProduct.weight && variationProduct.weight.unit
|
|
@@ -103,7 +112,9 @@ function mapProductBase(defaultProduct, variationProduct, ctx) {
|
|
|
103
112
|
...(variationProduct.field_species
|
|
104
113
|
? { field_species: variationProduct.field_species }
|
|
105
114
|
: {}),
|
|
106
|
-
...(variationProduct.field_volume
|
|
115
|
+
...(variationProduct.field_volume
|
|
116
|
+
? { field_volume: variationProduct.field_volume }
|
|
117
|
+
: {}),
|
|
107
118
|
...(defaultProduct.field_manufacturer
|
|
108
119
|
? {
|
|
109
120
|
field_manufacturer: {
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@q2devel/q2-core",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"main": "dist/index.js",
|
|
5
|
-
"types": "dist/index.d.ts",
|
|
6
|
-
"exports": {
|
|
7
|
-
".": {
|
|
8
|
-
"import": "./dist/index.js",
|
|
9
|
-
"types": "./dist/index.d.ts"
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
"scripts": {
|
|
13
|
-
"build": "tsc",
|
|
14
|
-
"prepare": "npm run build",
|
|
15
|
-
"prepublishOnly": "npm version patch && npm run build",
|
|
16
|
-
"publish:patch": "npm publish",
|
|
17
|
-
"publish:minor": "npm version minor && npm run build && npm publish",
|
|
18
|
-
"publish:major": "npm version major && npm run build && npm publish"
|
|
19
|
-
},
|
|
20
|
-
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
],
|
|
23
|
-
"license": "MIT",
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@types/js-cookie": "^3.0.6",
|
|
26
|
-
"@types/jwt-decode": "^2.2.1",
|
|
27
|
-
"@types/react": "^19.1.7",
|
|
28
|
-
"@types/react-dom": "^19.1.6",
|
|
29
|
-
"typescript": "^5.8.3"
|
|
30
|
-
},
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"@tanstack/react-query": "^5.80.6",
|
|
33
|
-
"axios": "^1.9.0",
|
|
34
|
-
"drupal-jsonapi-params": "^2.3.2",
|
|
35
|
-
"js-cookie": "^3.0.5",
|
|
36
|
-
"jwt-decode": "^4.0.0",
|
|
37
|
-
"react": "^19.1.0"
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@q2devel/q2-core",
|
|
3
|
+
"version": "1.0.57",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"prepare": "npm run build",
|
|
15
|
+
"prepublishOnly": "npm version patch && npm run build",
|
|
16
|
+
"publish:patch": "npm publish",
|
|
17
|
+
"publish:minor": "npm version minor && npm run build && npm publish",
|
|
18
|
+
"publish:major": "npm version major && npm run build && npm publish"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/js-cookie": "^3.0.6",
|
|
26
|
+
"@types/jwt-decode": "^2.2.1",
|
|
27
|
+
"@types/react": "^19.1.7",
|
|
28
|
+
"@types/react-dom": "^19.1.6",
|
|
29
|
+
"typescript": "^5.8.3"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@tanstack/react-query": "^5.80.6",
|
|
33
|
+
"axios": "^1.9.0",
|
|
34
|
+
"drupal-jsonapi-params": "^2.3.2",
|
|
35
|
+
"js-cookie": "^3.0.5",
|
|
36
|
+
"jwt-decode": "^4.0.0",
|
|
37
|
+
"react": "^19.1.0"
|
|
38
|
+
}
|
|
39
|
+
}
|