-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
87 lines (83 loc) · 2.87 KB
/
helpers.js
File metadata and controls
87 lines (83 loc) · 2.87 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
export const sortObjectByEtalonArray = (sortingObject, etalonArray, key) => {
let result = sortingObject
.map(el => {
return el;
})
.sort((firstElement, secondElement) => {
return etalonArray.indexOf(firstElement[key]) - etalonArray.indexOf(secondElement[key]);
});
return result;
};
import models from '../../database/models/index';
export const getNotFoundServiceData = (result, user, body, type) => {
let setItcherValue = new Set();
let setIsbnValue = new Set();
let setImdbValue = new Set();
const map = new Map();
const unicResult = [];
for (const rawItem of result) {
let item = standardizationOfItem(rawItem, type);
if (!map.has(item.itcher_id)) {
map.set(item.itcher_id, true);
setItcherValue.add(item.itcher_id);
if (item.type !== 'other' && item.product_attribute_type_name === 'isbn') {
if (type === 'addProducts') {
models.Product.findOrCreate({
where: {
isbn_id: item.product_attribute_value,
itcher_id: item.itcher_id,
partnerId: user.id,
categoryId: item.category_id,
},
});
}
setIsbnValue.add(item.product_attribute_value);
} else if (item.type !== 'other' && item.product_attribute_type_name === 'imdb_id') {
if (type === 'addProducts') {
models.Product.findOrCreate({
where: {
imdb_id: item.product_attribute_value,
itcher_id: item.itcher_id,
partnerId: user.id,
categoryId: item.category_id,
},
});
}
setImdbValue.add(item.product_attribute_value);
} else {
if (type === 'addProducts') {
models.Product.findOrCreate({
where: {
itcher_id: item.itcher_id,
partnerId: user.id,
categoryId: item.category_id,
},
});
}
}
unicResult.push({
id: item.itcher_id,
type: item.type,
product_attribute_value: item.product_attribute_value,
categoryId: item.category_id,
});
}
}
let resultItcher = new Set([...new Set(body.itcher_product_ids)].filter(x => !setItcherValue.has(x)));
let resultIsbns = new Set([...new Set(body.isbns)].filter(x => !setIsbnValue.has(x)));
let resultImdb = new Set([...new Set(body.imdb_ids)].filter(x => !setImdbValue.has(x)));
return { resultItcher, resultIsbns, resultImdb, unicResult };
};
export const standardizationOfItem = (item, type) => {
if (type === 'removeProducts') {
if (item.isbn_id) {
item.product_attribute_type_name = 'isbn';
item.product_attribute_value = item.isbn_id;
} else if (item.imdb_id) {
item.product_attribute_type_name = 'imdb_id';
item.product_attribute_value = item.imdb_id;
}
item.category_id = item.categoryId;
}
return item;
};