router-remote-products.js
3 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
const express = require('express');
// const sortObj = require('sort-object');
const naturalCompare = require('string-natural-compare');
const config = require('../config');
const matrix = require('../matrix');
const router = express.Router();
module.exports = router;
const splitProductSeparator = / *[;,]+ */;
function pageIndex(req, res) {
res.json({
method: '/products',
error: null,
result: config.remote_products,
});
}
function pageSet(req, res) {
function responseWithUsageHelp() {
res.json({
method: '/remote-products/set',
error: true,
error_msg: 'Usage: /remote-products/set/<LOCAL_PRODUCT>/<REMOTE_PRODUCT>',
});
}
if (!req.params.localProduct && !req.query.local) {
responseWithUsageHelp();
return;
}
if (!req.params.remoteProduct && !req.query.remote) {
responseWithUsageHelp();
return;
}
const localProduct = (req.params.localProduct || req.query.local).trim().toUpperCase();
const remoteProduct = (req.params.remoteProduct || req.query.remote).trim();
config.remote_products[localProduct] = remoteProduct;
/*
config.remote_products = sortObj(config.remote_products, {
sort: naturalCompare.caseInsensitive
});
*/
const sortedRemoteProducts = {};
const remoteProductsKeys = Object.keys(
config.remote_products,
).sort(naturalCompare.caseInsensitive);
remoteProductsKeys.forEach((item) => {
sortedRemoteProducts[item] = config.remote_products[item];
});
config.remote_products = sortedRemoteProducts;
matrix.config_is_dirty = true;
res.json({
method: '/remote-products/set',
error: null,
local_product: localProduct,
remote_product: remoteProduct,
remote_products: config.remote_products,
});
}
function pageDel(req, res) {
function responseWithUsageHelp() {
res.json({
method: '/remote-products/del',
error: true,
error_msg: 'Usage: /remote-products/del/<LOCAL_PRODUCT>',
});
}
let localProducts = req.params.localProduct || req.query.local;
if (!localProducts) {
responseWithUsageHelp();
return;
}
if (typeof localProducts === 'string') {
localProducts = localProducts.split(splitProductSeparator);
}
const localProductsCount = localProducts.length;
for (let i = 0; i < localProductsCount; i += 1) {
const localProduct = localProducts[i];
if (localProduct) {
delete config.remote_products[localProduct];
matrix.config_is_dirty = true;
}
}
res.json({
method: '/remote-products/del',
error: null,
deleted_product: localProducts,
remote_products: config.remote_products,
});
}
router.get('/', pageIndex);
router.get('/set/:localProduct/:remoteProduct', pageSet);
router.get('/set', pageSet);
router.get('/del/:localProduct', pageDel);
router.get('/del', pageDel);