-
Notifications
You must be signed in to change notification settings - Fork 0
/
restoreUnbanTasks.ts
61 lines (60 loc) · 1.28 KB
/
restoreUnbanTasks.ts
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
import clientPromise from "./lib/mongodb";
import { DateTime } from "luxon";
import { UnbanTask } from "./types";
import environment from "./environment";
(async () => {
const date = DateTime.local()
.setZone("America/Buenos_Aires")
.startOf("minute");
try {
const client = await clientPromise;
const db = client.db();
let docs = await db
.collection(environment.banLogCollection)
.aggregate([
{
$group: {
_id: "$playerid",
startdate: {
$last: "$startdate"
},
enddate: {
$last: "$enddate"
},
reason: {
$last: "$reason"
}
}
},
{
$sort: {
startdate: -1
}
}
])
.toArray();
let oldTasks = await db
.collection(environment.unbanTasksCollection)
.find({})
.toArray();
let newTasks: UnbanTask[] = [];
let docsFiltered = docs.filter(e => DateTime.fromISO(e.enddate) >= date);
docsFiltered.forEach(e => {
if (!oldTasks.find(e2 => e2.playerid === e._id)) {
newTasks.push({
playerid: e._id,
date: e.enddate
});
}
});
console.log(newTasks);
if (newTasks.length) {
await db
.collection(environment.unbanTasksCollection)
.insertMany(newTasks);
}
console.log("You can now CTRL-C");
} catch (error) {
console.error(error);
}
})();