-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpredictionsController.js
50 lines (47 loc) · 1.45 KB
/
predictionsController.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
const Contest = require("../models/contest");
exports.get = function (req, res) {
res.set("Access-Control-Allow-Origin", "*");
if (!req.query.contestId || !req.query.handles) {
res.status(400).send("Invalid query params");
return;
}
const contestId = req.query.contestId;
const handles = req.query.handles
.split(";")
.map((handle) => {
return handle.trim();
})
.filter((handle) => handle != "");
handles.length = Math.min(handles.length, 50);
Contest.aggregate(
[
{
$project: {
contest_id: 1,
_id: 1,
"rankings._id": 1,
"rankings.delta": 1,
"rankings.data_region": 1,
},
},
{ $match: { _id: contestId } },
{ $unwind: "$rankings" },
{ $match: { "rankings._id": { $in: handles } } },
],
function (err, result) {
let resp = {};
if (err) {
resp.status = "FAILED";
res.status(500).send(err);
} else {
resp.status = "OK";
resp.meta = {
total_count: result.length,
contest_id: contestId,
};
resp.items = result.map((item) => item.rankings);
res.send(resp);
}
}
);
};