2024-02-16 13:28:40 +01:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import { EmailService } from './email.classes.emailservice.js';
|
|
|
|
import { logger } from '../logger.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);
|
2025-03-15 16:04:03 +00:00
|
|
|
|
|
|
|
// Register API endpoints
|
|
|
|
this.registerApiEndpoints();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register API endpoints for email functionality
|
|
|
|
*/
|
|
|
|
private registerApiEndpoints() {
|
|
|
|
// Register the SendEmail endpoint
|
2024-02-16 13:28:40 +01:00
|
|
|
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IRequest_SendEmail>(
|
2024-02-15 20:30:38 +01:00
|
|
|
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(
|
2024-02-16 13:28:40 +01:00
|
|
|
await plugins.smartfile.SmartFile.fromString(
|
2024-02-15 20:30:38 +01:00
|
|
|
attachment.name,
|
|
|
|
attachment.binaryAttachmentString,
|
|
|
|
'binary'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
// Send email through the service which will route to the appropriate connector
|
|
|
|
const emailId = await this.emailRef.sendEmail(mailToSend, requestData.to, {});
|
|
|
|
|
2024-02-15 20:30:38 +01:00
|
|
|
logger.log(
|
|
|
|
'info',
|
2025-03-15 16:04:03 +00:00
|
|
|
`sent an email to ${requestData.to} with subject '${mailToSend.getSubject()}'`,
|
2024-02-15 20:30:38 +01:00
|
|
|
{
|
|
|
|
eventType: 'sentEmail',
|
|
|
|
email: {
|
|
|
|
to: requestData.to,
|
|
|
|
subject: mailToSend.getSubject(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2025-03-15 16:04:03 +00:00
|
|
|
|
2024-02-15 20:30:38 +01:00
|
|
|
return {
|
2025-03-15 16:04:03 +00:00
|
|
|
responseId: emailId,
|
2024-02-15 20:30:38 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2025-03-15 16:04:03 +00:00
|
|
|
|
|
|
|
// Add endpoint to check email status
|
|
|
|
this.typedRouter.addTypedHandler<{ emailId: string }>(
|
|
|
|
new plugins.typedrequest.TypedHandler('checkEmailStatus', async (requestData) => {
|
|
|
|
// If MTA is enabled, use it to check status
|
|
|
|
if (this.emailRef.mtaConnector) {
|
|
|
|
const status = await this.emailRef.mtaConnector.checkEmailStatus(requestData.emailId);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For Mailgun, we don't have a status check implementation currently
|
|
|
|
return {
|
|
|
|
status: 'unknown',
|
|
|
|
details: { message: 'Status tracking not available for current provider' }
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add statistics endpoint
|
|
|
|
this.typedRouter.addTypedHandler<void>(
|
|
|
|
new plugins.typedrequest.TypedHandler('getEmailStats', async () => {
|
|
|
|
return this.emailRef.getStats();
|
|
|
|
})
|
|
|
|
);
|
2024-02-15 20:30:38 +01:00
|
|
|
}
|
2025-03-15 16:04:03 +00:00
|
|
|
}
|