Skip to content

Commit

Permalink
Merge pull request #19 from jhotmann/small-improvements
Browse files Browse the repository at this point in the history
Fix group count on admin page
  • Loading branch information
jhotmann authored Oct 14, 2021
2 parents 0dab05a + 7b3f627 commit 4294ae5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ const app = express();

const nunjucksEnv = nunjucks.configure('views', { express: app, autoescape: true });
nunjucksEnv.addFilter('arrayFilter', (arr, prop, value) => arr.filter((a) => a[prop] === value));
nunjucksEnv.addFilter('arrayFilterPropIncludes', (arr, prop, subprop, value) => {
if (subprop && !value) {
return arr.filter((a) => a[prop].includes(subprop));
}
if (subprop && value) {
return arr.filter((a) => a[prop].find((p) => p[subprop] === value));
}
return [];
});

if (process.env.BEHIND_PROXY === 'true') app.set('trust proxy', true);

Expand Down
2 changes: 1 addition & 1 deletion routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const db = require('../src/database');

const router = express.Router();

router.get('/', db.mwUser, db.mwAllUsers, db.mwAllReg, db.mwAllDevices, async (req, res) => {
router.get('/', db.mwUser, db.mwAllUsers, db.mwAllReg, db.mwAllDevices, db.mwAllGroups, async (req, res) => {
req.pageData.baseUrl = `${req.protocol}://${req.hostname}${req.hostname === 'localhost' ? ':8000' : ''}`;
res.render('admin.html', req.pageData);
});
Expand Down
15 changes: 15 additions & 0 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ module.exports.createGroup = async (name, userData) => {
}
};

module.exports.getAllGroups = async () => {
try {
return await db.groups.find({});
} catch {
return [];
}
};

// Devices Middleware - depends upon mwUser
module.exports.mwAllGroups = async (req, res, next) => {
if (!req.pageData) req.pageData = {};
req.pageData.allGroups = await this.getAllGroups();
next();
};

module.exports.getGroup = async (_id) => {
try {
return await db.groups.findOne({ _id });
Expand Down
2 changes: 1 addition & 1 deletion views/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h2>Users</h2>
<th scope="row">{{ user.username }}</th>
<td>{{ allDevices | arrayFilter('userId', user._id) | length }}</td>
<td>{{ user.friends.length }}</td>
<td>{{ user.groups.length }}</td>
<td>{{ allGroups | arrayFilterPropIncludes('members', 'userId', user._id) | length }}</td>
<td>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="{{ user._id }}-admin-switch" value="{{ user._id }}"{% if user.isAdmin %} checked{% endif %}>
Expand Down

0 comments on commit 4294ae5

Please sign in to comment.