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 @@
// var ip = req['x-forwarded-for] || req.socket.remoteAddress

View File

@ -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();

View File

@ -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,
};

View 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,
};

View File

@ -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

View File

@ -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
};