2022-02-03 00:33:15 +01:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
|
|
|
|
const server_constants = require('../tools/server_constants');
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const {authenticate} = require('../middleware/authenticate');
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const {User} = require('../models/user');
|
|
|
|
|
const {MyGroup} = require('../models/mygroup');
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const {ObjectID} = require('mongodb');
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-03-30 22:51:03 +02:00
|
|
|
async function getGroupRecAdminsInfo(idapp, data) {
|
|
|
|
|
|
|
|
|
|
for (const admin of data.admins) {
|
|
|
|
|
const myuser = await User.findOne({idapp, username: admin.username}).lean();
|
|
|
|
|
admin.profile = { img: myuser.profile.img };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
router.post('/load', authenticate, async (req, res) => {
|
|
|
|
|
const idapp = req.body.idapp;
|
|
|
|
|
const groupname = req.body.groupname;
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
try {
|
|
|
|
|
const whatshow = MyGroup.getWhatToShow(idapp, req.user.username);
|
2022-03-30 22:51:03 +02:00
|
|
|
let data = await MyGroup.findOne({idapp, groupname}, whatshow).lean();
|
|
|
|
|
|
|
|
|
|
data = await getGroupRecAdminsInfo(idapp, data);
|
2022-02-05 23:28:15 +01:00
|
|
|
|
|
|
|
|
const whatshowUsers = await User.getWhatToShow_IfFriends(idapp, req.user.username);
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const users_in_group = await User.find(
|
|
|
|
|
{
|
|
|
|
|
idapp,
|
|
|
|
|
'profile.mygroups': {
|
|
|
|
|
$elemMatch: {groupname: {$eq: groupname}},
|
2022-03-30 22:51:03 +02:00
|
|
|
},
|
2022-02-05 23:28:15 +01:00
|
|
|
},
|
2022-03-30 22:51:03 +02:00
|
|
|
whatshowUsers,
|
2022-02-05 23:28:15 +01:00
|
|
|
);
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
res.send({mygroup: data, users_in_group});
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Error in MyGroups', e);
|
2022-02-03 00:33:15 +01:00
|
|
|
return res.status(400).send(e);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const ris = null;
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|