import * as plugins from '../../plugins.js'; import { EmailService } from './classes.emailservice.js'; import { logger } from '../../logger.js'; import { Email, type IEmailOptions, type IAttachment } from '../core/classes.email.js'; export class ApiManager { public emailRef: EmailService; public typedRouter = new plugins.typedrequest.TypedRouter(); constructor(emailRefArg: EmailService) { this.emailRef = emailRefArg; this.emailRef.typedrouter.addTypedRouter(this.typedRouter); // Register API endpoints this.registerApiEndpoints(); } /** * Register API endpoints for email functionality */ private registerApiEndpoints() { // Register the SendEmail endpoint this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler('sendEmail', async (requestData) => { // Build attachments array const attachments: IAttachment[] = []; if (requestData.attachments) { for (const attachment of requestData.attachments) { attachments.push({ filename: attachment.name, content: Buffer.from(attachment.binaryAttachmentString, 'binary'), contentType: 'application/octet-stream' }); } } // Create Email instance const emailOptions: IEmailOptions = { from: requestData.from, to: requestData.to, subject: requestData.title, text: requestData.body, attachments }; const mailToSend = new Email(emailOptions); // Send email through the service which will route to the appropriate connector const emailId = await this.emailRef.sendEmail(mailToSend, undefined, {}); logger.log( 'info', `sent an email to ${requestData.to} with subject '${mailToSend.subject}'`, { eventType: 'sentEmail', email: { to: requestData.to, subject: mailToSend.subject, }, } ); return { responseId: emailId, }; }) ); // Add endpoint to check email status this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler('checkEmailStatus', async (requestData) => { // Check if we can get status - temporarily disabled during transition // Simplified response during migration const detailedStatus = { status: 'UNKNOWN', details: { message: 'Email status checking is not available during system migration' } }; // Convert to the expected API response format const apiResponse: plugins.servezoneInterfaces.platformservice.mta.IReq_CheckEmailStatus['response'] = { status: detailedStatus.status.toString(), // Convert enum to string details: { message: detailedStatus.details?.message || `Status: ${detailedStatus.status}` } }; return apiResponse; }) ); // Add statistics endpoint this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler('getEmailStats', async () => { return this.emailRef.getStats(); }) ); } }