86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
export default defineEventHandler(async (event) => {
|
|
const query = getQuery(event)
|
|
const page = parseInt(query.page) || 1
|
|
const limit = parseInt(query.limit) || 10
|
|
const status = query.status
|
|
const type = query.type
|
|
|
|
try {
|
|
const where = {
|
|
type: 'bulk',
|
|
}
|
|
|
|
if (status) {
|
|
where.status = status
|
|
}
|
|
|
|
if (type) {
|
|
where.content_type = type
|
|
}
|
|
|
|
const [batches, total] = await prisma.$transaction([
|
|
prisma.notifications.findMany({
|
|
where,
|
|
orderBy: {
|
|
created_at: 'desc'
|
|
},
|
|
skip: (page - 1) * limit,
|
|
take: limit,
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
status: true,
|
|
type: true,
|
|
priority: true,
|
|
delivery_type: true,
|
|
scheduled_at: true,
|
|
created_at: true,
|
|
actual_sent: true,
|
|
estimated_reach: true,
|
|
notification_recipients: {
|
|
select: {
|
|
status: true,
|
|
_count: true
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
prisma.notifications.count({ where })
|
|
])
|
|
|
|
// Calculate processed count and format response
|
|
const formattedBatches = batches.map(batch => {
|
|
const processed = batch.notification_recipients.reduce((acc, curr) => {
|
|
return acc + (curr.status === 'sent' || curr.status === 'delivered' ? curr._count : 0)
|
|
}, 0)
|
|
|
|
return {
|
|
id: batch.id,
|
|
name: batch.title,
|
|
description: `${batch.type} notification - ${batch.priority} priority`,
|
|
status: batch.status,
|
|
processed,
|
|
total: batch.estimated_reach,
|
|
time: batch.created_at,
|
|
delivery_type: batch.delivery_type,
|
|
scheduled_at: batch.scheduled_at
|
|
}
|
|
})
|
|
|
|
return {
|
|
batches: formattedBatches,
|
|
pagination: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching batches:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to fetch batches'
|
|
})
|
|
}
|
|
})
|