-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (81 loc) · 2.59 KB
/
app.js
File metadata and controls
97 lines (81 loc) · 2.59 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
const express = require('express');
const qr = require('qr-image');
const {
searchDatabase,
addMarkdown,
addSimilarItems,
} = require('./googleSpreadsheet');
const app = express();
const allScans = new Map();
app.set('view engine', 'ejs');
app.use(express.static(`${__dirname}/public`));
app.get(['/favicon.ico', '/robots.txt'], (req, res) => {
res.sendStatus(204);
});
function addRecentlyScanned(uuid, item, nbFound = 0) {
// TRICK: only record uuids
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) {
return;
}
const duplicatedItem = item;
duplicatedItem.time = new Date();
duplicatedItem.duplicated = nbFound > 1;
duplicatedItem.link = item.cellRef;
if (nbFound === 0) {
duplicatedItem.fixture = '';
duplicatedItem.uuid = uuid;
duplicatedItem.status = 'missing';
}
allScans.set(uuid, duplicatedItem);
}
app.get('/search', (req, res) => {
searchDatabase(req.query, queryResult => res.render('search', {
matches: queryResult.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)),
}));
});
app.get('/qrlist', (req, res) => {
function renderQrList(qrList) {
const qrItems = qrList.filter(item => item.uuid !== '')
.filter(item => item.uuid !== undefined)
.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1));
qrItems.forEach((item) => {
const itemWithQr = item;
itemWithQr.qr = qr.imageSync(item.uuid, { type: 'svg' });
return itemWithQr;
});
res.render('qrList', { matches: qrItems });
}
searchDatabase(req.query, queryResult => renderQrList(queryResult));
});
app.get('/recent', (req, res) => {
res.render('recent', { allScans });
});
app.get('/:uuid', (req, res) => {
function renderUuid(uuidsList) {
let uuids = uuidsList[0];
if (uuidsList.length === 0) {
addRecentlyScanned(req.params.uuid, {});
res.status(404).render('notFound', {
item: '',
id: req.params.uuid,
});
return;
}
if (uuidsList.length > 1) {
console.log(`Too much matches for uuid ${req.params.uuid} length = ${uuidsList.length}`);
}
// copying twice just to remove a lint warning (no-param-reassign): bad bad bad !
uuids = addMarkdown(uuids);
uuids = addSimilarItems(uuids);
addRecentlyScanned(req.params.uuid, uuids, uuidsList.length);
res.render('item', uuids);
}
searchDatabase(req.params, queryResult => renderUuid(queryResult));
});
app.get('/', (req, res) => {
res.render('home', {});
});
const port = process.env.PORT || 1234;
app.listen(port, () => {
console.log(`working on ${port}`);
});