53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import * as plugins from './email.plugins.js';
|
|
import { Email } from './email.classes.email.js';
|
|
import { request } from 'http';
|
|
import { logger } from './email.logging.js';
|
|
|
|
export class ApiManager {
|
|
public emailRef: Email;
|
|
|
|
public typedRouter = new plugins.typedrequest.TypedRouter();
|
|
|
|
constructor(emailRefArg: Email) {
|
|
this.emailRef = emailRefArg;
|
|
this.emailRef.mainTypedRouter.addTypedRouter(this.typedRouter);
|
|
this.typedRouter.addTypedHandler<plugins.lointEmail.IRequestSendEmail>(
|
|
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'
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
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
|
|
};
|
|
})
|
|
);
|
|
}
|
|
}
|