You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
// admin.js
|
|
// Copyright (C) 2024 Digital Telepresence, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import path, { dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line
|
|
|
|
import mongoose from 'mongoose';
|
|
const User = mongoose.model('User');
|
|
const ChatRoom = mongoose.model('ChatRoom');
|
|
const ChatMessage = mongoose.model('ChatMessage');
|
|
const ChatImage = mongoose.model('Image');
|
|
const Video = mongoose.model('Video');
|
|
|
|
import express from 'express';
|
|
|
|
import { SiteController, SiteError } from '../../lib/site-lib.js';
|
|
|
|
export default class AdminController extends SiteController {
|
|
|
|
static get name ( ) { return 'AdminController'; }
|
|
static get slug ( ) { return 'admin'; }
|
|
|
|
constructor (dtp) {
|
|
super(dtp, AdminController);
|
|
}
|
|
|
|
async start ( ) {
|
|
const { session: sessionService } = this.dtp.services;
|
|
|
|
const router = express.Router();
|
|
this.dtp.app.use('/admin', router);
|
|
|
|
const authRequired = sessionService.authCheckMiddleware({ requireLogin: true, requireAdmin: true });
|
|
router.use(authRequired);
|
|
|
|
router.use('/user', await this.loadChild(path.join(__dirname, 'admin', 'user.js')));
|
|
|
|
router.get(
|
|
'/',
|
|
this.getDashboard.bind(this),
|
|
);
|
|
|
|
return router;
|
|
}
|
|
|
|
async getDashboard (req, res, next) {
|
|
const { user: userService } = this.dtp.services;
|
|
try {
|
|
res.locals.currentView = 'admin';
|
|
|
|
res.locals.stats = {
|
|
userCount: await User.estimatedDocumentCount(),
|
|
chatRoomCount: await ChatRoom.estimatedDocumentCount(),
|
|
chatMessageCount: await ChatMessage.estimatedDocumentCount(),
|
|
imageCount: await ChatImage.estimatedDocumentCount(),
|
|
videoCount: await Video.estimatedDocumentCount(),
|
|
};
|
|
|
|
res.locals.latestSignups = await userService.getLatestSignups(10);
|
|
|
|
res.render('admin/dashboard');
|
|
} catch (error) {
|
|
this.error.log('failed to present the admin dashboard', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
}
|