-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrecommendations.ts
More file actions
191 lines (164 loc) · 4.67 KB
/
Copy pathrecommendations.ts
File metadata and controls
191 lines (164 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { AppContext } from "../mod.ts";
import { getFilterParam, toRecommendation } from "../utils/transform.ts";
import {
ComplexPageType,
FilterProp,
SmarthintRecommendation,
} from "../utils/typings.ts";
import { getSessionCookie } from "../utils/getSession.ts";
import { Category } from "../../commerce/types.ts";
export interface Props {
/**
* @hide
*/
filter?: FilterProp[];
/**
* @hide
*/
categories?: string;
/**
* @hide
*/
products?: string[];
/**
* @description Your recommendations are divided by positions, defining which position of the recommendations according to your desire. All recommendations configured in the Admin Panel will be returned.
*/
position: string;
/**
* @description Type of page you are setting up.
*/
pagetype: ComplexPageType;
/**
* @default padrao
*/
channel?: string;
}
export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) {
if (productsParam.length) {
return productsParam.map((productId) => `productid:${productId}`).join("&");
}
if (pagetype.type == "product" && pagetype.page) {
return `productId:${(pagetype.page.product.isVariantOf?.productGroupID ??
pagetype.page.product.productID)}`;
}
return;
}
export const buildCategoryParam = ({
segments,
path = "",
categoryList,
names = [],
url,
}: {
segments: string[];
path?: string;
categoryList: Category[];
names?: string[];
url: URL;
}): string | undefined => {
if (segments.length === 0) return names.join(" > ");
const [segment, ...restSegments] = segments;
const newPath = `${path}/${segment}`;
const matchedCategory = categoryList.find((category) => {
const categoryUrl = new URL(category.url, url.origin);
return categoryUrl.pathname === newPath;
});
if (!matchedCategory) return;
return buildCategoryParam({
segments: restSegments,
path: newPath,
categoryList: matchedCategory.children || [],
names: [...names, matchedCategory.name],
url,
});
};
export function getCategoriesParam({
categoriesParam,
categoryTree,
url,
}: {
categoriesParam?: string;
url: URL;
categoryTree?: Category | Category[];
}): string | undefined {
if (categoriesParam) return categoriesParam;
if (!categoryTree) return;
const categoryTreeList = Array.isArray(categoryTree)
? categoryTree
: [categoryTree];
const urlSegments = url.pathname.split("/").filter(Boolean);
return buildCategoryParam({
segments: urlSegments,
categoryList: categoryTreeList,
url,
});
}
/**
* @title SmartHint Integration
* @description Recommendations
*/
const loader = async (
props: Props,
req: Request,
ctx: AppContext,
): Promise<SmarthintRecommendation[] | null> => {
const { recs, shcode, publicUrl, categoryTree } = ctx;
const {
categories: categoriesParam,
filter = [],
position,
products: productsParam = [],
pagetype,
channel = "padrao",
} = props;
const url = new URL(req.url);
const { anonymous } = getSessionCookie(req.headers);
const pageIdentifier = new URL(url.pathname, publicUrl)?.href;
const filters = getFilterParam(url, filter);
const productsString = getProductParam(pagetype, productsParam);
const categories = getCategoriesParam({
categoriesParam,
categoryTree,
url,
});
const data = await recs["GET /recommendationByPage/withProducts"]({
shcode,
anonymous,
categories,
channel,
filter: filters,
pageIdentifier,
pagetype: pagetype.type,
position,
products: productsString,
}).then((r) => r.json());
const positionItem = data.find((item) =>
Number(item.SmartHintPosition) == Number(position)
);
if (!positionItem) return null;
const RecommendationsProducts =
positionItem.RecommendationsProducts?.map((item) =>
toRecommendation(item, position, "RecommendationsProducts", categories)
) ?? [];
const RecommendationsPromotional =
positionItem.RecommendationsProducts?.map((item) =>
toRecommendation(item, position, "RecommendationsPromotional", categories)
) ?? [];
const RecommendationsCombination =
positionItem.RecommendationsProducts?.map((item) =>
toRecommendation(item, position, "RecommendationsCombination", categories)
) ?? [];
const Recommendations =
positionItem.RecommendationsProducts?.map((item) =>
toRecommendation(item, position, "Recommendations", categories)
) ?? [];
const allItems: SmarthintRecommendation[] = [
...RecommendationsProducts,
...RecommendationsPromotional,
...RecommendationsCombination,
...Recommendations,
];
const sortedItem = allItems.toSorted((a, b) => a.order! - b.order!);
return sortedItem;
};
export default loader;