latest changes and fixes
This commit is contained in:
		
							
								
								
									
										18
									
								
								app/controllers/acarsController.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								app/controllers/acarsController.js
									
									
									
									
									
										Normal 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,
 | 
			
		||||
};
 | 
			
		||||
@@ -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,
 | 
			
		||||
}
 | 
			
		||||
@@ -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,
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										1
									
								
								app/db/mongo/mongoAudit.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								app/db/mongo/mongoAudit.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
// var ip = req['x-forwarded-for] || req.socket.remoteAddress
 | 
			
		||||
@@ -31,8 +31,9 @@ const mongoExecute = async function(fn, opts) {
 | 
			
		||||
    }
 | 
			
		||||
    return await fn({ database, connection });
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log('err :>> ', err);
 | 
			
		||||
    // console.log('err :>> ', err);
 | 
			
		||||
    console.log('MOMGODB ERROR:', err.message);
 | 
			
		||||
    throw err;
 | 
			
		||||
  } finally {
 | 
			
		||||
    if (connection) {
 | 
			
		||||
      await connection.close();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,24 +1,20 @@
 | 
			
		||||
const { getMongoConnection } = require("./mongoDBPool");
 | 
			
		||||
const {
 | 
			
		||||
  MONGO_DB,
 | 
			
		||||
} = process.env;
 | 
			
		||||
const { mongoExecute } = require("./mongoDBPool");
 | 
			
		||||
 | 
			
		||||
const DB = MONGO_DB || 'lts';
 | 
			
		||||
const colName = 'pilots_ref';
 | 
			
		||||
 | 
			
		||||
async function getUserFromReferenceTable(vid) {
 | 
			
		||||
  const conn = await getMongoConnection();
 | 
			
		||||
  try {
 | 
			
		||||
    const db = conn.db(DB);
 | 
			
		||||
    const col = db.collection('pilots_ref');
 | 
			
		||||
    const user = await col.findOne({ vid }, {});
 | 
			
		||||
    return user;
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.error(err);
 | 
			
		||||
  } finally {
 | 
			
		||||
    await conn.close();
 | 
			
		||||
  }
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    return await collection.findOne({ vid });
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getAllUsersFromReferenceTable() {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    return await collection.find({}).toArray();
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getUserFromReferenceTable
 | 
			
		||||
  getUserFromReferenceTable,
 | 
			
		||||
  getAllUsersFromReferenceTable,
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										42
									
								
								app/db/mongo/mongoSessionTracks.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								app/db/mongo/mongoSessionTracks.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
const { mongoExecute } = require("./mongoDBPool");
 | 
			
		||||
 | 
			
		||||
const colName = 'sessionTracks';
 | 
			
		||||
 | 
			
		||||
async function insertSessionTracks(sessionTracks, clear) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    if (clear) {
 | 
			
		||||
      collection.deleteMany({});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await collection.insertMany(sessionTracks);
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function insertOneSessionTracks(sessionTracks) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    await collection.insertOne(sessionTracks);
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSessionTracks(sessionId) {
 | 
			
		||||
  return await mongoExecute(
 | 
			
		||||
    async({ collection }) => {
 | 
			
		||||
      return await collection.findOne({ sessionId });
 | 
			
		||||
    }, { colName }
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function updateSessionTracks(tracks) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const tracksCollection = database.collection('sessionTracks');
 | 
			
		||||
    await tracksCollection.updateOne({ _id: tracks._id }, { $set: { calculatedTime: tracks.calculatedTime } });
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  insertSessionTracks,
 | 
			
		||||
  getSessionTracks,
 | 
			
		||||
  updateSessionTracks,
 | 
			
		||||
  insertOneSessionTracks,
 | 
			
		||||
};
 | 
			
		||||
@@ -1,46 +1,21 @@
 | 
			
		||||
const { mongoExecute } = require("./mongoDBPool");
 | 
			
		||||
 | 
			
		||||
const colName = 'sessions';
 | 
			
		||||
 | 
			
		||||
async function insertSessions(sessions, clear) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const sessionsCollection = database.collection('sessions');
 | 
			
		||||
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    if (clear) {
 | 
			
		||||
      sessionsCollection.deleteMany({});
 | 
			
		||||
      collection.deleteMany({});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await sessionsCollection.insertMany(sessions);
 | 
			
		||||
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function insertOneSessionTracks(sessionTracks) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const sessionsCollection = database.collection('sessionTracks');
 | 
			
		||||
 | 
			
		||||
    await sessionsCollection.insertOne(sessionTracks);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function insertSessionTracks(sessionTracks, clear) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const sessionsCollection = database.collection('sessionTracks');
 | 
			
		||||
 | 
			
		||||
    if (clear) {
 | 
			
		||||
      sessionsCollection.deleteMany({});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await sessionsCollection.insertMany(sessionTracks);
 | 
			
		||||
  });
 | 
			
		||||
    await collection.insertMany(sessions);
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSessions(start, end) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const sessionsCollection = database.collection('sessions');
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    const startDate = start + 'T00:00:00.000Z';
 | 
			
		||||
    const endDate = end + 'T23:59:59.999Z';
 | 
			
		||||
    const result = await sessionsCollection.aggregate([{
 | 
			
		||||
    const result = await collection.aggregate([{
 | 
			
		||||
      $addFields: {
 | 
			
		||||
        completedDate: {
 | 
			
		||||
          $dateFromString: {
 | 
			
		||||
@@ -67,33 +42,62 @@ async function getSessions(start, end) {
 | 
			
		||||
      }
 | 
			
		||||
    }]).toArray();
 | 
			
		||||
    return result;
 | 
			
		||||
  });
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSessionTracks(sessionId) {
 | 
			
		||||
  return await mongoExecute(
 | 
			
		||||
    async({ database }) => {
 | 
			
		||||
      const tracksCollection = database.collection('sessionTracks');
 | 
			
		||||
      const tracks = await tracksCollection.findOne({ sessionId });
 | 
			
		||||
      return tracks;
 | 
			
		||||
    }
 | 
			
		||||
  );
 | 
			
		||||
async function getSession(sessionId) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    const session = await collection.findOne({ id: sessionId });
 | 
			
		||||
    return session;
 | 
			
		||||
  }, { colName: 'sessions' });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function updateSessionTracks(tracks) {
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const tracksCollection = database.collection('sessionTracks');
 | 
			
		||||
    await tracksCollection.updateOne({ _id: tracks._id }, { $set: { calculatedTime: tracks.calculatedTime } });
 | 
			
		||||
  });
 | 
			
		||||
async function updateSessionCalculatedTime(session) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    await collection.updateOne({ _id: session._id }, { $set: { calculatedTime: session.calculatedTime } });
 | 
			
		||||
  }, { colName: 'sessions' });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSessionsTotalCalculatedTimeByPilot(start, end) {
 | 
			
		||||
  const startDate = start + 'T00:00:00.000Z';
 | 
			
		||||
  const endDate = end + 'T23:59:59.999Z';
 | 
			
		||||
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    return await collection.aggregate([{
 | 
			
		||||
      $addFields: {
 | 
			
		||||
        createdDate: {
 | 
			
		||||
          $dateFromString: {
 | 
			
		||||
            dateString: "$createdAt",
 | 
			
		||||
          },
 | 
			
		||||
        },
 | 
			
		||||
      },
 | 
			
		||||
    }, {
 | 
			
		||||
      $match: {
 | 
			
		||||
        createdDate: {
 | 
			
		||||
          $gt: new Date(startDate),
 | 
			
		||||
          $lte: new Date(endDate)
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }, {
 | 
			
		||||
      $group: {
 | 
			
		||||
        _id: "$userId",
 | 
			
		||||
        total: {
 | 
			
		||||
          $count: {},
 | 
			
		||||
        },
 | 
			
		||||
        time: {
 | 
			
		||||
          $sum: "$calculatedTime",
 | 
			
		||||
        },
 | 
			
		||||
      },
 | 
			
		||||
    }, ]).toArray();
 | 
			
		||||
  }, { colName });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  insertSessions,
 | 
			
		||||
  insertSessionTracks,
 | 
			
		||||
  insertOneSessionTracks,
 | 
			
		||||
  getSession,
 | 
			
		||||
  getSessions,
 | 
			
		||||
  getSessionTracks,
 | 
			
		||||
  updateSessionTracks,
 | 
			
		||||
  getSessionsTotalCalculatedTimeByPilot,
 | 
			
		||||
  updateSessionCalculatedTime,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2023-01-05T00:00:00&to=2023-01-05T23:59:59
 | 
			
		||||
@@ -4,11 +4,13 @@ async function createUserMongo({ id, username, hash, roles = [], firstname, last
 | 
			
		||||
  return await mongoExecute(async({ database }) => {
 | 
			
		||||
    const usersCol = database.collection('users');
 | 
			
		||||
    const createdOn = new Date();
 | 
			
		||||
    const updatedOn = new Date();
 | 
			
		||||
    await usersCol.insertOne({
 | 
			
		||||
      id,
 | 
			
		||||
      username,
 | 
			
		||||
      hash,
 | 
			
		||||
      createdOn,
 | 
			
		||||
      updatedOn,
 | 
			
		||||
      roles,
 | 
			
		||||
      firstname,
 | 
			
		||||
      lastname,
 | 
			
		||||
@@ -17,6 +19,20 @@ async function createUserMongo({ id, username, hash, roles = [], firstname, last
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function updateUserHash(id, hash) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    return await collection.updateOne({
 | 
			
		||||
      id
 | 
			
		||||
    }, {
 | 
			
		||||
      $set: {
 | 
			
		||||
        hash,
 | 
			
		||||
        updatedOn: new Date()
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
  }, { colName: 'users' });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getUserMongo(username) {
 | 
			
		||||
  return await mongoExecute(async({ collection }) => {
 | 
			
		||||
    return await collection.findOne({ username });
 | 
			
		||||
@@ -26,4 +42,5 @@ async function getUserMongo(username) {
 | 
			
		||||
module.exports = {
 | 
			
		||||
  createUserMongo,
 | 
			
		||||
  getUserMongo,
 | 
			
		||||
  updateUserHash
 | 
			
		||||
};
 | 
			
		||||
@@ -26,7 +26,6 @@ const getHistoricalSessions = async({ callsign, userId, from, to }) => {
 | 
			
		||||
    },
 | 
			
		||||
    params,
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  return await _requestHistoricalRecursive([], url, options);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -71,9 +70,7 @@ async function getIvaoPilotsNow(all = false) {
 | 
			
		||||
      apiKey: process.env.IVAO_APIKEY,
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
  const redisUsers = await RedisClient.getPair('users');
 | 
			
		||||
  const pilots = await request(url, options);
 | 
			
		||||
  // console.log('redisUsers :>> ', redisUsers);
 | 
			
		||||
 | 
			
		||||
  return all ? pilots : pilots.filter(d => d.callsign.startsWith('LTS'));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								app/routes/acars.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								app/routes/acars.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
const express = require('express');
 | 
			
		||||
const { updatePosition, getPosition } = require('../controllers/acarsController');
 | 
			
		||||
const router = express.Router();
 | 
			
		||||
 | 
			
		||||
router.post(`/report`, (req, res) => {
 | 
			
		||||
  updatePosition(req);
 | 
			
		||||
  res.status(200).send();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get(`/position`, async(req, res) => {
 | 
			
		||||
  res.status(200).send(await getPosition(req));
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
@@ -1,9 +1,17 @@
 | 
			
		||||
const express = require('express');
 | 
			
		||||
const passport = require('passport')
 | 
			
		||||
const LocalStrategy = require('passport-local');
 | 
			
		||||
const { initSessionsData, createUser, authenticate } = require('../controllers/adminController');
 | 
			
		||||
const { initSessionsData, createUser, authenticate, changePassword } = require('../controllers/adminController');
 | 
			
		||||
const router = express.Router();
 | 
			
		||||
 | 
			
		||||
const NOT_AUTHENTICATED = 'not-authenticated';
 | 
			
		||||
const WRONG_PASSWORD = 'wrong-password';
 | 
			
		||||
const checkAuthenticated = (req) => {
 | 
			
		||||
  if (!req.isAuthenticated()) {
 | 
			
		||||
    throw new Error(NOT_AUTHENTICATED);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
passport.use(new LocalStrategy(async function verify(username, password, cb) {
 | 
			
		||||
  try {
 | 
			
		||||
    const user = await authenticate(username, password);
 | 
			
		||||
@@ -49,10 +57,28 @@ router.post('/user/create', async(req, res) => {
 | 
			
		||||
router.post('/user/authenticate',
 | 
			
		||||
  passport.authenticate('local'),
 | 
			
		||||
  function(req, res) {
 | 
			
		||||
    console.log('req.user :>> ', req.isAuthenticated(), req.user);
 | 
			
		||||
    res.json(req.user);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
router.post('/user/password-change',
 | 
			
		||||
  async function(req, res, next) {
 | 
			
		||||
    try {
 | 
			
		||||
      checkAuthenticated(req);
 | 
			
		||||
      await changePassword(req);
 | 
			
		||||
      console.log('pasword changed');
 | 
			
		||||
      res.status(200).send();
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      if (err.message === NOT_AUTHENTICATED || err.message === WRONG_PASSWORD) {
 | 
			
		||||
        res.status(401).json({
 | 
			
		||||
          error: err.message
 | 
			
		||||
        });
 | 
			
		||||
        next();
 | 
			
		||||
      } else {
 | 
			
		||||
        res.status(500).send();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
router.get('/user/alive',
 | 
			
		||||
  function(req, res) {
 | 
			
		||||
    console.log('req.user :>> ', req.isAuthenticated());
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
const express = require('express');
 | 
			
		||||
const router = express.Router();
 | 
			
		||||
 | 
			
		||||
const { getList, getWhitelist } = require('../controllers/sessionsController');
 | 
			
		||||
const { getList, getWhitelist, getPilotInfoInTime } = require('../controllers/sessionsController');
 | 
			
		||||
const { getSessions } = require('../db/mongo/mongoSessions');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -17,7 +17,7 @@ router.get('/sessions', async(req, res) => {
 | 
			
		||||
 | 
			
		||||
router.get('/list/today', async(req, res) => {
 | 
			
		||||
  try {
 | 
			
		||||
    const data = await getList('LTS');
 | 
			
		||||
    const data = await getList('LTS', req.isAuthenticated());
 | 
			
		||||
    res.status(200).json(data);
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log(err);
 | 
			
		||||
@@ -31,6 +31,15 @@ router.get('/whitelist', async(req, res) => {
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log(err);
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/list/previous/:start/:end', async(req, res) => {
 | 
			
		||||
  try {
 | 
			
		||||
    const data = await getPilotInfoInTime(req, );
 | 
			
		||||
    res.status(200).json(data);
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log(err);
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
@@ -18,6 +18,7 @@ async function taskSyncLSAUsers() {
 | 
			
		||||
      ['users_whitelist', whitelist],
 | 
			
		||||
    ]);
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log('err :>> ', err);
 | 
			
		||||
    console.log('ERR executing taskSyncLSAUsers');
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -40,16 +41,21 @@ async function taskSyncPrevioudDaySessions(callsign) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = function() {
 | 
			
		||||
  if (canRunTasks) {
 | 
			
		||||
    cron.schedule(process.env.SYNC_TASK_SCHEDULE, () => {
 | 
			
		||||
      taskSyncPrevioudDaySessions('LTS');
 | 
			
		||||
    });
 | 
			
		||||
    cron.schedule(process.env.USERS_TASK_SCHEDULE, async() => {
 | 
			
		||||
      taskSyncLSAUsers();
 | 
			
		||||
    });
 | 
			
		||||
    console.log('Tasks started.');
 | 
			
		||||
  } else {
 | 
			
		||||
    console.log('Tasks skipped.');
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
module.exports = {
 | 
			
		||||
  sync: function() {
 | 
			
		||||
    console.log('canRunTasks :>> ', canRunTasks);
 | 
			
		||||
    if (canRunTasks) {
 | 
			
		||||
      cron.schedule(process.env.SYNC_TASK_SCHEDULE, () => {
 | 
			
		||||
        taskSyncPrevioudDaySessions('LTS');
 | 
			
		||||
      });
 | 
			
		||||
      // cron.schedule(process.env.USERS_TASK_SCHEDULE, async() => {
 | 
			
		||||
      //   taskSyncLSAUsers();
 | 
			
		||||
      // });
 | 
			
		||||
      console.log('Tasks started.');
 | 
			
		||||
    } else {
 | 
			
		||||
      console.log('Tasks skipped.');
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  taskSyncLSAUsers,
 | 
			
		||||
  taskSyncPrevioudDaySessions,
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user