first commit: Complete phishing test management panel with Node.js backend and React frontend
This commit is contained in:
239
backend/src/controllers/token.controller.js
Normal file
239
backend/src/controllers/token.controller.js
Normal file
@@ -0,0 +1,239 @@
|
||||
const { TrackingToken, Company, ClickLog } = require('../models');
|
||||
const tokenService = require('../services/token.service');
|
||||
const logger = require('../config/logger');
|
||||
|
||||
// Get all tokens
|
||||
exports.getAllTokens = async (req, res, next) => {
|
||||
try {
|
||||
const { company_id, limit = 50, offset = 0 } = req.query;
|
||||
|
||||
const where = {};
|
||||
if (company_id) {
|
||||
where.company_id = company_id;
|
||||
}
|
||||
|
||||
const tokens = await TrackingToken.findAll({
|
||||
where,
|
||||
include: [{ model: Company, as: 'company', attributes: ['id', 'name', 'industry'] }],
|
||||
order: [['created_at', 'DESC']],
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
});
|
||||
|
||||
const total = await TrackingToken.count({ where });
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: tokens,
|
||||
pagination: {
|
||||
total,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
hasMore: parseInt(offset) + parseInt(limit) < total,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Get token by ID
|
||||
exports.getTokenById = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const token = await TrackingToken.findByPk(id, {
|
||||
include: [{ model: Company, as: 'company' }],
|
||||
});
|
||||
|
||||
if (!token) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Token not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: token,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Create token (without sending mail)
|
||||
exports.createToken = async (req, res, next) => {
|
||||
try {
|
||||
const { company_id, target_email, employee_name, template_type } = req.body;
|
||||
|
||||
const token = await tokenService.createToken({
|
||||
company_id,
|
||||
target_email,
|
||||
employee_name,
|
||||
template_type,
|
||||
});
|
||||
|
||||
const trackingUrl = `${process.env.BASE_URL}/t/${token.token}`;
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Token created successfully',
|
||||
data: {
|
||||
...token.toJSON(),
|
||||
tracking_url: trackingUrl,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Create token and send mail
|
||||
exports.createAndSendToken = async (req, res, next) => {
|
||||
try {
|
||||
const { company_id, target_email, employee_name, template_type } = req.body;
|
||||
|
||||
// Create token
|
||||
const token = await tokenService.createToken({
|
||||
company_id,
|
||||
target_email,
|
||||
employee_name,
|
||||
template_type,
|
||||
});
|
||||
|
||||
// Send mail
|
||||
try {
|
||||
await tokenService.sendMail(token.id);
|
||||
} catch (mailError) {
|
||||
logger.error('Failed to send mail:', mailError);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: 'Token created but failed to send mail',
|
||||
details: mailError.message,
|
||||
token_id: token.id,
|
||||
});
|
||||
}
|
||||
|
||||
const trackingUrl = `${process.env.BASE_URL}/t/${token.token}`;
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Token created and mail sent successfully',
|
||||
data: {
|
||||
...token.toJSON(),
|
||||
tracking_url: trackingUrl,
|
||||
mail_sent: true,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Send mail for existing token
|
||||
exports.sendTokenMail = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
await tokenService.sendMail(id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Mail sent successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Update token
|
||||
exports.updateToken = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { notes } = req.body;
|
||||
|
||||
const token = await TrackingToken.findByPk(id);
|
||||
|
||||
if (!token) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Token not found',
|
||||
});
|
||||
}
|
||||
|
||||
await token.update({ notes });
|
||||
|
||||
logger.info(`Token updated: ${id}`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Token updated successfully',
|
||||
data: token,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Delete token
|
||||
exports.deleteToken = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const token = await TrackingToken.findByPk(id);
|
||||
|
||||
if (!token) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Token not found',
|
||||
});
|
||||
}
|
||||
|
||||
const companyId = token.company_id;
|
||||
|
||||
await token.destroy();
|
||||
|
||||
// Update company stats
|
||||
await tokenService.updateCompanyStats(companyId);
|
||||
|
||||
logger.info(`Token deleted: ${id}`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Token deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
// Get token click logs
|
||||
exports.getTokenClicks = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const token = await TrackingToken.findByPk(id);
|
||||
|
||||
if (!token) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Token not found',
|
||||
});
|
||||
}
|
||||
|
||||
const clicks = await ClickLog.findAll({
|
||||
where: { token_id: id },
|
||||
order: [['clicked_at', 'DESC']],
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: clicks,
|
||||
count: clicks.length,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user