adding redis, tasks, api calls

This commit is contained in:
José Conde
2023-01-06 19:12:43 +01:00
parent 8b557d3c76
commit 34ed6355c9
12 changed files with 4147 additions and 15 deletions

View File

@ -1,6 +1,7 @@
const moment = require('moment');
const { getUserFromReferenceTable } = require('../db/mongo/mongoPilots');
const { getSessions } = require('../db/mongo/mongoSessions');
const { RedisClient } = require('../db/redis/redis');
const { getHistoricalSessions } = require('../requests/ivao/session');
async function getTodaySessionsFromIvao(callsign, incompletes) {
@ -19,10 +20,21 @@ async function getTodaySessionsFromIvao(callsign, incompletes) {
}
async function checkUsername(user, key) {
async function checkUsername(user, key, usersList) {
const u = {...user };
if (!u.name) {
const ref = await getUserFromReferenceTable(key);
const ref1 = await getUserFromReferenceTable(key) || {};
const ref2 = usersList.find(d => key === d.vid) || {};
let ref;
if (ref2.name) {
ref = ref2;
if (!ref1.name) {
// TODO: update mongo
}
} else if (ref1.name) {
ref = ref1
}
if (ref) {
u.name = ref.name;
} else {
@ -32,12 +44,19 @@ async function checkUsername(user, key) {
return u;
}
async function getWhitelist() {
const redisUsers = await RedisClient.getPair('users_whitelist');
return redisUsers.sort((a, b) => b.flightTime - a.flightTime);
}
async function getList(callsign) {
const from = moment().utc().startOf('month');
const to = moment().subtract(1, 'day').utc().endOf('day');
const todayData = await getTodaySessionsFromIvao(callsign);
const monthData = await getSessions(from, to);
const allData = [...todayData, ...monthData];
const redisUsers = await RedisClient.getPair('users');
const totalsByUserId = allData.reduce((acc, d) => {
const userId = d.userId;
@ -72,7 +91,7 @@ async function getList(callsign) {
const array = [];
for (const key in totalsByUserId) {
if (Object.hasOwnProperty.call(totalsByUserId, key)) {
const user = await checkUsername(totalsByUserId[key], key);
const user = await checkUsername(totalsByUserId[key], key, redisUsers);
user.vid = key;
array.push(user);
}
@ -83,4 +102,5 @@ async function getList(callsign) {
module.exports = {
getList,
getWhitelist,
};