Commit d5dfe4ff42710b10cab6e93308a36e028a3fa455
1 parent
1de2955e1c
Exists in
master
Fix eslint on api-server/router-locations
Showing 1 changed file with 23 additions and 21 deletions Inline Diff
api-server/router-locations.js
1 | "use strict"; | ||
2 | |||
3 | const express = require('express'); | 1 | const express = require('express'); |
4 | const naturalSort = require('node-natural-sort'); | 2 | const naturalSort = require('node-natural-sort'); |
5 | const unique = require('array-unique'); | 3 | const unique = require('array-unique'); |
6 | 4 | ||
7 | const config = require('../config'); | 5 | const config = require('../config'); |
8 | const matrix = require('../matrix'); | 6 | const matrix = require('../matrix'); |
9 | 7 | ||
10 | const router = express.Router(); | 8 | const router = express.Router(); |
11 | module.exports = router; | 9 | module.exports = router; |
12 | 10 | ||
13 | function pageIndex(req, res) { | 11 | function pageIndex(req, res) { |
14 | res.json({ | 12 | res.json({ |
15 | method: '/locations', | 13 | method: '/locations', |
16 | error: null, | 14 | error: null, |
17 | result: config.locations | 15 | result: config.locations, |
18 | }); | 16 | }); |
19 | } | 17 | } |
20 | 18 | ||
21 | function pageAdd(req, res) { | 19 | function pageAdd(req, res) { |
22 | let locations = req.params.locations || req.query.locations | 20 | let locations = req.params.locations || req.query.locations; |
23 | 21 | ||
24 | if (!locations) { | 22 | if (!locations) { |
25 | res.json({ | 23 | res.json({ |
26 | method: '/locations/add', | 24 | method: '/locations/add', |
27 | error: true, | 25 | error: true, |
28 | error_msg: 'Usage: /locations/add/<NEW_LOCATION>' | 26 | error_msg: 'Usage: /locations/add/<NEW_LOCATION>', |
29 | }); | 27 | }); |
30 | 28 | ||
31 | return; | 29 | return; |
32 | } | 30 | } |
33 | 31 | ||
34 | if (typeof locations === 'string') { | 32 | if (typeof locations === 'string') { |
35 | locations = locations.trim().split(/[\s,]+/).filter((el) => { return el.toUpperCase() !== 'ALL'; }); | 33 | locations = locations |
34 | .trim() | ||
35 | .split(/[\s,]+/) | ||
36 | .filter((loc) => typeof loc === 'string') | ||
37 | .map((loc) => loc.trim().toUpperCase()) | ||
38 | .filter((loc) => loc && (loc !== 'ALL')); | ||
36 | } | 39 | } |
37 | 40 | ||
38 | const locationsCount = locations.length; | 41 | const locationsCount = locations.length; |
39 | for (let i=0; i<locationsCount; i++) { | 42 | for (let i = 0; i < locationsCount; i += 1) { |
40 | const location = locations[i]; | 43 | const location = locations[i]; |
41 | if (!location.trim()) { | ||
42 | continue; | ||
43 | } | ||
44 | 44 | ||
45 | if (!config.locations) config.locations = []; | 45 | if (!config.locations) config.locations = []; |
46 | config.locations.push(location.trim().toUpperCase()); | 46 | config.locations.push(location); |
47 | } | 47 | } |
48 | 48 | ||
49 | config.locations.map(function(x) { return x.toUpperCase(); }); | 49 | // config.locations.map((x) => x.toUpperCase()); |
50 | |||
50 | unique(config.locations); | 51 | unique(config.locations); |
51 | config.locations.sort(naturalSort()); | 52 | config.locations.sort(naturalSort()); |
52 | matrix.config_is_dirty = true; | 53 | matrix.config_is_dirty = true; |
53 | 54 | ||
54 | res.json({ | 55 | res.json({ |
55 | method: '/locations/add', | 56 | method: '/locations/add', |
56 | error: null, | 57 | error: null, |
57 | new_location: locations, | 58 | new_location: locations, |
58 | locations: config.locations | 59 | locations: config.locations, |
59 | }) | 60 | }); |
60 | } | 61 | } |
61 | 62 | ||
62 | function pageDel(req, res) { | 63 | function pageDel(req, res) { |
63 | let locations = req.params.locations || req.query.locations | 64 | let locations = req.params.locations || req.query.locations; |
64 | if (!locations) { | 65 | if (!locations) { |
65 | res.json({ | 66 | res.json({ |
66 | method: '/locations/del', | 67 | method: '/locations/del', |
67 | error: true, | 68 | error: true, |
68 | error_msg: 'Usage: /locations/del/<LOCATION_TO_DELETE> or /locations/del?locations=<LOCATION_TO_DELETE>' | 69 | error_msg: 'Usage: /locations/del/<LOCATION_TO_DELETE> or /locations/del?locations=<LOCATION_TO_DELETE>', |
69 | }); | 70 | }); |
70 | 71 | ||
71 | return; | 72 | return; |
72 | } | 73 | } |
73 | 74 | ||
74 | if (typeof locations === 'string') { | 75 | if (typeof locations === 'string') { |
75 | locations = locations.trim().split(/[\s,]+/); | 76 | locations = locations.trim().split(/[\s,]+/); |
76 | } | 77 | } |
77 | 78 | ||
78 | config.locations.map(function(x) { return x.toUpperCase(); }); | 79 | // config.locations.map((x) => x.toUpperCase()); |
79 | const locationsCount = locations.length; | 80 | const locationsCount = locations.length; |
80 | for (let i=0; i<locationsCount; i++) { | 81 | |
82 | for (let i = 0; i < locationsCount; i += 1) { | ||
81 | const location = locations[i].toUpperCase(); | 83 | const location = locations[i].toUpperCase(); |
82 | const idx = config.locations.indexOf(location); | 84 | const idx = config.locations.indexOf(location); |
83 | if (idx >= 0) { | 85 | if (idx >= 0) { |
84 | matrix.config_is_dirty = true; | 86 | matrix.config_is_dirty = true; |
85 | config.locations.splice(idx, 1) | 87 | config.locations.splice(idx, 1); |
86 | } | 88 | } |
87 | } | 89 | } |
88 | 90 | ||
89 | res.json({ | 91 | res.json({ |
90 | method: '/locations/del', | 92 | method: '/locations/del', |
91 | error: null, | 93 | error: null, |
92 | locations_to_delete: locations, | 94 | locations_to_delete: locations, |
93 | locations: config.locations | 95 | locations: config.locations, |
94 | }) | 96 | }); |
95 | } | 97 | } |
96 | 98 | ||
97 | router.get('/', pageIndex); | 99 | router.get('/', pageIndex); |
98 | router.get('/add/:locations', pageAdd); | 100 | router.get('/add/:locations', pageAdd); |
99 | router.get('/add', pageAdd); | 101 | router.get('/add', pageAdd); |
100 | 102 | ||
101 | router.get('/del/:locations', pageDel); | 103 | router.get('/del/:locations', pageDel); |
102 | router.get('/delete/:locations', pageDel); | 104 | router.get('/delete/:locations', pageDel); |