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

66 lines
1.7 KiB
JavaScript

import prisma from "~/server/utils/prisma";
export default defineEventHandler(async (event) => {
try {
const query = getQuery(event);
const limit = parseInt(query.limit) || 10;
// Fetch recent notifications with related data
const recentNotifications = await prisma.notifications.findMany({
take: limit,
orderBy: {
created_at: 'desc'
},
include: {
notification_categories: {
select: {
name: true,
value: true
}
},
notification_channels: {
select: {
channel_type: true
}
},
_count: {
select: {
notification_recipients: true
}
}
}
});
// Format the response
const formatted = recentNotifications.map(notif => ({
id: notif.id,
title: notif.title,
type: notif.type,
priority: notif.priority,
status: notif.status,
category: notif.notification_categories?.name || 'Unknown',
channels: notif.notification_channels.map(ch => ch.channel_type),
recipientCount: notif._count.notification_recipients,
estimatedReach: notif.estimated_reach,
actualSent: notif.actual_sent,
createdAt: notif.created_at,
sentAt: notif.sent_at,
scheduledAt: notif.scheduled_at,
createdBy: notif.created_by,
}));
return {
success: true,
data: formatted
};
} catch (error) {
console.error("Error fetching recent notifications:", error);
throw createError({
statusCode: 500,
statusMessage: "Failed to fetch recent notifications",
data: { error: error.message },
});
} finally {
}
});