first commit
This commit is contained in:
		
							
								
								
									
										21
									
								
								app/db/mongo/mongoDBPool.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/db/mongo/mongoDBPool.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
const { MongoClient } = require('mongodb');
 | 
			
		||||
const {
 | 
			
		||||
  MONGO_HOST,
 | 
			
		||||
  MONGO_PORT,
 | 
			
		||||
  MONGO_USER,
 | 
			
		||||
  MONGO_PASS,
 | 
			
		||||
  MONGO_DB,
 | 
			
		||||
} = process.env;
 | 
			
		||||
 | 
			
		||||
const uri = `mongodb://${MONGO_USER}:${MONGO_PASS}@${MONGO_HOST}:${MONGO_PORT}/?maxPoolSize=20`;
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getMongoConnection: async() => {
 | 
			
		||||
    const client = new MongoClient(uri);
 | 
			
		||||
    return await client.connect();
 | 
			
		||||
  },
 | 
			
		||||
  getMongoDatabase: (client, db) => {
 | 
			
		||||
    const DB = db || MONGO_DB || 'lsa_leaderboard';
 | 
			
		||||
    return client.db(DB);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										24
									
								
								app/db/mongo/mongoPilots.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								app/db/mongo/mongoPilots.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
const { getMongoConnection } = require("./mongoDBPool");
 | 
			
		||||
const {
 | 
			
		||||
  MONGO_DB,
 | 
			
		||||
} = process.env;
 | 
			
		||||
 | 
			
		||||
const DB = MONGO_DB || 'lts';
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getUserFromReferenceTable
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										75
									
								
								app/db/mongo/mongoSessions.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								app/db/mongo/mongoSessions.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,75 @@
 | 
			
		||||
const moment = require("moment/moment");
 | 
			
		||||
const { getMongoConnection } = require("./mongoDBPool");
 | 
			
		||||
const {
 | 
			
		||||
  MONGO_DB,
 | 
			
		||||
} = process.env;
 | 
			
		||||
 | 
			
		||||
const DB = MONGO_DB || 'lts';
 | 
			
		||||
 | 
			
		||||
async function insertSessions(sessions, clear) {
 | 
			
		||||
  const conn = await getMongoConnection();
 | 
			
		||||
  try {
 | 
			
		||||
    const db = conn.db(DB);
 | 
			
		||||
    const sessionsCollection = db.collection('sessions');
 | 
			
		||||
 | 
			
		||||
    if (clear) {
 | 
			
		||||
      sessionsCollection.deleteMany({});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await sessionsCollection.insertMany(sessions);
 | 
			
		||||
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.error(err);
 | 
			
		||||
  } finally {
 | 
			
		||||
    await conn.close();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSessions(start, end) {
 | 
			
		||||
  const conn = await getMongoConnection();
 | 
			
		||||
  try {
 | 
			
		||||
    const db = conn.db(DB);
 | 
			
		||||
    const sessionsCollection = db.collection('sessions');
 | 
			
		||||
 | 
			
		||||
    const startDate = moment(start).utc().startOf('day').toDate();
 | 
			
		||||
    const endDate = moment(end).utc().endOf('day').toDate();
 | 
			
		||||
    const result = await sessionsCollection.aggregate([{
 | 
			
		||||
      $addFields: {
 | 
			
		||||
        completedDate: {
 | 
			
		||||
          $dateFromString: {
 | 
			
		||||
            "dateString": "$completedAt"
 | 
			
		||||
          }
 | 
			
		||||
        },
 | 
			
		||||
        createdDate: {
 | 
			
		||||
          $dateFromString: {
 | 
			
		||||
            "dateString": "$createdAt"
 | 
			
		||||
          }
 | 
			
		||||
        },
 | 
			
		||||
        updatedDate: {
 | 
			
		||||
          $dateFromString: {
 | 
			
		||||
            "dateString": "$updatedAt"
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }, {
 | 
			
		||||
      $match: {
 | 
			
		||||
        createdDate: {
 | 
			
		||||
          $gt: startDate,
 | 
			
		||||
          $lte: endDate
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }]).toArray();
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.error(err);
 | 
			
		||||
  } finally {
 | 
			
		||||
    await conn.close();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  insertSessions,
 | 
			
		||||
  getSessions,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2022-01-01T00:00:00&to=2023-01-04T23:59:59
 | 
			
		||||
		Reference in New Issue
	
	Block a user