router-locations.js
2.79 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
const express = require('express');
const naturalSort = require('node-natural-sort');
const unique = require('array-unique');
const config = require('../config');
const matrix = require('../matrix');
const router = express.Router();
module.exports = router;
function pageIndex(req, res) {
res.json({
method: '/locations',
error: null,
result: config.locations,
});
}
function pageAdd(req, res) {
let locations = req.params.locations || req.query.locations;
if (!locations) {
res.json({
method: '/locations/add',
error: true,
error_msg: 'Usage: /locations/add/<NEW_LOCATION>',
});
return;
}
if (typeof locations === 'string') {
locations = locations
.trim()
.split(/[\s,]+/)
.filter((loc) => typeof loc === 'string')
.map((loc) => loc.trim().toUpperCase())
.filter((loc) => loc && (loc !== 'ALL'));
}
const locationsCount = locations.length;
for (let i = 0; i < locationsCount; i += 1) {
const location = locations[i];
if (!config.locations) config.locations = [];
config.locations.push(location);
}
// config.locations.map((x) => x.toUpperCase());
unique(config.locations);
config.locations.sort(naturalSort());
matrix.config_is_dirty = true;
res.json({
method: '/locations/add',
error: null,
new_location: locations,
locations: config.locations,
});
}
function pageDel(req, res) {
let locations = req.params.locations || req.query.locations;
if (!locations) {
res.json({
method: '/locations/del',
error: true,
error_msg: 'Usage: /locations/del/<LOCATION_TO_DELETE> or /locations/del?locations=<LOCATION_TO_DELETE>',
});
return;
}
if (typeof locations === 'string') {
locations = locations.trim().split(/[\s,]+/);
}
// config.locations.map((x) => x.toUpperCase());
const locationsCount = locations.length;
for (let i = 0; i < locationsCount; i += 1) {
const location = locations[i].toUpperCase();
const idx = config.locations.indexOf(location);
if (idx >= 0) {
matrix.config_is_dirty = true;
config.locations.splice(idx, 1);
}
}
res.json({
method: '/locations/del',
error: null,
locations_to_delete: locations,
locations: config.locations,
});
}
router.get('/', pageIndex);
router.get('/add/:locations', pageAdd);
router.get('/add', pageAdd);
router.get('/del/:locations', pageDel);
router.get('/delete/:locations', pageDel);
router.get('/remove/:locations', pageDel);
router.get('/del', pageDel);
router.get('/delete', pageDel);
router.get('/remove', pageDel);