feature: authentuication
This commit is contained in:
@ -19,18 +19,23 @@ const getMongoDatabase = (client, db) => {
|
||||
return client.db(DB);
|
||||
};
|
||||
|
||||
const mongoExecute = async(fn, dbName = MONGO_DB) => {
|
||||
let conn;
|
||||
const mongoExecute = async function(fn, opts) {
|
||||
const { dbName, colName } = { dbName: MONGO_DB, ...opts };
|
||||
let connection;
|
||||
try {
|
||||
conn = await getMongoConnection();
|
||||
const db = conn.db(dbName);
|
||||
const result = await fn(db, conn);
|
||||
return result;
|
||||
connection = await getMongoConnection();
|
||||
const database = connection.db(dbName);
|
||||
if (colName) {
|
||||
const collection = database.collection(colName);
|
||||
return await fn({ collection, database, connection });
|
||||
}
|
||||
return await fn({ database, connection });
|
||||
} catch (err) {
|
||||
console.log('err :>> ', err);
|
||||
console.log('MOMGODB ERROR:', err.message);
|
||||
} finally {
|
||||
if (conn) {
|
||||
await conn.close();
|
||||
if (connection) {
|
||||
await connection.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
const { mongoExecute } = require("./mongoDBPool");
|
||||
|
||||
async function insertSessions(sessions, clear) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessions');
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const sessionsCollection = database.collection('sessions');
|
||||
|
||||
if (clear) {
|
||||
sessionsCollection.deleteMany({});
|
||||
@ -16,16 +16,16 @@ async function insertSessions(sessions, clear) {
|
||||
|
||||
|
||||
async function insertOneSessionTracks(sessionTracks) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessionTracks');
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const sessionsCollection = database.collection('sessionTracks');
|
||||
|
||||
await sessionsCollection.insertOne(sessionTracks);
|
||||
});
|
||||
}
|
||||
|
||||
async function insertSessionTracks(sessionTracks, clear) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessionTracks');
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const sessionsCollection = database.collection('sessionTracks');
|
||||
|
||||
if (clear) {
|
||||
sessionsCollection.deleteMany({});
|
||||
@ -36,8 +36,8 @@ async function insertSessionTracks(sessionTracks, clear) {
|
||||
}
|
||||
|
||||
async function getSessions(start, end) {
|
||||
return mongoExecute(async(db) => {
|
||||
const sessionsCollection = db.collection('sessions');
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const sessionsCollection = database.collection('sessions');
|
||||
const startDate = start + 'T00:00:00.000Z';
|
||||
const endDate = end + 'T23:59:59.999Z';
|
||||
const result = await sessionsCollection.aggregate([{
|
||||
@ -71,9 +71,9 @@ async function getSessions(start, end) {
|
||||
}
|
||||
|
||||
async function getSessionTracks(sessionId) {
|
||||
return mongoExecute(
|
||||
async(db) => {
|
||||
const tracksCollection = db.collection('sessionTracks');
|
||||
return await mongoExecute(
|
||||
async({ database }) => {
|
||||
const tracksCollection = database.collection('sessionTracks');
|
||||
const tracks = await tracksCollection.findOne({ sessionId });
|
||||
return tracks;
|
||||
}
|
||||
@ -81,8 +81,8 @@ async function getSessionTracks(sessionId) {
|
||||
}
|
||||
|
||||
async function updateSessionTracks(tracks) {
|
||||
return mongoExecute(async(db) => {
|
||||
const tracksCollection = db.collection('sessionTracks');
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const tracksCollection = database.collection('sessionTracks');
|
||||
await tracksCollection.updateOne({ _id: tracks._id }, { $set: { calculatedTime: tracks.calculatedTime } });
|
||||
});
|
||||
}
|
||||
|
29
app/db/mongo/mongoUsers.js
Normal file
29
app/db/mongo/mongoUsers.js
Normal file
@ -0,0 +1,29 @@
|
||||
const { mongoExecute } = require('./mongoDBPool');
|
||||
|
||||
async function createUserMongo({ id, username, hash, roles = [], firstname, lastname, vid }) {
|
||||
return await mongoExecute(async({ database }) => {
|
||||
const usersCol = database.collection('users');
|
||||
const createdOn = new Date();
|
||||
await usersCol.insertOne({
|
||||
id,
|
||||
username,
|
||||
hash,
|
||||
createdOn,
|
||||
roles,
|
||||
firstname,
|
||||
lastname,
|
||||
vid
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getUserMongo(username) {
|
||||
return await mongoExecute(async({ collection }) => {
|
||||
return await collection.findOne({ username });
|
||||
}, { colName: 'users' })
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createUserMongo,
|
||||
getUserMongo,
|
||||
};
|
Reference in New Issue
Block a user