latest changes and fixes

This commit is contained in:
José Conde
2023-09-09 18:24:57 +02:00
parent 15f012530e
commit ed51c02ddc
18 changed files with 470 additions and 132 deletions

View File

@ -0,0 +1,18 @@
const { RedisClient } = require('../db/redis/redis');
const USER_POSITION_DATA = 'user_position_data';
function updatePosition(req) {
RedisClient.setPair(USER_POSITION_DATA, JSON.stringify([req.body]));
}
async function getPosition() {
const data = await RedisClient.getPair(USER_POSITION_DATA);
return data;
}
module.exports = {
updatePosition,
getPosition,
};

View File

@ -2,9 +2,10 @@ const moment = require('moment');
const bcrypt = require('bcryptjs');
const crypto = require("crypto");
const { insertSessions, insertSessionTracks } = require("../db/mongo/mongoSessions");
const { createUserMongo, getUserMongo } = require('../db/mongo/mongoUsers');
const { insertSessions } = require("../db/mongo/mongoSessions");
const { createUserMongo, getUserMongo, updateUserHash } = require('../db/mongo/mongoUsers');
const { getHistoricalSessions, getIvaoSessionTracks } = require("../requests/ivao/session");
const { insertSessionTracks } = require('../db/mongo/mongoSessionTracks');
const saltRounds = 10;
@ -80,10 +81,22 @@ async function authenticate(username, password) {
}
}
async function changePassword(req) {
const { username } = req.user;
const { currentPassword, newPassword } = req.body;
console.log('currentPassword, newPassword :>> ', currentPassword, newPassword);
const user = await getUserMongo(username);
if (bcrypt.compareSync(currentPassword, user.hash)) {
const newHash = getHashedPassword(newPassword);
return await updateUserHash(user.id, newHash);
} else {
throw new Error('wrong-password');
}
}
module.exports = {
initSessionsData,
createUser,
authenticate,
getHashedPassword,
changePassword,
}

View File

@ -1,6 +1,7 @@
const moment = require('moment');
const { getUserFromReferenceTable } = require('../db/mongo/mongoPilots');
const { getSessions, getSessionTracks, updateSessionTracks, insertOneSessionTracks } = require('../db/mongo/mongoSessions');
const { getUserFromReferenceTable, getAllUsersFromReferenceTable } = require('../db/mongo/mongoPilots');
const { getSessions, updateSessionCalculatedTime, getSessionsTotalCalculatedTimeByPilot } = require('../db/mongo/mongoSessions');
const { getSessionTracks, updateSessionTracks, insertOneSessionTracks } = require('../db/mongo/mongoSessionTracks');
const { RedisClient } = require('../db/redis/redis');
const { getHistoricalSessions, getIvaoSessionTracks, getIvaoPilotsNow, getIvaoLatestSessionFlightPlan, getIvaoSessionLatestTrack } = require('../requests/ivao/session');
const { getAirTime } = require('./trackerAnalizer');
@ -26,7 +27,7 @@ async function checkUsername(user, key, usersList) {
if (!u.name) {
const ref1 = await getUserFromReferenceTable(key) || {};
const ref2 = usersList.find(d => key === d.vid) || {};
const ref2 = (usersList || []).find(d => key === d.vid) || {};
let ref;
if (ref2.name) {
ref = ref2;
@ -51,32 +52,69 @@ async function getWhitelist() {
}
async function getSessionCalculatedTime(sessionId) {
async function getSessionCalculatedTime(session) {
const sessionId = session.id;
let tracks = await getSessionTracks(sessionId);
let calculatedTime;
if (tracks && !Number.isInteger(tracks.calculatedTime)) {
tracks.calculatedTime = getAirTime(tracks);
await updateSessionTracks(tracks);
calculatedTime = getAirTime(tracks);
tracks.calculatedTime = calculatedTime;
try {
await updateSessionTracks(tracks);
} catch (err) {
console.log('Error updateSessionTracks');
}
} else if (!tracks) {
const t = await getIvaoSessionTracks(sessionId);
calculatedTime = getAirTime(t);
tracks = {
sessionId,
tracks: t,
calculatedTime: getAirTime(t)
calculatedTime,
};
await insertOneSessionTracks(tracks);
try {
await insertOneSessionTracks(tracks);
} catch (err) {
console.log('Error insertOneSessionTracks');
}
}
await setSessionCalculatedTime(session, tracks, calculatedTime);
return tracks.calculatedTime;
}
async function getList(callsign) {
async function setSessionCalculatedTime(session, tracks, calculatedTime) {
if (!Number.isInteger(session.calculatedTime)) {
session.calculatedTime = calculatedTime ? calculatedTime : tracks.calculatedTime;
await updateSessionCalculatedTime(session);
}
}
function obfuscate(name, isAuthenticated, from = 1) {
if (isAuthenticated) {
return name;
}
if (typeof name === 'undefined') {
return '';
}
const arr = name.split(' ');
const obfuscated = arr.map((token, i) => {
if (i < from) {
return token;
}
const xs = Array.apply(null, Array(token.length ? token.length - 1 : 0)).map(() => '*');
return token.charAt(0).toUpperCase() + xs.join('');
});
return obfuscated.join(' ');
}
async function getList(callsign, isAuthenticated) {
const from = moment().startOf('month').format('YYYY-MM-DD');
const to = moment().subtract(1, 'day').endOf('day').format('YYYY-MM-DD');
const todayData = await getTodaySessionsFromIvao(callsign);
const monthData = await getSessions(from, to);
const allData = [...todayData, ...monthData];
const redisUsers = await RedisClient.getPair('users');
const totalsByUserId = {};
for (let index = 0; index < allData.length; index++) {
@ -84,7 +122,7 @@ async function getList(callsign) {
const userId = session.userId;
const flightPlan = session.flightPlans[session.flightPlans.length - 1];
const date = moment(session.completedAt);
const calculated = await getSessionCalculatedTime(session.id);
const calculated = await getSessionCalculatedTime(session);
if (!totalsByUserId[userId]) {
totalsByUserId[userId] = {
time: 0,
@ -107,7 +145,6 @@ async function getList(callsign) {
}
totalsByUserId[userId].division = session.user.divisionId;
}
}
const array = [];
@ -115,6 +152,7 @@ async function getList(callsign) {
if (Object.hasOwnProperty.call(totalsByUserId, key)) {
const user = await checkUsername(totalsByUserId[key], key, redisUsers);
user.vid = key;
user.name = obfuscate(user.name, isAuthenticated);
array.push(user);
}
}
@ -149,9 +187,24 @@ async function getLatestsFlightPlans() {
return response;
}
async function getPilotInfoInTime(req) {
const { start, end } = req.params;
const redisUsers = await RedisClient.getPair('users');
const pilots = await getAllUsersFromReferenceTable();
const data = await getSessionsTotalCalculatedTimeByPilot(start, end);
for (let index = 0; index < data.length; index++) {
const row = data[index];
const user = (redisUsers || pilots).find(d => Number(d.vid) === row._id) || { name: '657396' };
user.name = obfuscate(user.name, req.isAuthenticated());
row.user = user;
}
return data.sort((a, b) => b.time - a.time);
}
module.exports = {
getList,
getWhitelist,
getLatestSessions,
getLatestsFlightPlans,
getPilotInfoInTime,
};