102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
'use server';
|
|
|
|
import { getEmails } from '@/lib/services/email-service';
|
|
import { EmailMessage, EmailContent } from '@/types/email';
|
|
import { formatEmailForReplyOrForward } from '@/lib/utils/email-utils';
|
|
|
|
/**
|
|
* Server action to fetch emails
|
|
*/
|
|
export async function fetchEmails(userId: string, folder = 'INBOX', page = 1, perPage = 20, searchQuery = '') {
|
|
try {
|
|
const result = await getEmails(userId, folder, page, perPage, searchQuery);
|
|
return { success: true, data: result };
|
|
} catch (error) {
|
|
console.error('Error fetching emails:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Failed to fetch emails'
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Server action to format email for reply or forward operations
|
|
* Uses the centralized email formatter
|
|
*/
|
|
export async function formatEmailServerSide(
|
|
email: {
|
|
id: string;
|
|
from: string;
|
|
fromName?: string;
|
|
to: string;
|
|
subject: string;
|
|
content: string;
|
|
cc?: string;
|
|
date: string;
|
|
},
|
|
type: 'reply' | 'reply-all' | 'forward'
|
|
) {
|
|
try {
|
|
// Convert the client email format to the server EmailMessage format
|
|
const serverEmail: EmailMessage = {
|
|
id: email.id,
|
|
subject: email.subject,
|
|
from: email.from,
|
|
to: email.to,
|
|
cc: email.cc,
|
|
date: email.date,
|
|
flags: [],
|
|
content: {
|
|
text: email.content,
|
|
isHtml: email.content.includes('<'),
|
|
direction: 'ltr'
|
|
}
|
|
};
|
|
|
|
// Use the centralized formatter
|
|
const formatted = formatEmailForReplyOrForward(serverEmail, type);
|
|
|
|
return {
|
|
success: true,
|
|
data: formatted
|
|
};
|
|
} catch (error) {
|
|
console.error('Error formatting email:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Failed to format email'
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send an email from the server
|
|
*/
|
|
export async function sendEmailServerSide(
|
|
userId: string,
|
|
emailData: {
|
|
to: string;
|
|
cc?: string;
|
|
bcc?: string;
|
|
subject: string;
|
|
body: string;
|
|
attachments?: Array<{
|
|
name: string;
|
|
content: string;
|
|
type: string;
|
|
}>;
|
|
}
|
|
) {
|
|
try {
|
|
const { sendEmail } = await import('@/lib/services/email-service');
|
|
const result = await sendEmail(userId, emailData);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error sending email:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Failed to send email'
|
|
};
|
|
}
|
|
}
|