2022-08-30 17:00:48 +02:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
|
|
|
|
const server_constants = require('../tools/server_constants');
|
|
|
|
|
|
|
|
|
|
const {authenticate} = require('../middleware/authenticate');
|
|
|
|
|
|
2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2022-08-30 17:00:48 +02:00
|
|
|
|
|
|
|
|
const {User} = require('../models/user');
|
|
|
|
|
const {Circuit} = require('../models/circuit');
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
|
|
const {ObjectID} = require('mongodb');
|
|
|
|
|
|
|
|
|
|
async function getCircuitRecAdminsInfo(idapp, data) {
|
|
|
|
|
|
|
|
|
|
if (data && data.admins) {
|
|
|
|
|
for (const admin of data.admins) {
|
|
|
|
|
const myuser = await User.findOne({idapp, username: admin.username}, {'profile.img': 1}).lean();
|
|
|
|
|
admin.profile = {img: myuser.profile.img};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.post('/load', authenticate, async (req, res) => {
|
|
|
|
|
const idapp = req.body.idapp;
|
|
|
|
|
const path = req.body.path;
|
|
|
|
|
const usernameOrig = req.user.username;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const {SendNotif} = require('../models/sendnotif');
|
2022-09-11 11:45:33 +02:00
|
|
|
const {Movement} = require('../models/movement');
|
2022-08-30 17:00:48 +02:00
|
|
|
|
|
|
|
|
// Check if ìs a Notif to read
|
|
|
|
|
const idnotif = req.body['idnotif'] ? req.body['idnotif'] : '';
|
|
|
|
|
SendNotif.setNotifAsRead(idapp, usernameOrig, idnotif);
|
|
|
|
|
|
|
|
|
|
const whatshow = Circuit.getWhatToShow(idapp, req.user.username);
|
|
|
|
|
let data = await Circuit.findOne({idapp, path}, whatshow).lean();
|
|
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
const whatshowUsers = await User.getWhatToShow_IfFriends(idapp, req.user.username);
|
|
|
|
|
|
2022-11-06 13:39:01 +01:00
|
|
|
/*let users_in_circuit = [];
|
2022-09-02 02:25:38 +02:00
|
|
|
|
|
|
|
|
if (data) {
|
2022-08-30 17:00:48 +02:00
|
|
|
users_in_circuit = await User.find(
|
|
|
|
|
{
|
|
|
|
|
idapp,
|
2022-09-02 02:25:38 +02:00
|
|
|
'profile.mycircuits': {
|
|
|
|
|
$elemMatch: {circuitname: {$eq: data.name}},
|
|
|
|
|
},
|
2022-08-30 17:00:48 +02:00
|
|
|
},
|
|
|
|
|
whatshowUsers,
|
|
|
|
|
).lean();
|
|
|
|
|
}
|
2022-11-06 13:39:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const users_in_circuit = await Circuit.getUsersSingleCircuit(idapp, req.user.username, data.name, data._id);
|
2022-08-30 17:00:48 +02:00
|
|
|
|
|
|
|
|
data = await getCircuitRecAdminsInfo(idapp, data);
|
|
|
|
|
|
2022-09-12 18:37:08 +02:00
|
|
|
if (data) {
|
|
|
|
|
data.movements = await Movement.getMovsByCircuitId(idapp, usernameOrig, data._id);
|
|
|
|
|
}
|
2022-09-11 11:45:33 +02:00
|
|
|
|
2022-08-30 17:00:48 +02:00
|
|
|
res.send({circuit: data, users_in_circuit});
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Error in Circuits', e);
|
|
|
|
|
return res.status(400).send(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ris = null;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|