dcrouter/ts/mail/services/classes.apimanager.ts

100 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-05-08 01:13:54 +00:00
import * as plugins from '../../plugins.js';
import { EmailService } from './classes.emailservice.js';
2025-05-08 01:13:54 +00:00
import { logger } from '../../logger.js';
import { Email, type IEmailOptions, type IAttachment } from '../core/classes.email.js';
2024-02-15 20:30:38 +01:00
export class ApiManager {
2024-02-16 13:28:40 +01:00
public emailRef: EmailService;
2024-02-15 20:30:38 +01:00
public typedRouter = new plugins.typedrequest.TypedRouter();
2024-02-16 13:28:40 +01:00
constructor(emailRefArg: EmailService) {
2024-02-15 20:30:38 +01:00
this.emailRef = emailRefArg;
2024-02-16 13:28:40 +01:00
this.emailRef.typedrouter.addTypedRouter(this.typedRouter);
// Register API endpoints
this.registerApiEndpoints();
}
/**
* Register API endpoints for email functionality
*/
private registerApiEndpoints() {
// Register the SendEmail endpoint
2025-05-07 14:33:20 +00:00
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_SendEmail>(
2024-02-15 20:30:38 +01:00
new plugins.typedrequest.TypedHandler('sendEmail', async (requestData) => {
// Build attachments array
const attachments: IAttachment[] = [];
2024-02-15 20:30:38 +01:00
if (requestData.attachments) {
for (const attachment of requestData.attachments) {
attachments.push({
filename: attachment.name,
content: Buffer.from(attachment.binaryAttachmentString, 'binary'),
contentType: 'application/octet-stream'
});
2024-02-15 20:30:38 +01:00
}
}
// Create Email instance
const emailOptions: IEmailOptions = {
from: requestData.from,
to: requestData.to,
subject: requestData.title,
text: requestData.body,
attachments
};
const mailToSend = new Email(emailOptions);
2024-02-15 20:30:38 +01:00
// Send email through the service which will route to the appropriate connector
const emailId = await this.emailRef.sendEmail(mailToSend, undefined, {});
2024-02-15 20:30:38 +01:00
logger.log(
'info',
`sent an email to ${requestData.to} with subject '${mailToSend.subject}'`,
2024-02-15 20:30:38 +01:00
{
eventType: 'sentEmail',
email: {
to: requestData.to,
subject: mailToSend.subject,
2024-02-15 20:30:38 +01:00
},
}
);
2024-02-15 20:30:38 +01:00
return {
responseId: emailId,
2024-02-15 20:30:38 +01:00
};
})
);
// Add endpoint to check email status
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_CheckEmailStatus>(
new plugins.typedrequest.TypedHandler('checkEmailStatus', async (requestData) => {
2025-05-21 00:12:49 +00:00
// 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'
}
};
2025-05-21 00:12:49 +00:00
// 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}`
}
};
2025-05-21 00:12:49 +00:00
return apiResponse;
})
);
// Add statistics endpoint
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IReq_GetEMailStats>(
new plugins.typedrequest.TypedHandler('getEmailStats', async () => {
return this.emailRef.getStats();
})
);
2024-02-15 20:30:38 +01:00
}
}