-
Notifications
You must be signed in to change notification settings - Fork 3
/
activations.js
84 lines (77 loc) · 1.99 KB
/
activations.js
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
const axios = require('axios');
const moment = require('moment');
const express = require('express');
const config = require('./config');
const db = require('./db');
const summits = require('./summits');
let router = express.Router();
module.exports = router;
router.get('/:callsign', (req, res) => {
res.cacheControl = {
noCache: true
};
let query = {callsign: req.params.callsign}
if (/^[0-9]+$/.test(req.params.callsign)) {
// User ID
query = {userId: req.params.callsign}
}
db.getDb().collection('activators').findOne(query, (err, activator) => {
if (err) {
res.status(500).end();
return;
}
if (!activator) {
res.status(404).end();
return;
}
axios.get('https://api-db2.sota.org.uk/logs/activator/' + activator.userId + '/99999/0')
.then(response => {
let activations = response.data.map(activation => {
return {
id: activation.ActivationId,
date: moment.utc(activation.ActivationDate).toDate(),
callsignUsed: activation.OwnCallsign,
qsos: activation.QSOs,
modeQsos: extractModeQsos(activation),
bandQsos: extractBandQsos(activation),
points: activation.Points,
bonus: activation.BonusPoints,
summit: {
code: activation.SummitCode
}
}
});
summits.lookupSummits(activations)
.then(activationsWithSummits => {
res.json(activationsWithSummits);
})
})
.catch(error => {
console.error(error);
if (error.response && error.response.status === 401) {
res.status(401);
} else {
res.status(500);
}
res.end();
return;
})
});
});
function extractModeQsos(activation) {
return {
'cw': activation.QSOcw,
'ssb': activation.QSOssb,
'fm': activation.QSOfm
}
}
function extractBandQsos(activation) {
let bands = ['160','80','60','40','30','20','17','15','12','10','6','4','2','70c','23c'];
let bandQsos = {};
bands.forEach(band => {
if (activation['QSO' + band] > 0) {
bandQsos[band + 'm'] = activation['QSO' + band];
}
});
return bandQsos;
}