Files
Nas-Notification/server/api/notifications/dashboard/channel-performance.get.js

77 lines
2.0 KiB
JavaScript

import prisma from "~/server/utils/prisma";
export default defineEventHandler(async (event) => {
try {
const now = new Date();
const last30Days = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
// Get all channel types
const channelTypes = ['email', 'push', 'sms'];
const channelPerformance = [];
for (const channelType of channelTypes) {
// Get stats for this channel
const [totalSent, successful, failed, pending] = await Promise.all([
prisma.notification_recipients.count({
where: {
channel_type: channelType,
created_at: { gte: last30Days }
}
}),
prisma.notification_recipients.count({
where: {
channel_type: channelType,
status: 'sent',
created_at: { gte: last30Days }
}
}),
prisma.notification_recipients.count({
where: {
channel_type: channelType,
status: 'failed',
created_at: { gte: last30Days }
}
}),
prisma.notification_recipients.count({
where: {
channel_type: channelType,
status: 'pending',
created_at: { gte: last30Days }
}
}),
]);
const successRate = totalSent > 0
? ((successful / totalSent) * 100).toFixed(1)
: 0;
const failureRate = totalSent > 0
? ((failed / totalSent) * 100).toFixed(1)
: 0;
channelPerformance.push({
channel: channelType,
totalSent,
successful,
failed,
pending,
successRate: parseFloat(successRate),
failureRate: parseFloat(failureRate),
});
}
return {
success: true,
data: channelPerformance
};
} catch (error) {
console.error("Error fetching channel performance:", error);
throw createError({
statusCode: 500,
statusMessage: "Failed to fetch channel performance",
data: { error: error.message },
});
} finally {
}
});