Files
freeplanet_serverside/src/server/router/mygroups_router.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

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');
const {authenticate} = require('../middleware/authenticate');
2022-02-03 00:33:15 +01:00
2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
2022-02-03 00:33:15 +01:00
const {User} = require('../models/user');
const {MyGroup} = require('../models/mygroup');
2022-02-03 00:33:15 +01:00
const _ = require('lodash');
const {ObjectID} = require('mongodb');
2022-02-03 00:33:15 +01:00
async function getGroupRecAdminsInfo(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;
}
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-07-28 23:27:51 +02:00
const usernameOrig = req.user.username;
2022-02-03 00:33:15 +01:00
try {
2022-07-28 23:27:51 +02:00
const {SendNotif} = require('../models/sendnotif');
// Check if ìs a Notif to read
const idnotif = req.body['idnotif'] ? req.body['idnotif'] : '';
SendNotif.setNotifAsRead(idapp, usernameOrig, idnotif);
const whatshow = MyGroup.getWhatToShow(idapp, req.user.username);
let data = await MyGroup.findOne({idapp, groupname}, whatshow).lean();
let cities = [];
if (data) {
cities = await MyGroup.extractCitiesName(idapp, data._id);
if (cities && cities.length > 0) {
cities = cities[0].mycities;
}
}
data = await getGroupRecAdminsInfo(idapp, data);
const whatshowUsers = await User.getWhatToShow_IfFriends(idapp, req.user.username);
2022-02-03 00:33:15 +01:00
const users_in_group = await User.find(
{
idapp,
'profile.mygroups': {
$elemMatch: {groupname: {$eq: groupname}},
},
},
whatshowUsers,
).lean();
2022-02-03 00:33:15 +01:00
res.send({mygroup: data, users_in_group, cities});
} catch (e) {
console.error('Error in MyGroups', e);
2022-02-03 00:33:15 +01:00
return res.status(400).send(e);
}
const ris = null;
2022-02-03 00:33:15 +01:00
});
module.exports = router;