Files
balikci/backend/src/controllers/stats.controller.js

104 lines
2.5 KiB
JavaScript
Raw Normal View History

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;