first commit: Complete phishing test management panel with Node.js backend and React frontend
This commit is contained in:
103
backend/src/controllers/stats.controller.js
Normal file
103
backend/src/controllers/stats.controller.js
Normal file
@@ -0,0 +1,103 @@
|
||||
const { Company, TrackingToken, ClickLog, sequelize } = require('../models');
|
||||
|
||||
// Dashboard stats
|
||||
exports.getDashboardStats = async (req, res, next) => {
|
||||
try {
|
||||
// Get overall stats
|
||||
const totalCompanies = await Company.count();
|
||||
const totalTokens = await TrackingToken.count();
|
||||
const clickedTokens = await TrackingToken.count({ where: { clicked: true } });
|
||||
const totalClicks = await TrackingToken.sum('click_count') || 0;
|
||||
|
||||
const clickRate = totalTokens > 0 ? ((clickedTokens / totalTokens) * 100).toFixed(2) : 0;
|
||||
|
||||
// Get today's activity
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
const todayClicks = await ClickLog.count({
|
||||
where: {
|
||||
clicked_at: {
|
||||
[sequelize.Sequelize.Op.gte]: today,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Get company-based summary
|
||||
const companyStats = await Company.findAll({
|
||||
attributes: ['id', 'name', 'industry', 'total_tokens', 'total_clicks', 'click_rate'],
|
||||
order: [['total_clicks', 'DESC']],
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
overview: {
|
||||
total_companies: totalCompanies,
|
||||
total_tokens: totalTokens,
|
||||
clicked_tokens: clickedTokens,
|
||||
total_clicks: parseInt(totalClicks),
|
||||
click_rate: parseFloat(clickRate),
|
||||
today_clicks: todayClicks,
|
||||
},
|
||||
top_companies: companyStats,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Recent clicks
|
||||
exports.getRecentClicks = async (req, res, next) => {
|
||||
try {
|
||||
const { limit = 20 } = req.query;
|
||||
|
||||
const clicks = await ClickLog.findAll({
|
||||
include: [
|
||||
{
|
||||
model: TrackingToken,
|
||||
as: 'token',
|
||||
attributes: ['target_email', 'employee_name', 'company_id'],
|
||||
include: [
|
||||
{
|
||||
model: Company,
|
||||
as: 'company',
|
||||
attributes: ['name', 'industry'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
order: [['clicked_at', 'DESC']],
|
||||
limit: parseInt(limit),
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: clicks,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Company-based stats for charts
|
||||
exports.getCompanyBasedStats = async (req, res, next) => {
|
||||
try {
|
||||
const companies = await Company.findAll({
|
||||
attributes: ['id', 'name', 'total_tokens', 'total_clicks', 'click_rate'],
|
||||
order: [['name', 'ASC']],
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: companies,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = exports;
|
||||
|
||||
Reference in New Issue
Block a user