platformservice/ts/email/email.classes.apimanager.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-02-16 12:28:40 +00:00
import * as plugins from '../plugins.js';
import { EmailService } from './email.classes.emailservice.js';
import { logger } from '../logger.js';
2024-02-15 19:30:38 +00:00
export class ApiManager {
2024-02-16 12:28:40 +00:00
public emailRef: EmailService;
2024-02-15 19:30:38 +00:00
public typedRouter = new plugins.typedrequest.TypedRouter();
2024-02-16 12:28:40 +00:00
constructor(emailRefArg: EmailService) {
2024-02-15 19:30:38 +00:00
this.emailRef = emailRefArg;
2024-02-16 12:28:40 +00:00
this.emailRef.typedrouter.addTypedRouter(this.typedRouter);
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.platformservice.mta.IRequest_SendEmail>(
2024-02-15 19:30:38 +00: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 12:28:40 +00:00
await plugins.smartfile.SmartFile.fromString(
2024-02-15 19:30:38 +00:00
attachment.name,
attachment.binaryAttachmentString,
'binary'
)
);
}
}
await this.emailRef.mailgunConnector.sendEmail(mailToSend, requestData.to, {});
logger.log(
'info',
`send an email to ${requestData.to} with subject '${mailToSend.getSubject()}'`,
{
eventType: 'sentEmail',
email: {
to: requestData.to,
subject: mailToSend.getSubject(),
},
}
);
return {
responseId: 'abc', // TODO: generate proper response id
};
})
);
}
}