2023-01-15 18:42:26 +01:00
|
|
|
const { mongoExecute } = require("./mongoDBPool");
|
2023-01-06 00:58:55 +01:00
|
|
|
|
2023-09-09 18:24:57 +02:00
|
|
|
const colName = 'sessions';
|
2023-01-15 18:42:26 +01:00
|
|
|
|
2023-09-09 18:24:57 +02:00
|
|
|
async function insertSessions(sessions, clear) {
|
|
|
|
return await mongoExecute(async({ collection }) => {
|
2023-01-15 18:42:26 +01:00
|
|
|
if (clear) {
|
2023-09-09 18:24:57 +02:00
|
|
|
collection.deleteMany({});
|
2023-01-15 18:42:26 +01:00
|
|
|
}
|
2023-09-09 18:24:57 +02:00
|
|
|
await collection.insertMany(sessions);
|
|
|
|
}, { colName });
|
2023-01-06 00:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getSessions(start, end) {
|
2023-09-09 18:24:57 +02:00
|
|
|
return await mongoExecute(async({ collection }) => {
|
2023-01-15 18:42:26 +01:00
|
|
|
const startDate = start + 'T00:00:00.000Z';
|
|
|
|
const endDate = end + 'T23:59:59.999Z';
|
2023-09-09 18:24:57 +02:00
|
|
|
const result = await collection.aggregate([{
|
2023-01-06 00:58:55 +01:00
|
|
|
$addFields: {
|
|
|
|
completedDate: {
|
|
|
|
$dateFromString: {
|
|
|
|
"dateString": "$completedAt"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createdDate: {
|
|
|
|
$dateFromString: {
|
|
|
|
"dateString": "$createdAt"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updatedDate: {
|
|
|
|
$dateFromString: {
|
|
|
|
"dateString": "$updatedAt"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
$match: {
|
|
|
|
createdDate: {
|
2023-01-15 18:42:26 +01:00
|
|
|
$gt: new Date(startDate),
|
|
|
|
$lte: new Date(endDate)
|
2023-01-06 00:58:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]).toArray();
|
|
|
|
return result;
|
2023-09-09 18:24:57 +02:00
|
|
|
}, { colName });
|
2023-01-15 18:42:26 +01:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:24:57 +02:00
|
|
|
async function getSession(sessionId) {
|
|
|
|
return await mongoExecute(async({ collection }) => {
|
|
|
|
const session = await collection.findOne({ id: sessionId });
|
|
|
|
return session;
|
|
|
|
}, { colName: 'sessions' });
|
2023-01-15 18:42:26 +01:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:24:57 +02:00
|
|
|
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 });
|
2023-01-06 00:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
insertSessions,
|
2023-09-09 18:24:57 +02:00
|
|
|
getSession,
|
2023-01-06 00:58:55 +01:00
|
|
|
getSessions,
|
2023-09-09 18:24:57 +02:00
|
|
|
getSessionsTotalCalculatedTimeByPilot,
|
|
|
|
updateSessionCalculatedTime,
|
2023-01-06 00:58:55 +01:00
|
|
|
};
|
|
|
|
|
2023-01-06 19:12:43 +01:00
|
|
|
//http://localhost:3001/api/v1/ivao/init-sessions?callsign=LTS&from=2023-01-05T00:00:00&to=2023-01-05T23:59:59
|