Update various configuration files, components, and assets; enhance notification system and API endpoints; improve documentation and styles across the application.
This commit is contained in:
82
server/api/notifications/delivery/email-config.get.js
Normal file
82
server/api/notifications/delivery/email-config.get.js
Normal file
@@ -0,0 +1,82 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Get all email provider configurations
|
||||
const emailConfigs = await prisma.notification_delivery_config.findMany({
|
||||
where: {
|
||||
channel_type: 'email'
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
is_enabled: true,
|
||||
provider: true,
|
||||
provider_config: true,
|
||||
status: true,
|
||||
success_rate: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!emailConfigs || emailConfigs.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
providers: [],
|
||||
activeProvider: null
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Convert to provider-keyed object
|
||||
const providersData = {};
|
||||
emailConfigs.forEach(config => {
|
||||
providersData[config.provider.toLowerCase().replace(/\s+/g, '-')] = {
|
||||
enabled: config.is_enabled,
|
||||
provider: config.provider,
|
||||
status: config.status,
|
||||
successRate: config.success_rate,
|
||||
config: config.provider_config
|
||||
};
|
||||
});
|
||||
|
||||
// Find active provider (fallback to first enabled or first in list)
|
||||
const activeConfig = emailConfigs.find(c => c.is_enabled) || emailConfigs[0];
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
providers: providersData,
|
||||
activeProvider: activeConfig.provider.toLowerCase().replace(/\s+/g, '-'),
|
||||
// For backward compatibility
|
||||
enabled: activeConfig.is_enabled,
|
||||
provider: activeConfig.provider.toLowerCase().replace(/\s+/g, '-'),
|
||||
status: activeConfig.status,
|
||||
successRate: activeConfig.success_rate,
|
||||
config: activeConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching email configuration:', error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to fetch email configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
118
server/api/notifications/delivery/email-config.put.js
Normal file
118
server/api/notifications/delivery/email-config.put.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from "~/server/utils/prisma";
|
||||
import { readBody } from 'h3';
|
||||
|
||||
const emailConfigSchema = z.object({
|
||||
enabled: z.boolean(),
|
||||
provider: z.string(),
|
||||
config: z.record(z.any()).optional()
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
const userId = user?.userID;
|
||||
if (!userId) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required (no user id)",
|
||||
});
|
||||
}
|
||||
const now = new Date();
|
||||
|
||||
// Validate request body
|
||||
const body = emailConfigSchema.parse(await readBody(event));
|
||||
|
||||
// Normalize provider name for database lookup
|
||||
const providerName = body.provider === 'mailtrap' ? 'Mailtrap' :
|
||||
body.provider === 'aws-ses' ? 'AWS SES' :
|
||||
body.provider;
|
||||
|
||||
// If enabling this provider, disable all others for this channel
|
||||
if (body.enabled) {
|
||||
await prisma.notification_delivery_config.updateMany({
|
||||
where: {
|
||||
channel_type: 'email',
|
||||
provider: { not: providerName }
|
||||
},
|
||||
data: {
|
||||
is_enabled: false,
|
||||
updated_at: now,
|
||||
updated_by: userId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Check if config already exists for this provider
|
||||
const existingConfig = await prisma.notification_delivery_config.findFirst({
|
||||
where: {
|
||||
channel_type: 'email',
|
||||
provider: providerName
|
||||
}
|
||||
});
|
||||
|
||||
let emailConfig;
|
||||
if (existingConfig) {
|
||||
// Update existing config
|
||||
emailConfig = await prisma.notification_delivery_config.update({
|
||||
where: {
|
||||
id: existingConfig.id
|
||||
},
|
||||
data: {
|
||||
is_enabled: body.enabled,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
updated_at: now,
|
||||
updated_by: userId
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Create new config
|
||||
emailConfig = await prisma.notification_delivery_config.create({
|
||||
data: {
|
||||
channel_type: 'email',
|
||||
is_enabled: body.enabled,
|
||||
provider: providerName,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
success_rate: 0,
|
||||
created_by: userId,
|
||||
updated_by: userId,
|
||||
updated_at: now
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: emailConfig.is_enabled,
|
||||
provider: emailConfig.provider,
|
||||
status: emailConfig.status,
|
||||
successRate: emailConfig.success_rate,
|
||||
config: emailConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating email configuration:', error);
|
||||
|
||||
if (error instanceof z.ZodError) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request data',
|
||||
data: error.errors
|
||||
});
|
||||
}
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update email configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
65
server/api/notifications/delivery/push-config.get.js
Normal file
65
server/api/notifications/delivery/push-config.get.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Get push notification configuration
|
||||
const pushConfig = await prisma.notification_delivery_config.findFirst({
|
||||
where: {
|
||||
channel_type: 'push'
|
||||
},
|
||||
select: {
|
||||
is_enabled: true,
|
||||
provider: true,
|
||||
provider_config: true,
|
||||
status: true,
|
||||
success_rate: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!pushConfig) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: false,
|
||||
provider: 'firebase',
|
||||
status: 'Not Configured',
|
||||
successRate: 0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: pushConfig.is_enabled,
|
||||
provider: pushConfig.provider,
|
||||
status: pushConfig.status,
|
||||
successRate: pushConfig.success_rate,
|
||||
config: pushConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching push configuration:', error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to fetch push configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
80
server/api/notifications/delivery/push-config.put.js
Normal file
80
server/api/notifications/delivery/push-config.put.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
const pushConfigSchema = z.object({
|
||||
enabled: z.boolean(),
|
||||
provider: z.string(),
|
||||
config: z.record(z.any()).optional()
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Validate request body
|
||||
const body = await readValidatedBody(event, pushConfigSchema.parse);
|
||||
|
||||
// Update or create push notification configuration
|
||||
const pushConfig = await prisma.notification_delivery_config.upsert({
|
||||
where: {
|
||||
channel_type: 'push'
|
||||
},
|
||||
update: {
|
||||
is_enabled: body.enabled,
|
||||
provider: body.provider,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
updated_at: new Date(),
|
||||
updated_by: user.id
|
||||
},
|
||||
create: {
|
||||
channel_type: 'push',
|
||||
is_enabled: body.enabled,
|
||||
provider: body.provider,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
success_rate: 0,
|
||||
created_by: user.id,
|
||||
updated_by: user.id
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: pushConfig.is_enabled,
|
||||
provider: pushConfig.provider,
|
||||
status: pushConfig.status,
|
||||
successRate: pushConfig.success_rate,
|
||||
config: pushConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating push configuration:', error);
|
||||
|
||||
if (error instanceof z.ZodError) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request data',
|
||||
data: error.errors
|
||||
});
|
||||
}
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update push configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
66
server/api/notifications/delivery/settings.get.js
Normal file
66
server/api/notifications/delivery/settings.get.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Get delivery settings
|
||||
const settings = await prisma.notification_delivery_settings.findFirst({
|
||||
select: {
|
||||
auto_retry: true,
|
||||
enable_fallback: true,
|
||||
max_retries: true,
|
||||
retry_delay: true,
|
||||
priority: true,
|
||||
enable_reports: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!settings) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
autoRetry: true,
|
||||
enableFallback: true,
|
||||
maxRetries: 3,
|
||||
retryDelay: 30,
|
||||
priority: 'normal',
|
||||
enableReports: true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
autoRetry: settings.auto_retry,
|
||||
enableFallback: settings.enable_fallback,
|
||||
maxRetries: settings.max_retries,
|
||||
retryDelay: settings.retry_delay,
|
||||
priority: settings.priority,
|
||||
enableReports: settings.enable_reports
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching delivery settings:', error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to fetch delivery settings'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
87
server/api/notifications/delivery/settings.put.js
Normal file
87
server/api/notifications/delivery/settings.put.js
Normal file
@@ -0,0 +1,87 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
autoRetry: z.boolean(),
|
||||
enableFallback: z.boolean(),
|
||||
maxRetries: z.number().int().min(0).max(5),
|
||||
retryDelay: z.number().int().min(1).max(300),
|
||||
priority: z.enum(['low', 'normal', 'high']),
|
||||
enableReports: z.boolean()
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Validate request body
|
||||
const body = await readValidatedBody(event, settingsSchema.parse);
|
||||
|
||||
// Update or create delivery settings
|
||||
const settings = await prisma.notification_delivery_settings.upsert({
|
||||
where: {
|
||||
id: 1 // Only one settings record
|
||||
},
|
||||
update: {
|
||||
auto_retry: body.autoRetry,
|
||||
enable_fallback: body.enableFallback,
|
||||
max_retries: body.maxRetries,
|
||||
retry_delay: body.retryDelay,
|
||||
priority: body.priority,
|
||||
enable_reports: body.enableReports,
|
||||
updated_at: new Date(),
|
||||
updated_by: user.id
|
||||
},
|
||||
create: {
|
||||
id: 1,
|
||||
auto_retry: body.autoRetry,
|
||||
enable_fallback: body.enableFallback,
|
||||
max_retries: body.maxRetries,
|
||||
retry_delay: body.retryDelay,
|
||||
priority: body.priority,
|
||||
enable_reports: body.enableReports,
|
||||
created_by: user.id,
|
||||
updated_by: user.id
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
autoRetry: settings.auto_retry,
|
||||
enableFallback: settings.enable_fallback,
|
||||
maxRetries: settings.max_retries,
|
||||
retryDelay: settings.retry_delay,
|
||||
priority: settings.priority,
|
||||
enableReports: settings.enable_reports
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating delivery settings:', error);
|
||||
|
||||
if (error instanceof z.ZodError) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request data',
|
||||
data: error.errors
|
||||
});
|
||||
}
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update delivery settings'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
54
server/api/notifications/delivery/sms-config.get.js
Normal file
54
server/api/notifications/delivery/sms-config.get.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
const smsConfig = await prisma.notification_delivery_config.findFirst({
|
||||
where: { channel_type: 'sms' },
|
||||
select: {
|
||||
is_enabled: true,
|
||||
provider: true,
|
||||
provider_config: true,
|
||||
status: true,
|
||||
success_rate: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
if (!smsConfig) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: false,
|
||||
provider: 'twilio',
|
||||
status: 'Not Configured',
|
||||
successRate: 0
|
||||
}
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: smsConfig.is_enabled,
|
||||
provider: smsConfig.provider,
|
||||
status: smsConfig.status,
|
||||
successRate: smsConfig.success_rate,
|
||||
config: smsConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching SMS configuration:', error);
|
||||
if (error.statusCode) throw error;
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to fetch SMS configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
67
server/api/notifications/delivery/sms-config.put.js
Normal file
67
server/api/notifications/delivery/sms-config.put.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
const smsConfigSchema = z.object({
|
||||
enabled: z.boolean(),
|
||||
provider: z.string(),
|
||||
config: z.record(z.any()).optional()
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
const body = await readValidatedBody(event, smsConfigSchema.parse);
|
||||
const smsConfig = await prisma.notification_delivery_config.upsert({
|
||||
where: { channel_type: 'sms' },
|
||||
update: {
|
||||
is_enabled: body.enabled,
|
||||
provider: body.provider,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
updated_at: new Date(),
|
||||
updated_by: user.id
|
||||
},
|
||||
create: {
|
||||
channel_type: 'sms',
|
||||
is_enabled: body.enabled,
|
||||
provider: body.provider,
|
||||
provider_config: body.config || {},
|
||||
status: body.enabled ? 'Connected' : 'Disabled',
|
||||
success_rate: 0,
|
||||
created_by: user.id,
|
||||
updated_by: user.id
|
||||
}
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
enabled: smsConfig.is_enabled,
|
||||
provider: smsConfig.provider,
|
||||
status: smsConfig.status,
|
||||
successRate: smsConfig.success_rate,
|
||||
config: smsConfig.provider_config
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating SMS configuration:', error);
|
||||
if (error instanceof z.ZodError) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid request data',
|
||||
data: error.errors
|
||||
});
|
||||
}
|
||||
if (error.statusCode) throw error;
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update SMS configuration'
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
85
server/api/notifications/delivery/stats.get.js
Normal file
85
server/api/notifications/delivery/stats.get.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import { prisma } from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current user from auth middleware
|
||||
const user = event.context.user;
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
// Get delivery statistics from the last 30 days
|
||||
const thirtyDaysAgo = new Date();
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
|
||||
|
||||
// Get email statistics
|
||||
const emailStats = await prisma.notification_delivery.groupBy({
|
||||
by: ['is_success'],
|
||||
where: {
|
||||
channel_type: 'email',
|
||||
created_at: {
|
||||
gte: thirtyDaysAgo
|
||||
}
|
||||
},
|
||||
_count: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
// Get push notification statistics
|
||||
const pushStats = await prisma.notification_delivery.groupBy({
|
||||
by: ['is_success'],
|
||||
where: {
|
||||
channel_type: 'push',
|
||||
created_at: {
|
||||
gte: thirtyDaysAgo
|
||||
}
|
||||
},
|
||||
_count: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate totals
|
||||
const emailCount = emailStats.reduce((sum, stat) => sum + stat._count.id, 0);
|
||||
const pushCount = pushStats.reduce((sum, stat) => sum + stat._count.id, 0);
|
||||
const emailSuccess = emailStats.find(stat => stat.is_success)?._count.id || 0;
|
||||
const pushSuccess = pushStats.find(stat => stat.is_success)?._count.id || 0;
|
||||
|
||||
// Calculate success rate
|
||||
const totalDeliveries = emailCount + pushCount;
|
||||
const totalSuccessful = emailSuccess + pushSuccess;
|
||||
const successRate = totalDeliveries > 0
|
||||
? (totalSuccessful / totalDeliveries) * 100
|
||||
: 100;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
emailsSent: emailCount,
|
||||
pushSent: pushCount,
|
||||
successRate: Number(successRate.toFixed(2)),
|
||||
failed: totalDeliveries - totalSuccessful
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching delivery stats:', {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
meta: error.meta,
|
||||
stack: error.stack
|
||||
});
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: `Failed to fetch delivery statistics: ${error.message}`
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user