1 | import produce from 'immer';
|
2 | import { validateProduct } from './validate-product';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export function normalizeServerList(serverList) {
|
10 | var _a, _b, _c, _d;
|
11 | const list = {};
|
12 | if (!(serverList === null || serverList === void 0 ? void 0 : serverList.customerProductList))
|
13 | return list;
|
14 | const items = serverList.customerProductList.items;
|
15 | const newList = Object.assign({}, list);
|
16 | for (const item of items) {
|
17 | if (item.variant) {
|
18 | newList[item.product.articleNumber] = {
|
19 | options: ((_a = newList[item.product.articleNumber]) === null || _a === void 0 ? void 0 : _a.options) || null,
|
20 | variants: produce(((_b = newList[item.product.articleNumber]) === null || _b === void 0 ? void 0 : _b.variants) || {}, draft => {
|
21 | draft[item.variant.articleNumber] = {
|
22 | options: {
|
23 | quantity: item.quantity || 1,
|
24 | description: item.description
|
25 | },
|
26 | parentArticleNumber: item.product.articleNumber
|
27 | };
|
28 | })
|
29 | };
|
30 | }
|
31 | else {
|
32 | newList[item.product.articleNumber] = {
|
33 | variants: ((_c = newList[item.product.articleNumber]) === null || _c === void 0 ? void 0 : _c.variants) || null,
|
34 | options: {
|
35 | quantity: (_d = item.quantity) !== null && _d !== void 0 ? _d : 1,
|
36 | description: item.description
|
37 | }
|
38 | };
|
39 | }
|
40 | }
|
41 | return newList;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | export function getProductDetailForFlattenedProductList(
|
47 | /** May contain base products, variants, or a combination of both. */
|
48 | flattenedList,
|
49 | /** Contains the results of a products query, which returns only base products */
|
50 | productQueryResult) {
|
51 | const { data } = productQueryResult;
|
52 | const productsFromQuery = (data === null || data === void 0 ? void 0 : data.products) || [];
|
53 | return flattenedList
|
54 | .map(listItem => {
|
55 | const baseProductArticleNumber = listItem.parentArticleNumber || listItem.articleNumber;
|
56 |
|
57 | const product = productsFromQuery.find(prod => prod.articleNumber === baseProductArticleNumber);
|
58 | if (!product)
|
59 | return null;
|
60 |
|
61 | const selectedVariant = getVariantFromProductDetails(listItem.articleNumber, product);
|
62 | const productListProduct = Object.assign(Object.assign({}, product), { quantity: listItem.options.quantity, isVariant: !!listItem.parentArticleNumber, variant: selectedVariant });
|
63 | return Object.assign(Object.assign({}, productListProduct), { validation: validateProduct(productListProduct) });
|
64 | })
|
65 | .filter(Boolean);
|
66 | }
|
67 | export function getVariantFromProductDetails(variant, product) {
|
68 | if (!variant)
|
69 | return null;
|
70 | const matchedVariant = product.variants.values.find(value => value.articleNumber === variant);
|
71 | return matchedVariant || null;
|
72 | }
|
73 | export function flattenList(list) {
|
74 | const flattenedList = [];
|
75 | const baseProducts = Object.keys(list);
|
76 | for (const baseArticleNumber of baseProducts) {
|
77 | const variants = list[baseArticleNumber].variants;
|
78 | if (variants) {
|
79 | const variantArticleNumbers = Object.keys(variants);
|
80 | for (const articleNumber of variantArticleNumbers) {
|
81 | flattenedList.push({
|
82 | articleNumber,
|
83 | parentArticleNumber: baseArticleNumber,
|
84 | options: variants[articleNumber].options
|
85 | });
|
86 | }
|
87 | if (list[baseArticleNumber].options) {
|
88 | flattenedList.push({
|
89 | articleNumber: baseArticleNumber,
|
90 | options: list[baseArticleNumber].options
|
91 | });
|
92 | }
|
93 | }
|
94 | else {
|
95 | flattenedList.push({
|
96 | articleNumber: baseArticleNumber,
|
97 | options: list[baseArticleNumber].options
|
98 | });
|
99 | }
|
100 | }
|
101 | return flattenedList;
|
102 | }
|
103 |
|
\ | No newline at end of file |