import * as plugins from '../../plugins.js'; import { EmailService } from './classes.emailservice.js'; import { logger } from '../../logger.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) => { const mailToSend = new plugins.smartmail.Smartmail({ body: requestData.body, from: requestData.from, subject: requestData.title, }); if (requestData.attachments) { for (const attachment of requestData.attachments) { mailToSend.addAttachment( await plugins.smartfile.SmartFile.fromString( attachment.name, attachment.binaryAttachmentString, 'binary' ) ); } } // Send email through the service which will route to the appropriate connector const emailId = await this.emailRef.sendEmail(mailToSend, requestData.to, {}); logger.log( 'info', `sent an email to ${requestData.to} with subject '${mailToSend.getSubject()}'`, { eventType: 'sentEmail', email: { to: requestData.to, subject: mailToSend.getSubject(), }, } ); 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(); }) ); } }